Skip to content

Commit 61984e0

Browse files
committed
[SIL] Add utils to visit prev/next insts.
For instructions at the back of a block, visiting the subsequent instructions means visiting the instructions at the front of every successor. For instructions at the front of a block, visiting the prior instructions means visiting the instructions at the back of every predecessor.
1 parent 198069d commit 61984e0

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

include/swift/SIL/SILBasicBlock.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,29 @@ inline SILFunction *SILInstruction::getFunction() const {
742742
return getParent()->getParent();
743743
}
744744

745+
inline bool SILInstruction::visitPriorInstructions(
746+
llvm::function_ref<bool(SILInstruction *)> visitor) {
747+
if (auto *previous = getPreviousInstruction()) {
748+
return visitor(previous);
749+
}
750+
for (auto *predecessor : getParent()->getPredecessorBlocks()) {
751+
if (!visitor(&predecessor->back()))
752+
return false;
753+
}
754+
return true;
755+
}
756+
inline bool SILInstruction::visitSubsequentInstructions(
757+
llvm::function_ref<bool(SILInstruction *)> visitor) {
758+
if (auto *next = getNextInstruction()) {
759+
return visitor(next);
760+
}
761+
for (auto *successor : getParent()->getSuccessorBlocks()) {
762+
if (!visitor(&successor->front()))
763+
return false;
764+
}
765+
return true;
766+
}
767+
745768
inline SILInstruction *SILInstruction::getPreviousInstruction() {
746769
auto pos = getIterator();
747770
return pos == getParent()->begin() ? nullptr : &*std::prev(pos);

include/swift/SIL/SILInstruction.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,24 @@ class SILInstruction : public llvm::ilist_node<SILInstruction> {
517517
/// instruction in its block.
518518
SILInstruction *getNextInstruction();
519519

520+
/// Calls \p visitor with each instruction that is immediately prior. Returns
521+
/// false and stops visiting if \p visitor returns false.
522+
///
523+
/// If \p this is is the first instruction in a block, then \p visitor is
524+
/// called with the back of each predecessor. In particular if \p this is
525+
/// the first instruction of the entry block, \p visitor is never called.
526+
bool
527+
visitPriorInstructions(llvm::function_ref<bool(SILInstruction *)> visitor);
528+
529+
/// Calls \p visitor with each instruction that is immediately subsequent.
530+
/// Returns false and stops visiting if \p visitor returns false.
531+
///
532+
/// If \p this is the last instruction in a block, then \p visitor is called
533+
/// with the front of each successor. In particular if \p this is the last
534+
/// instruction of a block without successors, \p visitor is never called.
535+
bool visitSubsequentInstructions(
536+
llvm::function_ref<bool(SILInstruction *)> visitor);
537+
520538
/// This method unlinks 'self' from the containing basic block and deletes it.
521539
void eraseFromParent();
522540

0 commit comments

Comments
 (0)