4de7bfcb898d2609308695c6f745cb27ec6a01c6
[oota-llvm.git] / test / Transforms / SROA / big-endian.ll
1 ; RUN: opt < %s -sroa -S | FileCheck %s
2 ; RUN: opt < %s -sroa -force-ssa-updater -S | FileCheck %s
3
4 target datalayout = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-n8:16:32:64"
5
6 define i8 @test1() {
7 ; We fully promote these to the i24 load or store size, resulting in just masks
8 ; and other operations that instcombine will fold, but no alloca. Note this is
9 ; the same as test12 in basictest.ll, but here we assert big-endian byte
10 ; ordering.
11 ;
12 ; CHECK-LABEL: @test1(
13
14 entry:
15   %a = alloca [3 x i8]
16   %b = alloca [3 x i8]
17 ; CHECK-NOT: alloca
18
19   %a0ptr = getelementptr [3 x i8], [3 x i8]* %a, i64 0, i32 0
20   store i8 0, i8* %a0ptr
21   %a1ptr = getelementptr [3 x i8], [3 x i8]* %a, i64 0, i32 1
22   store i8 0, i8* %a1ptr
23   %a2ptr = getelementptr [3 x i8], [3 x i8]* %a, i64 0, i32 2
24   store i8 0, i8* %a2ptr
25   %aiptr = bitcast [3 x i8]* %a to i24*
26   %ai = load i24, i24* %aiptr
27 ; CHECK-NOT: store
28 ; CHECK-NOT: load
29 ; CHECK:      %[[ext2:.*]] = zext i8 0 to i24
30 ; CHECK-NEXT: %[[mask2:.*]] = and i24 undef, -256
31 ; CHECK-NEXT: %[[insert2:.*]] = or i24 %[[mask2]], %[[ext2]]
32 ; CHECK-NEXT: %[[ext1:.*]] = zext i8 0 to i24
33 ; CHECK-NEXT: %[[shift1:.*]] = shl i24 %[[ext1]], 8
34 ; CHECK-NEXT: %[[mask1:.*]] = and i24 %[[insert2]], -65281
35 ; CHECK-NEXT: %[[insert1:.*]] = or i24 %[[mask1]], %[[shift1]]
36 ; CHECK-NEXT: %[[ext0:.*]] = zext i8 0 to i24
37 ; CHECK-NEXT: %[[shift0:.*]] = shl i24 %[[ext0]], 16
38 ; CHECK-NEXT: %[[mask0:.*]] = and i24 %[[insert1]], 65535
39 ; CHECK-NEXT: %[[insert0:.*]] = or i24 %[[mask0]], %[[shift0]]
40
41   %biptr = bitcast [3 x i8]* %b to i24*
42   store i24 %ai, i24* %biptr
43   %b0ptr = getelementptr [3 x i8], [3 x i8]* %b, i64 0, i32 0
44   %b0 = load i8, i8* %b0ptr
45   %b1ptr = getelementptr [3 x i8], [3 x i8]* %b, i64 0, i32 1
46   %b1 = load i8, i8* %b1ptr
47   %b2ptr = getelementptr [3 x i8], [3 x i8]* %b, i64 0, i32 2
48   %b2 = load i8, i8* %b2ptr
49 ; CHECK-NOT: store
50 ; CHECK-NOT: load
51 ; CHECK:      %[[shift0:.*]] = lshr i24 %[[insert0]], 16
52 ; CHECK-NEXT: %[[trunc0:.*]] = trunc i24 %[[shift0]] to i8
53 ; CHECK-NEXT: %[[shift1:.*]] = lshr i24 %[[insert0]], 8
54 ; CHECK-NEXT: %[[trunc1:.*]] = trunc i24 %[[shift1]] to i8
55 ; CHECK-NEXT: %[[trunc2:.*]] = trunc i24 %[[insert0]] to i8
56
57   %bsum0 = add i8 %b0, %b1
58   %bsum1 = add i8 %bsum0, %b2
59   ret i8 %bsum1
60 ; CHECK:      %[[sum0:.*]] = add i8 %[[trunc0]], %[[trunc1]]
61 ; CHECK-NEXT: %[[sum1:.*]] = add i8 %[[sum0]], %[[trunc2]]
62 ; CHECK-NEXT: ret i8 %[[sum1]]
63 }
64
65 define i64 @test2() {
66 ; Test for various mixed sizes of integer loads and stores all getting
67 ; promoted.
68 ;
69 ; CHECK-LABEL: @test2(
70
71 entry:
72   %a = alloca [7 x i8]
73 ; CHECK-NOT: alloca
74
75   %a0ptr = getelementptr [7 x i8], [7 x i8]* %a, i64 0, i32 0
76   %a1ptr = getelementptr [7 x i8], [7 x i8]* %a, i64 0, i32 1
77   %a2ptr = getelementptr [7 x i8], [7 x i8]* %a, i64 0, i32 2
78   %a3ptr = getelementptr [7 x i8], [7 x i8]* %a, i64 0, i32 3
79
80 ; CHECK-NOT: store
81 ; CHECK-NOT: load
82
83   %a0i16ptr = bitcast i8* %a0ptr to i16*
84   store i16 1, i16* %a0i16ptr
85
86   store i8 1, i8* %a2ptr
87 ; CHECK:      %[[mask1:.*]] = and i40 undef, 4294967295
88 ; CHECK-NEXT: %[[insert1:.*]] = or i40 %[[mask1]], 4294967296
89
90   %a3i24ptr = bitcast i8* %a3ptr to i24*
91   store i24 1, i24* %a3i24ptr
92 ; CHECK-NEXT: %[[mask2:.*]] = and i40 %[[insert1]], -4294967041
93 ; CHECK-NEXT: %[[insert2:.*]] = or i40 %[[mask2]], 256
94
95   %a2i40ptr = bitcast i8* %a2ptr to i40*
96   store i40 1, i40* %a2i40ptr
97 ; CHECK-NEXT: %[[ext3:.*]] = zext i40 1 to i56
98 ; CHECK-NEXT: %[[mask3:.*]] = and i56 undef, -1099511627776
99 ; CHECK-NEXT: %[[insert3:.*]] = or i56 %[[mask3]], %[[ext3]]
100
101 ; CHECK-NOT: store
102 ; CHECK-NOT: load
103
104   %aiptr = bitcast [7 x i8]* %a to i56*
105   %ai = load i56, i56* %aiptr
106   %ret = zext i56 %ai to i64
107   ret i64 %ret
108 ; CHECK-NEXT: %[[ext4:.*]] = zext i16 1 to i56
109 ; CHECK-NEXT: %[[shift4:.*]] = shl i56 %[[ext4]], 40
110 ; CHECK-NEXT: %[[mask4:.*]] = and i56 %[[insert3]], 1099511627775
111 ; CHECK-NEXT: %[[insert4:.*]] = or i56 %[[mask4]], %[[shift4]]
112 ; CHECK-NEXT: %[[ret:.*]] = zext i56 %[[insert4]] to i64
113 ; CHECK-NEXT: ret i64 %[[ret]]
114 }
115
116 define i64 @PR14132(i1 %flag) {
117 ; CHECK-LABEL: @PR14132(
118 ; Here we form a PHI-node by promoting the pointer alloca first, and then in
119 ; order to promote the other two allocas, we speculate the load of the
120 ; now-phi-node-pointer. In doing so we end up loading a 64-bit value from an i8
121 ; alloca. While this is a bit dubious, we were asserting on trying to
122 ; rewrite it. The trick is that the code using the value may carefully take
123 ; steps to only use the not-undef bits, and so we need to at least loosely
124 ; support this. This test is particularly interesting because how we handle
125 ; a load of an i64 from an i8 alloca is dependent on endianness.
126 entry:
127   %a = alloca i64, align 8
128   %b = alloca i8, align 8
129   %ptr = alloca i64*, align 8
130 ; CHECK-NOT: alloca
131
132   %ptr.cast = bitcast i64** %ptr to i8**
133   store i64 0, i64* %a
134   store i8 1, i8* %b
135   store i64* %a, i64** %ptr
136   br i1 %flag, label %if.then, label %if.end
137
138 if.then:
139   store i8* %b, i8** %ptr.cast
140   br label %if.end
141 ; CHECK-NOT: store
142 ; CHECK: %[[ext:.*]] = zext i8 1 to i64
143 ; CHECK: %[[shift:.*]] = shl i64 %[[ext]], 56
144
145 if.end:
146   %tmp = load i64*, i64** %ptr
147   %result = load i64, i64* %tmp
148 ; CHECK-NOT: load
149 ; CHECK: %[[result:.*]] = phi i64 [ %[[shift]], %if.then ], [ 0, %entry ]
150
151   ret i64 %result
152 ; CHECK-NEXT: ret i64 %[[result]]
153 }
154
155 declare void @f(i64 %x, i32 %y)
156
157 define void @test3() {
158 ; CHECK-LABEL: @test3(
159 ;
160 ; This is a test that specifically exercises the big-endian lowering because it
161 ; ends up splitting a 64-bit integer into two smaller integers and has a number
162 ; of tricky aspects (the i24 type) that make that hard. Historically, SROA
163 ; would miscompile this by either dropping a most significant byte or least
164 ; significant byte due to shrinking the [4,8) slice to an i24, or by failing to
165 ; move the bytes around correctly.
166 ;
167 ; The magical number 34494054408 is used because it has bits set in various
168 ; bytes so that it is clear if those bytes fail to be propagated.
169 ;
170 ; If you're debugging this, rather than using the direct magical numbers, run
171 ; the IR through '-sroa -instcombine'. With '-instcombine' these will be
172 ; constant folded, and if the i64 doesn't round-trip correctly, you've found
173 ; a bug!
174 ;
175 entry:
176   %a = alloca { i32, i24 }, align 4
177 ; CHECK-NOT: alloca
178
179   %tmp0 = bitcast { i32, i24 }* %a to i64*
180   store i64 34494054408, i64* %tmp0
181   %tmp1 = load i64, i64* %tmp0, align 4
182   %tmp2 = bitcast { i32, i24 }* %a to i32*
183   %tmp3 = load i32, i32* %tmp2, align 4
184 ; CHECK: %[[HI_EXT:.*]] = zext i32 134316040 to i64
185 ; CHECK: %[[HI_INPUT:.*]] = and i64 undef, -4294967296
186 ; CHECK: %[[HI_MERGE:.*]] = or i64 %[[HI_INPUT]], %[[HI_EXT]]
187 ; CHECK: %[[LO_EXT:.*]] = zext i32 8 to i64
188 ; CHECK: %[[LO_SHL:.*]] = shl i64 %[[LO_EXT]], 32
189 ; CHECK: %[[LO_INPUT:.*]] = and i64 %[[HI_MERGE]], 4294967295
190 ; CHECK: %[[LO_MERGE:.*]] = or i64 %[[LO_INPUT]], %[[LO_SHL]]
191
192   call void @f(i64 %tmp1, i32 %tmp3)
193 ; CHECK: call void @f(i64 %[[LO_MERGE]], i32 8)
194   ret void
195 ; CHECK: ret void
196 }
197
198 define void @test4() {
199 ; CHECK-LABEL: @test4
200 ;
201 ; Much like @test3, this is specifically testing big-endian management of data.
202 ; Also similarly, it uses constants with particular bits set to help track
203 ; whether values are corrupted, and can be easily evaluated by running through
204 ; -instcombine to see that the i64 round-trips.
205 ;
206 entry:
207   %a = alloca { i32, i24 }, align 4
208   %a2 = alloca i64, align 4
209 ; CHECK-NOT: alloca
210
211   store i64 34494054408, i64* %a2
212   %tmp0 = bitcast { i32, i24 }* %a to i8*
213   %tmp1 = bitcast i64* %a2 to i8*
214   call void @llvm.memcpy.p0i8.p0i8.i64(i8* %tmp0, i8* %tmp1, i64 8, i32 4, i1 false)
215 ; CHECK: %[[LO_SHR:.*]] = lshr i64 34494054408, 32
216 ; CHECK: %[[LO_START:.*]] = trunc i64 %[[LO_SHR]] to i32
217 ; CHECK: %[[HI_START:.*]] = trunc i64 34494054408 to i32
218
219   %tmp2 = bitcast { i32, i24 }* %a to i64*
220   %tmp3 = load i64, i64* %tmp2, align 4
221   %tmp4 = bitcast { i32, i24 }* %a to i32*
222   %tmp5 = load i32, i32* %tmp4, align 4
223 ; CHECK: %[[HI_EXT:.*]] = zext i32 %[[HI_START]] to i64
224 ; CHECK: %[[HI_INPUT:.*]] = and i64 undef, -4294967296
225 ; CHECK: %[[HI_MERGE:.*]] = or i64 %[[HI_INPUT]], %[[HI_EXT]]
226 ; CHECK: %[[LO_EXT:.*]] = zext i32 %[[LO_START]] to i64
227 ; CHECK: %[[LO_SHL:.*]] = shl i64 %[[LO_EXT]], 32
228 ; CHECK: %[[LO_INPUT:.*]] = and i64 %[[HI_MERGE]], 4294967295
229 ; CHECK: %[[LO_MERGE:.*]] = or i64 %[[LO_INPUT]], %[[LO_SHL]]
230
231   call void @f(i64 %tmp3, i32 %tmp5)
232 ; CHECK: call void @f(i64 %[[LO_MERGE]], i32 %[[LO_START]])
233   ret void
234 ; CHECK: ret void
235 }
236
237 declare void @llvm.memcpy.p0i8.p0i8.i64(i8*, i8*, i64, i32, i1)