[llvm] r237874 - [MemCpyOpt] Do move the memset, but look at its dest's dependencies.
Ahmed Bougacha
ahmed.bougacha at gmail.com
Wed May 20 18:43:40 PDT 2015
Author: ab
Date: Wed May 20 20:43:39 2015
New Revision: 237874
URL: http://llvm.org/viewvc/llvm-project?rev=237874&view=rev
Log:
[MemCpyOpt] Do move the memset, but look at its dest's dependencies.
In effect a partial revert of r237858, which was a dumb shortcut.
Looking at the dependencies of the destination should be the proper
fix: if the new memset would depend on anything other than itself,
the transformation isn't correct.
Modified:
llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp
llvm/trunk/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll
Modified: llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp?rev=237874&r1=237873&r2=237874&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp Wed May 20 20:43:39 2015
@@ -859,6 +859,13 @@ bool MemCpyOpt::processMemSetMemCpyDepen
if (MemSet->getDest() != MemCpy->getDest())
return false;
+ // Check that there are no other dependencies on the memset destination.
+ MemDepResult DstDepInfo =
+ MD->getPointerDependencyFrom(AliasAnalysis::getLocationForDest(MemSet),
+ false, MemCpy, MemCpy->getParent());
+ if (DstDepInfo.getInst() != MemSet)
+ return false;
+
// Use the same i8* dest as the memcpy, killing the memset dest if different.
Value *Dest = MemCpy->getRawDest();
Value *DestSize = MemSet->getLength();
@@ -874,7 +881,7 @@ bool MemCpyOpt::processMemSetMemCpyDepen
if (ConstantInt *SrcSizeC = dyn_cast<ConstantInt>(SrcSize))
Align = MinAlign(SrcSizeC->getZExtValue(), DestAlign);
- IRBuilder<> Builder(MemSet);
+ IRBuilder<> Builder(MemCpy);
// If the sizes have different types, zext the smaller one.
if (DestSize->getType() != SrcSize->getType()) {
Modified: llvm/trunk/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll?rev=237874&r1=237873&r2=237874&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll (original)
+++ llvm/trunk/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll Wed May 20 20:43:39 2015
@@ -1,4 +1,4 @@
-; RUN: opt -memcpyopt -S %s | FileCheck %s
+; RUN: opt -basicaa -memcpyopt -S %s | FileCheck %s
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
@@ -140,6 +140,26 @@ define i8 @test_intermediate_read(i8* %a
ret i8 %r
}
+%struct = type { [8 x i8], [8 x i8] }
+
+; CHECK-LABEL: define void @test_intermediate_write
+; CHECK-NEXT: %a = alloca %struct
+; CHECK-NEXT: %a0 = getelementptr %struct, %struct* %a, i32 0, i32 0, i32 0
+; CHECK-NEXT: %a1 = getelementptr %struct, %struct* %a, i32 0, i32 1, i32 0
+; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* %a0, i8 0, i64 16, i32 1, i1 false)
+; CHECK-NEXT: store i8 1, i8* %a1
+; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a0, i8* %b, i64 8, i32 1, i1 false)
+; CHECK-NEXT: ret void
+define void @test_intermediate_write(i8* %b) #0 {
+ %a = alloca %struct
+ %a0 = getelementptr %struct, %struct* %a, i32 0, i32 0, i32 0
+ %a1 = getelementptr %struct, %struct* %a, i32 0, i32 1, i32 0
+ call void @llvm.memset.p0i8.i64(i8* %a0, i8 0, i64 16, i32 1, i1 false)
+ store i8 1, i8* %a1
+ call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a0, i8* %b, i64 8, i32 1, i1 false)
+ ret void
+}
+
declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1)
declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture readonly, i64, i32, i1)
declare void @llvm.memset.p0i8.i32(i8* nocapture, i8, i32, i32, i1)
More information about the llvm-commits
mailing list