diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp --- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp +++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp @@ -831,8 +831,14 @@ Builder.Insert(GEP); StoreInst *NSI = Builder.CreateStore(NewElement, GEP); NSI->copyMetadata(*SI); - if (SI->getAlign() < NSI->getAlign()) - NSI->setAlignment(SI->getAlign()); + Align NewAlignment(1); + if (auto *C = dyn_cast(Idx)) { + NewAlignment = std::max(SI->getAlign(), Load->getAlign()); + NewAlignment = commonAlignment( + NewAlignment, + C->getZExtValue() * DL.getTypeStoreSize(NewElement->getType())); + } + NSI->setAlignment(NewAlignment); replaceValue(I, *NSI); // Need erasing the store manually. I.eraseFromParent(); diff --git a/llvm/test/Transforms/VectorCombine/load-insert-store.ll b/llvm/test/Transforms/VectorCombine/load-insert-store.ll --- a/llvm/test/Transforms/VectorCombine/load-insert-store.ll +++ b/llvm/test/Transforms/VectorCombine/load-insert-store.ll @@ -20,7 +20,7 @@ ; CHECK-LABEL: @insert_store_i16_align1( ; CHECK-NEXT: entry: ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds <8 x i16>, <8 x i16>* [[Q:%.*]], i32 0, i32 3 -; CHECK-NEXT: store i16 [[S:%.*]], i16* [[TMP0]], align 1 +; CHECK-NEXT: store i16 [[S:%.*]], i16* [[TMP0]], align 2 ; CHECK-NEXT: ret void ; entry: