diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index b504d3310f57..253a7243bcf5 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -399,6 +399,12 @@ Constant *llvm::ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, } while (ElemC && DL.getTypeSizeInBits(ElemC->getType()).isZero()); C = ElemC; } else { + // For non-byte-sized vector elements, the first element is not + // necessarily located at the vector base address. + if (auto *VT = dyn_cast(SrcTy)) + if (!DL.typeSizeEqualsStoreSize(VT->getElementType())) + return nullptr; + C = C->getAggregateElement(0u); } } while (C); diff --git a/llvm/test/Transforms/InstCombine/load-store-forward.ll b/llvm/test/Transforms/InstCombine/load-store-forward.ll index 1f4c49376f9b..c1a01454772f 100644 --- a/llvm/test/Transforms/InstCombine/load-store-forward.ll +++ b/llvm/test/Transforms/InstCombine/load-store-forward.ll @@ -47,7 +47,8 @@ define i17 @vec_store_load_first_odd_size(i17* %p) { ; CHECK-LABEL: @vec_store_load_first_odd_size( ; CHECK-NEXT: [[P2:%.*]] = bitcast i17* [[P:%.*]] to <2 x i17>* ; CHECK-NEXT: store <2 x i17> , <2 x i17>* [[P2]], align 8 -; CHECK-NEXT: ret i17 1 +; CHECK-NEXT: [[LOAD:%.*]] = load i17, i17* [[P]], align 4 +; CHECK-NEXT: ret i17 [[LOAD]] ; %p2 = bitcast i17* %p to <2 x i17>* store <2 x i17> , <2 x i17>* %p2