Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
[PM/Unswitch] Fix a nasty bug in the new PM's unswitch introduced in
Browse files Browse the repository at this point in the history
r335553 with the non-trivial unswitching of switches.

The code correctly updated most aspects of the CFG and analyses, but
missed some crucial aspects:
1) When multiple cases have the same successor, we unswitch that
   a single time and replace the switch with a direct branch. The CFG
   here is correct, but the target of this direct branch may have had
   a PHI node with multiple entries in it.
2) When we still have to clone a successor of the switch into an
   unswitched copy of the loop, we'll delete potentially multiple edges
   entering this successor, not just one.
3) We also have to delete multiple edges entering the successors in the
   original loop when they have to be retained.
4) When the "retained successor" *also* occurs as a case successor, we
   just assert failed everywhere. This doesn't happen very easily
   because its always valid to simply drop the case -- the retained
   successor for switches is always the default successor. However, it
   is likely possible through some contrivance of different loop passes,
   unrolling, and simplifying for this to occur in practice and
   certainly there is nothing "invalid" about the IR so this pass needs
   to handle it.
5) In the case of #4, we also will replace these multiple edges with
   a direct branch much like in #1 and need to collapse the entries in
   any PHI nodes to a single enrty.

All of this stems from the delightful fact that the same successor can
show up in multiple parts of the switch terminator, and each of these
are considered a distinct edge for the purpose of PHI nodes (and
iterating the successors and predecessors) but not for unswitching
itself, the dominator tree, or many other things. For the record,
I intensely dislike this "feature" of the IR in large part because of
the complexity it causes in passes like this. We already have a ton of
logic building sets and handling duplicates, and we just had to add
a bunch more.

I've added a complex test case that covers all five of the above failure
modes. I've also added a variation on it where #4 and #5 occur in loop
exit, adding fun where we have an LCSSA PHI node with "multiple entries"
despite have dedicated exits. There were no additional issues found by
this, but it seems a useful corner case to cover with testing.

One thing that working on all of this code has made painfully clear for
me as well is how amazingly inefficient our PHI node representation is
(in terms of the in-memory data structures and the APIs used to update
them). This code has truly marvelous complexity bounds because every
time we remove an entry from a PHI node we do a linear scan to find it
and then a linear update to the data structure to remove it. We could in
theory batch all of the PHI node updates into a single linear walk of
the operands making this much more efficient, but the APIs fight hard
against this and the fact that we have to handle duplicates in the
peculiar manner we do (removing all but one in some cases) makes even
implementing that very tedious and annoying. Anyways, none of this is
new here or specific to loop unswitching. All code in LLVM that updates
PHI node operands suffers from these problems.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@336536 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
chandlerc committed Jul 9, 2018
1 parent 5070d36 commit 1cbf1da
Show file tree
Hide file tree
Showing 2 changed files with 499 additions and 92 deletions.
107 changes: 81 additions & 26 deletions lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,19 +949,6 @@ static BasicBlock *buildClonedLoopBlocks(
AC.registerAssumption(II);
}

// Remove the cloned parent as a predecessor of the cloned continue successor
// if we did in fact clone it.
auto *ClonedParentBB = cast<BasicBlock>(VMap.lookup(ParentBB));
if (auto *ClonedContinueSuccBB =
cast_or_null<BasicBlock>(VMap.lookup(ContinueSuccBB)))
ClonedContinueSuccBB->removePredecessor(ClonedParentBB,
/*DontDeleteUselessPHIs*/ true);
// Replace the cloned branch with an unconditional branch to the cloned
// unswitched successor.
auto *ClonedSuccBB = cast<BasicBlock>(VMap.lookup(UnswitchedSuccBB));
ClonedParentBB->getTerminator()->eraseFromParent();
BranchInst::Create(ClonedSuccBB, ClonedParentBB);

// Update any PHI nodes in the cloned successors of the skipped blocks to not
// have spurious incoming values.
for (auto *LoopBB : L.blocks())
Expand All @@ -971,6 +958,45 @@ static BasicBlock *buildClonedLoopBlocks(
for (PHINode &PN : ClonedSuccBB->phis())
PN.removeIncomingValue(LoopBB, /*DeletePHIIfEmpty*/ false);

// Remove the cloned parent as a predecessor of any successor we ended up
// cloning other than the unswitched one.
auto *ClonedParentBB = cast<BasicBlock>(VMap.lookup(ParentBB));
for (auto *SuccBB : successors(ParentBB)) {
if (SuccBB == UnswitchedSuccBB)
continue;

auto *ClonedSuccBB = cast_or_null<BasicBlock>(VMap.lookup(SuccBB));
if (!ClonedSuccBB)
continue;

ClonedSuccBB->removePredecessor(ClonedParentBB,
/*DontDeleteUselessPHIs*/ true);
}

// Replace the cloned branch with an unconditional branch to the cloned
// unswitched successor.
auto *ClonedSuccBB = cast<BasicBlock>(VMap.lookup(UnswitchedSuccBB));
ClonedParentBB->getTerminator()->eraseFromParent();
BranchInst::Create(ClonedSuccBB, ClonedParentBB);

// If there are duplicate entries in the PHI nodes because of multiple edges
// to the unswitched successor, we need to nuke all but one as we replaced it
// with a direct branch.
for (PHINode &PN : ClonedSuccBB->phis()) {
bool Found = false;
// Loop over the incoming operands backwards so we can easily delete as we
// go without invalidating the index.
for (int i = PN.getNumOperands() - 1; i >= 0; --i) {
if (PN.getIncomingBlock(i) != ClonedParentBB)
continue;
if (!Found) {
Found = true;
continue;
}
PN.removeIncomingValue(i, /*DeletePHIIfEmpty*/ false);
}
}

// Record the domtree updates for the new blocks.
SmallPtrSet<BasicBlock *, 4> SuccSet;
for (auto *ClonedBB : NewBlocks) {
Expand Down Expand Up @@ -1779,7 +1805,8 @@ static bool unswitchNontrivialInvariants(
UnswitchedSuccBBs.insert(BI->getSuccessor(ClonedSucc));
else
for (auto Case : SI->cases())
UnswitchedSuccBBs.insert(Case.getCaseSuccessor());
if (Case.getCaseSuccessor() != RetainedSuccBB)
UnswitchedSuccBBs.insert(Case.getCaseSuccessor());

assert(!UnswitchedSuccBBs.count(RetainedSuccBB) &&
"Should not unswitch the same successor we are retaining!");
Expand Down Expand Up @@ -1873,19 +1900,44 @@ static bool unswitchNontrivialInvariants(
// nuke the initial terminator placed in the split block.
SplitBB->getTerminator()->eraseFromParent();
if (FullUnswitch) {
for (BasicBlock *SuccBB : UnswitchedSuccBBs) {
// First we need to unhook the successor relationship as we'll be replacing
// the terminator with a direct branch. This is much simpler for branches
// than switches so we handle those first.
if (BI) {
// Remove the parent as a predecessor of the unswitched successor.
SuccBB->removePredecessor(ParentBB,
/*DontDeleteUselessPHIs*/ true);
DTUpdates.push_back({DominatorTree::Delete, ParentBB, SuccBB});
assert(UnswitchedSuccBBs.size() == 1 &&
"Only one possible unswitched block for a branch!");
BasicBlock *UnswitchedSuccBB = *UnswitchedSuccBBs.begin();
UnswitchedSuccBB->removePredecessor(ParentBB,
/*DontDeleteUselessPHIs*/ true);
DTUpdates.push_back({DominatorTree::Delete, ParentBB, UnswitchedSuccBB});
} else {
// Note that we actually want to remove the parent block as a predecessor
// of *every* case successor. The case successor is either unswitched,
// completely eliminating an edge from the parent to that successor, or it
// is a duplicate edge to the retained successor as the retained successor
// is always the default successor and as we'll replace this with a direct
// branch we no longer need the duplicate entries in the PHI nodes.
assert(SI->getDefaultDest() == RetainedSuccBB &&
"Not retaining default successor!");
for (auto &Case : SI->cases())
Case.getCaseSuccessor()->removePredecessor(
ParentBB,
/*DontDeleteUselessPHIs*/ true);

// We need to use the set to populate domtree updates as even when there
// are multiple cases pointing at the same successor we only want to
// remove and insert one edge in the domtree.
for (BasicBlock *SuccBB : UnswitchedSuccBBs)
DTUpdates.push_back({DominatorTree::Delete, ParentBB, SuccBB});
}

// Now splice the terminator from the original loop and rewrite its
// successors.
// Now that we've unhooked the successor relationship, splice the terminator
// from the original loop to the split.
SplitBB->getInstList().splice(SplitBB->end(), ParentBB->getInstList(), TI);

// Now wire up the terminator to the preheaders.
if (BI) {
assert(UnswitchedSuccBBs.size() == 1 &&
"Only one possible unswitched block for a branch!");
BasicBlock *ClonedPH = ClonedPHs.begin()->second;
BI->setSuccessor(ClonedSucc, ClonedPH);
BI->setSuccessor(1 - ClonedSucc, LoopPH);
Expand All @@ -1894,16 +1946,19 @@ static bool unswitchNontrivialInvariants(
assert(SI && "Must either be a branch or switch!");

// Walk the cases and directly update their successors.
SI->setDefaultDest(LoopPH);
for (auto &Case : SI->cases())
Case.setSuccessor(ClonedPHs.find(Case.getCaseSuccessor())->second);
if (Case.getCaseSuccessor() == RetainedSuccBB)
Case.setSuccessor(LoopPH);
else
Case.setSuccessor(ClonedPHs.find(Case.getCaseSuccessor())->second);

// We need to use the set to populate domtree updates as even when there
// are multiple cases pointing at the same successor we only want to
// insert one edge in the domtree.
// remove and insert one edge in the domtree.
for (BasicBlock *SuccBB : UnswitchedSuccBBs)
DTUpdates.push_back(
{DominatorTree::Insert, SplitBB, ClonedPHs.find(SuccBB)->second});

SI->setDefaultDest(LoopPH);
}

// Create a new unconditional branch to the continuing block (as opposed to
Expand Down
Loading

0 comments on commit 1cbf1da

Please sign in to comment.