Index: llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp =================================================================== --- llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -65,6 +65,7 @@ #include "llvm/IR/PassManager.h" #include "llvm/IR/PatternMatch.h" #include "llvm/IR/Value.h" +#include "llvm/IR/IRBuilder.h" #include "llvm/InitializePasses.h" #include "llvm/Pass.h" #include "llvm/Support/Casting.h" @@ -76,6 +77,7 @@ #include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Utils/AssumeBundleBuilder.h" +#include "llvm/Transforms/Utils/BuildLibCalls.h" #include "llvm/Transforms/Utils/Local.h" #include #include @@ -622,7 +624,11 @@ BasicBlock::iterator SecondBBI(SecondI); BasicBlock *FirstBB = FirstI->getParent(); BasicBlock *SecondBB = SecondI->getParent(); - MemoryLocation MemLoc = MemoryLocation::get(SecondI); + MemoryLocation MemLoc; + if (auto *MemSet = dyn_cast(SecondI)) + MemLoc = MemoryLocation::getForDest(MemSet); + else + MemLoc = MemoryLocation::get(SecondI); auto *MemLocPtr = const_cast(MemLoc.Ptr); // Start checking the SecondBB. @@ -926,13 +932,17 @@ } // Check if we can ignore \p D for DSE. -bool canSkipDef(MemoryDef *D, bool DefVisibleToCaller) { +bool canSkipDef(MemoryDef *D, bool DefVisibleToCaller, + const TargetLibraryInfo &TLI) { Instruction *DI = D->getMemoryInst(); // Calls that only access inaccessible memory cannot read or write any memory // locations we consider for elimination. if (auto *CB = dyn_cast(DI)) - if (CB->onlyAccessesInaccessibleMemory()) - return true; + if (CB->onlyAccessesInaccessibleMemory()) { + if (isAllocLikeFn(DI, &TLI)) + return false; + return true; + } // We can eliminate stores to locations not visible to the caller across // throwing instructions. @@ -948,7 +958,7 @@ return true; // Skip intrinsics that do not really read or modify memory. - if (isNoopIntrinsic(D->getMemoryInst())) + if (isNoopIntrinsic(DI)) return true; return false; @@ -1346,7 +1356,7 @@ MemoryDef *CurrentDef = cast(Current); Instruction *CurrentI = CurrentDef->getMemoryInst(); - if (canSkipDef(CurrentDef, !isInvisibleToCallerBeforeRet(DefUO))) { + if (canSkipDef(CurrentDef, !isInvisibleToCallerBeforeRet(DefUO), TLI)) { StepAgain = true; Current = CurrentDef->getDefiningAccess(); continue; @@ -1778,14 +1788,34 @@ StoredConstant = dyn_cast(MemSet->getValue()); if (StoredConstant && StoredConstant->isNullValue()) { - auto *DefUOInst = dyn_cast(DefUO); - if (DefUOInst && isCallocLikeFn(DefUOInst, &TLI)) { - auto *UnderlyingDef = cast(MSSA.getMemoryAccess(DefUOInst)); - // If UnderlyingDef is the clobbering access of Def, no instructions - // between them can modify the memory location. - auto *ClobberDef = - MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def); - return UnderlyingDef == ClobberDef; + Instruction *DefUOInst = const_cast(dyn_cast(DefUO)); + if (DefUOInst) { + if (isCallocLikeFn(DefUOInst, &TLI)) { + auto *UnderlyingDef = cast(MSSA.getMemoryAccess(DefUOInst)); + // If UnderlyingDef is the clobbering access of Def, no instructions + // between them can modify the memory location. + auto *ClobberDef = + MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def); + return UnderlyingDef == ClobberDef; + } + + if (isMallocLikeFn(DefUOInst, &TLI) && MemSet && + cast(DefUOInst)->getOperand(0) == MemSet->getLength()) { + auto *Malloc = cast(DefUOInst); + if (DT.dominates(DefUOInst, cast(MemSet)) && + memoryIsNotModifiedBetween(Malloc, MemSet, BatchAA, DL, &DT)) { + IRBuilder<> IRB(Malloc); + const DataLayout &DL = Malloc->getModule()->getDataLayout(); + IntegerType *SizeType = DL.getIntPtrType(IRB.GetInsertBlock()->getContext()); + if (Value *Calloc = emitCalloc(ConstantInt::get(SizeType, 1), + Malloc->getArgOperand(0), Malloc->getAttributes(), IRB, TLI)) { + Malloc->replaceAllUsesWith(Calloc); + Malloc->eraseFromParent(); + return true; + } + return false; + } + } } } @@ -1840,6 +1870,7 @@ } }; + bool eliminateDeadStores(Function &F, AliasAnalysis &AA, MemorySSA &MSSA, DominatorTree &DT, PostDominatorTree &PDT, const TargetLibraryInfo &TLI) { Index: llvm/test/Transforms/DeadStoreElimination/noop-stores.ll =================================================================== --- llvm/test/Transforms/DeadStoreElimination/noop-stores.ll +++ llvm/test/Transforms/DeadStoreElimination/noop-stores.ll @@ -309,6 +309,36 @@ ret void } +declare noalias i8* @malloc(i64) +declare void @clobber_memory(float*) + +; based on pr25892_lite +define i8* @zero_memset_after_malloc(i64 %size) { +; CHECK-LABEL: @zero_memset_after_malloc( +; CHECK-NEXT: [[CALL:%.*]] = call i8* @calloc(i64 1, i64 [[SIZE:%.*]]) +; CHECK-NEXT: ret i8* [[CALL]] +; + %call = call i8* @malloc(i64 %size) inaccessiblememonly + call void @llvm.memset.p0i8.i64(i8* %call, i8 0, i64 %size, i1 false) + ret i8* %call +} + +; based on pr25892_lite +define i8* @zero_memset_after_malloc_with_intermediate_clobbering(i64 %size) { +; CHECK-LABEL: @zero_memset_after_malloc_with_intermediate_clobbering( +; CHECK-NEXT: [[CALL:%.*]] = call i8* @malloc(i64 [[SIZE:%.*]]) +; CHECK-NEXT: [[BC:%.*]] = bitcast i8* [[CALL]] to float* +; CHECK-NEXT: call void @clobber_memory(float* [[BC]]) +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* [[CALL]], i8 0, i64 [[SIZE]], i1 false) +; CHECK-NEXT: ret i8* [[CALL]] +; + %call = call i8* @malloc(i64 %size) inaccessiblememonly + %bc = bitcast i8* %call to float* + call void @clobber_memory(float* %bc) + call void @llvm.memset.p0i8.i64(i8* %call, i8 0, i64 %size, i1 false) + ret i8* %call +} + ; PR50143 define i8* @store_zero_after_calloc_inaccessiblememonly() { ; CHECK-LABEL: @store_zero_after_calloc_inaccessiblememonly(