[SimplifyCFG] Teach simplifyIndirectBr() to preserve DomTree

This commit is contained in:
Roman Lebedev 2020-12-31 22:46:42 +03:00
parent b7c463d7b8
commit 9f17dab1f4
No known key found for this signature in database
GPG Key ID: 083C3EBB4A1689E0
2 changed files with 8 additions and 1 deletions

View File

@ -6148,10 +6148,13 @@ bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst *IBI) {
bool Changed = false;
// Eliminate redundant destinations.
std::vector<DominatorTree::UpdateType> Updates;
SmallPtrSet<Value *, 8> Succs;
for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
BasicBlock *Dest = IBI->getDestination(i);
if (!Dest->hasAddressTaken() || !Succs.insert(Dest).second) {
if (!Dest->hasAddressTaken())
Updates.push_back({DominatorTree::Delete, BB, Dest});
Dest->removePredecessor(BB);
IBI->removeDestination(i);
--i;
@ -6160,6 +6163,10 @@ bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst *IBI) {
}
}
if (DTU)
DTU->applyUpdatesPermissive(Updates);
Updates.clear();
if (IBI->getNumDestinations() == 0) {
// If the indirectbr has no successors, change it to unreachable.
new UnreachableInst(IBI->getContext(), IBI);

View File

@ -1,4 +1,4 @@
; RUN: opt -S -simplifycfg < %s | FileCheck %s
; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | FileCheck %s
; SimplifyCFG should eliminate redundant indirectbr edges.