Lines Matching full:memcpy
1 //===- MemCpyOptimizer.cpp - Optimize use of memcpy and friends -----------===//
9 // This pass performs various transformations related to eliminating memcpy
71 STATISTIC(NumMemCpyInstr, "Number of memcpy instructions deleted");
73 STATISTIC(NumMoveToCpy, "Number of memmoves converted to memcpy");
357 /// together into a memcpy/memset.
396 // A[1] = 2; strlen(A); A[2] = 2; -> memcpy(A, ...); strlen(A). in tryMergingIntoMemset()
642 // Don't introduce calls to memcpy/memmove intrinsics out of thin air if in processStoreOfLoad()
674 // the load/store pair to a memcpy. in processStoreOfLoad()
677 // memmove must be used to preserve semantic. If not, memcpy can in processStoreOfLoad()
678 // be used. Also, if we load from constant memory, memcpy can be used in processStoreOfLoad()
717 // a memcpy. in processStoreOfLoad()
765 // memcpy/memset would not be able to preserve the nontemporal hint. in processStore()
782 // Load to store forwarding can be interpreted as memcpy. in processStore()
793 // There are two cases that are interesting for this code to handle: memcpy in processStore()
850 /// Takes a memcpy and a call that it depends on,
852 /// the call write its result directly into the destination of the memcpy.
862 // memcpy(dest, src, ...) in performCallSlotOptzn()
866 // memcpy(dest, src, ...) in performCallSlotOptzn()
869 // Since moving the memcpy is technically awkward, we additionally check that in performCallSlotOptzn()
871 // the memcpy can be discarded rather than moved. in performCallSlotOptzn()
916 // the call and the store/memcpy. in performCallSlotOptzn()
975 // Check that src is not accessed except via the call and the memcpy. This in performCallSlotOptzn()
977 // memcpy can be dropped), that it is not read or written between the call and in performCallSlotOptzn()
978 // the memcpy, and that writing beyond the end of it is undefined. in performCallSlotOptzn()
1123 /// We've found that the (upward scanning) memory dependence of memcpy 'M' is
1124 /// the memcpy 'MDep'. Try to simplify M to copy from MDep's input if we can.
1131 // memcpy(a <- a) in processMemCpyMemCpyDependence()
1132 // memcpy(b <- a) in processMemCpyMemCpyDependence()
1136 // We can only optimize non-volatile memcpy's. in processMemCpyMemCpyDependence()
1142 // We can only transforms memcpy's where the dest of one is the source of the in processMemCpyMemCpyDependence()
1152 // The length of the memcpy's must be the same, or the preceding one in processMemCpyMemCpyDependence()
1180 // memcpy(d1 <- s1) in processMemCpyMemCpyDependence()
1181 // memcpy(d2 <- d1+o) in processMemCpyMemCpyDependence()
1183 // memcpy(d2 <- s1+o) in processMemCpyMemCpyDependence()
1203 // memcpy(a <- b) in processMemCpyMemCpyDependence()
1205 // memcpy(c <- a) in processMemCpyMemCpyDependence()
1206 // It would be invalid to transform the second memcpy into memcpy(c <- b). in processMemCpyMemCpyDependence()
1209 // then we could still perform the xform by moving M up to the first memcpy. in processMemCpyMemCpyDependence()
1214 // No need to create `memcpy(a <- a)`. in processMemCpyMemCpyDependence()
1226 // memmove instead of memcpy. in processMemCpyMemCpyDependence()
1229 // Don't convert llvm.memcpy.inline into memmove because memmove can be in processMemCpyMemCpyDependence()
1230 // lowered as a call, and that is not allowed for llvm.memcpy.inline (and in processMemCpyMemCpyDependence()
1238 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Forwarding memcpy->memcpy src:\n" in processMemCpyMemCpyDependence()
1242 // TODO: Is this worth it if we're creating a less aligned memcpy? For in processMemCpyMemCpyDependence()
1250 // llvm.memcpy may be promoted to llvm.memcpy.inline, but the converse is in processMemCpyMemCpyDependence()
1273 /// We've found that the (upward scanning) memory dependence of \p MemCpy is
1275 /// weren't copied over by \p MemCpy.
1281 /// memcpy(dst, src, src_size);
1287 /// memcpy(dst, src, src_size);
1290 /// The memset is sunk to just before the memcpy to ensure that src_size is
1292 bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy, in processMemSetMemCpyDependence() argument
1295 // We can only transform memset/memcpy with the same destination. in processMemSetMemCpyDependence()
1296 if (!BAA.isMustAlias(MemSet->getDest(), MemCpy->getDest())) in processMemSetMemCpyDependence()
1303 Value *SrcSize = MemCpy->getLength(); in processMemSetMemCpyDependence()
1305 SimplifyQuery(MemCpy->getDataLayout(), DT, AC, MemCpy))) in processMemSetMemCpyDependence()
1308 // Check that src and dst of the memcpy aren't the same. While memcpy in processMemSetMemCpyDependence()
1310 if (isModSet(BAA.getModRefInfo(MemCpy, MemoryLocation::getForSource(MemCpy)))) in processMemSetMemCpyDependence()
1318 MSSA->getMemoryAccess(MemCpy))) in processMemSetMemCpyDependence()
1321 // Use the same i8* dest as the memcpy, killing the memset dest if different. in processMemSetMemCpyDependence()
1322 Value *Dest = MemCpy->getRawDest(); in processMemSetMemCpyDependence()
1325 if (mayBeVisibleThroughUnwinding(Dest, MemSet, MemCpy)) in processMemSetMemCpyDependence()
1340 MemCpy->getDestAlign().valueOrOne()); in processMemSetMemCpyDependence()
1345 IRBuilder<> Builder(MemCpy); in processMemSetMemCpyDependence()
1352 assert(MemSet->getParent() == MemCpy->getParent() && in processMemSetMemCpyDependence()
1373 assert(isa<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(MemCpy)) && in processMemSetMemCpyDependence()
1374 "MemCpy must be a MemoryDef"); in processMemSetMemCpyDependence()
1375 // The new memset is inserted before the memcpy, and it is known that the in processMemSetMemCpyDependence()
1376 // memcpy's defining access is the memset about to be removed. in processMemSetMemCpyDependence()
1378 cast<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(MemCpy)); in processMemSetMemCpyDependence()
1423 /// Transform memcpy to memset when its source was just memset.
1427 /// memcpy(dst2, dst1, dst2_size);
1435 bool MemCpyOptPass::performMemCpyToMemSetOptzn(MemCpyInst *MemCpy, in performMemCpyToMemSetOptzn() argument
1438 // Make sure that memcpy(..., memset(...), ...), that is we are memsetting and in performMemCpyToMemSetOptzn()
1440 if (!BAA.isMustAlias(MemSet->getRawDest(), MemCpy->getRawSource())) in performMemCpyToMemSetOptzn()
1444 Value *CopySize = MemCpy->getLength(); in performMemCpyToMemSetOptzn()
1447 // Make sure the memcpy doesn't read any more than what the memset wrote. in performMemCpyToMemSetOptzn()
1455 // A known memcpy size is also required. in performMemCpyToMemSetOptzn()
1460 // If the memcpy is larger than the memset, but the memory was undef prior in performMemCpyToMemSetOptzn()
1464 MemoryLocation MemCpyLoc = MemoryLocation::getForSource(MemCpy); in performMemCpyToMemSetOptzn()
1470 if (hasUndefContents(MSSA, BAA, MemCpy->getSource(), MD, CopySize)) in performMemCpyToMemSetOptzn()
1479 IRBuilder<> Builder(MemCpy); in performMemCpyToMemSetOptzn()
1481 Builder.CreateMemSet(MemCpy->getRawDest(), MemSet->getOperand(1), in performMemCpyToMemSetOptzn()
1482 CopySize, MemCpy->getDestAlign()); in performMemCpyToMemSetOptzn()
1484 cast<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(MemCpy)); in performMemCpyToMemSetOptzn()
1720 /// Perform simplification of memcpy's. If we have memcpy A
1721 /// which copies X to Y, and memcpy B which copies Y to Z, then we can rewrite
1722 /// B to be a memcpy from X to Z (or potentially a memmove, depending on
1723 /// circumstances). This allows later passes to remove the first memcpy
1726 // We can only optimize non-volatile memcpy's. in processMemCpy()
1730 // If the source and destination of the memcpy are the same, then zap it. in processMemCpy()
1737 // If the size is zero, remove the memcpy. in processMemCpy()
1746 // Degenerate case: memcpy marked as not accessing memory. in processMemCpy()
1749 // If copying from a constant, try to turn the memcpy into a memset. in processMemCpy()
1774 // Try to turn a partially redundant memset + memcpy into in processMemCpy()
1775 // smaller memset + memcpy. We don't need the memcpy size for this. in processMemCpy()
1776 // The memcpy must post-dom the memset, so limit this to the same basic in processMemCpy()
1787 // There are five possible optimizations we can do for memcpy: in processMemCpy()
1788 // a) memcpy-memcpy xform which exposes redundance for DSE. in processMemCpy()
1789 // b) call-memcpy xform for return slot optimization. in processMemCpy()
1790 // c) memcpy from freshly alloca'd space or space that has just started in processMemCpy()
1792 // the memcpy in favor of the data that was already at the destination. in processMemCpy()
1793 // d) memcpy from a just-memset'd source can be turned into memset. in processMemCpy()
1794 // e) elimination of memcpy via stack-move optimization. in processMemCpy()
1805 << " memcpy: " << *M << "\n"); in processMemCpy()
1817 LLVM_DEBUG(dbgs() << "Converted memcpy to memset\n"); in processMemCpy()
1826 LLVM_DEBUG(dbgs() << "Removed memcpy from undef\n"); in processMemCpy()
1857 /// Transforms memmove calls to memcpy calls when the src/dst are guaranteed
1864 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Optimizing memmove -> memcpy: " << *M in processMemMove()
1871 Intrinsic::getDeclaration(M->getModule(), Intrinsic::memcpy, ArgTys)); in processMemMove()
1873 // For MemorySSA nothing really changes (except that memcpy may imply stricter in processMemMove()
1898 // If the byval argument isn't fed by a memcpy, ignore it. If it is fed by in processByValArgument()
1899 // a memcpy, see if we can byval from the source of the memcpy instead of the in processByValArgument()
1905 // The length of the memcpy must be larger or equal to the size of the byval. in processByValArgument()
1917 // If it is greater than the memcpy, then we check to see if we can force the in processByValArgument()
1918 // source of the memcpy to the alignment we need. If we fail, we bail out. in processByValArgument()
1925 // The type of the memcpy source must match the byval argument in processByValArgument()
1929 // Verify that the copied-from memory doesn't change in between the memcpy and in processByValArgument()
1931 // memcpy(a <- b) in processByValArgument()
1934 // It would be invalid to transform the second memcpy into foo(*b). in processByValArgument()
1939 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Forwarding memcpy to byval:\n" in processByValArgument()
1950 /// This is called on memcpy dest pointer arguments attributed as immutable
1951 /// during call. Try to use memcpy source directly if all of the following
1953 /// 1. The memcpy dst is neither modified during the call nor captured by the
1955 /// 2. The memcpy dst is an alloca with known alignment & size.
1956 /// 2-1. The memcpy length == the alloca size which ensures that the new
1960 /// 3. The memcpy dst and src is not modified between the memcpy and the call.
1962 /// 4. The memcpy src is not modified during the call. (ModRef check shows no
1973 // TODO: Even if the arg gets back to branches, we can remove memcpy if all in processImmutArgument()
1996 // If the immut argument isn't fed by a memcpy, ignore it. If it is fed by in processImmutArgument()
1997 // a memcpy, check that the arg equals the memcpy dest. in processImmutArgument()
2001 // The type of the memcpy source must match the immut argument in processImmutArgument()
2005 // 2-1. The length of the memcpy must be equal to the size of the alloca. in processImmutArgument()
2010 // 2-2. the memcpy source align must be larger than or equal the alloca's in processImmutArgument()
2011 // align. If not so, we check to see if we can force the source of the memcpy in processImmutArgument()
2020 // 3. Verify that the source doesn't change in between the memcpy and in processImmutArgument()
2022 // memcpy(a <- b) in processImmutArgument()
2025 // It would be invalid to transform the second memcpy into foo(*b). in processImmutArgument()
2030 // 4. The memcpy src must not be modified during the call. in processImmutArgument()
2034 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Forwarding memcpy to Immut src:\n" in processImmutArgument()