@@ -638,6 +638,7 @@ bool MemCpyOptPass::processStoreOfLoad(StoreInst *SI, LoadInst *LI,
638
638
if (!LI->isSimple () || !LI->hasOneUse () || LI->getParent () != SI->getParent ())
639
639
return false ;
640
640
641
+ BatchAAResults BAA (*AA);
641
642
auto *T = LI->getType ();
642
643
// Don't introduce calls to memcpy/memmove intrinsics out of thin air if
643
644
// the corresponding libcalls are not available.
@@ -647,19 +648,17 @@ bool MemCpyOptPass::processStoreOfLoad(StoreInst *SI, LoadInst *LI,
647
648
(EnableMemCpyOptWithoutLibcalls ||
648
649
(TLI->has (LibFunc_memcpy) && TLI->has (LibFunc_memmove)))) {
649
650
MemoryLocation LoadLoc = MemoryLocation::get (LI);
650
-
651
- // We use alias analysis to check if an instruction may store to
652
- // the memory we load from in between the load and the store. If
653
- // such an instruction is found, we try to promote there instead
654
- // of at the store position.
655
- // TODO: Can use MSSA for this.
656
- Instruction *P = SI;
657
- for (auto &I : make_range (++LI->getIterator (), SI->getIterator ())) {
658
- if (isModSet (AA->getModRefInfo (&I, LoadLoc))) {
659
- P = &I;
660
- break ;
661
- }
662
- }
651
+ MemoryUseOrDef *LoadAccess = MSSA->getMemoryAccess (LI),
652
+ *StoreAccess = MSSA->getMemoryAccess (SI);
653
+
654
+ // We use MSSA to check if an instruction may store to the memory we load
655
+ // from in between the load and the store. If such an instruction is found,
656
+ // we try to promote there instead of at the store position.
657
+ auto *Clobber = MSSA->getWalker ()->getClobberingMemoryAccess (
658
+ StoreAccess->getDefiningAccess (), LoadLoc, BAA);
659
+ Instruction *P = MSSA->dominates (LoadAccess, Clobber)
660
+ ? cast<MemoryUseOrDef>(Clobber)->getMemoryInst ()
661
+ : SI;
663
662
664
663
// If we found an instruction that may write to the loaded memory,
665
664
// we can try to promote at this position instead of the store
@@ -707,7 +706,6 @@ bool MemCpyOptPass::processStoreOfLoad(StoreInst *SI, LoadInst *LI,
707
706
// Detect cases where we're performing call slot forwarding, but
708
707
// happen to be using a load-store pair to implement it, rather than
709
708
// a memcpy.
710
- BatchAAResults BAA (*AA);
711
709
auto GetCall = [&]() -> CallInst * {
712
710
// We defer this expensive clobber walk until the cheap checks
713
711
// have been done on the source inside performCallSlotOptzn.
0 commit comments