[llvm] 381054a - [InstCombine] visitBitCast(): do not crash on weird `bitcast <1 x i8*> to i8*`

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 24 14:59:21 PDT 2020


Author: Roman Lebedev
Date: 2020-06-25T00:58:53+03:00
New Revision: 381054a989ebd0b585fee46f2a01a7c5de10acf7

URL: https://github.com/llvm/llvm-project/commit/381054a989ebd0b585fee46f2a01a7c5de10acf7
DIFF: https://github.com/llvm/llvm-project/commit/381054a989ebd0b585fee46f2a01a7c5de10acf7.diff

LOG: [InstCombine] visitBitCast(): do not crash on weird `bitcast <1 x i8*> to i8*`

Even if we know that RHS of a bitcast is a pointer,
we can't assume LHS is, because it might be
a single-element vector of pointer.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
    llvm/test/Transforms/InstCombine/bitcast.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 3750f31e3cff..a8c87ea35588 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -2471,8 +2471,9 @@ Instruction *InstCombiner::visitBitCast(BitCastInst &CI) {
   if (DestTy == Src->getType())
     return replaceInstUsesWith(CI, Src);
 
-  if (PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
+  if (isa<PointerType>(SrcTy) && isa<PointerType>(DestTy)) {
     PointerType *SrcPTy = cast<PointerType>(SrcTy);
+    PointerType *DstPTy = cast<PointerType>(DestTy);
     Type *DstElTy = DstPTy->getElementType();
     Type *SrcElTy = SrcPTy->getElementType();
 

diff  --git a/llvm/test/Transforms/InstCombine/bitcast.ll b/llvm/test/Transforms/InstCombine/bitcast.ll
index 0f0cbdb364af..c4ee52f27a8c 100644
--- a/llvm/test/Transforms/InstCombine/bitcast.ll
+++ b/llvm/test/Transforms/InstCombine/bitcast.ll
@@ -561,3 +561,9 @@ define void @constant_fold_vector_to_half() {
   store volatile half bitcast (<4 x i4> <i4 0, i4 0, i4 0, i4 4> to half), half* undef
   ret void
 }
+
+; Ensure that we do not crash when looking at such a weird bitcast.
+define i8* @bitcast_from_single_element_pointer_vector_to_pointer(<1 x i8*> %ptrvec) {
+  %ptr = bitcast <1 x i8*> %ptrvec to i8*
+  ret i8* %ptr
+}


        


More information about the llvm-commits mailing list