[llvm] 94704ed - [MemCpyOpt] Add helper to erase instructions (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 2 12:54:12 PDT 2020


Author: Nikita Popov
Date: 2020-10-02T21:52:10+02:00
New Revision: 94704ed008f78e71aa42a452d8b03c122e0f78cd

URL: https://github.com/llvm/llvm-project/commit/94704ed008f78e71aa42a452d8b03c122e0f78cd
DIFF: https://github.com/llvm/llvm-project/commit/94704ed008f78e71aa42a452d8b03c122e0f78cd.diff

LOG: [MemCpyOpt] Add helper to erase instructions (NFC)

Next to erasing the instruction, we also always want to remove
it from MSSA and MD. Use a common function to do so.

This is a refactoring split out from D26739.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/Scalar/MemCpyOptimizer.h b/llvm/include/llvm/Transforms/Scalar/MemCpyOptimizer.h
index 89a2e24af288..ea6f37192d5e 100644
--- a/llvm/include/llvm/Transforms/Scalar/MemCpyOptimizer.h
+++ b/llvm/include/llvm/Transforms/Scalar/MemCpyOptimizer.h
@@ -70,6 +70,7 @@ class MemCpyOptPass : public PassInfoMixin<MemCpyOptPass> {
   Instruction *tryMergingIntoMemset(Instruction *I, Value *StartPtr,
                                     Value *ByteVal);
 
+  void eraseInstruction(Instruction *I);
   bool iterateOnFunction(Function &F);
 };
 

diff  --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
index 4d30804f1680..b8c0d20d0321 100644
--- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -302,6 +302,13 @@ INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
 INITIALIZE_PASS_END(MemCpyOptLegacyPass, "memcpyopt", "MemCpy Optimization",
                     false, false)
 
+void MemCpyOptPass::eraseInstruction(Instruction *I) {
+  if (MSSAU)
+    MSSAU->removeMemoryAccess(I);
+  MD->removeInstruction(I);
+  I->eraseFromParent();
+}
+
 /// When scanning forward over instructions, we look for some other patterns to
 /// fold away. In particular, this looks for stores to neighboring locations of
 /// memory. If it sees enough consecutive ones, it attempts to merge them
@@ -442,12 +449,8 @@ Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst,
     }
 
     // Zap all the stores.
-    for (Instruction *SI : Range.TheStores) {
-      if (MSSAU)
-        MSSAU->removeMemoryAccess(SI);
-      MD->removeInstruction(SI);
-      SI->eraseFromParent();
-    }
+    for (Instruction *SI : Range.TheStores)
+      eraseInstruction(SI);
 
     ++NumMemSetInfer;
   }
@@ -633,14 +636,10 @@ bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
             auto *NewAccess =
                 MSSAU->createMemoryAccessAfter(M, LastDef, LastDef);
             MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
-            MSSAU->removeMemoryAccess(SI);
-            MSSAU->removeMemoryAccess(LI);
           }
 
-          MD->removeInstruction(SI);
-          SI->eraseFromParent();
-          MD->removeInstruction(LI);
-          LI->eraseFromParent();
+          eraseInstruction(SI);
+          eraseInstruction(LI);
           ++NumMemCpyInstr;
 
           // Make sure we do not invalidate the iterator.
@@ -685,15 +684,8 @@ bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
             DL.getTypeStoreSize(SI->getOperand(0)->getType()),
             commonAlignment(SI->getAlign(), LI->getAlign()), C);
         if (changed) {
-          if (MSSAU) {
-            MSSAU->removeMemoryAccess(SI);
-            MSSAU->removeMemoryAccess(LI);
-          }
-
-          MD->removeInstruction(SI);
-          SI->eraseFromParent();
-          MD->removeInstruction(LI);
-          LI->eraseFromParent();
+          eraseInstruction(SI);
+          eraseInstruction(LI);
           ++NumMemCpyInstr;
           return true;
         }
@@ -733,11 +725,9 @@ bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
             cast<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(SI));
         auto *NewAccess = MSSAU->createMemoryAccessAfter(M, LastDef, LastDef);
         MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
-        MSSAU->removeMemoryAccess(SI);
       }
 
-      MD->removeInstruction(SI);
-      SI->eraseFromParent();
+      eraseInstruction(SI);
       NumMemSetInfer++;
 
       // Make sure we do not invalidate the iterator.
@@ -1028,12 +1018,10 @@ bool MemCpyOptPass::processMemCpyMemCpyDependence(MemCpyInst *M,
     auto *LastDef = cast<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(M));
     auto *NewAccess = MSSAU->createMemoryAccessAfter(NewM, LastDef, LastDef);
     MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
-    MSSAU->removeMemoryAccess(M);
   }
 
   // Remove the instruction we're replacing.
-  MD->removeInstruction(M);
-  M->eraseFromParent();
+  eraseInstruction(M);
   ++NumMemCpyInstr;
   return true;
 }
@@ -1111,11 +1099,9 @@ bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy,
     auto *NewAccess = MSSAU->createMemoryAccessBefore(
         NewMemSet, LastDef->getDefiningAccess(), LastDef);
     MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
-    MSSAU->removeMemoryAccess(MemSet);
   }
 
-  MD->removeInstruction(MemSet);
-  MemSet->eraseFromParent();
+  eraseInstruction(MemSet);
   return true;
 }
 
@@ -1203,11 +1189,7 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
   // If the source and destination of the memcpy are the same, then zap it.
   if (M->getSource() == M->getDest()) {
     ++BBI;
-    if (MSSAU)
-      MSSAU->removeMemoryAccess(M);
-
-    MD->removeInstruction(M);
-    M->eraseFromParent();
+    eraseInstruction(M);
     return true;
   }
 
@@ -1226,11 +1208,9 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
           auto *NewAccess =
               MSSAU->createMemoryAccessAfter(NewM, LastDef, LastDef);
           MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
-          MSSAU->removeMemoryAccess(M);
         }
 
-        MD->removeInstruction(M);
-        M->eraseFromParent();
+        eraseInstruction(M);
         ++NumCpyToSet;
         return true;
       }
@@ -1263,11 +1243,7 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
                                  M->getSourceAlign().valueOrOne());
       if (performCallSlotOptzn(M, M->getDest(), M->getSource(),
                                CopySize->getZExtValue(), Alignment, C)) {
-        if (MSSAU)
-          MSSAU->removeMemoryAccess(M);
-
-        MD->removeInstruction(M);
-        M->eraseFromParent();
+        eraseInstruction(M);
         ++NumMemCpyInstr;
         return true;
       }
@@ -1283,11 +1259,7 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
       return processMemCpyMemCpyDependence(M, MDep);
   } else if (SrcDepInfo.isDef()) {
     if (hasUndefContents(SrcDepInfo.getInst(), CopySize)) {
-      if (MSSAU)
-        MSSAU->removeMemoryAccess(M);
-
-      MD->removeInstruction(M);
-      M->eraseFromParent();
+      eraseInstruction(M);
       ++NumMemCpyInstr;
       return true;
     }
@@ -1296,10 +1268,7 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
   if (SrcDepInfo.isClobber())
     if (MemSetInst *MDep = dyn_cast<MemSetInst>(SrcDepInfo.getInst()))
       if (performMemCpyToMemSetOptzn(M, MDep)) {
-        if (MSSAU)
-          MSSAU->removeMemoryAccess(M);
-        MD->removeInstruction(M);
-        M->eraseFromParent();
+        eraseInstruction(M);
         ++NumCpyToSet;
         return true;
       }


        


More information about the llvm-commits mailing list