@@ -1497,11 +1497,11 @@ static bool sinkUnusedInvariantsFromPreheaderToExit(
14971497
14981498 bool MadeAnyChanges = false ;
14991499 MemoryAccess *ExitDef = nullptr ;
1500+ MemorySSA *MSSA = MSSAU.getMemorySSA ();
15001501
15011502 for (Instruction &I : llvm::make_early_inc_range (llvm::reverse (*Preheader))) {
1502-
15031503 // Skip terminator.
1504- if (Preheader-> getTerminator () == &I )
1504+ if (I. isTerminator () )
15051505 continue ;
15061506
15071507 // New instructions were inserted at the end of the preheader.
@@ -1520,18 +1520,13 @@ static bool sinkUnusedInvariantsFromPreheaderToExit(
15201520
15211521 // Determine if there is a use in or before the loop (direct or
15221522 // otherwise).
1523- bool UsedInLoopOrPreheader = false ;
1524- for (Use &U : I.uses ()) {
1523+ bool UsedInLoopOrPreheader = llvm::any_of (I.uses (), [Preheader, L](Use &U) {
15251524 auto *UserI = cast<Instruction>(U.getUser ());
15261525 BasicBlock *UseBB = UserI->getParent ();
1527- if (auto *PN = dyn_cast<PHINode>(UserI)) {
1526+ if (auto *PN = dyn_cast<PHINode>(UserI))
15281527 UseBB = PN->getIncomingBlock (U);
1529- }
1530- if (UseBB == Preheader || L->contains (UseBB)) {
1531- UsedInLoopOrPreheader = true ;
1532- break ;
1533- }
1534- }
1528+ return UseBB == Preheader || L->contains (UseBB);
1529+ });
15351530 if (UsedInLoopOrPreheader)
15361531 continue ;
15371532
@@ -1543,43 +1538,22 @@ static bool sinkUnusedInvariantsFromPreheaderToExit(
15431538 SE->forgetValue (&I);
15441539
15451540 // Update MemorySSA.
1546- if (auto *OldMA = MSSAU. getMemorySSA () ->getMemoryAccess (&I)) {
1547- // apviding the expensive getPreviousDefRecursive call by manually
1541+ if (auto *OldMA = MSSA ->getMemoryAccess (&I)) {
1542+ // avoiding the expensive getPreviousDefRecursive call by manually
15481543 // setting the defining access.
1549- if (!ExitDef) {
1550- if (auto *MPhi = MSSAU.getMemorySSA ()->getMemoryAccess (ExitBlock)) {
1551- ExitDef = MPhi;
1552- } else {
1553- BasicBlock *Current = *predecessors (ExitBlock).begin ();
1554- while (true ) {
1555- if (auto *Accesses =
1556- MSSAU.getMemorySSA ()->getBlockAccesses (Current)) {
1557- if (!Accesses->empty ()) {
1558- MemoryAccess *Back =
1559- const_cast <MemoryAccess *>(&Accesses->back ());
1560- if (isa<MemoryDef>(Back) || isa<MemoryPhi>(Back))
1561- ExitDef = Back;
1562- else
1563- ExitDef = MSSAU.getMemorySSA ()
1564- ->getWalker ()
1565- ->getClobberingMemoryAccess (Back);
1566- break ;
1567- }
1568- }
1569-
1570- if (Current == L->getHeader ()) {
1571- Current = Preheader;
1572- continue ;
1573- }
1574-
1575- if (pred_empty (Current)) {
1576- ExitDef = MSSAU.getMemorySSA ()->getLiveOnEntryDef ();
1577- break ;
1578- }
1579- Current = *pred_begin (Current);
1580- }
1544+ auto FindExitDef = [MSSA, Preheader]() -> MemoryAccess * {
1545+ if (auto *Accesses = MSSA->getBlockAccesses (Preheader)) {
1546+ if (Accesses->empty ())
1547+ return nullptr ;
1548+ MemoryAccess *Back = const_cast <MemoryAccess *>(&Accesses->back ());
1549+ if (!isa<MemoryUse>(Back))
1550+ return Back;
1551+ return MSSA->getWalker ()->getClobberingMemoryAccess (Back);
15811552 }
1582- }
1553+ return nullptr ;
1554+ };
1555+ if (!ExitDef)
1556+ ExitDef = FindExitDef ();
15831557 MemoryAccess *NewMA = MSSAU.createMemoryAccessInBB (&I, ExitDef, ExitBlock,
15841558 MemorySSA::Beginning);
15851559 OldMA->replaceAllUsesWith (NewMA);
0 commit comments