From 77d5fb40c8f71a5bef45e7ad8aeb8a444a6e3df4 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Wed, 26 Mar 2014 23:45:15 +0000 Subject: Treat lifetime.start'd memory like we treat freshly alloca'd memory. Patch by Björn Steinbrink! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit llvm-svn: 204876 --- llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'llvm/lib/Transforms') diff --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp index 143ba38ec98..2603c969c5a 100644 --- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp +++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp @@ -851,9 +851,9 @@ bool MemCpyOpt::processMemCpy(MemCpyInst *M) { // The are three possible optimizations we can do for memcpy: // a) memcpy-memcpy xform which exposes redundance for DSE. // b) call-memcpy xform for return slot optimization. - // c) memcpy from freshly alloca'd space copies undefined data, and we can - // therefore eliminate the memcpy in favor of the data that was already - // at the destination. + // c) memcpy from freshly alloca'd space or space that has just started its + // lifetime copies undefined data, and we can therefore eliminate the + // memcpy in favor of the data that was already at the destination. MemDepResult DepInfo = MD->getDependency(M); if (DepInfo.isClobber()) { if (CallInst *C = dyn_cast(DepInfo.getInst())) { @@ -874,7 +874,19 @@ bool MemCpyOpt::processMemCpy(MemCpyInst *M) { if (MemCpyInst *MDep = dyn_cast(SrcDepInfo.getInst())) return processMemCpyMemCpyDependence(M, MDep, CopySize->getZExtValue()); } else if (SrcDepInfo.isDef()) { - if (isa(SrcDepInfo.getInst())) { + Instruction *I = SrcDepInfo.getInst(); + bool hasUndefContents = false; + + if (isa(I)) { + hasUndefContents = true; + } else if (IntrinsicInst *II = dyn_cast(I)) { + if (II->getIntrinsicID() == Intrinsic::lifetime_start) + if (ConstantInt *LTSize = dyn_cast(II->getArgOperand(0))) + if (LTSize->getZExtValue() >= CopySize->getZExtValue()) + hasUndefContents = true; + } + + if (hasUndefContents) { MD->removeInstruction(M); M->eraseFromParent(); ++NumMemCpyInstr; -- cgit v1.2.3