From 39dac9d1cd94d136f4dbee6c3a937691539ca91d Mon Sep 17 00:00:00 2001 From: liaozhiyu Date: Tue, 9 Sep 2025 16:31:12 +0800 Subject: [PATCH] prevent fold scalar to vector The pass instcombine would fold %0 = bitcast i128 %a to <4 x i32> %1 = bitcast i128 %b to <4 x i32> %xor = xor <4 x i32> %0, %1 to %xor = xor i128 %a, %b %0 = bitcast i128 %xor to <4 x i32> which cause the use of scalar operation rather than vector operation. Check if the source type of the cast is scalar and the dest type of the cast is vector, prevent apply the pass. --- .../InstCombine/InstCombineAndOrXor.cpp | 5 +++++ .../InstCombine/bitcast-inseltpoison.ll | 18 +++++++++++------- llvm/test/Transforms/InstCombine/bitcast.ll | 18 +++++++++++------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 8a1fb6b7f17e..694f660caefb 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 061182fdaf3c..84ecc8bea914 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 58bd81297b0d..a00f57e0343e 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. -- Gitee