Skip to content

Commit cc10417

Browse files
committed
SILVerifier: fix quadratic complexity in verifying the predecessor-successor structure of basic blocks
Use sets instead of nested iterating over the predecessor/successor lists.
1 parent b741aa0 commit cc10417

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6289,18 +6289,6 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
62896289
}
62906290

62916291
void visitSILBasicBlock(SILBasicBlock *BB) {
6292-
// Make sure that each of the successors/predecessors of this basic block
6293-
// have this basic block in its predecessor/successor list.
6294-
for (const auto *SuccBB : BB->getSuccessorBlocks()) {
6295-
require(SuccBB->isPredecessorBlock(BB),
6296-
"Must be a predecessor of each successor.");
6297-
}
6298-
6299-
for (const SILBasicBlock *PredBB : BB->getPredecessorBlocks()) {
6300-
require(PredBB->isSuccessorBlock(BB),
6301-
"Must be a successor of each predecessor.");
6302-
}
6303-
63046292
SILInstructionVisitor::visitSILBasicBlock(BB);
63056293
verifyDebugScopeHoles(BB);
63066294
}
@@ -6331,6 +6319,33 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
63316319
continue;
63326320
visitSILBasicBlock(&BB);
63336321
}
6322+
6323+
verifyPredecessorSucessorStructure(F);
6324+
}
6325+
6326+
// Make sure that each of the successors/predecessors of a basic block
6327+
// have this basic block in its predecessor/successor list.
6328+
void verifyPredecessorSucessorStructure(SILFunction *f) {
6329+
using PredSuccPair = std::pair<SILBasicBlock *, SILBasicBlock *>;
6330+
llvm::DenseSet<PredSuccPair> foundSuccessors;
6331+
llvm::DenseSet<PredSuccPair> foundPredecessors;
6332+
6333+
for (auto &block : *f) {
6334+
for (SILBasicBlock *succ : block.getSuccessorBlocks()) {
6335+
foundSuccessors.insert({&block, succ});
6336+
}
6337+
for (SILBasicBlock *pred : block.getPredecessorBlocks()) {
6338+
foundPredecessors.insert({pred, &block});
6339+
}
6340+
}
6341+
for (PredSuccPair predSucc : foundSuccessors) {
6342+
require(foundPredecessors.contains(predSucc),
6343+
"block is not predecessor of its successor");
6344+
}
6345+
for (PredSuccPair predSucc : foundPredecessors) {
6346+
require(foundSuccessors.contains(predSucc),
6347+
"block is not successor of its predecessor");
6348+
}
63346349
}
63356350

63366351
void visitSILFunction(SILFunction *F) {

0 commit comments

Comments
 (0)