[llvm] [MemCpyOpt] allow memcpy-to-memcpy optimization with smaller dest than src (PR #176010)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 14 11:07:55 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Jameson Nash (vtjnash)
<details>
<summary>Changes</summary>
Resize the alloca if needed to a common size, as long as the dest was still fully initialized by the copy.
Extracted from PR optimization improvement series at https://github.com/llvm/llvm-project/pull/150792 (included all tests additions from there as well)
---
Full diff: https://github.com/llvm/llvm-project/pull/176010.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp (+17-6)
- (modified) llvm/test/Transforms/MemCpyOpt/stack-move.ll (+72-29)
``````````diff
diff --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
index f054b21f2e9dd..f7e761550f211 100644
--- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -1516,12 +1516,13 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
// Check that copy is full with static size.
const DataLayout &DL = DestAlloca->getDataLayout();
std::optional<TypeSize> SrcSize = SrcAlloca->getAllocationSize(DL);
- if (!SrcSize || Size != *SrcSize) {
- LLVM_DEBUG(dbgs() << "Stack Move: Source alloca size mismatch\n");
- return false;
- }
std::optional<TypeSize> DestSize = DestAlloca->getAllocationSize(DL);
- if (!DestSize || Size != *DestSize) {
+ if (!SrcSize || !DestSize)
+ return false;
+ if (*SrcSize != *DestSize)
+ if (!SrcSize->isFixed() || !DestSize->isFixed())
+ return false;
+ if (Size != *DestSize) {
LLVM_DEBUG(dbgs() << "Stack Move: Destination alloca size mismatch\n");
return false;
}
@@ -1680,6 +1681,16 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
SrcAlloca->setAlignment(
std::max(SrcAlloca->getAlign(), DestAlloca->getAlign()));
+ // Size the allocas appropriately.
+ if (*SrcSize != *DestSize) {
+ // Only possible if both sizes are fixed (due to earlier check)
+ // Set Src to the type and array size of Dest if Dest was larger
+ if (DestSize->getFixedValue() > SrcSize->getFixedValue()) {
+ SrcAlloca->setAllocatedType(DestAlloca->getAllocatedType());
+ SrcAlloca->setOperand(0, DestAlloca->getArraySize());
+ }
+ }
+
// Merge the two allocas.
DestAlloca->replaceAllUsesWith(SrcAlloca);
eraseInstruction(DestAlloca);
@@ -1707,7 +1718,7 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
I->setMetadata(LLVMContext::MD_tbaa_struct, nullptr);
}
- LLVM_DEBUG(dbgs() << "Stack Move: Performed staack-move optimization\n");
+ LLVM_DEBUG(dbgs() << "Stack Move: Performed stack-move optimization\n");
NumStackMove++;
return true;
}
diff --git a/llvm/test/Transforms/MemCpyOpt/stack-move.ll b/llvm/test/Transforms/MemCpyOpt/stack-move.ll
index 0c2e05fa8fed6..cf91ea98b2a55 100644
--- a/llvm/test/Transforms/MemCpyOpt/stack-move.ll
+++ b/llvm/test/Transforms/MemCpyOpt/stack-move.ll
@@ -1022,6 +1022,30 @@ bb2:
ret void
}
+; Tests merging allocas with different sizes
+define void @mismatched_alloca_size() {
+; CHECK-LABEL: define void @mismatched_alloca_size() {
+; CHECK-NEXT: [[SRC:%.*]] = alloca i8, i64 24, align 4
+; CHECK-NEXT: store [[STRUCT_FOO:%.*]] { i32 10, i32 20, i32 30 }, ptr [[SRC]], align 4
+; CHECK-NEXT: [[TMP1:%.*]] = call i32 @use_nocapture(ptr captures(none) [[SRC]])
+; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr captures(none) [[SRC]])
+; CHECK-NEXT: ret void
+;
+ %src = alloca i8, i64 24, align 4
+ %dest = alloca i8, i64 12, align 4
+ call void @llvm.lifetime.start.p0(ptr nocapture %src)
+ call void @llvm.lifetime.start.p0(ptr nocapture %dest)
+ store %struct.Foo { i32 10, i32 20, i32 30 }, ptr %src
+ %1 = call i32 @use_nocapture(ptr nocapture %src)
+
+ call void @llvm.memcpy.p0.p0.i64(ptr align 4 %dest, ptr align 4 %src, i64 12, i1 false)
+
+ %2 = call i32 @use_nocapture(ptr nocapture %dest)
+ call void @llvm.lifetime.end.p0(ptr nocapture %src)
+ call void @llvm.lifetime.end.p0(ptr nocapture %dest)
+ ret void
+}
+
; Optimization failures follow:
@@ -1085,6 +1109,54 @@ define void @incomplete_store() {
ret void
}
+; Tests merging allocas with different types
+define void @mismatched_alloca_type() {
+; CHECK-LABEL: define void @mismatched_alloca_type() {
+; CHECK-NEXT: [[SRC:%.*]] = alloca i16, i64 6, align 4
+; CHECK-NEXT: store [[STRUCT_FOO:%.*]] { i32 10, i32 20, i32 30 }, ptr [[SRC]], align 4
+; CHECK-NEXT: [[TMP1:%.*]] = call i32 @use_nocapture(ptr captures(none) [[SRC]])
+; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr captures(none) [[SRC]])
+; CHECK-NEXT: ret void
+;
+ %src = alloca i16, i64 6, align 4
+ %dest = alloca i8, i64 12, align 4
+ call void @llvm.lifetime.start.p0(ptr nocapture %src)
+ call void @llvm.lifetime.start.p0(ptr nocapture %dest)
+ store %struct.Foo { i32 10, i32 20, i32 30 }, ptr %src
+ %1 = call i32 @use_nocapture(ptr nocapture %src)
+
+ call void @llvm.memcpy.p0.p0.i64(ptr align 4 %dest, ptr align 4 %src, i64 12, i1 false)
+
+ %2 = call i32 @use_nocapture(ptr nocapture %dest)
+ call void @llvm.lifetime.end.p0(ptr nocapture %src)
+ call void @llvm.lifetime.end.p0(ptr nocapture %dest)
+ ret void
+}
+
+; Tests merging allocas with different types and sizes
+define void @mismatched_alloca_type_size() {
+; CHECK-LABEL: define void @mismatched_alloca_type_size() {
+; CHECK-NEXT: [[SRC:%.*]] = alloca i16, i64 12, align 4
+; CHECK-NEXT: store [[STRUCT_FOO:%.*]] { i32 10, i32 20, i32 30 }, ptr [[SRC]], align 4
+; CHECK-NEXT: [[TMP1:%.*]] = call i32 @use_nocapture(ptr captures(none) [[SRC]])
+; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr captures(none) [[SRC]])
+; CHECK-NEXT: ret void
+;
+ %src = alloca i16, i64 12, align 4
+ %dest = alloca i8, i64 12, align 4
+ call void @llvm.lifetime.start.p0(ptr nocapture %src)
+ call void @llvm.lifetime.start.p0(ptr nocapture %dest)
+ store %struct.Foo { i32 10, i32 20, i32 30 }, ptr %src
+ %1 = call i32 @use_nocapture(ptr nocapture %src)
+
+ call void @llvm.memcpy.p0.p0.i64(ptr align 4 %dest, ptr align 4 %src, i64 12, i1 false)
+
+ %2 = call i32 @use_nocapture(ptr nocapture %dest)
+ call void @llvm.lifetime.end.p0(ptr nocapture %src)
+ call void @llvm.lifetime.end.p0(ptr nocapture %dest)
+ ret void
+}
+
; Tests that dynamically-sized allocas are never merged.
define void @dynamically_sized_alloca(i64 %i) {
; CHECK-LABEL: define void @dynamically_sized_alloca
@@ -1178,35 +1250,6 @@ define void @dynamically_sized_memcpy(i64 %size) {
ret void
}
-; Tests that allocas with different sizes aren't merged together.
-define void @mismatched_alloca_size() {
-; CHECK-LABEL: define void @mismatched_alloca_size() {
-; CHECK-NEXT: [[SRC:%.*]] = alloca i8, i64 24, align 4
-; CHECK-NEXT: [[DEST:%.*]] = alloca i8, i64 12, align 4
-; CHECK-NEXT: call void @llvm.lifetime.start.p0(ptr captures(none) [[SRC]])
-; CHECK-NEXT: call void @llvm.lifetime.start.p0(ptr captures(none) [[DEST]])
-; CHECK-NEXT: store [[STRUCT_FOO:%.*]] { i32 10, i32 20, i32 30 }, ptr [[SRC]], align 4
-; CHECK-NEXT: [[TMP1:%.*]] = call i32 @use_nocapture(ptr captures(none) [[SRC]])
-; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[DEST]], ptr align 4 [[SRC]], i64 12, i1 false)
-; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr captures(none) [[DEST]])
-; CHECK-NEXT: call void @llvm.lifetime.end.p0(ptr captures(none) [[SRC]])
-; CHECK-NEXT: call void @llvm.lifetime.end.p0(ptr captures(none) [[DEST]])
-; CHECK-NEXT: ret void
-;
- %src = alloca i8, i64 24, align 4
- %dest = alloca i8, i64 12, align 4
- call void @llvm.lifetime.start.p0(ptr nocapture %src)
- call void @llvm.lifetime.start.p0(ptr nocapture %dest)
- store %struct.Foo { i32 10, i32 20, i32 30 }, ptr %src
- %1 = call i32 @use_nocapture(ptr nocapture %src)
-
- call void @llvm.memcpy.p0.p0.i64(ptr align 4 %dest, ptr align 4 %src, i64 12, i1 false)
-
- %2 = call i32 @use_nocapture(ptr nocapture %dest)
- call void @llvm.lifetime.end.p0(ptr nocapture %src)
- call void @llvm.lifetime.end.p0(ptr nocapture %dest)
- ret void
-}
; Tests that allocas with mismatched address spaces aren't combined.
define void @mismatched_alloca_addrspace() {
``````````
</details>
https://github.com/llvm/llvm-project/pull/176010
More information about the llvm-commits
mailing list