From 8eca988b00ad9dde185183c664f422615ef87b48 Mon Sep 17 00:00:00 2001 From: Ahmed Bougacha Date: Thu, 21 May 2015 01:43:39 +0000 Subject: [PATCH] [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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@237874 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/MemCpyOptimizer.cpp | 9 +++++++- .../memset-memcpy-redundant-memset.ll | 22 ++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/lib/Transforms/Scalar/MemCpyOptimizer.cpp index a90204d26bd..66d6ac6f3a0 100644 --- a/lib/Transforms/Scalar/MemCpyOptimizer.cpp +++ b/lib/Transforms/Scalar/MemCpyOptimizer.cpp @@ -859,6 +859,13 @@ bool MemCpyOpt::processMemSetMemCpyDependence(MemCpyInst *MemCpy, 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::processMemSetMemCpyDependence(MemCpyInst *MemCpy, if (ConstantInt *SrcSizeC = dyn_cast(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()) { diff --git a/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll b/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll index 25411f26982..993c8a1bb2d 100644 --- a/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll +++ b/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll @@ -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, i8* %b) #0 { 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)