Skip to content

Commit 5bce60f

Browse files
authored
Merge pull request llvm#39567 from atrick/new-sil-utils
2 parents 87bf0e4 + fde9825 commit 5bce60f

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

include/swift/SIL/BasicBlockUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ void findJointPostDominatingSet(
148148
function_ref<void(SILBasicBlock *)> foundJointPostDomSetCompletionBlocks,
149149
function_ref<void(SILBasicBlock *)> inputBlocksInJointPostDomSet = {});
150150

151+
#ifndef NDEBUG
152+
bool checkDominates(SILBasicBlock *sourceBlock, SILBasicBlock *destBlock);
153+
#endif
154+
151155
} // namespace swift
152156

153157
#endif

include/swift/SIL/SILInstruction.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8771,10 +8771,13 @@ template <typename BaseTy> class CastBranchInstBase : public BaseTy {
87718771

87728772
TermInst::SuccessorListTy getSuccessors() { return DestBBs; }
87738773

8774-
SILBasicBlock *getSuccessBB() { return DestBBs[0]; }
8775-
const SILBasicBlock *getSuccessBB() const { return DestBBs[0]; }
8776-
SILBasicBlock *getFailureBB() { return DestBBs[1]; }
8777-
const SILBasicBlock *getFailureBB() const { return DestBBs[1]; }
8774+
// Enumerate the successor indices
8775+
enum SuccessorPath { SuccessIdx = 0, FailIdx = 1};
8776+
8777+
SILBasicBlock *getSuccessBB() { return DestBBs[SuccessIdx]; }
8778+
const SILBasicBlock *getSuccessBB() const { return DestBBs[SuccessIdx]; }
8779+
SILBasicBlock *getFailureBB() { return DestBBs[FailIdx]; }
8780+
const SILBasicBlock *getFailureBB() const { return DestBBs[FailIdx]; }
87788781

87798782
/// The number of times the True branch was executed
87808783
ProfileCounter getTrueBBCount() const { return DestBBs[0].getCount(); }

lib/SIL/Utils/BasicBlockUtils.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/SIL/BasicBlockUtils.h"
14+
#include "swift/SIL/BasicBlockDatastructures.h"
1415
#include "swift/Basic/Defer.h"
1516
#include "swift/Basic/STLExtras.h"
1617
#include "swift/SIL/Dominance.h"
@@ -521,3 +522,38 @@ void swift::findJointPostDominatingSet(
521522
}
522523
}
523524
}
525+
526+
//===----------------------------------------------------------------------===//
527+
// checkReachingBlockDominance
528+
//===----------------------------------------------------------------------===//
529+
530+
#ifndef NDEBUG
531+
/// Check that \p sourceBlock dominates \p destBlock.
532+
///
533+
/// Useful for *temporary* assertions when Dominance is unavailable. This is
534+
/// worst case O(numberOfBlocksInFunction). It should only be used when \p
535+
/// sourceBlock is expected to be "close to" \p destBlock in almost all
536+
/// cases. Because of the potential for quadratic behavior, it should only be
537+
/// used during feature development, never as a permanent check. If a dominance
538+
/// check is required for correctness, then DominanceInfo should be passed down
539+
/// to the utility function that needs this check.
540+
bool
541+
swift::checkDominates(SILBasicBlock *sourceBlock, SILBasicBlock *destBlock) {
542+
SILBasicBlock *entryBlock = sourceBlock->getParent()->getEntryBlock();
543+
BasicBlockWorklist worklist(destBlock);
544+
bool reaches = false;
545+
while (SILBasicBlock *block = worklist.pop()) {
546+
if (block == sourceBlock) {
547+
reaches = true;
548+
continue;
549+
}
550+
if (block == entryBlock) {
551+
return false; // does not dominate
552+
}
553+
for (auto *predBlock : block->getPredecessorBlocks()) {
554+
worklist.pushIfNotVisited(predBlock);
555+
}
556+
}
557+
return reaches;
558+
}
559+
#endif

0 commit comments

Comments
 (0)