[PATCH] D55120: [MemCpyOpt] memset->memcpy forwarding with undef tail

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 30 05:18:52 PST 2018


nikic created this revision.
nikic added a reviewer: efriedma.
Herald added subscribers: llvm-commits, JDevlieghere.

Currently memcpyopt optimizes cases like

  memset(a, byte, N);
  memcpy(b, a, M);
  // to
  memset(a, byte, N);
  memset(b, byte, M);

if M <= N. Often this allows further simplifications down the line, which drop the first memset entirely.

This patch extends this optimization for the case where M > N, but we know that the bytes a[N..M] are undef due to alloca/lifetime.start.

This situation arises relatively often for Rust code, because Rust does not initialize trailing structure padding and loves to insert redundant memcpys. This also fixes https://bugs.llvm.org/show_bug.cgi?id=39844.

For the implementation, I'm reusing a bit of code for a similar existing optimization (direct memcpy of undef). I've also added memset support to MemDepAnalysis GetLocation -- I could also use getPointerDependencyFrom, but it seems to make more sense to add this to GetLocation and thus make the computation cachable.


Repository:
  rL LLVM

https://reviews.llvm.org/D55120

Files:
  lib/Analysis/MemoryDependenceAnalysis.cpp
  lib/Transforms/Scalar/MemCpyOptimizer.cpp
  test/Transforms/MemCpyOpt/memset-memcpy-oversized.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55120.176095.patch
Type: text/x-patch
Size: 8292 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181130/d373fac0/attachment.bin>


More information about the llvm-commits mailing list