[llvm] 990cc49 - [InstCombine] avoid crash on fold of icmp with cast operand

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed May 18 06:17:38 PDT 2022


Author: Sanjay Patel
Date: 2022-05-18T09:16:30-04:00
New Revision: 990cc49ca0ca216b3901beb015e5c00d2da40bf2

URL: https://github.com/llvm/llvm-project/commit/990cc49ca0ca216b3901beb015e5c00d2da40bf2
DIFF: https://github.com/llvm/llvm-project/commit/990cc49ca0ca216b3901beb015e5c00d2da40bf2.diff

LOG: [InstCombine] avoid crash on fold of icmp with cast operand

We could do better by inserting a bitcast from scalar int
to vector int or using an insertelement (the alternate test
does not crash because there's an independent fold like that).

But this doesn't seem like a likely pattern, so just bail out
for now.

Fixes issue #55516.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/test/Transforms/InstCombine/cast-int-icmp-eq-0.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 96845aee4c8e..6769defaf0fb 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2875,8 +2875,10 @@ Instruction *InstCombinerImpl::foldICmpBitCast(ICmpInst &Cmp) {
   Type *SrcType = Bitcast->getSrcTy();
   Type *DstType = Bitcast->getType();
 
-  // Make sure the bitcast doesn't change the number of vector elements.
-  if (SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits()) {
+  // Make sure the bitcast doesn't change between scalar and vector and
+  // doesn't change the number of vector elements.
+  if (SrcType->isVectorTy() == DstType->isVectorTy() &&
+      SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits()) {
     // Zero-equality and sign-bit checks are preserved through sitofp + bitcast.
     Value *X;
     if (match(BCSrcOp, m_SIToFP(m_Value(X)))) {

diff  --git a/llvm/test/Transforms/InstCombine/cast-int-icmp-eq-0.ll b/llvm/test/Transforms/InstCombine/cast-int-icmp-eq-0.ll
index e7ce2a8543dd..9b51a7649992 100644
--- a/llvm/test/Transforms/InstCombine/cast-int-icmp-eq-0.ll
+++ b/llvm/test/Transforms/InstCombine/cast-int-icmp-eq-0.ll
@@ -707,3 +707,30 @@ define i1 @i16_cast_cmp_eq_int_0_bitcast_vector_to_scalar_uitofp(<3 x i16> %i) {
   %cmp = icmp eq i96 %b, 0
   ret i1 %cmp
 }
+
+; Don't crash trying to fold scalar <-> vector bitcast.
+
+define <1 x i1> @PR55516(i64 %x) {
+; CHECK-LABEL: @PR55516(
+; CHECK-NEXT:    [[T0:%.*]] = sitofp i64 [[X:%.*]] to double
+; CHECK-NEXT:    [[BC:%.*]] = bitcast double [[T0]] to <1 x i64>
+; CHECK-NEXT:    [[R:%.*]] = icmp eq <1 x i64> [[BC]], zeroinitializer
+; CHECK-NEXT:    ret <1 x i1> [[R]]
+;
+  %t0 = sitofp i64 %x to double
+  %bc = bitcast double %t0 to <1 x i64>
+  %r = icmp eq <1 x i64> %bc, zeroinitializer
+  ret <1 x i1> %r
+}
+
+define i1 @PR55516_alt(<1 x i64> %x) {
+; CHECK-LABEL: @PR55516_alt(
+; CHECK-NEXT:    [[TMP1:%.*]] = extractelement <1 x i64> [[X:%.*]], i64 0
+; CHECK-NEXT:    [[R:%.*]] = icmp eq i64 [[TMP1]], 0
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %t0 = sitofp <1 x i64> %x to <1 x double>
+  %bc = bitcast <1 x double> %t0 to i64
+  %r = icmp eq i64 %bc, zeroinitializer
+  ret i1 %r
+}


        


More information about the llvm-commits mailing list