Skip to content

Commit ec64297

Browse files
committed
Add PrunedLiveness::isWithinBoundary API
A perfectly straightforward liveness check. Used by OSSA utilities to check that an instruction is within a lifetime or scope.
1 parent add3406 commit ec64297

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

include/swift/SIL/PrunedLiveness.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ class PrunedLiveness {
242242
return NonUser;
243243
return useIter->second ? LifetimeEndingUse : NonLifetimeEndingUse;
244244
}
245+
246+
/// Return true if \p inst occurs before the liveness boundary. Used when the
247+
/// client already knows that inst occurs after the start of liveness.
248+
bool isWithinBoundary(SILInstruction *inst);
245249
};
246250

247251
} // namespace swift

lib/SIL/Utils/PrunedLiveness.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,28 @@ bool PrunedLiveness::updateForBorrowingOperand(Operand *op) {
116116
}
117117
return true;
118118
}
119+
120+
bool PrunedLiveness::isWithinBoundary(SILInstruction *inst) {
121+
SILBasicBlock *block = inst->getParent();
122+
switch (getBlockLiveness(block)) {
123+
case PrunedLiveBlocks::Dead:
124+
return false;
125+
case PrunedLiveBlocks::LiveWithin:
126+
break;
127+
case PrunedLiveBlocks::LiveOut:
128+
return true;
129+
}
130+
// The boundary is within this block. This instruction is before the boundary
131+
// iff any interesting uses occur after it.
132+
for (SILInstruction &inst :
133+
make_range(std::next(inst->getIterator()), block->end())) {
134+
switch (isInterestingUser(&inst)) {
135+
case PrunedLiveness::NonUser:
136+
break;
137+
case PrunedLiveness::NonLifetimeEndingUse:
138+
case PrunedLiveness::LifetimeEndingUse:
139+
return true;
140+
}
141+
}
142+
return false;
143+
}

0 commit comments

Comments
 (0)