llvm.org GIT mirror llvm / d9dffbd
[ConstantFold] Improve the bitcast folding logic for constant vectors. The constant folder didn't know how to always fold bitcasts of constant integer vectors. In particular, it was unable to handle the case where a constant vector had some undef elements, and the resulting (i.e. bitcasted) vector type had more elements than the original vector type. Example: %cast = bitcast <2 x i64><i64 undef, i64 2> to <4 x i32> On a little endian target, %cast could have been folded to: <4 x i32><i32 undef, i32 undef, i32 2, i32 0> This patch improves the folding logic by teaching how to correctly propagate undef elements in the folded vector. Differential Revision: https://reviews.llvm.org/D24301 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@281343 91177308-0d34-0410-b5e6-96231b3b80d8 Andrea Di Biagio 3 years ago
3 changed file(s) with 35 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
223223
224224 // Loop over each source value, expanding into multiple results.
225225 for (unsigned i = 0; i != NumSrcElt; ++i) {
226 auto *Src = dyn_cast_or_null(C->getAggregateElement(i));
227 if (!Src) // Reject constantexpr elements.
226 auto *Element = C->getAggregateElement(i);
227
228 if (!Element) // Reject constantexpr elements.
229 return ConstantExpr::getBitCast(C, DestTy);
230
231 if (isa(Element)) {
232 // Correctly Propagate undef values.
233 Result.append(Ratio, UndefValue::get(DstEltTy));
234 continue;
235 }
236
237 auto *Src = dyn_cast(Element);
238 if (!Src)
228239 return ConstantExpr::getBitCast(C, DestTy);
229240
230241 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
5656
5757 define <2 x i64> @test_extrq_call_constexpr(<2 x i64> %x) {
5858 ; CHECK-LABEL: @test_extrq_call_constexpr(
59 ; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i64> @llvm.x86.sse4a.extrq(<2 x i64> %x, <16 x i8> bitcast (<2 x i64> to <16 x i8>))
60 ; CHECK-NEXT: ret <2 x i64> [[TMP1]]
59 ; CHECK-NEXT: ret <2 x i64> %x
6160 ;
6261 %1 = call <2 x i64> @llvm.x86.sse4a.extrq(<2 x i64> %x, <16 x i8> bitcast (<2 x i64> to <16 x i8>))
6362 ret <2 x i64> %1
132131
133132 define <2 x i64> @test_extrqi_call_constexpr() {
134133 ; CHECK-LABEL: @test_extrqi_call_constexpr(
135 ; CHECK-NEXT: ret <2 x i64> bitcast (<16 x i8> trunc (<16 x i16> bitcast (<4 x i64> to <16 x i16>) to <16 x i8>), i32 2), i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef> to <2 x i64>)
134 ; CHECK-NEXT: ret <2 x i64> zeroinitializer
136135 ;
137136 %1 = tail call <2 x i64> @llvm.x86.sse4a.extrqi(<2 x i64> bitcast (<16 x i8> trunc (<16 x i16> bitcast (<4 x i64> to <16 x i16>) to <16 x i8>) to <2 x i64>), i8 8, i8 16)
138137 ret <2 x i64> %1
178177
179178 define <2 x i64> @test_insertq_call_constexpr(<2 x i64> %x) {
180179 ; CHECK-LABEL: @test_insertq_call_constexpr(
181 ; CHECK-NEXT: [[TMP1:%.*]] = tail call <2 x i64> @llvm.x86.sse4a.insertq(<2 x i64> %x, <2 x i64> bitcast (<16 x i8> trunc (<16 x i16> bitcast (<4 x i64> to <16 x i16>) to <16 x i8>) to <2 x i64>))
180 ; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i64> @llvm.x86.sse4a.insertqi(<2 x i64> %x, <2 x i64> , i8 2, i8 0)
182181 ; CHECK-NEXT: ret <2 x i64> [[TMP1]]
183182 ;
184183 %1 = tail call <2 x i64> @llvm.x86.sse4a.insertq(<2 x i64> %x, <2 x i64> bitcast (<16 x i8> trunc (<16 x i16> bitcast (<4 x i64> to <16 x i16>) to <16 x i8>) to <2 x i64>))
223222
224223 define <2 x i64> @test_insertqi_call_constexpr(<2 x i64> %x) {
225224 ; CHECK-LABEL: @test_insertqi_call_constexpr(
226 ; CHECK-NEXT: [[TMP1:%.*]] = tail call <2 x i64> @llvm.x86.sse4a.insertqi(<2 x i64> %x, <2 x i64> bitcast (<16 x i8> trunc (<16 x i16> bitcast (<4 x i64> to <16 x i16>) to <16 x i8>) to <2 x i64>), i8 48, i8 3)
225 ; CHECK-NEXT: [[TMP1:%.*]] = tail call <2 x i64> @llvm.x86.sse4a.insertqi(<2 x i64> %x, <2 x i64> , i8 48, i8 3)
227226 ; CHECK-NEXT: ret <2 x i64> [[TMP1]]
228227 ;
229228 %1 = tail call <2 x i64> @llvm.x86.sse4a.insertqi(<2 x i64> %x, <2 x i64> bitcast (<16 x i8> trunc (<16 x i16> bitcast (<4 x i64> to <16 x i16>) to <16 x i8>) to <2 x i64>), i8 48, i8 3)
125125
126126 define <4 x i32> @bitcast_constexpr_4i32_2i64_u2() {
127127 ; CHECK-LABEL: @bitcast_constexpr_4i32_2i64_u2(
128 ; CHECK-NEXT: ret <4 x i32> bitcast (<2 x i64> to <4 x i32>)
128 ; CHECK-NEXT: ret <4 x i32>
129129 ;
130130 %cast = bitcast <2 x i64> to <4 x i32>
131131 ret <4 x i32> %cast
133133
134134 define <4 x i32> @bitcast_constexpr_4i32_2i64_1u() {
135135 ; CHECK-LABEL: @bitcast_constexpr_4i32_2i64_1u(
136 ; CHECK-NEXT: ret <4 x i32> bitcast (<2 x i64> to <4 x i32>)
136 ; CHECK-NEXT: ret <4 x i32>
137137 ;
138138 %cast = bitcast <2 x i64> to <4 x i32>
139139 ret <4 x i32> %cast
141141
142142 define <4 x i32> @bitcast_constexpr_4i32_2i64() {
143143 ; CHECK-LABEL: @bitcast_constexpr_4i32_2i64(
144 ; CHECK-NEXT: ret <4 x i32> bitcast (<2 x i64> to <4 x i32>)
144 ; CHECK-NEXT: ret <4 x i32>
145145 ;
146146 %cast = bitcast <2 x i64> to <4 x i32>
147147 ret <4 x i32> %cast
149149
150150 define <8 x i16> @bitcast_constexpr_8i16_2i64_u2() {
151151 ; CHECK-LABEL: @bitcast_constexpr_8i16_2i64_u2(
152 ; CHECK-NEXT: ret <8 x i16> bitcast (<2 x i64> to <8 x i16>)
152 ; CHECK-NEXT: ret <8 x i16>
153153 ;
154154 %cast = bitcast <2 x i64> to <8 x i16>
155155 ret <8 x i16> %cast
157157
158158 define <8 x i16> @bitcast_constexpr_8i16_2i64_1u() {
159159 ; CHECK-LABEL: @bitcast_constexpr_8i16_2i64_1u(
160 ; CHECK-NEXT: ret <8 x i16> bitcast (<2 x i64> to <8 x i16>)
160 ; CHECK-NEXT: ret <8 x i16>
161161 ;
162162 %cast = bitcast <2 x i64> to <8 x i16>
163163 ret <8 x i16> %cast
165165
166166 define <8 x i16> @bitcast_constexpr_8i16_2i64_u65536() {
167167 ; CHECK-LABEL: @bitcast_constexpr_8i16_2i64_u65536(
168 ; CHECK-NEXT: ret <8 x i16> bitcast (<2 x i64> to <8 x i16>)
168 ; CHECK-NEXT: ret <8 x i16>
169169 ;
170170 %cast = bitcast <2 x i64> to <8 x i16>
171171 ret <8 x i16> %cast
173173
174174 define <16 x i8> @bitcast_constexpr_16i8_2i64_u2() {
175175 ; CHECK-LABEL: @bitcast_constexpr_16i8_2i64_u2(
176 ; CHECK-NEXT: ret <16 x i8> bitcast (<2 x i64> to <16 x i8>)
176 ; CHECK-NEXT: ret <16 x i8>
177177 ;
178178 %cast = bitcast <2 x i64> to <16 x i8>
179179 ret <16 x i8> %cast
181181
182182 define <16 x i8> @bitcast_constexpr_16i8_2i64_256u() {
183183 ; CHECK-LABEL: @bitcast_constexpr_16i8_2i64_256u(
184 ; CHECK-NEXT: ret <16 x i8> bitcast (<2 x i64> to <16 x i8>)
184 ; CHECK-NEXT: ret <16 x i8>
185185 ;
186186 %cast = bitcast <2 x i64> to <16 x i8>
187187 ret <16 x i8> %cast
189189
190190 define <16 x i8> @bitcast_constexpr_16i8_2i64_u256() {
191191 ; CHECK-LABEL: @bitcast_constexpr_16i8_2i64_u256(
192 ; CHECK-NEXT: ret <16 x i8> bitcast (<2 x i64> to <16 x i8>)
192 ; CHECK-NEXT: ret <16 x i8>
193193 ;
194194 %cast = bitcast <2 x i64> to <16 x i8>
195195 ret <16 x i8> %cast
197197
198198 define <8 x i16> @bitcast_constexpr_8i16_4i32_uu22() {
199199 ; CHECK-LABEL: @bitcast_constexpr_8i16_4i32_uu22(
200 ; CHECK-NEXT: ret <8 x i16> bitcast (<4 x i32> to <8 x i16>)
200 ; CHECK-NEXT: ret <8 x i16>
201201 ;
202202 %cast = bitcast <4 x i32> to <8 x i16>
203203 ret <8 x i16> %cast
205205
206206 define <8 x i16> @bitcast_constexpr_8i16_4i32_10uu() {
207207 ; CHECK-LABEL: @bitcast_constexpr_8i16_4i32_10uu(
208 ; CHECK-NEXT: ret <8 x i16> bitcast (<4 x i32> to <8 x i16>)
208 ; CHECK-NEXT: ret <8 x i16>
209209 ;
210210 %cast = bitcast <4 x i32> to <8 x i16>
211211 ret <8 x i16> %cast
213213
214214 define <8 x i16> @bitcast_constexpr_8i16_4i32_u257u256() {
215215 ; CHECK-LABEL: @bitcast_constexpr_8i16_4i32_u257u256(
216 ; CHECK-NEXT: ret <8 x i16> bitcast (<4 x i32> to <8 x i16>)
216 ; CHECK-NEXT: ret <8 x i16>
217217 ;
218218 %cast = bitcast <4 x i32> to <8 x i16>
219219 ret <8 x i16> %cast
221221
222222 define <16 x i8> @bitcast_constexpr_16i8_4i32_u2u2() {
223223 ; CHECK-LABEL: @bitcast_constexpr_16i8_4i32_u2u2(
224 ; CHECK-NEXT: ret <16 x i8> bitcast (<4 x i32> to <16 x i8>)
224 ; CHECK-NEXT: ret <16 x i8>
225225 ;
226226 %cast = bitcast <4 x i32> to <16 x i8>
227227 ret <16 x i8> %cast
229229
230230 define <16 x i8> @bitcast_constexpr_16i8_4i32_1u1u() {
231231 ; CHECK-LABEL: @bitcast_constexpr_16i8_4i32_1u1u(
232 ; CHECK-NEXT: ret <16 x i8> bitcast (<4 x i32> to <16 x i8>)
232 ; CHECK-NEXT: ret <16 x i8>
233233 ;
234234 %cast = bitcast <4 x i32> to <16 x i8>
235235 ret <16 x i8> %cast
237237
238238 define <16 x i8> @bitcast_constexpr_16i8_4i32_u256uu() {
239239 ; CHECK-LABEL: @bitcast_constexpr_16i8_4i32_u256uu(
240 ; CHECK-NEXT: ret <16 x i8> bitcast (<4 x i32> to <16 x i8>)
240 ; CHECK-NEXT: ret <16 x i8>
241241 ;
242242 %cast = bitcast <4 x i32> to <16 x i8>
243243 ret <16 x i8> %cast
245245
246246 define <16 x i8> @bitcast_constexpr_16i8_8i16_u2u2u2u2() {
247247 ; CHECK-LABEL: @bitcast_constexpr_16i8_8i16_u2u2u2u2(
248 ; CHECK-NEXT: ret <16 x i8> bitcast (<8 x i16> to <16 x i8>)
248 ; CHECK-NEXT: ret <16 x i8>
249249 ;
250250 %cast = bitcast <8 x i16> to <16 x i8>
251251 ret <16 x i8> %cast
253253
254254 define <16 x i8> @bitcast_constexpr_16i8_8i16_1u1u1u1u() {
255255 ; CHECK-LABEL: @bitcast_constexpr_16i8_8i16_1u1u1u1u(
256 ; CHECK-NEXT: ret <16 x i8> bitcast (<8 x i16> to <16 x i8>)
256 ; CHECK-NEXT: ret <16 x i8>
257257 ;
258258 %cast = bitcast <8 x i16> to <16 x i8>
259259 ret <16 x i8> %cast
261261
262262 define <16 x i8> @bitcast_constexpr_16i8_8i16_u256uuu256uu() {
263263 ; CHECK-LABEL: @bitcast_constexpr_16i8_8i16_u256uuu256uu(
264 ; CHECK-NEXT: ret <16 x i8> bitcast (<8 x i16> to <16 x i8>)
264 ; CHECK-NEXT: ret <16 x i8>
265265 ;
266266 %cast = bitcast <8 x i16> to <16 x i8>
267267 ret <16 x i8> %cast