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).
642 // Don't introduce calls to memcpy/memmove intrinsics out of thin air if
674 // the load/store pair to a memcpy.
677 // memmove must be used to preserve semantic. If not, memcpy can
678 // be used. Also, if we load from constant memory, memcpy can be used
717 // a memcpy.
765 // memcpy/memset would not be able to preserve the nontemporal hint.
782 // Load to store forwarding can be interpreted as memcpy.
793 // There are two cases that are interesting for this code to handle: memcpy
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, ...)
866 // memcpy(dest, src, ...)
869 // Since moving the memcpy is technically awkward, we additionally check that
871 // the memcpy can be discarded rather than moved.
916 // the call and the store/memcpy.
975 // Check that src is not accessed except via the call and the memcpy. This
977 // memcpy can be dropped), that it is not read or written between the call and
978 // the memcpy, and that writing beyond the end of it is undefined.
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)
1132 // memcpy(b <- a)
1136 // We can only optimize non-volatile memcpy's.
1142 // We can only transforms memcpy's where the dest of one is the source of the
1152 // The length of the memcpy's must be the same, or the preceding one
1180 // memcpy(d1 <- s1)
1181 // memcpy(d2 <- d1+o)
1183 // memcpy(d2 <- s1+o)
1203 // memcpy(a <- b)
1205 // memcpy(c <- a)
1206 // It would be invalid to transform the second memcpy into memcpy(c <- b).
1209 // then we could still perform the xform by moving M up to the first memcpy.
1214 // No need to create `memcpy(a <- a)`.
1226 // memmove instead of memcpy.
1229 // Don't convert llvm.memcpy.inline into memmove because memmove can be
1230 // lowered as a call, and that is not allowed for llvm.memcpy.inline (and
1238 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Forwarding memcpy->memcpy src:\n"
1242 // TODO: Is this worth it if we're creating a less aligned memcpy? For
1250 // llvm.memcpy may be promoted to llvm.memcpy.inline, but the converse is
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,
1295 // We can only transform memset/memcpy with the same destination.
1296 if (!BAA.isMustAlias(MemSet->getDest(), MemCpy->getDest()))
1303 Value *SrcSize = MemCpy->getLength();
1305 SimplifyQuery(MemCpy->getDataLayout(), DT, AC, MemCpy)))
1308 // Check that src and dst of the memcpy aren't the same. While memcpy
1310 if (isModSet(BAA.getModRefInfo(MemCpy, MemoryLocation::getForSource(MemCpy))))
1318 MSSA->getMemoryAccess(MemCpy)))
1321 // Use the same i8* dest as the memcpy, killing the memset dest if different.
1322 Value *Dest = MemCpy->getRawDest();
1325 if (mayBeVisibleThroughUnwinding(Dest, MemSet, MemCpy))
1340 MemCpy->getDestAlign().valueOrOne());
1345 IRBuilder<> Builder(MemCpy);
1352 assert(MemSet->getParent() == MemCpy->getParent() &&
1373 assert(isa<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(MemCpy)) &&
1374 "MemCpy must be a MemoryDef");
1375 // The new memset is inserted before the memcpy, and it is known that the
1376 // memcpy's defining access is the memset about to be removed.
1378 cast<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(MemCpy));
1423 /// Transform memcpy to memset when its source was just memset.
1427 /// memcpy(dst2, dst1, dst2_size);
1435 bool MemCpyOptPass::performMemCpyToMemSetOptzn(MemCpyInst *MemCpy,
1438 // Make sure that memcpy(..., memset(...), ...), that is we are memsetting and
1440 if (!BAA.isMustAlias(MemSet->getRawDest(), MemCpy->getRawSource()))
1444 Value *CopySize = MemCpy->getLength();
1447 // Make sure the memcpy doesn't read any more than what the memset wrote.
1455 // A known memcpy size is also required.
1460 // If the memcpy is larger than the memset, but the memory was undef prior
1464 MemoryLocation MemCpyLoc = MemoryLocation::getForSource(MemCpy);
1470 if (hasUndefContents(MSSA, BAA, MemCpy->getSource(), MD, CopySize))
1479 IRBuilder<> Builder(MemCpy);
1481 Builder.CreateMemSet(MemCpy->getRawDest(), MemSet->getOperand(1),
1482 CopySize, MemCpy->getDestAlign());
1484 cast<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(MemCpy));
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.
1730 // If the source and destination of the memcpy are the same, then zap it.
1737 // If the size is zero, remove the memcpy.
1746 // Degenerate case: memcpy marked as not accessing memory.
1749 // If copying from a constant, try to turn the memcpy into a memset.
1774 // Try to turn a partially redundant memset + memcpy into
1775 // smaller memset + memcpy. We don't need the memcpy size for this.
1776 // The memcpy must post-dom the memset, so limit this to the same basic
1787 // There are five possible optimizations we can do for memcpy:
1788 // a) memcpy-memcpy xform which exposes redundance for DSE.
1789 // b) call-memcpy xform for return slot optimization.
1790 // c) memcpy from freshly alloca'd space or space that has just started
1792 // the memcpy in favor of the data that was already at the destination.
1793 // d) memcpy from a just-memset'd source can be turned into memset.
1794 // e) elimination of memcpy via stack-move optimization.
1805 << " memcpy: " << *M << "\n");
1817 LLVM_DEBUG(dbgs() << "Converted memcpy to memset\n");
1826 LLVM_DEBUG(dbgs() << "Removed memcpy from undef\n");
1857 /// Transforms memmove calls to memcpy calls when the src/dst are guaranteed
1864 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Optimizing memmove -> memcpy: " << *M
1871 Intrinsic::getDeclaration(M->getModule(), Intrinsic::memcpy, ArgTys));
1873 // For MemorySSA nothing really changes (except that memcpy may imply stricter
1898 // If the byval argument isn't fed by a memcpy, ignore it. If it is fed by
1899 // a memcpy, see if we can byval from the source of the memcpy instead of the
1905 // The length of the memcpy must be larger or equal to the size of the byval.
1917 // If it is greater than the memcpy, then we check to see if we can force the
1918 // source of the memcpy to the alignment we need. If we fail, we bail out.
1925 // The type of the memcpy source must match the byval argument
1929 // Verify that the copied-from memory doesn't change in between the memcpy and
1931 // memcpy(a <- b)
1934 // It would be invalid to transform the second memcpy into foo(*b).
1939 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Forwarding memcpy to byval:\n"
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
1996 // If the immut argument isn't fed by a memcpy, ignore it. If it is fed by
1997 // a memcpy, check that the arg equals the memcpy dest.
2001 // The type of the memcpy source must match the immut argument
2005 // 2-1. The length of the memcpy must be equal to the size of the alloca.
2010 // 2-2. the memcpy source align must be larger than or equal the alloca's
2011 // align. If not so, we check to see if we can force the source of the memcpy
2020 // 3. Verify that the source doesn't change in between the memcpy and
2022 // memcpy(a <- b)
2025 // It would be invalid to transform the second memcpy into foo(*b).
2030 // 4. The memcpy src must not be modified during the call.
2034 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Forwarding memcpy to Immut src:\n"