From: Daniel Berlin Date: Sat, 28 Jan 2017 02:22:52 +0000 (+0000) Subject: MemorySSA: Fix block numbering invalidation and replacement bugs discovered by updater X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4fcfd74d0696406bda90e69f619710f639bb3574;p=llvm MemorySSA: Fix block numbering invalidation and replacement bugs discovered by updater git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@293361 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Utils/MemorySSA.cpp b/lib/Transforms/Utils/MemorySSA.cpp index dbf54309053..4c70514ff9c 100644 --- a/lib/Transforms/Utils/MemorySSA.cpp +++ b/lib/Transforms/Utils/MemorySSA.cpp @@ -1597,6 +1597,7 @@ void MemorySSA::insertIntoListsForBlock(MemoryAccess *NewAccess, Defs->push_back(*NewAccess); } } + BlockNumberingValid.erase(BB); } void MemorySSA::insertIntoListsBefore(MemoryAccess *What, const BasicBlock *BB, @@ -1624,6 +1625,7 @@ void MemorySSA::insertIntoListsBefore(MemoryAccess *What, const BasicBlock *BB, Defs->insert(InsertPt->getDefsIterator(), *What); } } + BlockNumberingValid.erase(BB); } // Move What before Where in the IR. The end result is taht What will belong to @@ -1638,13 +1640,19 @@ void MemorySSA::moveTo(MemoryUseOrDef *What, BasicBlock *BB, insertIntoListsBefore(What, BB, Where); } +void MemorySSA::moveTo(MemoryUseOrDef *What, BasicBlock *BB, + InsertionPlace Point) { + removeFromLists(What, false); + What->setBlock(BB); + insertIntoListsForBlock(What, BB, Point); +} + MemoryPhi *MemorySSA::createMemoryPhi(BasicBlock *BB) { assert(!getMemoryAccess(BB) && "MemoryPhi already exists for this BB"); MemoryPhi *Phi = new MemoryPhi(BB->getContext(), BB, NextID++); + // Phi's always are placed at the front of the block. insertIntoListsForBlock(Phi, BB, Beginning); ValueToMemoryAccess[BB] = Phi; - // Phi's always are placed at the front of the block. - BlockNumberingValid.erase(BB); return Phi; } @@ -1665,7 +1673,6 @@ MemoryAccess *MemorySSA::createMemoryAccessInBB(Instruction *I, InsertionPlace Point) { MemoryUseOrDef *NewAccess = createDefinedAccess(I, Definition); insertIntoListsForBlock(NewAccess, BB, Point); - BlockNumberingValid.erase(BB); return NewAccess; } @@ -1675,7 +1682,6 @@ MemoryUseOrDef *MemorySSA::createMemoryAccessBefore(Instruction *I, assert(I->getParent() == InsertPt->getBlock() && "New and old access must be in the same block"); MemoryUseOrDef *NewAccess = createDefinedAccess(I, Definition); - BlockNumberingValid.erase(InsertPt->getBlock()); insertIntoListsBefore(NewAccess, InsertPt->getBlock(), InsertPt->getIterator()); return NewAccess; @@ -1687,7 +1693,6 @@ MemoryUseOrDef *MemorySSA::createMemoryAccessAfter(Instruction *I, assert(I->getParent() == InsertPt->getBlock() && "New and old access must be in the same block"); MemoryUseOrDef *NewAccess = createDefinedAccess(I, Definition); - BlockNumberingValid.erase(InsertPt->getBlock()); insertIntoListsBefore(NewAccess, InsertPt->getBlock(), ++(InsertPt->getIterator())); return NewAccess; diff --git a/lib/Transforms/Utils/MemorySSAUpdater.cpp b/lib/Transforms/Utils/MemorySSAUpdater.cpp index 792ddef4e70..d7140f22eaf 100644 --- a/lib/Transforms/Utils/MemorySSAUpdater.cpp +++ b/lib/Transforms/Utils/MemorySSAUpdater.cpp @@ -243,13 +243,17 @@ void MemorySSAUpdater::insertDef(MemoryDef *MD) { // of that thing with us, since we are in the way of whatever was there // before. // We now define that def's memorydefs and memoryphis - for (auto UI = DefBefore->use_begin(), UE = DefBefore->use_end(); UI != UE;) { - Use &U = *UI++; - // Leave the uses alone - if (isa(U.getUser())) - continue; - U.set(MD); + if (DefBeforeSameBlock) { + for (auto UI = DefBefore->use_begin(), UE = DefBefore->use_end(); + UI != UE;) { + Use &U = *UI++; + // Leave the uses alone + if (isa(U.getUser())) + continue; + U.set(MD); + } } + // and that def is now our defining access. // We change them in this order otherwise we will appear in the use list // above and reset ourselves.