Repository navigation
[MemCpyOpt] Extend call slot optimization for non-dereferenceable destinations. - #217436
Merged
Merged
Conversation
Allow call slot optimization for non-dereferenceable destinations when execution is guaranteed to reach the store. For this, check if the call has both willreturn and nounwind attributes, and there are no instructions between the call and the store that might trap or throw. Since the store would trap anyway if the destination pointer was not dereferenceable, we can forward the pointer to the call. This optimization is restricted to non-memcpy/memset calls because these are handled separately after the callSlotOptimization.
|
@llvm/pr-subscribers-llvm-transforms Author: Alina Sbirlea (alinas) ChangesAllow call slot optimization for non-dereferenceable destinations when execution is guaranteed to reach the store. For this, check if the call has both willreturn and nounwind attributes, and there are no instructions between the call and the store that might trap or throw. Since the store would trap anyway if the destination pointer was not dereferenceable, we can forward the pointer to the call. This optimization is restricted to non-memcpy/memset calls because these are handled separately after the callSlotOptimization. 2 Files Affected:
diff --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
index 92e1d9cf21240..09760694c9730 100644
--- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -924,8 +924,18 @@ bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpyLoad,
ExplicitlyDereferenceableOnly) ||
!isDereferenceablePointer(cpyDest, APInt(64, cpySize),
SimplifyQuery(DL, DT, AC, C))) {
- LLVM_DEBUG(dbgs() << "Call Slot: Dest pointer not dereferenceable\n");
- return false;
+ // If the call is guaranteed to return normally (willreturn + nounwind),
+ // and there are no instructions between the call and the store that might
+ // trap or throw, execution will reach the store. Since the store would
+ // trap anyway if the pointer was not dereferenceable, we can forward the
+ // pointer to the call. Perform optimization only for non-memcpy/memset
+ // calls, as those are special cased later.
+ if (isa<AnyMemCpyInst>(C) || isa<AnyMemSetInst>(C) ||
+ !isGuaranteedToTransferExecutionToSuccessor(C->getIterator(),
+ cpyStore->getIterator())) {
+ LLVM_DEBUG(dbgs() << "Call Slot: Dest pointer not dereferenceable\n");
+ return false;
+ }
}
// Make sure that nothing can observe cpyDest being written early. There are
diff --git a/llvm/test/Transforms/MemCpyOpt/callslot_deref.ll b/llvm/test/Transforms/MemCpyOpt/callslot_deref.ll
index dcbede105a3b1..f806277d6b7bf 100644
--- a/llvm/test/Transforms/MemCpyOpt/callslot_deref.ll
+++ b/llvm/test/Transforms/MemCpyOpt/callslot_deref.ll
@@ -2,7 +2,7 @@
; RUN: opt < %s -S -passes=memcpyopt -verify-memoryssa | FileCheck %s
target datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
-declare void @llvm.memcpy.p0.p0.i64(ptr nocapture, ptr nocapture readonly, i64, i1) unnamed_addr nounwind
+declare void @llvm.memcpy.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1) unnamed_addr nounwind
declare void @llvm.memset.p0.i64(ptr nocapture, i8, i64, i1) nounwind
; all bytes of %dst that are touch by the memset are dereferenceable
@@ -32,3 +32,77 @@ define void @must_not_remove_memcpy(ptr noalias nocapture writable dereferenceab
call void @llvm.memcpy.p0.p0.i64(ptr %dst, ptr %src, i64 4096, i1 false) #2
ret void
}
+
+; enable copy elision when call has both willreturn and nounwind.
+define void @test_positive_willreturn_nounwind(ptr %val) {
+; CHECK-LABEL: @test_positive_willreturn_nounwind(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[TMP:%.*]] = alloca [1024 x i32], align 4
+; CHECK-NEXT: call void @bar_willreturn_nounwind(ptr sret([1024 x i32]) align 4 [[VAL:%.*]]) #[[ATTR3:[0-9]+]]
+; CHECK-NEXT: ret void
+;
+entry:
+ %tmp = alloca [1024 x i32], align 4
+ call void @bar_willreturn_nounwind(ptr sret([1024 x i32]) align 4 %tmp) #0
+ call void @llvm.memcpy.p0.p0.i64(ptr align 4 %val, ptr align 4 %tmp, i64 4096, i1 false)
+ ret void
+}
+
+; missing willreturn attribute; optimization should not occur.
+define void @test_negative_missing_willreturn(ptr %val) {
+; CHECK-LABEL: @test_negative_missing_willreturn(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[TMP:%.*]] = alloca [1024 x i32], align 4
+; CHECK-NEXT: call void @bar_missing_willreturn(ptr sret([1024 x i32]) align 4 [[TMP]]) #[[ATTR4:[0-9]+]]
+; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[VAL:%.*]], ptr align 4 [[TMP]], i64 4096, i1 false)
+; CHECK-NEXT: ret void
+;
+entry:
+ %tmp = alloca [1024 x i32], align 4
+ call void @bar_missing_willreturn(ptr sret([1024 x i32]) align 4 %tmp) #1
+ call void @llvm.memcpy.p0.p0.i64(ptr align 4 %val, ptr align 4 %tmp, i64 4096, i1 false)
+ ret void
+}
+
+; missing nounwind attribute; optimization should not occur.
+define void @test_negative_missing_nounwind(ptr %val) {
+; CHECK-LABEL: @test_negative_missing_nounwind(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[TMP:%.*]] = alloca [1024 x i32], align 4
+; CHECK-NEXT: call void @bar_missing_nounwind(ptr sret([1024 x i32]) align 4 [[TMP]]) #[[ATTR5:[0-9]+]]
+; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[VAL:%.*]], ptr align 4 [[TMP]], i64 4096, i1 false)
+; CHECK-NEXT: ret void
+;
+entry:
+ %tmp = alloca [1024 x i32], align 4
+ call void @bar_missing_nounwind(ptr sret([1024 x i32]) align 4 %tmp) #2
+ call void @llvm.memcpy.p0.p0.i64(ptr align 4 %val, ptr align 4 %tmp, i64 4096, i1 false)
+ ret void
+}
+
+; instruction between call and memcpy that may trap.
+define void @test_negative_may_trap_between(ptr %val, ptr %unknown) {
+; CHECK-LABEL: @test_negative_may_trap_between(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[TMP:%.*]] = alloca [1024 x i32], align 4
+; CHECK-NEXT: call void @bar_willreturn_nounwind(ptr sret([1024 x i32]) align 4 [[TMP]]) #[[ATTR3]]
+; CHECK-NEXT: [[VAL_LOADED:%.*]] = load volatile i32, ptr [[UNKNOWN:%.*]], align 4
+; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[VAL:%.*]], ptr align 4 [[TMP]], i64 4096, i1 false)
+; CHECK-NEXT: ret void
+;
+entry:
+ %tmp = alloca [1024 x i32], align 4
+ call void @bar_willreturn_nounwind(ptr sret([1024 x i32]) align 4 %tmp) #0
+ ; A volatile load can trap and has side effects.
+ %val_loaded = load volatile i32, ptr %unknown
+ call void @llvm.memcpy.p0.p0.i64(ptr align 4 %val, ptr align 4 %tmp, i64 4096, i1 false)
+ ret void
+}
+
+declare void @bar_willreturn_nounwind(ptr nocapture sret([1024 x i32]) align 4) #0
+declare void @bar_missing_willreturn(ptr nocapture sret([1024 x i32]) align 4) #1
+declare void @bar_missing_nounwind(ptr nocapture sret([1024 x i32]) align 4) #2
+
+attributes #0 = { willreturn nounwind memory(argmem: write) }
+attributes #1 = { nounwind memory(argmem: write) }
+attributes #2 = { willreturn memory(argmem: write) }
|
nikic
reviewed
Aug 19, 2026
nikic
reviewed
Aug 19, 2026
nikic
approved these changes
Aug 26, 2026
Comment on lines
+931
to
+932
| // pointer to the call. Perform optimization only for non-memcpy/memset | ||
| // calls, as those are special cased later. |
Contributor
There was a problem hiding this comment.
Suggested change
| // pointer to the call. Perform optimization only for non-memcpy/memset | |
| // calls, as those are special cased later. | |
| // pointer to the call. |
Contributor
|
wlemkows
pushed a commit
to wlemkows/llvm-project
that referenced
this pull request
Sep 4, 2026
…tinations. (llvm#217436) Allow call slot optimization for non-dereferenceable destinations when execution is guaranteed to reach the store. For this, check if the call has both willreturn and nounwind attributes, and there are no instructions between the call and the store that might trap or throw. Since the store would trap anyway if the destination pointer was not dereferenceable, we can forward the pointer to the call.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Allow call slot optimization for non-dereferenceable destinations when execution is guaranteed to reach the store. For this, check if the call has both willreturn and nounwind attributes, and there are no instructions between the call and the store that might trap or throw. Since the store would trap anyway if the destination pointer was not dereferenceable, we can forward the pointer to the call.
This optimization is restricted to non-memcpy/memset calls because these are handled separately after the callSlotOptimization.