Make memcpyopt TBAA-aware.
authorDan Gohman <gohman@apple.com>
Thu, 16 Dec 2010 02:51:19 +0000 (02:51 +0000)
committerDan Gohman <gohman@apple.com>
Thu, 16 Dec 2010 02:51:19 +0000 (02:51 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121944 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/AliasAnalysis.cpp
lib/Transforms/Scalar/MemCpyOptimizer.cpp
test/Analysis/TypeBasedAliasAnalysis/memcpyopt.ll [new file with mode: 0644]

index 588d68da33bfb9a207e2b5cb92e20c5851d54659..7a94c431deeafe49825175aed8caec13f0379757 100644 (file)
@@ -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);
 }
 
 
index 4c487e0a345dc9f5047b230e5345d1ede65640c1..a92e848b845bbf74cdacc55c49855b7fed2f4a51 100644 (file)
@@ -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 (file)
index 0000000..c2407df
--- /dev/null
@@ -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}