[PATCH] D107702: [MemCpyOpt] Optimize MemoryDef insertion

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 7 14:16:35 PDT 2021


nikic created this revision.
nikic added reviewers: asbirlea, aeubanks, george.burgess.iv.
Herald added a subscriber: hiraditya.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

When converting a store into a memset, we currently insert the new MemoryDef after the store MemoryDef, which requires all uses to be renamed to the new def using a whole block scan. Instead, we can insert the new MemoryDef before the store and not rename uses, because we know that the location is immediately overwritten, so all uses should still refer to the old MemoryDef. Those uses will get renamed when the old MemoryDef is actually dropped, which is efficient.

I expect something similar can be done for some of the other MSSA updates in MemCpyOpt. This may be an alternative to D107513 <https://reviews.llvm.org/D107513>, at least for this particular case.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107702

Files:
  llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp


Index: llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -799,11 +799,12 @@
 
       LLVM_DEBUG(dbgs() << "Promoting " << *SI << " to " << *M << "\n");
 
-      assert(isa<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(SI)));
-      auto *LastDef =
-          cast<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(SI));
-      auto *NewAccess = MSSAU->createMemoryAccessAfter(M, LastDef, LastDef);
-      MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
+      // The newly inserted memset is immediately overwritten by the original
+      // store, so we do not need to rename uses.
+      auto *StoreDef = cast<MemoryDef>(MSSA->getMemoryAccess(SI));
+      auto *NewAccess = MSSAU->createMemoryAccessBefore(
+          M, StoreDef->getDefiningAccess(), StoreDef);
+      MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/false);
 
       eraseInstruction(SI);
       NumMemSetInfer++;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107702.364985.patch
Type: text/x-patch
Size: 1089 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210807/3bae346e/attachment.bin>


More information about the llvm-commits mailing list