Skip to content

Commit b09b05b

Browse files
nikictstellar
authored andcommitted
[MemCpyOpt] Fix clobber check in fca2memcpy optimization
This effectively reverts #108535. The old AA code was looking for the *first* clobber between the load and store and then trying to move all the way up there. The new MSSA based code instead found the *last* clobber. There might still be an earlier clobber that has not been accounted for. Fixes #130632. (cherry picked from commit 5da9044)
1 parent 64ae641 commit b09b05b

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

Diff for: llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -638,17 +638,19 @@ bool MemCpyOptPass::processStoreOfLoad(StoreInst *SI, LoadInst *LI,
638638
(EnableMemCpyOptWithoutLibcalls ||
639639
(TLI->has(LibFunc_memcpy) && TLI->has(LibFunc_memmove)))) {
640640
MemoryLocation LoadLoc = MemoryLocation::get(LI);
641-
MemoryUseOrDef *LoadAccess = MSSA->getMemoryAccess(LI),
642-
*StoreAccess = MSSA->getMemoryAccess(SI);
643-
644-
// We use MSSA to check if an instruction may store to the memory we load
645-
// from in between the load and the store. If such an instruction is found,
646-
// we try to promote there instead of at the store position.
647-
auto *Clobber = MSSA->getWalker()->getClobberingMemoryAccess(
648-
StoreAccess->getDefiningAccess(), LoadLoc, BAA);
649-
Instruction *P = MSSA->dominates(LoadAccess, Clobber)
650-
? cast<MemoryUseOrDef>(Clobber)->getMemoryInst()
651-
: SI;
641+
642+
// We use alias analysis to check if an instruction may store to
643+
// the memory we load from in between the load and the store. If
644+
// such an instruction is found, we try to promote there instead
645+
// of at the store position.
646+
// TODO: Can use MSSA for this.
647+
Instruction *P = SI;
648+
for (auto &I : make_range(++LI->getIterator(), SI->getIterator())) {
649+
if (isModSet(BAA.getModRefInfo(&I, LoadLoc))) {
650+
P = &I;
651+
break;
652+
}
653+
}
652654

653655
// If we found an instruction that may write to the loaded memory,
654656
// we can try to promote at this position instead of the store

Diff for: llvm/test/Transforms/MemCpyOpt/fca2memcpy.ll

+33-7
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ define void @destroysrc(ptr %src, ptr %dst) {
5151

5252
define void @destroynoaliassrc(ptr noalias %src, ptr %dst) {
5353
; CHECK-LABEL: @destroynoaliassrc(
54-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[DST:%.*]], ptr align 8 [[SRC]], i64 16, i1 false)
55-
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[SRC:%.*]], i8 0, i64 16, i1 false)
54+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[DST:%.*]], ptr align 8 [[SRC:%.*]], i64 16, i1 false)
55+
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[SRC]], i8 0, i64 16, i1 false)
5656
; CHECK-NEXT: ret void
5757
;
5858
%1 = load %S, ptr %src
@@ -79,9 +79,9 @@ define void @copyalias(ptr %src, ptr %dst) {
7979
; sure we lift the computation as well if needed and possible.
8080
define void @addrproducer(ptr %src, ptr %dst) {
8181
; CHECK-LABEL: @addrproducer(
82-
; CHECK-NEXT: [[DST2:%.*]] = getelementptr [[S:%.*]], ptr [[DST]], i64 1
82+
; CHECK-NEXT: [[DST2:%.*]] = getelementptr [[S:%.*]], ptr [[DST:%.*]], i64 1
8383
; CHECK-NEXT: call void @llvm.memmove.p0.p0.i64(ptr align 8 [[DST2]], ptr align 8 [[SRC:%.*]], i64 16, i1 false)
84-
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[DST:%.*]], i8 undef, i64 16, i1 false)
84+
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[DST]], i8 undef, i64 16, i1 false)
8585
; CHECK-NEXT: ret void
8686
;
8787
%1 = load %S, ptr %src
@@ -113,8 +113,8 @@ define void @noaliasaddrproducer(ptr %src, ptr noalias %dst, ptr noalias %dstidp
113113
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[DSTIDPTR:%.*]], align 4
114114
; CHECK-NEXT: [[DSTINDEX:%.*]] = or i32 [[TMP2]], 1
115115
; CHECK-NEXT: [[DST2:%.*]] = getelementptr [[S:%.*]], ptr [[DST:%.*]], i32 [[DSTINDEX]]
116-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[DST2]], ptr align 8 [[SRC]], i64 16, i1 false)
117-
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[SRC:%.*]], i8 undef, i64 16, i1 false)
116+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[DST2]], ptr align 8 [[SRC:%.*]], i64 16, i1 false)
117+
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[SRC]], i8 undef, i64 16, i1 false)
118118
; CHECK-NEXT: ret void
119119
;
120120
%1 = load %S, ptr %src
@@ -130,7 +130,7 @@ define void @throwing_call(ptr noalias %src, ptr %dst) {
130130
; CHECK-LABEL: @throwing_call(
131131
; CHECK-NEXT: [[TMP1:%.*]] = load [[S:%.*]], ptr [[SRC:%.*]], align 8
132132
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[SRC]], i8 0, i64 16, i1 false)
133-
; CHECK-NEXT: call void @call() [[ATTR2:#.*]]
133+
; CHECK-NEXT: call void @call() #[[ATTR2:[0-9]+]]
134134
; CHECK-NEXT: store [[S]] [[TMP1]], ptr [[DST:%.*]], align 8
135135
; CHECK-NEXT: ret void
136136
;
@@ -156,4 +156,30 @@ loop:
156156
br label %loop
157157
}
158158

159+
; There are multiple instructions that can clobber the source memory here.
160+
; We can move the dest write past the store to %ptr.24, but not the memcpy.
161+
; Make sure we don't perform fca2memcpy conversion in this case.
162+
define void @multiple_clobbering(ptr %ptr, ptr %ptr.copy) {
163+
; CHECK-LABEL: @multiple_clobbering(
164+
; CHECK-NEXT: [[PTR_8:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR:%.*]], i64 8
165+
; CHECK-NEXT: [[PTR_24:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR]], i64 24
166+
; CHECK-NEXT: [[PTR_32:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR]], i64 32
167+
; CHECK-NEXT: [[PTR_COPY_8:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR_COPY:%.*]], i64 8
168+
; CHECK-NEXT: [[STRUCT:%.*]] = load { i32, i64 }, ptr [[PTR_COPY_8]], align 8
169+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[PTR_8]], ptr [[PTR_32]], i64 12, i1 false)
170+
; CHECK-NEXT: store i64 1, ptr [[PTR_24]], align 8
171+
; CHECK-NEXT: store { i32, i64 } [[STRUCT]], ptr [[PTR_32]], align 8
172+
; CHECK-NEXT: ret void
173+
;
174+
%ptr.8 = getelementptr inbounds nuw i8, ptr %ptr, i64 8
175+
%ptr.24 = getelementptr inbounds nuw i8, ptr %ptr, i64 24
176+
%ptr.32 = getelementptr inbounds nuw i8, ptr %ptr, i64 32
177+
%ptr.copy.8 = getelementptr inbounds nuw i8, ptr %ptr.copy, i64 8
178+
%struct = load { i32, i64 }, ptr %ptr.copy.8, align 8
179+
call void @llvm.memcpy.p0.p0.i64(ptr %ptr.8, ptr %ptr.32, i64 12, i1 false)
180+
store i64 1, ptr %ptr.24, align 8
181+
store { i32, i64 } %struct, ptr %ptr.32, align 8
182+
ret void
183+
}
184+
159185
declare void @call()

0 commit comments

Comments
 (0)