diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 8a1fb6b7f17e7b5aa1b798b46400ea48c82bf361..694f660caefbec284a30345aefef88dd7d4a4685 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -1769,6 +1769,11 @@ Instruction *InstCombinerImpl::foldCastedBitwiseLogic(BinaryOperator &I) { if (CastOpcode != Cast1->getOpcode()) return nullptr; + // If the cast from a scalar to a vector, don't fold it, so that we can + // ensure the use of vector operations instead of scalar operations. + if (!SrcTy->isVectorTy() && DestTy->isVectorTy()) + return nullptr; + // If the source types do not match, but the casts are matching extends, we // can still narrow the logic op. if (SrcTy != Cast1->getSrcTy()) { diff --git a/llvm/test/Transforms/InstCombine/bitcast-inseltpoison.ll b/llvm/test/Transforms/InstCombine/bitcast-inseltpoison.ll index 061182fdaf3c80afc9b6bf4417389a6b030273de..84ecc8bea91448d8cf604890fc43a74cb4437fde 100644 --- a/llvm/test/Transforms/InstCombine/bitcast-inseltpoison.ll +++ b/llvm/test/Transforms/InstCombine/bitcast-inseltpoison.ll @@ -4,17 +4,21 @@ 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" target triple = "x86_64-apple-darwin10.0.0" -; Bitcasts between vectors and scalars are valid. -; PR4487 -define i32 @test1(i64 %a) { +; Bitcasts from vectors to scalars are invalid. + +define i64 @test1(i64 %a, i64 %b) { ; CHECK-LABEL: @test1( -; CHECK-NEXT: ret i32 0 +; CHECK-NEXT: [[T1:%.*]] = bitcast i64 [[A:%.*]] to <2 x i32> +; CHECK-NEXT: [[T2:%.*]] = bitcast i64 [[B:%.*]] to <2 x i32> +; CHECK-NEXT: [[T3:%.*]] = xor <2 x i32> [[T1]], [[T2]] +; CHECK-NEXT: [[T4:%.*]] = bitcast <2 x i32> [[T3]] to i64 +; CHECK-NEXT: ret i64 [[T4]] ; %t1 = bitcast i64 %a to <2 x i32> - %t2 = bitcast i64 %a to <2 x i32> + %t2 = bitcast i64 %b to <2 x i32> %t3 = xor <2 x i32> %t1, %t2 - %t4 = extractelement <2 x i32> %t3, i32 0 - ret i32 %t4 + %t4 = bitcast <2 x i32> %t3 to i64 + ret i64 %t4 } ; Perform the bitwise logic in the source type of the operands to eliminate bitcasts. diff --git a/llvm/test/Transforms/InstCombine/bitcast.ll b/llvm/test/Transforms/InstCombine/bitcast.ll index 58bd81297b0dd95c14f13a081bbec9af030a99cb..a00f57e0343ef35c15d0508c6e6f9ef613a888ce 100644 --- a/llvm/test/Transforms/InstCombine/bitcast.ll +++ b/llvm/test/Transforms/InstCombine/bitcast.ll @@ -6,17 +6,21 @@ target triple = "x86_64-apple-darwin10.0.0" declare void @use_vec(<2 x i64>) -; Bitcasts between vectors and scalars are valid. -; PR4487 -define i32 @test1(i64 %a) { +; Bitcasts from vectors to scalars are invalid. + +define i64 @test1(i64 %a, i64 %b) { ; CHECK-LABEL: @test1( -; CHECK-NEXT: ret i32 0 +; CHECK-NEXT: [[T1:%.*]] = bitcast i64 [[A:%.*]] to <2 x i32> +; CHECK-NEXT: [[T2:%.*]] = bitcast i64 [[B:%.*]] to <2 x i32> +; CHECK-NEXT: [[T3:%.*]] = xor <2 x i32> [[T1]], [[T2]] +; CHECK-NEXT: [[T4:%.*]] = bitcast <2 x i32> [[T3]] to i64 +; CHECK-NEXT: ret i64 [[T4]] ; %t1 = bitcast i64 %a to <2 x i32> - %t2 = bitcast i64 %a to <2 x i32> + %t2 = bitcast i64 %b to <2 x i32> %t3 = xor <2 x i32> %t1, %t2 - %t4 = extractelement <2 x i32> %t3, i32 0 - ret i32 %t4 + %t4 = bitcast <2 x i32> %t3 to i64 + ret i64 %t4 } ; Perform the bitwise logic in the source type of the operands to eliminate bitcasts.