Skip to content

Commit 2c25618

Browse files
committed
Add new utility swift::areUsesWithinValueLifetime
1 parent 2dbffa2 commit 2c25618

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

include/swift/SILOptimizer/Utils/OwnershipOptUtils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ class OwnershipRAUWHelper {
309309
/// specified lexical value.
310310
bool areUsesWithinLexicalValueLifetime(SILValue, ArrayRef<Operand *>);
311311

312+
/// Whether the provided uses lie within the current liveness of the
313+
/// specified value.
314+
bool areUsesWithinValueLifetime(SILValue value, ArrayRef<Operand *> uses,
315+
DeadEndBlocks *deBlocks);
316+
312317
/// A utility composed ontop of OwnershipFixupContext that knows how to replace
313318
/// a single use of a value with another value with a different ownership. We
314319
/// allow for the values to have different types.

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,30 @@ bool swift::areUsesWithinLexicalValueLifetime(SILValue value,
456456
return false;
457457
}
458458

459+
bool swift::areUsesWithinValueLifetime(SILValue value, ArrayRef<Operand *> uses,
460+
DeadEndBlocks *deBlocks) {
461+
assert(value->getFunction()->hasOwnership());
462+
463+
if (value->getOwnershipKind() == OwnershipKind::None) {
464+
return true;
465+
}
466+
if (value->getOwnershipKind() != OwnershipKind::Guaranteed &&
467+
value->getOwnershipKind() != OwnershipKind::Owned) {
468+
return false;
469+
}
470+
if (value->getOwnershipKind() == OwnershipKind::Guaranteed) {
471+
value = findOwnershipReferenceAggregate(value);
472+
BorrowedValue borrowedValue(value);
473+
if (!borrowedValue.isLocalScope()) {
474+
return true;
475+
}
476+
}
477+
SSAPrunedLiveness liveness(value->getFunction());
478+
liveness.initializeDef(value);
479+
liveness.computeSimple();
480+
return liveness.areUsesWithinBoundary(uses, deBlocks);
481+
}
482+
459483
//===----------------------------------------------------------------------===//
460484
// BorrowedLifetimeExtender
461485
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)