[llvm] r349002 - Revert r348645 - "[MemCpyOpt] memset->memcpy forwarding with undef tail"
David L. Jones via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 12 19:15:11 PST 2018
Author: dlj
Date: Wed Dec 12 19:15:11 2018
New Revision: 349002
URL: http://llvm.org/viewvc/llvm-project?rev=349002&view=rev
Log:
Revert r348645 - "[MemCpyOpt] memset->memcpy forwarding with undef tail"
This revision caused trucated memsets for structs with padding. See:
http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20181210/610520.html
Modified:
llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp
llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp
llvm/trunk/test/Transforms/MemCpyOpt/memset-memcpy-oversized.ll
Modified: llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp?rev=349002&r1=349001&r2=349002&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp Wed Dec 12 19:15:11 2018
@@ -154,12 +154,6 @@ static ModRefInfo GetLocation(const Inst
return ModRefInfo::Mod;
}
- if (const MemSetInst *MI = dyn_cast<MemSetInst>(Inst)) {
- Loc = MemoryLocation::getForDest(MI);
- // Conversatively assume ModRef for volatile memset.
- return MI->isVolatile() ? ModRefInfo::ModRef : ModRefInfo::Mod;
- }
-
if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
switch (II->getIntrinsicID()) {
case Intrinsic::lifetime_start:
Modified: llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp?rev=349002&r1=349001&r2=349002&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp Wed Dec 12 19:15:11 2018
@@ -1144,21 +1144,6 @@ bool MemCpyOptPass::processMemSetMemCpyD
return true;
}
-/// Determine whether the instruction has undefined content for the given Size,
-/// either because it was freshly alloca'd or started its lifetime.
-static bool hasUndefContents(Instruction *I, ConstantInt *Size) {
- if (isa<AllocaInst>(I))
- return true;
-
- if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
- if (II->getIntrinsicID() == Intrinsic::lifetime_start)
- if (ConstantInt *LTSize = dyn_cast<ConstantInt>(II->getArgOperand(0)))
- if (LTSize->getZExtValue() >= Size->getZExtValue())
- return true;
-
- return false;
-}
-
/// Transform memcpy to memset when its source was just memset.
/// In other words, turn:
/// \code
@@ -1182,23 +1167,12 @@ bool MemCpyOptPass::performMemCpyToMemSe
if (!AA.isMustAlias(MemSet->getRawDest(), MemCpy->getRawSource()))
return false;
- // A known memset size is required.
+ ConstantInt *CopySize = cast<ConstantInt>(MemCpy->getLength());
ConstantInt *MemSetSize = dyn_cast<ConstantInt>(MemSet->getLength());
- if (!MemSetSize)
- return false;
-
// Make sure the memcpy doesn't read any more than what the memset wrote.
// Don't worry about sizes larger than i64.
- ConstantInt *CopySize = cast<ConstantInt>(MemCpy->getLength());
- if (CopySize->getZExtValue() > MemSetSize->getZExtValue()) {
- // If the memcpy is larger than the memset, but the memory was undef prior
- // to the memset, we can just ignore the tail.
- MemDepResult DepInfo = MD->getDependency(MemSet);
- if (DepInfo.isDef() && hasUndefContents(DepInfo.getInst(), CopySize))
- CopySize = MemSetSize;
- else
- return false;
- }
+ if (!MemSetSize || CopySize->getZExtValue() > MemSetSize->getZExtValue())
+ return false;
IRBuilder<> Builder(MemCpy);
Builder.CreateMemSet(MemCpy->getRawDest(), MemSet->getOperand(1),
@@ -1278,7 +1252,19 @@ bool MemCpyOptPass::processMemCpy(MemCpy
if (MemCpyInst *MDep = dyn_cast<MemCpyInst>(SrcDepInfo.getInst()))
return processMemCpyMemCpyDependence(M, MDep);
} else if (SrcDepInfo.isDef()) {
- if (hasUndefContents(SrcDepInfo.getInst(), CopySize)) {
+ Instruction *I = SrcDepInfo.getInst();
+ bool hasUndefContents = false;
+
+ if (isa<AllocaInst>(I)) {
+ hasUndefContents = true;
+ } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
+ if (II->getIntrinsicID() == Intrinsic::lifetime_start)
+ if (ConstantInt *LTSize = dyn_cast<ConstantInt>(II->getArgOperand(0)))
+ if (LTSize->getZExtValue() >= CopySize->getZExtValue())
+ hasUndefContents = true;
+ }
+
+ if (hasUndefContents) {
MD->removeInstruction(M);
M->eraseFromParent();
++NumMemCpyInstr;
Modified: llvm/trunk/test/Transforms/MemCpyOpt/memset-memcpy-oversized.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/MemCpyOpt/memset-memcpy-oversized.ll?rev=349002&r1=349001&r2=349002&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/MemCpyOpt/memset-memcpy-oversized.ll (original)
+++ llvm/trunk/test/Transforms/MemCpyOpt/memset-memcpy-oversized.ll Wed Dec 12 19:15:11 2018
@@ -12,7 +12,7 @@ define void @test_alloca(i8* %result) {
; CHECK-NEXT: [[A:%.*]] = alloca [[T:%.*]], align 8
; CHECK-NEXT: [[B:%.*]] = bitcast %T* [[A]] to i8*
; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 8 [[B]], i8 0, i64 12, i1 false)
-; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* [[RESULT:%.*]], i8 0, i64 12, i1 false)
+; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[RESULT:%.*]], i8* align 8 [[B]], i64 16, i1 false)
; CHECK-NEXT: ret void
;
%a = alloca %T, align 8
@@ -28,7 +28,7 @@ define void @test_alloca_with_lifetimes(
; CHECK-NEXT: [[B:%.*]] = bitcast %T* [[A]] to i8*
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 16, i8* [[B]])
; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 8 [[B]], i8 0, i64 12, i1 false)
-; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* [[RESULT:%.*]], i8 0, i64 12, i1 false)
+; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[RESULT:%.*]], i8* align 8 [[B]], i64 16, i1 false)
; CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 16, i8* [[B]])
; CHECK-NEXT: ret void
;
@@ -46,7 +46,7 @@ define void @test_malloc_with_lifetimes(
; CHECK-NEXT: [[A:%.*]] = call i8* @malloc(i64 16)
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 16, i8* [[A]])
; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 8 [[A]], i8 0, i64 12, i1 false)
-; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* [[RESULT:%.*]], i8 0, i64 12, i1 false)
+; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[RESULT:%.*]], i8* align 8 [[A]], i64 16, i1 false)
; CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 16, i8* [[A]])
; CHECK-NEXT: call void @free(i8* [[A]])
; CHECK-NEXT: ret void
@@ -98,7 +98,7 @@ define void @test_volatile_memset(i8* %r
; CHECK-NEXT: [[A:%.*]] = alloca [[T:%.*]], align 8
; CHECK-NEXT: [[B:%.*]] = bitcast %T* [[A]] to i8*
; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 8 [[B]], i8 0, i64 12, i1 true)
-; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* [[RESULT:%.*]], i8 0, i64 12, i1 false)
+; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[RESULT:%.*]], i8* align 8 [[B]], i64 16, i1 false)
; CHECK-NEXT: ret void
;
%a = alloca %T, align 8
More information about the llvm-commits
mailing list