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
}
// 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;
}
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.
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;
}
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.
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;
}
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;
}
// 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;
}
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;
}
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;
}
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;
}
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;
}