summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryant Wong <llvm-commits@xorshift.org>2016-12-27 17:58:12 +0000
committerBryant Wong <llvm-commits@xorshift.org>2016-12-27 17:58:12 +0000
commit7cb744621b28e27364908d977785191877a5b8a5 (patch)
tree3ff994acd65546d807b23cc98f2f8510ded5f2ed
parente0ee5cf7c8cae4be0e4eaab3eb71779ee6272976 (diff)
downloadbcm5719-llvm-7cb744621b28e27364908d977785191877a5b8a5.tar.gz
bcm5719-llvm-7cb744621b28e27364908d977785191877a5b8a5.zip
[MemCpyOpt] Don't sink LoadInst below possible clobber.
Differential Revision: https://reviews.llvm.org/D26811 llvm-svn: 290611
-rw-r--r--llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp16
-rw-r--r--llvm/test/Transforms/MemCpyOpt/load-store-to-memcpy.ll36
2 files changed, 47 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
index b19e663cc05..1b590140f70 100644
--- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -489,7 +489,8 @@ static unsigned findCommonAlignment(const DataLayout &DL, const StoreInst *SI,
// It will lift the store and its argument + that anything that
// may alias with these.
// The method returns true if it was successful.
-static bool moveUp(AliasAnalysis &AA, StoreInst *SI, Instruction *P) {
+static bool moveUp(AliasAnalysis &AA, StoreInst *SI, Instruction *P,
+ const LoadInst *LI) {
// If the store alias this position, early bail out.
MemoryLocation StoreLoc = MemoryLocation::get(SI);
if (AA.getModRefInfo(P, StoreLoc) != MRI_NoModRef)
@@ -506,12 +507,13 @@ static bool moveUp(AliasAnalysis &AA, StoreInst *SI, Instruction *P) {
SmallVector<Instruction*, 8> ToLift;
// Memory locations of lifted instructions.
- SmallVector<MemoryLocation, 8> MemLocs;
- MemLocs.push_back(StoreLoc);
+ SmallVector<MemoryLocation, 8> MemLocs{StoreLoc};
// Lifted callsites.
SmallVector<ImmutableCallSite, 8> CallSites;
+ const MemoryLocation LoadLoc = MemoryLocation::get(LI);
+
for (auto I = --SI->getIterator(), E = P->getIterator(); I != E; --I) {
auto *C = &*I;
@@ -535,7 +537,11 @@ static bool moveUp(AliasAnalysis &AA, StoreInst *SI, Instruction *P) {
continue;
if (MayAlias) {
- if (auto CS = ImmutableCallSite(C)) {
+ // Since LI is implicitly moved downwards past the lifted instructions,
+ // none of them may modify its source.
+ if (AA.getModRefInfo(C, LoadLoc) & MRI_Mod)
+ return false;
+ else if (auto CS = ImmutableCallSite(C)) {
// If we can't lift this before P, it's game over.
if (AA.getModRefInfo(P, CS) != MRI_NoModRef)
return false;
@@ -610,7 +616,7 @@ bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
// position if nothing alias the store memory after this and the store
// destination is not in the range.
if (P && P != SI) {
- if (!moveUp(AA, SI, P))
+ if (!moveUp(AA, SI, P, LI))
P = nullptr;
}
diff --git a/llvm/test/Transforms/MemCpyOpt/load-store-to-memcpy.ll b/llvm/test/Transforms/MemCpyOpt/load-store-to-memcpy.ll
new file mode 100644
index 00000000000..f6b175e4171
--- /dev/null
+++ b/llvm/test/Transforms/MemCpyOpt/load-store-to-memcpy.ll
@@ -0,0 +1,36 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -basicaa -scoped-noalias -memcpyopt -S %s | FileCheck %s
+
+%T = type { i8, i32 }
+
+; memcpy(%d, %a) should not be generated since store2 may-aliases load %a.
+define void @f(%T* %a, %T* %b, %T* %c, %T* %d) {
+; CHECK-LABEL: @f(
+; CHECK-NEXT: [[VAL:%.*]] = load %T, %T* %a, !alias.scope !0
+; CHECK-NEXT: store %T { i8 23, i32 23 }, %T* %b, !alias.scope !3
+; CHECK-NEXT: store %T { i8 44, i32 44 }, %T* %c, !alias.scope !6, !noalias !3
+; CHECK-NEXT: store %T [[VAL]], %T* %d, !alias.scope !9, !noalias !12
+; CHECK-NEXT: ret void
+;
+ %val = load %T, %T* %a, !alias.scope !{!10}
+
+ ; store1 may-aliases the load
+ store %T { i8 23, i32 23 }, %T* %b, !alias.scope !{!11}
+
+ ; store2 may-aliases the load and store3
+ store %T { i8 44, i32 44 }, %T* %c, !alias.scope !{!12}, !noalias !{!11}
+
+ ; store3
+ store %T %val, %T* %d, !alias.scope !{!13}, !noalias !{!10, !11}
+ ret void
+}
+
+!0 = !{!0}
+!1 = !{!1}
+!2 = !{!2}
+!3 = !{!3}
+
+!10 = !{ !10, !0 }
+!11 = !{ !11, !1 }
+!12 = !{ !12, !2 }
+!13 = !{ !13, !3 }
OpenPOWER on IntegriCloud