[llvm] 17db125 - [MemCpyOpt] Optimize MemoryDef insertion

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 10 12:28:37 PDT 2021


Author: Nikita Popov
Date: 2021-08-10T21:28:29+02:00
New Revision: 17db125b487faa69d789b80d47b19da49522b168

URL: https://github.com/llvm/llvm-project/commit/17db125b487faa69d789b80d47b19da49522b168
DIFF: https://github.com/llvm/llvm-project/commit/17db125b487faa69d789b80d47b19da49522b168.diff

LOG: [MemCpyOpt] Optimize MemoryDef insertion

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 is an alternative to D107513, at least
for this particular case.

Differential Revision: https://reviews.llvm.org/D107702

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
index d15bd4749d0d5..5dd913dfafa6f 100644
--- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -799,11 +799,12 @@ bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
 
       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++;


        


More information about the llvm-commits mailing list