Skip to content

Commit ec9d4b4

Browse files
committed
Add SILInstruction::getNextInstruction() and getPreviousInstruction()
1 parent 321351c commit ec9d4b4

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,14 @@ class SILInstruction : public llvm::ilist_node<SILInstruction> {
459459
locationStorage = loc.storage;
460460
}
461461

462+
/// Return the next instruction or nullptr if this is the last instruction in
463+
/// its block.
464+
SILInstruction *getPreviousInstruction();
465+
466+
/// Return the previous instruction or nullptr if this is the first
467+
/// instruction in its block.
468+
SILInstruction *getNextInstruction();
469+
462470
/// This method unlinks 'self' from the containing basic block and deletes it.
463471
void eraseFromParent();
464472

@@ -1013,6 +1021,14 @@ class SingleValueInstruction : public SILInstruction, public ValueBase {
10131021
/// If this is an instruction which "defines" an opened archetype, it is
10141022
/// returned.
10151023
CanArchetypeType getOpenedArchetype() const;
1024+
1025+
SILInstruction *getPreviousInstruction() {
1026+
return SILInstruction::getPreviousInstruction();
1027+
}
1028+
1029+
SILInstruction *getNextInstruction() {
1030+
return SILInstruction::getNextInstruction();
1031+
}
10161032
};
10171033

10181034
struct SILNodeOffsetChecker {

lib/SIL/IR/SILInstruction.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ SILModule &SILInstruction::getModule() const {
103103
return getFunction()->getModule();
104104
}
105105

106+
SILInstruction *SILInstruction::getPreviousInstruction() {
107+
auto pos = getIterator();
108+
return pos == getParent()->begin() ? nullptr : &*std::prev(pos);
109+
}
110+
111+
SILInstruction *SILInstruction::getNextInstruction() {
112+
auto nextPos = std::next(getIterator());
113+
return nextPos == getParent()->end() ? nullptr : &*nextPos;
114+
}
115+
106116
void SILInstruction::removeFromParent() {
107117
#ifndef NDEBUG
108118
for (auto result : getResults()) {

lib/SILOptimizer/Utils/ShrinkBorrowScope.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,6 @@
2020

2121
using namespace swift;
2222

23-
//===----------------------------------------------------------------------===//
24-
// MARK: Local utilities
25-
//===----------------------------------------------------------------------===//
26-
27-
// TODO: Move to be member function on SILInstruction.
28-
static SILInstruction *getPreviousInstruction(SILInstruction *inst) {
29-
auto pos = inst->getIterator();
30-
return pos == inst->getParent()->begin() ? nullptr
31-
: &*std::prev(inst->getIterator());
32-
}
33-
34-
// TODO: Move to be member function on SILInstruction.
35-
static SILInstruction *getNextInstruction(SILInstruction *inst) {
36-
auto nextPos = std::next(inst->getIterator());
37-
return nextPos == inst->getParent()->end() ? nullptr : &*nextPos;
38-
}
39-
4023
//===----------------------------------------------------------------------===//
4124
// MARK: ShrinkBorrowScope
4225
//===----------------------------------------------------------------------===//
@@ -318,7 +301,7 @@ void ShrinkBorrowScope::findBarriers() {
318301
assert(tryHoistOverInstruction(block->getTerminator()));
319302
}
320303
SILInstruction *barrier = nullptr;
321-
while ((instruction = getPreviousInstruction(instruction))) {
304+
while ((instruction = instruction->getPreviousInstruction())) {
322305
if (instruction == introducer) {
323306
barrier = instruction;
324307
break;
@@ -354,7 +337,7 @@ bool ShrinkBorrowScope::rewrite() {
354337

355338
// Insert the new end_borrow instructions that occur after deinit barriers.
356339
for (auto pair : barrierInstructions) {
357-
auto *insertionPoint = getNextInstruction(pair.second);
340+
auto *insertionPoint = pair.second->getNextInstruction();
358341
createdBorrow |= createEndBorrow(insertionPoint);
359342
}
360343

0 commit comments

Comments
 (0)