diff options
author | Dan Gohman <gohman@apple.com> | 2010-12-16 02:51:19 +0000 |
---|---|---|
committer | Dan Gohman <gohman@apple.com> | 2010-12-16 02:51:19 +0000 |
commit | 387f28aff41bae6a81311279b203a1281eaa443a (patch) | |
tree | d2c63b30ae9175f22baaea33a98c5f1f4875ae68 | |
parent | ad3ea3d85f164dc1b2746950e378334197d8688d (diff) | |
download | external_llvm-387f28aff41bae6a81311279b203a1281eaa443a.zip external_llvm-387f28aff41bae6a81311279b203a1281eaa443a.tar.gz external_llvm-387f28aff41bae6a81311279b203a1281eaa443a.tar.bz2 |
Make memcpyopt TBAA-aware.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121944 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Analysis/AliasAnalysis.cpp | 14 | ||||
-rw-r--r-- | lib/Transforms/Scalar/MemCpyOptimizer.cpp | 16 | ||||
-rw-r--r-- | test/Analysis/TypeBasedAliasAnalysis/memcpyopt.ll | 23 |
3 files changed, 37 insertions, 16 deletions
diff --git a/lib/Analysis/AliasAnalysis.cpp b/lib/Analysis/AliasAnalysis.cpp index 588d68d..7a94c43 100644 --- a/lib/Analysis/AliasAnalysis.cpp +++ b/lib/Analysis/AliasAnalysis.cpp @@ -219,8 +219,11 @@ AliasAnalysis::getLocationForSource(const MemTransferInst *MTI) { if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength())) Size = C->getValue().getZExtValue(); - // FIXME: Can memcpy/memmove have TBAA tags? - return Location(MTI->getRawSource(), Size, 0); + // memcpy/memmove can have TBAA tags. For memcpy, they apply + // to both the source and the destination. + MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa); + + return Location(MTI->getRawSource(), Size, TBAATag); } AliasAnalysis::Location @@ -228,9 +231,12 @@ AliasAnalysis::getLocationForDest(const MemIntrinsic *MTI) { uint64_t Size = UnknownSize; if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength())) Size = C->getValue().getZExtValue(); + + // memcpy/memmove can have TBAA tags. For memcpy, they apply + // to both the source and the destination. + MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa); - // FIXME: Can memcpy/memmove have TBAA tags? - return Location(MTI->getRawDest(), Size, 0); + return Location(MTI->getRawDest(), Size, TBAATag); } diff --git a/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/lib/Transforms/Scalar/MemCpyOptimizer.cpp index 4c487e0..a92e848 100644 --- a/lib/Transforms/Scalar/MemCpyOptimizer.cpp +++ b/lib/Transforms/Scalar/MemCpyOptimizer.cpp @@ -688,10 +688,6 @@ bool MemCpyOpt::processMemCpyMemCpyDependence(MemCpyInst *M, MemCpyInst *MDep, ConstantInt *C1 = dyn_cast<ConstantInt>(MDep->getLength()); if (!C1) return false; - uint64_t DepSize = C1->getValue().getZExtValue(); - if (DepSize < MSize) - return false; - AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); // Verify that the copied-from memory doesn't change in between the two @@ -716,7 +712,8 @@ bool MemCpyOpt::processMemCpyMemCpyDependence(MemCpyInst *M, MemCpyInst *MDep, // source and dest might overlap. We still want to eliminate the intermediate // value, but we have to generate a memmove instead of memcpy. Intrinsic::ID ResultFn = Intrinsic::memcpy; - if (!AA.isNoAlias(M->getRawDest(), MSize, MDep->getRawSource(), DepSize)) + if (AA.alias(AA.getLocationForDest(M), AA.getLocationForSource(MDep)) != + AliasAnalysis::NoAlias) ResultFn = Intrinsic::memmove; // If all checks passed, then we can transform M. @@ -795,14 +792,9 @@ bool MemCpyOpt::processMemCpy(MemCpyInst *M) { bool MemCpyOpt::processMemMove(MemMoveInst *M) { AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); - // If the memmove is a constant size, use it for the alias query, this allows - // us to optimize things like: memmove(P, P+64, 64); - uint64_t MemMoveSize = AliasAnalysis::UnknownSize; - if (ConstantInt *Len = dyn_cast<ConstantInt>(M->getLength())) - MemMoveSize = Len->getZExtValue(); - // See if the pointers alias. - if (AA.alias(M->getRawDest(), MemMoveSize, M->getRawSource(), MemMoveSize) != + if (AA.alias(AA.getLocationForDest(M), + AA.getLocationForSource(M)) != AliasAnalysis::NoAlias) return false; diff --git a/test/Analysis/TypeBasedAliasAnalysis/memcpyopt.ll b/test/Analysis/TypeBasedAliasAnalysis/memcpyopt.ll new file mode 100644 index 0000000..c2407df --- /dev/null +++ b/test/Analysis/TypeBasedAliasAnalysis/memcpyopt.ll @@ -0,0 +1,23 @@ +; RUN: opt -S -tbaa -basicaa -memcpyopt -instcombine < %s | FileCheck %s + +target datalayout = "e-p:64:64:64" + +; The second memcpy is redundant and can be deleted. There's an intervening store, but +; it has a TBAA tag which declares that it is unrelated. + +; CHECK: @foo +; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %p, i8* %q, i64 16, i32 1, i1 false), !tbaa !0 +; CHECK-NEXT: store i8 2, i8* %s, align 1, !tbaa !2 +; CHECK-NEXT: ret void +define void @foo(i8* nocapture %p, i8* nocapture %q, i8* nocapture %s) nounwind { + tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %p, i8* %q, i64 16, i32 1, i1 false), !tbaa !2 + store i8 2, i8* %s, align 1, !tbaa !1 + tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %q, i8* %p, i64 16, i32 1, i1 false), !tbaa !2 + ret void +} + +declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind + +!0 = metadata !{metadata !"tbaa root", null} +!1 = metadata !{metadata !"A", metadata !0} +!2 = metadata !{metadata !"B", metadata !0} |