Skip to content

Commit 8a4a0f2

Browse files
authored
[MemCpyOpt] Extend call slot optimization for non-dereferenceable destinations. (#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.
1 parent 68f9f8b commit 8a4a0f2

4 files changed

Lines changed: 87 additions & 8 deletions

File tree

‎llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,17 @@ bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpyLoad,
924924
ExplicitlyDereferenceableOnly) ||
925925
!isDereferenceablePointer(cpyDest, APInt(64, cpySize),
926926
SimplifyQuery(DL, DT, AC, C))) {
927-
LLVM_DEBUG(dbgs() << "Call Slot: Dest pointer not dereferenceable\n");
928-
return false;
927+
// If the call is guaranteed to return normally (willreturn + nounwind),
928+
// and there are no instructions between the call and the store that might
929+
// trap or throw, execution will reach the store. Since the store would
930+
// trap anyway if the pointer was not dereferenceable, we can forward the
931+
// pointer to the call. Perform optimization only for non-memcpy/memset
932+
// calls, as those are special cased later.
933+
if (!isGuaranteedToTransferExecutionToSuccessor(C->getIterator(),
934+
cpyStore->getIterator())) {
935+
LLVM_DEBUG(dbgs() << "Call Slot: Dest pointer not dereferenceable\n");
936+
return false;
937+
}
929938
}
930939

931940
// Make sure that nothing can observe cpyDest being written early. There are

‎llvm/test/Transforms/MemCpyOpt/callslot_deref.ll‎

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
; RUN: opt < %s -S -passes=memcpyopt -verify-memoryssa | FileCheck %s
33
target datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
44

5-
declare void @llvm.memcpy.p0.p0.i64(ptr nocapture, ptr nocapture readonly, i64, i1) unnamed_addr nounwind
5+
declare void @llvm.memcpy.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1) unnamed_addr nounwind
66
declare void @llvm.memset.p0.i64(ptr nocapture, i8, i64, i1) nounwind
77

88
; all bytes of %dst that are touch by the memset are dereferenceable
@@ -23,7 +23,6 @@ define void @must_remove_memcpy(ptr noalias nocapture writable dereferenceable(4
2323
define void @must_not_remove_memcpy(ptr noalias nocapture writable dereferenceable(1024) %dst) nofree nosync {
2424
; CHECK-LABEL: @must_not_remove_memcpy(
2525
; CHECK-NEXT: [[SRC:%.*]] = alloca [4096 x i8], align 1
26-
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr [[SRC]], i8 0, i64 4096, i1 false)
2726
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr [[DST:%.*]], i8 0, i64 4096, i1 false)
2827
; CHECK-NEXT: ret void
2928
;
@@ -32,3 +31,71 @@ define void @must_not_remove_memcpy(ptr noalias nocapture writable dereferenceab
3231
call void @llvm.memcpy.p0.p0.i64(ptr %dst, ptr %src, i64 4096, i1 false) #2
3332
ret void
3433
}
34+
35+
; enable copy elision when call has both willreturn and nounwind.
36+
define void @test_positive_willreturn_nounwind(ptr %val) {
37+
; CHECK-LABEL: @test_positive_willreturn_nounwind(
38+
; CHECK-NEXT: entry:
39+
; CHECK-NEXT: [[TMP:%.*]] = alloca [1024 x i32], align 4
40+
; CHECK-NEXT: call void @bar(ptr sret([1024 x i32]) align 4 [[VAL:%.*]]) #[[ATTR3:[0-9]+]]
41+
; CHECK-NEXT: ret void
42+
;
43+
entry:
44+
%tmp = alloca [1024 x i32], align 4
45+
call void @bar(ptr sret([1024 x i32]) align 4 %tmp) willreturn nounwind memory(argmem: write)
46+
call void @llvm.memcpy.p0.p0.i64(ptr align 4 %val, ptr align 4 %tmp, i64 4096, i1 false)
47+
ret void
48+
}
49+
50+
; missing willreturn attribute; optimization should not occur.
51+
define void @test_negative_missing_willreturn(ptr %val) {
52+
; CHECK-LABEL: @test_negative_missing_willreturn(
53+
; CHECK-NEXT: entry:
54+
; CHECK-NEXT: [[TMP:%.*]] = alloca [1024 x i32], align 4
55+
; CHECK-NEXT: call void @bar(ptr sret([1024 x i32]) align 4 [[TMP]]) #[[ATTR4:[0-9]+]]
56+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[VAL:%.*]], ptr align 4 [[TMP]], i64 4096, i1 false)
57+
; CHECK-NEXT: ret void
58+
;
59+
entry:
60+
%tmp = alloca [1024 x i32], align 4
61+
call void @bar(ptr sret([1024 x i32]) align 4 %tmp) nounwind memory(argmem: write)
62+
call void @llvm.memcpy.p0.p0.i64(ptr align 4 %val, ptr align 4 %tmp, i64 4096, i1 false)
63+
ret void
64+
}
65+
66+
; missing nounwind attribute; optimization should not occur.
67+
define void @test_negative_missing_nounwind(ptr %val) {
68+
; CHECK-LABEL: @test_negative_missing_nounwind(
69+
; CHECK-NEXT: entry:
70+
; CHECK-NEXT: [[TMP:%.*]] = alloca [1024 x i32], align 4
71+
; CHECK-NEXT: call void @bar(ptr sret([1024 x i32]) align 4 [[TMP]]) #[[ATTR5:[0-9]+]]
72+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[VAL:%.*]], ptr align 4 [[TMP]], i64 4096, i1 false)
73+
; CHECK-NEXT: ret void
74+
;
75+
entry:
76+
%tmp = alloca [1024 x i32], align 4
77+
call void @bar(ptr sret([1024 x i32]) align 4 %tmp) willreturn memory(argmem: write)
78+
call void @llvm.memcpy.p0.p0.i64(ptr align 4 %val, ptr align 4 %tmp, i64 4096, i1 false)
79+
ret void
80+
}
81+
82+
; willreturn nounwind attributes, but instruction between call and memcpy that may trap.
83+
define void @test_negative_may_trap_between(ptr %val, ptr %unknown) {
84+
; CHECK-LABEL: @test_negative_may_trap_between(
85+
; CHECK-NEXT: entry:
86+
; CHECK-NEXT: [[TMP:%.*]] = alloca [1024 x i32], align 4
87+
; CHECK-NEXT: call void @bar(ptr sret([1024 x i32]) align 4 [[TMP]]) #[[ATTR3]]
88+
; CHECK-NEXT: [[VAL_LOADED:%.*]] = load volatile i32, ptr [[UNKNOWN:%.*]], align 4
89+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[VAL:%.*]], ptr align 4 [[TMP]], i64 4096, i1 false)
90+
; CHECK-NEXT: ret void
91+
;
92+
entry:
93+
%tmp = alloca [1024 x i32], align 4
94+
call void @bar(ptr sret([1024 x i32]) align 4 %tmp) willreturn nounwind memory(argmem: write)
95+
; A volatile load can trap and has side effects.
96+
%val_loaded = load volatile i32, ptr %unknown
97+
call void @llvm.memcpy.p0.p0.i64(ptr align 4 %val, ptr align 4 %tmp, i64 4096, i1 false)
98+
ret void
99+
}
100+
101+
declare void @bar(ptr nocapture sret([1024 x i32]) align 4)

‎llvm/test/Transforms/MemCpyOpt/memcpy.ll‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ define void @test2_constant(ptr %Q) nounwind {
7474
; other should be related with a memcpy.
7575
define void @test2_memcpy(ptr noalias %P, ptr noalias %Q) nounwind {
7676
; CHECK-LABEL: @test2_memcpy(
77+
; CHECK-NEXT: %memtmp = alloca %0, align 16
7778
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 16 [[Q:%.*]], ptr align 16 [[P:%.*]], i32 32, i1 false)
7879
; CHECK-NEXT: ret void
7980
;
@@ -88,7 +89,8 @@ define void @test2_memcpy(ptr noalias %P, ptr noalias %Q) nounwind {
8889
; if the one eliminated was inline.
8990
define void @test3_memcpy(ptr noalias %P, ptr noalias %Q) nounwind {
9091
; CHECK-LABEL: @test3_memcpy(
91-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 16 [[Q:%.*]], ptr align 16 [[P:%.*]], i32 32, i1 false)
92+
; CHECK-NEXT: %memtmp = alloca %0, align 16
93+
; CHECK-NEXT: call void @llvm.memcpy.inline.p0.p0.i32(ptr align 16 [[Q:%.*]], ptr align 16 [[P:%.*]], i32 32, i1 false)
9294
; CHECK-NEXT: ret void
9395
;
9496
%memtmp = alloca %0, align 16
@@ -102,7 +104,8 @@ define void @test3_memcpy(ptr noalias %P, ptr noalias %Q) nounwind {
102104
; if the one eliminated was not inline.
103105
define void @test4_memcpy(ptr noalias %P, ptr noalias %Q) nounwind {
104106
; CHECK-LABEL: @test4_memcpy(
105-
; CHECK-NEXT: call void @llvm.memcpy.inline.p0.p0.i32(ptr align 16 [[Q:%.*]], ptr align 16 [[P:%.*]], i32 32, i1 false)
107+
; CHECK-NEXT: %memtmp = alloca %0, align 16
108+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 16 [[Q:%.*]], ptr align 16 [[P:%.*]], i32 32, i1 false)
106109
; CHECK-NEXT: ret void
107110
;
108111
%memtmp = alloca %0, align 16
@@ -115,6 +118,7 @@ define void @test4_memcpy(ptr noalias %P, ptr noalias %Q) nounwind {
115118
; Same as @test2_memcpy, and the inline-ness should be preserved.
116119
define void @test5_memcpy(ptr noalias %P, ptr noalias %Q) nounwind {
117120
; CHECK-LABEL: @test5_memcpy(
121+
; CHECK-NEXT: %memtmp = alloca %0, align 16
118122
; CHECK-NEXT: call void @llvm.memcpy.inline.p0.p0.i32(ptr align 16 [[Q:%.*]], ptr align 16 [[P:%.*]], i32 32, i1 false)
119123
; CHECK-NEXT: ret void
120124
;

‎llvm/test/Transforms/MemCpyOpt/stack-move-offset.ll‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,8 @@ define void @no_optimize_clobbering_store_to_src_offset(ptr noalias %dst) {
252252
; CHECK-NEXT: store i8 0, ptr [[DST_BUF]], align 1
253253
; CHECK-NEXT: call void @llvm.lifetime.end.p0(ptr [[TEMP1]])
254254
; CHECK-NEXT: call void @llvm.lifetime.start.p0(ptr [[TEMP2]])
255-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[TEMP2]], ptr align 8 [[LOCAL_BUF]], i64 16, i1 false)
255+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[DST_BUF]], ptr align 8 [[LOCAL_BUF]], i64 16, i1 false)
256256
; CHECK-NEXT: store i8 0, ptr [[LOCAL_BUF]], align 1
257-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[DST_BUF]], ptr align 8 [[TEMP2]], i64 16, i1 false)
258257
; CHECK-NEXT: call void @llvm.lifetime.end.p0(ptr [[TEMP2]])
259258
; CHECK-NEXT: ret void
260259
;

0 commit comments

Comments
 (0)