65#define DEBUG_TYPE "memcpyopt"
68 "enable-memcpyopt-without-libcalls",
cl::Hidden,
69 cl::desc(
"Enable memcpyopt even when libcalls are disabled"));
71STATISTIC(NumMemCpyInstr,
"Number of memcpy instructions deleted");
72STATISTIC(NumMemMoveInstr,
"Number of memmove instructions deleted");
73STATISTIC(NumMemSetInfer,
"Number of memsets inferred");
74STATISTIC(NumMoveToCpy,
"Number of memmoves converted to memcpy");
75STATISTIC(NumCpyToSet,
"Number of memcpys converted to memset");
76STATISTIC(NumCallSlot,
"Number of call slot optimizations performed");
77STATISTIC(NumStackMove,
"Number of stack-move optimizations performed");
106 bool isProfitableToUseMemset(
const DataLayout &
DL)
const;
114bool MemsetRange::isProfitableToUseMemset(
const DataLayout &
DL)
const {
116 if (TheStores.
size() >= 4 || End - Start >= 16)
120 if (TheStores.
size() < 2)
125 for (Instruction *SI : TheStores)
131 if (TheStores.size() == 2)
144 unsigned Bytes = unsigned(End - Start);
145 unsigned MaxIntSize =
DL.getLargestLegalIntTypeSizeInBits() / 8;
148 unsigned NumPointerStores = Bytes / MaxIntSize;
151 unsigned NumByteStores = Bytes % MaxIntSize;
156 return TheStores.size() > NumPointerStores + NumByteStores;
167 const DataLayout &
DL;
170 MemsetRanges(
const DataLayout &
DL) :
DL(
DL) {}
174 const_iterator
begin()
const {
return Ranges.begin(); }
175 const_iterator
end()
const {
return Ranges.end(); }
178 void addInst(int64_t OffsetFromFirst, Instruction *Inst) {
180 addStore(OffsetFromFirst, SI);
185 void addStore(int64_t OffsetFromFirst, StoreInst *SI) {
186 TypeSize StoreSize =
DL.getTypeStoreSize(
SI->getOperand(0)->getType());
189 SI->getPointerOperand(),
SI->getAlign(), SI);
192 void addMemSet(int64_t OffsetFromFirst, MemSetInst *MSI) {
206void MemsetRanges::addRange(int64_t Start, int64_t
Size,
Value *
Ptr,
207 MaybeAlign Alignment, Instruction *Inst) {
211 Ranges, [=](
const MemsetRange &O) {
return O.End <
Start; });
216 if (
I ==
Ranges.end() || End < I->Start) {
217 MemsetRange &
R = *
Ranges.insert(
I, MemsetRange());
221 R.Alignment = Alignment;
222 R.TheStores.push_back(Inst);
227 I->TheStores.push_back(Inst);
231 if (
I->Start <= Start &&
I->End >= End)
240 if (Start < I->Start) {
243 I->Alignment = Alignment;
251 range_iterator NextI =
I;
252 while (++NextI !=
Ranges.end() && End >= NextI->Start) {
254 I->TheStores.append(NextI->TheStores.begin(), NextI->TheStores.end());
255 if (NextI->End >
I->End)
271 assert(Start->getParent() == End->
getParent() &&
"Must be in same block");
273 if (Start->getFunction()->doesNotThrow())
278 bool RequiresNoCaptureBeforeUnwind;
280 RequiresNoCaptureBeforeUnwind) &&
281 !RequiresNoCaptureBeforeUnwind)
289void MemCpyOptPass::eraseInstruction(Instruction *
I) {
290 MSSAU->removeMemoryAccess(
I);
291 EEA->removeInstruction(
I);
292 I->eraseFromParent();
303 assert(Start->getBlock() == End->
getBlock() &&
"Only local supported");
309 if (
II &&
II->getIntrinsicID() == Intrinsic::lifetime_start &&
310 SkippedLifetimeStart && !*SkippedLifetimeStart) {
311 *SkippedLifetimeStart =
I;
330 return Start->getBlock() != End->
getBlock() ||
334 if (isa<MemoryUse>(&Acc))
336 Instruction *AccInst =
337 cast<MemoryUseOrDef>(&Acc)->getMemoryInst();
338 return isModSet(AA.getModRefInfo(AccInst, Loc));
352Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst,
359 if (
DL.getTypeStoreSize(
SI->getOperand(0)->getType()).isScalable())
373 MemoryUseOrDef *MemInsertPoint =
nullptr;
374 for (++BI; !BI->isTerminator(); ++BI) {
378 MemInsertPoint = CurrentAcc;
383 if (CB->onlyAccessesInaccessibleMemory())
391 if (BI->mayWriteToMemory() || BI->mayReadFromMemory())
398 if (!NextStore->isSimple())
401 Value *StoredVal = NextStore->getValueOperand();
409 if (
DL.getTypeStoreSize(StoredVal->
getType()).isScalable())
415 ByteVal = StoredByte;
416 if (ByteVal != StoredByte)
420 std::optional<int64_t>
Offset =
429 if (MSI->isVolatile() || ByteVal != MSI->getValue() ||
434 std::optional<int64_t>
Offset =
435 MSI->getDest()->getPointerOffsetFrom(StartPtr,
DL);
451 Ranges.addInst(0, StartInst);
461 for (
const MemsetRange &
Range : Ranges) {
462 if (
Range.TheStores.size() == 1)
466 if (!
Range.isProfitableToUseMemset(
DL))
471 StartPtr =
Range.StartPtr;
473 AMemSet = Builder.CreateMemSet(StartPtr, ByteVal,
Range.End -
Range.Start,
480 dbgs() <<
"With: " << *AMemSet <<
'\n');
481 if (!
Range.TheStores.empty())
486 ? MSSAU->createMemoryAccessBefore(AMemSet,
nullptr, MemInsertPoint)
487 : MSSAU->createMemoryAccessAfter(AMemSet,
nullptr, MemInsertPoint));
488 MSSAU->insertDef(NewDef,
true);
489 MemInsertPoint = NewDef;
492 for (Instruction *SI :
Range.TheStores)
505bool MemCpyOptPass::moveUp(StoreInst *SI, Instruction *
P,
const LoadInst *LI) {
513 DenseSet<Instruction *>
Args;
514 auto AddArg = [&](
Value *Arg) {
516 if (
I &&
I->getParent() ==
SI->getParent()) {
524 if (!AddArg(
SI->getPointerOperand()))
528 SmallVector<Instruction *, 8> ToLift{
SI};
538 for (
auto I = --
SI->getIterator(),
E =
P->getIterator();
I !=
E; --
I) {
546 bool MayAlias =
isModOrRefSet(AA->getModRefInfo(
C, std::nullopt));
548 bool NeedLift =
false;
568 if (
isModSet(AA->getModRefInfo(
C, LoadLoc)))
601 MemoryUseOrDef *MemInsertPoint =
nullptr;
602 if (MemoryUseOrDef *MA = MSSA->getMemoryAccess(
P)) {
608 if (MemoryUseOrDef *MA = MSSA->getMemoryAccess(&
I)) {
618 I->moveBefore(
P->getIterator());
619 assert(MemInsertPoint &&
"Must have found insert point");
620 if (MemoryUseOrDef *MA = MSSA->getMemoryAccess(
I)) {
621 MSSAU->moveAfter(MA, MemInsertPoint);
629bool MemCpyOptPass::processStoreOfLoad(StoreInst *SI, LoadInst *LI,
630 const DataLayout &
DL,
635 BatchAAResults BAA(*AA, EEA);
641 if (
T->isAggregateType() &&
643 (TLI->has(LibFunc_memcpy) && TLI->has(LibFunc_memmove)))) {
653 if (
isModSet(BAA.getModRefInfo(&
I, LoadLoc))) {
663 if (
P == SI || moveUp(SI,
P, LI)) {
668 bool UseMemMove =
false;
669 if (
isModSet(AA->getModRefInfo(SI, LoadLoc)))
674 Builder.CreateTypeSize(Builder.getInt64Ty(),
DL.getTypeStoreSize(
T));
677 M = Builder.CreateMemMove(
SI->getPointerOperand(),
SI->getAlign(),
681 M = Builder.CreateMemCpy(
SI->getPointerOperand(),
SI->getAlign(),
683 M->copyMetadata(*SI, LLVMContext::MD_DIAssignID);
685 LLVM_DEBUG(
dbgs() <<
"Promoting " << *LI <<
" to " << *SI <<
" => " << *M
689 auto *NewAccess = MSSAU->createMemoryAccessAfter(M,
nullptr, LastDef);
697 BBI =
M->getIterator();
705 auto GetCall = [&]() -> CallInst * {
709 MSSA->getWalker()->getClobberingMemoryAccess(LI, BAA)))
714 bool Changed = performCallSlotOptzn(
715 LI, SI,
SI->getPointerOperand()->stripPointerCasts(),
717 DL.getTypeStoreSize(
SI->getOperand(0)->getType()),
718 std::min(
SI->getAlign(), LI->
getAlign()), BAA, GetCall);
731 if (performStackMoveOptzn(LI, SI, DestAlloca, SrcAlloca,
732 DL.getTypeStoreSize(
T), BAA)) {
734 BBI =
SI->getNextNode()->getIterator();
756 if (
SI->getMetadata(LLVMContext::MD_nontemporal))
759 const DataLayout &
DL =
SI->getDataLayout();
761 Value *StoredVal =
SI->getValueOperand();
770 return processStoreOfLoad(SI, LI,
DL, BBI);
791 tryMergingIntoMemset(SI,
SI->getPointerOperand(), ByteVal)) {
792 BBI =
I->getIterator();
799 auto *
T =
V->getType();
800 if (!
T->isAggregateType())
803 TypeSize
Size =
DL.getTypeStoreSize(
T);
804 if (
Size.isScalable())
808 auto *
M = Builder.CreateMemSet(
SI->getPointerOperand(), ByteVal,
Size,
810 M->copyMetadata(*SI, LLVMContext::MD_DIAssignID);
812 LLVM_DEBUG(
dbgs() <<
"Promoting " << *SI <<
" to " << *M <<
"\n");
817 auto *NewAccess = MSSAU->createMemoryAccessBefore(M,
nullptr, StoreDef);
824 BBI =
M->getIterator();
834 BBI =
I->getIterator();
843bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpyLoad,
844 Instruction *cpyStore,
Value *cpyDest,
845 Value *cpySrc, TypeSize cpySize,
848 std::function<CallInst *()> GetC) {
877 TypeSize SrcAllocaSize =
DL.getTypeAllocSize(srcAlloca->getAllocatedType());
881 uint64_t srcSize = SrcAllocaSize * srcArraySize->
getZExtValue();
883 if (cpySize < srcSize)
886 CallInst *
C = GetC();
891 if (Function *
F =
C->getCalledFunction())
892 if (
F->isIntrinsic() &&
F->getIntrinsicID() == Intrinsic::lifetime_start)
895 if (
C->getParent() != cpyStore->
getParent()) {
900 MemoryLocation DestLoc =
903 : MemoryLocation::getForDest(
cast<MemCpyInst>(cpyStore));
909 MSSA->getMemoryAccess(cpyStore), &SkippedLifetimeStart)) {
910 LLVM_DEBUG(dbgs() <<
"Call Slot: Dest pointer modified after call\n");
917 if (SkippedLifetimeStart) {
920 if (LifetimeArg && LifetimeArg->getParent() ==
C->getParent() &&
921 C->comesBefore(LifetimeArg))
927 bool ExplicitlyDereferenceableOnly;
929 ExplicitlyDereferenceableOnly) ||
932 LLVM_DEBUG(
dbgs() <<
"Call Slot: Dest pointer not dereferenceable\n");
951 LLVM_DEBUG(
dbgs() <<
"Call Slot: Dest may be visible through unwinding\n");
956 Align srcAlign = srcAlloca->getAlign();
957 bool isDestSufficientlyAligned = srcAlign <= cpyDestAlign;
961 LLVM_DEBUG(
dbgs() <<
"Call Slot: Dest not sufficiently aligned\n");
970 while (!srcUseList.empty()) {
971 User *
U = srcUseList.pop_back_val();
980 if (U !=
C && U != cpyLoad) {
981 LLVM_DEBUG(
dbgs() <<
"Call slot: Source accessed by " << *U <<
"\n");
988 bool SrcIsCaptured =
any_of(
C->args(), [&](Use &U) {
989 return U->stripPointerCasts() == cpySrc &&
990 !C->doesNotCapture(C->getArgOperandNo(&U));
1007 MemoryLocation SrcLoc =
1009 for (Instruction &
I :
1010 make_range(++
C->getIterator(),
C->getParent()->end())) {
1013 if (
II->getIntrinsicID() == Intrinsic::lifetime_end &&
1014 II->getArgOperand(0) == srcAlloca)
1037 bool NeedMoveGEP =
false;
1038 if (!DT->dominates(cpyDest,
C)) {
1041 if (
GEP &&
GEP->hasAllConstantIndices() &&
1042 DT->dominates(
GEP->getPointerOperand(),
C))
1064 for (
unsigned ArgI = 0; ArgI <
C->arg_size(); ++ArgI)
1065 if (
C->getArgOperand(ArgI)->stripPointerCasts() == cpySrc &&
1066 cpySrc->
getType() !=
C->getArgOperand(ArgI)->getType())
1070 bool changedArgument =
false;
1071 for (
unsigned ArgI = 0; ArgI <
C->arg_size(); ++ArgI)
1072 if (
C->getArgOperand(ArgI)->stripPointerCasts() == cpySrc) {
1073 changedArgument =
true;
1074 C->setArgOperand(ArgI, cpyDest);
1077 if (!changedArgument)
1081 if (!isDestSufficientlyAligned) {
1088 GEP->moveBefore(
C->getIterator());
1091 if (SkippedLifetimeStart) {
1092 SkippedLifetimeStart->
moveBefore(
C->getIterator());
1093 MSSAU->moveBefore(MSSA->getMemoryAccess(SkippedLifetimeStart),
1094 MSSA->getMemoryAccess(
C));
1098 if (cpyLoad != cpyStore)
1107bool MemCpyOptPass::processMemCpyMemCpyDependence(MemCpyInst *M,
1109 BatchAAResults &BAA) {
1123 int64_t MForwardOffset = 0;
1124 const DataLayout &
DL =
M->getModule()->getDataLayout();
1127 if (
M->getSource() != MDep->
getDest()) {
1128 std::optional<int64_t>
Offset =
1129 M->getSource()->getPointerOffsetFrom(MDep->
getDest(),
DL);
1132 MForwardOffset = *
Offset;
1135 Value *CopyLength =
M->getLength();
1140 if (MForwardOffset != 0 || MDep->
getLength() != CopyLength) {
1146 if (!MDepLen || !MLen)
1148 if (MDepLen->getZExtValue() < MLen->getZExtValue() + MForwardOffset) {
1151 if (MDepLen->getZExtValue() <= (uint64_t)MForwardOffset)
1155 CopyLength = ConstantInt::get(CopyLength->
getType(),
1156 MDepLen->getZExtValue() - MForwardOffset);
1164 if (NewCopySource && NewCopySource->
use_empty())
1177 MCopyLoc = MCopyLoc.getWithNewSize(
1186 if (MForwardOffset > 0) {
1188 std::optional<int64_t> MDestOffset =
1190 if (MDestOffset == MForwardOffset)
1191 CopySource =
M->getDest();
1193 CopySource = Builder.CreateInBoundsPtrAdd(
1194 CopySource, Builder.getInt64(MForwardOffset));
1198 MCopyLoc = MCopyLoc.getWithNewPtr(CopySource);
1199 if (CopySourceAlign)
1212 if (
writtenBetween(MSSA, BAA, MCopyLoc, MSSA->getMemoryAccess(MDep),
1213 MSSA->getMemoryAccess(M)))
1229 bool UseMemMove =
false;
1234 if (
M->isForceInlined())
1240 LLVM_DEBUG(
dbgs() <<
"MemCpyOptPass: Forwarding memcpy->memcpy src:\n"
1248 NewM = Builder.CreateMemMove(
M->getDest(),
M->getDestAlign(), CopySource,
1249 CopySourceAlign, CopyLength,
M->isVolatile());
1250 else if (
M->isForceInlined())
1254 NewM = Builder.CreateMemCpyInline(
M->getDest(),
M->getDestAlign(),
1255 CopySource, CopySourceAlign, CopyLength,
1258 NewM = Builder.CreateMemCpy(
M->getDest(),
M->getDestAlign(), CopySource,
1259 CopySourceAlign, CopyLength,
M->isVolatile());
1265 auto *NewAccess = MSSAU->createMemoryAccessAfter(NewM,
nullptr, LastDef);
1293bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy,
1295 BatchAAResults &BAA) {
1318 MSSA->getMemoryAccess(MemSet),
1319 MSSA->getMemoryAccess(MemCpy)))
1331 if (DestSize == SrcSize) {
1354 "Preserving debug location based on moving memset within BB.");
1355 Builder.SetCurrentDebugLocation(MemSet->
getDebugLoc());
1361 SrcSize = Builder.CreateZExt(SrcSize, DestSize->
getType());
1363 DestSize = Builder.CreateZExt(DestSize, SrcSize->
getType());
1366 Value *Ule = Builder.CreateICmpULE(DestSize, SrcSize);
1367 Value *SizeDiff = Builder.CreateSub(DestSize, SrcSize);
1368 Value *MemsetLen = Builder.CreateSelect(
1369 Ule, ConstantInt::getNullValue(DestSize->
getType()), SizeDiff);
1375 Builder.CreateMemSet(Builder.CreatePtrAdd(Dest, SrcSize),
1376 MemSet->
getOperand(1), MemsetLen, Alignment);
1379 "MemCpy must be a MemoryDef");
1384 MSSAU->createMemoryAccessBefore(NewMemSet,
nullptr, LastDef);
1399 if (
II->getIntrinsicID() == Intrinsic::lifetime_start)
1401 return II->getArgOperand(0) == Alloca;
1435bool MemCpyOptPass::performMemCpyToMemSetOptzn(MemCpyInst *MemCpy,
1437 BatchAAResults &BAA) {
1441 int64_t MOffset = 0;
1446 std::optional<int64_t>
Offset =
1453 if (MOffset != 0 || MemSetSize != CopySize) {
1458 if (!CMemSetSize || !CCopySize ||
1459 CCopySize->getZExtValue() + MOffset > CMemSetSize->getZExtValue()) {
1463 if (CMemSetSize && CCopySize) {
1466 assert(CCopySize->getZExtValue() + MOffset >
1467 CMemSetSize->getZExtValue());
1469 CopySize = MemSetSize;
1472 ConstantInt::get(CopySize->
getType(),
1473 CMemSetSize->getZExtValue() <= (uint64_t)MOffset
1475 : CMemSetSize->getZExtValue() - MOffset);
1485 auto *NewAccess = MSSAU->createMemoryAccessAfter(NewM,
nullptr, LastDef);
1503bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
1504 AllocaInst *DestAlloca,
1505 AllocaInst *SrcAlloca, TypeSize
Size,
1506 BatchAAResults &BAA) {
1519 if (!SrcSize ||
Size != *SrcSize) {
1520 LLVM_DEBUG(
dbgs() <<
"Stack Move: Source alloca size mismatch\n");
1524 if (!DestSize ||
Size != *DestSize) {
1525 LLVM_DEBUG(
dbgs() <<
"Stack Move: Destination alloca size mismatch\n");
1537 SmallVector<Instruction *, 4> LifetimeMarkers;
1538 SmallPtrSet<Instruction *, 4> AAMetadataInstrs;
1539 bool SrcNotDom =
false;
1541 auto CaptureTrackingWithModRef =
1542 [&](
Instruction *AI, function_ref<bool(Instruction *)> ModRefCallback,
1543 bool &AddressCaptured) ->
bool {
1544 SmallVector<Instruction *, 8> Worklist;
1547 Worklist.
reserve(MaxUsesToExplore);
1548 SmallPtrSet<const Use *, 20> Visited;
1549 while (!Worklist.
empty()) {
1551 for (
const Use &U :
I->uses()) {
1555 if (!DT->dominates(SrcAlloca, UI))
1558 if (Visited.
size() >= MaxUsesToExplore) {
1561 <<
"Stack Move: Exceeded max uses to see ModRef, bailing\n");
1564 if (!Visited.
insert(&U).second)
1571 if (UI->mayReadOrWriteMemory()) {
1572 if (UI->isLifetimeStartOrEnd()) {
1581 AAMetadataInstrs.
insert(UI);
1583 if (!ModRefCallback(UI))
1598 ModRefInfo DestModRef = ModRefInfo::NoModRef;
1600 SmallVector<BasicBlock *, 8> ReachabilityWorklist;
1601 auto DestModRefCallback = [&](
Instruction *UI) ->
bool {
1611 if (UI->getParent() ==
Store->getParent()) {
1620 if (UI->comesBefore(Store))
1630 ReachabilityWorklist.
push_back(UI->getParent());
1636 bool DestAddressCaptured =
false;
1637 if (!CaptureTrackingWithModRef(DestAlloca, DestModRefCallback,
1638 DestAddressCaptured))
1641 if (!ReachabilityWorklist.
empty() &&
1643 nullptr, DT,
nullptr))
1651 auto SrcModRefCallback = [&](
Instruction *UI) ->
bool {
1654 if (PDT->dominates(Load, UI) || UI == Load || UI == Store)
1664 bool SrcAddressCaptured =
false;
1665 if (!CaptureTrackingWithModRef(SrcAlloca, SrcModRefCallback,
1666 SrcAddressCaptured))
1671 if (DestAddressCaptured && SrcAddressCaptured)
1678 SrcAlloca->
getParent()->getFirstInsertionPt());
1693 if (!LifetimeMarkers.
empty()) {
1694 for (Instruction *
I : LifetimeMarkers)
1703 for (Instruction *
I : AAMetadataInstrs) {
1704 I->setMetadata(LLVMContext::MD_alias_scope,
nullptr);
1705 I->setMetadata(LLVMContext::MD_noalias,
nullptr);
1706 I->setMetadata(LLVMContext::MD_tbaa,
nullptr);
1707 I->setMetadata(LLVMContext::MD_tbaa_struct,
nullptr);
1710 LLVM_DEBUG(
dbgs() <<
"Stack Move: Performed staack-move optimization\n");
1732 if (
M->isVolatile())
1736 if (
M->getSource() ==
M->getDest()) {
1749 MemoryUseOrDef *MA = MSSA->getMemoryAccess(M);
1756 if (GV->isConstant() && GV->hasDefinitiveInitializer())
1758 M->getDataLayout())) {
1761 M->getRawDest(), ByteVal,
M->getLength(),
M->getDestAlign(),
false);
1764 MSSAU->createMemoryAccessAfter(NewM,
nullptr, LastDef);
1772 BatchAAResults BAA(*AA, EEA);
1776 const MemoryAccess *DestClobber =
1777 MSSA->getWalker()->getClobberingMemoryAccess(AnyClobber, DestLoc, BAA);
1785 if (DestClobber->
getBlock() ==
M->getParent())
1786 if (processMemSetMemCpyDependence(M, MDep, BAA))
1789 MemoryAccess *SrcClobber = MSSA->getWalker()->getClobberingMemoryAccess(
1801 if (Instruction *
MI = MD->getMemoryInst()) {
1804 if (performCallSlotOptzn(M, M,
M->getDest(),
M->getSource(),
1806 M->getDestAlign().valueOrOne(), BAA,
1807 [
C]() -> CallInst * { return C; })) {
1809 <<
" call: " << *
C <<
"\n"
1810 <<
" memcpy: " << *M <<
"\n");
1818 if (processMemCpyMemCpyDependence(M, MDep, BAA))
1821 if (performMemCpyToMemSetOptzn(M, MDep, BAA)) {
1850 if (performStackMoveOptzn(M, M, DestAlloca, SrcAlloca,
1853 BBI =
M->getNextNode()->getIterator();
1864bool MemCpyOptPass::isMemMoveMemSetDependency(MemMoveInst *M) {
1865 const auto &
DL =
M->getDataLayout();
1866 MemoryUseOrDef *MemMoveAccess = MSSA->getMemoryAccess(M);
1872 auto *MemMoveSourceOp =
M->getSource();
1878 LocationSize MemMoveLocSize = SourceLoc.
Size;
1879 if (
Source->getPointerOperand() !=
M->getDest() ||
1885 uint64_t MemMoveSize = MemMoveLocSize.
getValue();
1886 LocationSize TotalSize =
1888 MemoryLocation CombinedLoc(
M->getDest(), TotalSize);
1892 BatchAAResults BAA(*AA);
1895 MSSA->getWalker()->getClobberingMemoryAccess(FirstDef, CombinedLoc, BAA));
1905 if (!MemSetLength || MemSetLength->getZExtValue() < MemMoveSize)
1922 if (!
M->isVolatile() && isMemMoveMemSetDependency(M)) {
1932 LLVM_DEBUG(
dbgs() <<
"MemCpyOptPass: Optimizing memmove -> memcpy: " << *M
1936 Type *ArgTys[3] = {
M->getRawDest()->getType(),
M->getRawSource()->getType(),
1937 M->getLength()->getType()};
1939 M->getModule(), Intrinsic::memcpy, ArgTys));
1949bool MemCpyOptPass::processByValArgument(CallBase &CB,
unsigned ArgNo) {
1954 TypeSize ByValSize =
DL.getTypeAllocSize(ByValTy);
1956 MemoryUseOrDef *CallAccess = MSSA->getMemoryAccess(&CB);
1959 MemCpyInst *MDep =
nullptr;
1960 BatchAAResults BAA(*AA, EEA);
1961 MemoryAccess *Clobber = MSSA->getWalker()->getClobberingMemoryAccess(
1975 if (!C1 || !TypeSize::isKnownGE(
1988 if ((!MemDepAlign || *MemDepAlign < *ByValAlign) &&
2004 MSSA->getMemoryAccess(MDep), CallAccess))
2007 LLVM_DEBUG(
dbgs() <<
"MemCpyOptPass: Forwarding memcpy to byval:\n"
2008 <<
" " << *MDep <<
"\n"
2009 <<
" " << CB <<
"\n");
2032bool MemCpyOptPass::processImmutArgument(CallBase &CB,
unsigned ArgNo) {
2033 BatchAAResults BAA(*AA, EEA);
2057 std::optional<TypeSize> AllocaSize = AI->getAllocationSize(
DL);
2060 if (!AllocaSize || AllocaSize->isScalable())
2063 MemoryUseOrDef *CallAccess = MSSA->getMemoryAccess(&CB);
2067 MemCpyInst *MDep =
nullptr;
2068 MemoryAccess *Clobber = MSSA->getWalker()->getClobberingMemoryAccess(
2084 if (!MDepLen || AllocaSize != MDepLen->getValue())
2091 Align AllocaAlign = AI->getAlign();
2092 if (MemDepAlign < AllocaAlign &&
2104 MSSA->getMemoryAccess(MDep), CallAccess))
2111 LLVM_DEBUG(
dbgs() <<
"MemCpyOptPass: Forwarding memcpy to Immut src:\n"
2112 <<
" " << *MDep <<
"\n"
2113 <<
" " << CB <<
"\n");
2123bool MemCpyOptPass::iterateOnFunction(Function &
F) {
2124 bool MadeChange =
false;
2127 for (BasicBlock &BB :
F) {
2132 if (!DT->isReachableFromEntry(&BB))
2139 bool RepeatInstruction =
false;
2142 MadeChange |= processStore(SI, BI);
2144 RepeatInstruction = processMemSet(M, BI);
2146 RepeatInstruction = processMemCpy(M, BI);
2148 RepeatInstruction = processMemMove(M, BI);
2150 for (
unsigned i = 0, e = CB->
arg_size(); i != e; ++i) {
2152 MadeChange |= processByValArgument(*CB, i);
2154 MadeChange |= processImmutArgument(*CB, i);
2159 if (RepeatInstruction) {
2160 if (BI != BB.
begin())
2178 bool MadeChange =
runImpl(
F, &TLI, AA, AC, DT, PDT, &MSSA->getMSSA());
2192 bool MadeChange =
false;
2205 if (!iterateOnFunction(
F))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseSet and SmallDenseSet classes.
static bool runImpl(Function &F, const TargetLowering &TLI, AssumptionCache *AC)
This is the interface for a simple mod/ref and alias analysis over globals.
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo, MemorySSAUpdater &MSSAU)
static bool mayBeVisibleThroughUnwinding(Value *V, Instruction *Start, Instruction *End)
static bool isZeroSize(Value *Size)
static bool hasUndefContents(MemorySSA *MSSA, BatchAAResults &AA, Value *V, MemoryDef *Def)
Determine whether the pointer V had only undefined content (due to Def), either because it was freshl...
static bool accessedBetween(BatchAAResults &AA, MemoryLocation Loc, const MemoryUseOrDef *Start, const MemoryUseOrDef *End, Instruction **SkippedLifetimeStart=nullptr)
static bool overreadUndefContents(MemorySSA *MSSA, MemCpyInst *MemCpy, MemIntrinsic *MemSrc, BatchAAResults &BAA)
static cl::opt< bool > EnableMemCpyOptWithoutLibcalls("enable-memcpyopt-without-libcalls", cl::Hidden, cl::desc("Enable memcpyopt even when libcalls are disabled"))
static bool writtenBetween(MemorySSA *MSSA, BatchAAResults &AA, MemoryLocation Loc, const MemoryUseOrDef *Start, const MemoryUseOrDef *End)
This file provides utility analysis objects describing memory locations.
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
This file contains the declarations for profiling metadata utility functions.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
A manager for alias analyses.
LLVM_ABI bool isStaticAlloca() const
Return true if this alloca is in the entry block of the function and is a constant size.
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
unsigned getAddressSpace() const
Return the address space for the allocation.
LLVM_ABI std::optional< TypeSize > getAllocationSize(const DataLayout &DL) const
Get allocation size in bytes.
void setAlignment(Align Align)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
iterator begin()
Instruction iterator methods.
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
InstListType::iterator iterator
Instruction iterators...
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
ModRefInfo getModRefInfo(const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
ModRefInfo callCapturesBefore(const Instruction *I, const MemoryLocation &MemLoc, DominatorTree *DT)
Represents analyses that only rely on functions' control flow.
bool doesNotCapture(unsigned OpNo) const
Determine whether this data operand is not captured.
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
bool isByValArgument(unsigned ArgNo) const
Determine whether this argument is passed by value.
MaybeAlign getParamAlign(unsigned ArgNo) const
Extract the alignment for a call or parameter (0=unknown).
bool onlyReadsMemory(unsigned OpNo) const
Type * getParamByValType(unsigned ArgNo) const
Extract the byval type for a call or parameter.
Value * getArgOperand(unsigned i) const
void setArgOperand(unsigned i, Value *v)
unsigned arg_size() const
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
A parsed version of the target data layout string in and methods for querying it.
Analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Context-sensitive CaptureAnalysis provider, which computes and caches the earliest common dominator c...
LLVM_ABI void mergeDIAssignID(ArrayRef< const Instruction * > SourceInstructions)
Merge the DIAssignID metadata from this instruction and those attached to instructions in SourceInstr...
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
LLVM_ABI void dropUnknownNonDebugMetadata(ArrayRef< unsigned > KnownIDs={})
Drop all unknown metadata except for debug locations.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM_ABI void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
Value * getPointerOperand()
Align getAlign() const
Return the alignment of the access that is being performed.
static LocationSize precise(uint64_t Value)
TypeSize getValue() const
This class wraps the llvm.memcpy intrinsic.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
bool runImpl(Function &F, TargetLibraryInfo *TLI, AAResults *AA, AssumptionCache *AC, DominatorTree *DT, PostDominatorTree *PDT, MemorySSA *MSSA)
Value * getLength() const
Value * getRawDest() const
Value * getDest() const
This is just like getRawDest, but it strips off any cast instructions (including addrspacecast) that ...
MaybeAlign getDestAlign() const
This is the common base class for memset/memcpy/memmove.
Value * getRawSource() const
Return the arguments to the instruction.
MaybeAlign getSourceAlign() const
Value * getSource() const
This is just like getRawSource, but it strips off any cast instructions that feed it,...
BasicBlock * getBlock() const
AllAccessType::self_iterator getIterator()
Get the iterators for the all access list and the defs only list We default to the all access list.
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
Representation for a specific memory location.
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
static LLVM_ABI MemoryLocation getForSource(const MemTransferInst *MTI)
Return a location representing the source of a memory transfer.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
static MemoryLocation getBeforeOrAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location before or after Ptr, while remaining within the underl...
static LLVM_ABI MemoryLocation getForDest(const MemIntrinsic *MI)
Return a location representing the destination of a memory set or transfer.
An analysis that produces MemorySSA for a function.
MemoryAccess * getClobberingMemoryAccess(const Instruction *I, BatchAAResults &AA)
Given a memory Mod/Ref/ModRef'ing instruction, calling this will give you the nearest dominating Memo...
Encapsulates MemorySSA, including all data associated with memory accesses.
LLVM_ABI bool dominates(const MemoryAccess *A, const MemoryAccess *B) const
Given two memory accesses in potentially different blocks, determine whether MemoryAccess A dominates...
LLVM_ABI void verifyMemorySSA(VerificationLevel=VerificationLevel::Fast) const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
LLVM_ABI MemorySSAWalker * getWalker()
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
Class that has the common methods + fields of memory uses/defs.
MemoryAccess * getDefiningAccess() const
Get the access that produces the memory state used by this Use.
Instruction * getMemoryInst() const
Get the instruction that this MemoryUse represents.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Analysis pass which computes a PostDominatorTree.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
PreservedAnalyses & preserve()
Mark an analysis as preserved.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
void reserve(size_type N)
typename SuperClass::const_iterator const_iterator
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
typename SuperClass::iterator iterator
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
LLVM_ABI unsigned getIntegerBitWidth() const
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
bool hasOneUse() const
Return true if there is exactly one use of this value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
LLVM_ABI std::optional< int64_t > getPointerOffsetFrom(const Value *Other, const DataLayout &DL) const
If this ptr is provably equal to Other plus a constant offset, return that offset in bytes.
constexpr ScalarTy getFixedValue() const
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
const ParentTy * getParent() const
reverse_self_iterator getReverseIterator()
self_iterator getIterator()
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
Abstract Attribute helper functions.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ C
The default llvm calling convention, compatible with C.
@ BasicBlock
Various leaf nodes.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
@ User
could "use" a pointer
friend class Instruction
Iterator for Instructions in a `BasicBlock.
LLVM_ABI iterator begin() const
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
bool capturesAddress(CaptureComponents CC)
LLVM_ABI bool isPotentiallyReachableFromMany(SmallVectorImpl< BasicBlock * > &Worklist, const BasicBlock *StopBB, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether there is at least one path from a block in 'Worklist' to 'StopBB' without passing t...
LLVM_ABI void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I, StringRef PassName, const Function *F=nullptr)
Like setExplicitlyUnknownBranchWeights(...), but only sets unknown branch weights in the new instruct...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
auto cast_or_null(const Y &Val)
LLVM_ABI unsigned getDefaultMaxUsesToExploreForCaptureTracking()
getDefaultMaxUsesToExploreForCaptureTracking - Return default value of the maximal number of uses to ...
LLVM_ABI bool PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures, const Instruction *I, const DominatorTree *DT, bool IncludeI=false, unsigned MaxUsesToExplore=0, const LoopInfo *LI=nullptr)
PointerMayBeCapturedBefore - Return true if this pointer value may be captured by the enclosing funct...
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
auto dyn_cast_or_null(const Y &Val)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
LLVM_ABI Align getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
Try to ensure that the alignment of V is at least PrefAlign bytes.
bool isModSet(const ModRefInfo MRI)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isModOrRefSet(const ModRefInfo MRI)
LLVM_ABI bool isNotVisibleOnUnwind(const Value *Object, bool &RequiresNoCaptureBeforeUnwind)
Return true if Object memory is not visible after an unwind, in the sense that program semantics cann...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
LLVM_ABI bool VerifyMemorySSA
Enables verification of MemorySSA.
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
DWARFExpression::Operation Op
LLVM_ABI bool isIdentifiedFunctionLocal(const Value *V)
Return true if V is umabigously identified at the function-level.
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
LLVM_ABI void combineAAMetadata(Instruction *K, const Instruction *J)
Combine metadata of two instructions, where instruction J is a memory access that has been merged int...
bool capturesAnything(CaptureComponents CC)
LLVM_ABI UseCaptureInfo DetermineUseCaptureKind(const Use &U, const Value *Base)
Determine what kind of capture behaviour U may exhibit.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
bool capturesAnyProvenance(CaptureComponents CC)
bool isRefSet(const ModRefInfo MRI)
LLVM_ABI bool isWritableObject(const Value *Object, bool &ExplicitlyDereferenceableOnly)
Return true if the Object is writable, in the sense that any location based on this pointer that can ...
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
CaptureComponents UseCC
Components captured by this use.
CaptureComponents ResultCC
Components captured by the return value of the user of this Use.