Skip to content

Commit 7990dda

Browse files
committed
Cleanup PrunedLiveness interface.
In preparation for adding OwnershipLiveness. Rename Simple LiveRangeSummary to LiveRangeSummary. Add initializeDefNode helpers to avoid confusion about the argument type. Add defBegin/defEnd iterators in MultiDefPrunedLiveness.
1 parent a06e4d4 commit 7990dda

File tree

6 files changed

+47
-34
lines changed

6 files changed

+47
-34
lines changed

include/swift/SIL/PrunedLiveness.h

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -410,21 +410,21 @@ inline InnerBorrowKind meet(InnerBorrowKind lhs, InnerBorrowKind rhs) {
410410
/// Summarize reborrows and pointer escapes that affect a live range. Reborrows
411411
/// and pointer escapes that are encapsulated in a nested borrow don't affect
412412
/// the outer live range.
413-
struct SimpleLiveRangeSummary {
413+
struct LiveRangeSummary {
414414
InnerBorrowKind innerBorrowKind;
415415
AddressUseKind addressUseKind;
416416

417-
SimpleLiveRangeSummary(): innerBorrowKind(InnerBorrowKind::Contained),
418-
addressUseKind(AddressUseKind::NonEscaping)
419-
{}
417+
LiveRangeSummary()
418+
: innerBorrowKind(InnerBorrowKind::Contained),
419+
addressUseKind(AddressUseKind::NonEscaping) {}
420420

421421
void meet(const InnerBorrowKind lhs) {
422422
innerBorrowKind = swift::meet(innerBorrowKind, lhs);
423423
}
424424
void meet(const AddressUseKind lhs) {
425425
addressUseKind = swift::meet(addressUseKind, lhs);
426426
}
427-
void meet(const SimpleLiveRangeSummary lhs) {
427+
void meet(const LiveRangeSummary lhs) {
428428
meet(lhs.innerBorrowKind);
429429
meet(lhs.addressUseKind);
430430
}
@@ -601,13 +601,15 @@ class PrunedLiveRange : public PrunedLiveness {
601601
PrunedLiveRange(SmallVectorImpl<SILBasicBlock *> *discoveredBlocks = nullptr)
602602
: PrunedLiveness(discoveredBlocks) {}
603603

604-
SimpleLiveRangeSummary recursivelyUpdateForDef(SILValue initialDef,
605-
ValueSet &visited,
606-
SILValue value);
604+
LiveRangeSummary recursivelyUpdateForDef(SILValue initialDef,
605+
ValueSet &visited,
606+
SILValue value);
607607

608608
public:
609-
/// Update liveness for all direct uses of \p def.
610-
SimpleLiveRangeSummary updateForDef(SILValue def);
609+
/// Update liveness for all direct uses of \p def. Transitively follows
610+
/// guaranteed forwards up to but not including guaranteed phis. If \p def is
611+
/// used by a guaranteed phi return InnerBorrowKind::Reborrowed.
612+
LiveRangeSummary updateForDef(SILValue def);
611613

612614
/// Check if \p inst occurs in between the definition this def and the
613615
/// liveness boundary.
@@ -716,7 +718,7 @@ class SSAPrunedLiveness : public PrunedLiveRange<SSAPrunedLiveness> {
716718
/// jointly-post dominate if dead-end blocks are present. Nested scopes may
717719
/// also lack scope-ending instructions, so the liveness of their nested uses
718720
/// may be ignored.
719-
SimpleLiveRangeSummary computeSimple() {
721+
LiveRangeSummary computeSimple() {
720722
assert(def && "SSA def uninitialized");
721723
return updateForDef(def);
722724
}
@@ -730,6 +732,13 @@ class MultiDefPrunedLiveness : public PrunedLiveRange<MultiDefPrunedLiveness> {
730732
NodeSetVector defs;
731733
BasicBlockSet defBlocks;
732734

735+
void initializeDefNode(SILNode *def) {
736+
defs.insert(def);
737+
auto *block = def->getParentBlock();
738+
defBlocks.insert(block);
739+
initializeDefBlock(block);
740+
}
741+
733742
public:
734743
MultiDefPrunedLiveness(
735744
SILFunction *function,
@@ -741,16 +750,25 @@ class MultiDefPrunedLiveness : public PrunedLiveRange<MultiDefPrunedLiveness> {
741750
llvm_unreachable("multi-def liveness cannot be reused");
742751
}
743752

744-
void initializeDef(SILNode *def) {
745-
assert(isa<SILInstruction>(def) || isa<SILArgument>(def));
746-
defs.insert(def);
747-
auto *block = def->getParentBlock();
748-
defBlocks.insert(block);
749-
initializeDefBlock(block);
753+
void initializeDef(SILInstruction *defInst) {
754+
initializeDefNode(defInst->asSILNode());
755+
}
756+
757+
void initializeDef(SILArgument *defArg) { initializeDefNode(defArg); }
758+
759+
void initializeDef(SILValue value) {
760+
if (auto arg = dyn_cast<SILArgument>(value)) {
761+
initializeDefNode(arg);
762+
} else {
763+
initializeDef(value->getDefiningInstruction());
764+
}
750765
}
751766

752767
bool isInitialized() const { return !defs.empty(); }
753768

769+
NodeSetVector::iterator defBegin() const { return defs.begin(); }
770+
NodeSetVector::iterator defEnd() const { return defs.end(); }
771+
754772
bool isDef(SILInstruction *inst) const {
755773
return defs.contains(cast<SILNode>(inst));
756774
}
@@ -779,7 +797,7 @@ class MultiDefPrunedLiveness : public PrunedLiveRange<MultiDefPrunedLiveness> {
779797
/// jointly-post dominate if dead-end blocks are present. Nested scopes may
780798
/// also lack scope-ending instructions, so the liveness of their nested uses
781799
/// may be ignored.
782-
SimpleLiveRangeSummary computeSimple();
800+
LiveRangeSummary computeSimple();
783801
};
784802

785803
//===----------------------------------------------------------------------===//

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ bool AddressOwnership::areUsesWithinLifetime(
11311131
// Compute the reference value's liveness.
11321132
SSAPrunedLiveness liveness;
11331133
liveness.initializeDef(root);
1134-
SimpleLiveRangeSummary summary = liveness.computeSimple();
1134+
LiveRangeSummary summary = liveness.computeSimple();
11351135
// Conservatively ignore InnerBorrowKind::Reborrowed and
11361136
// AddressUseKind::PointerEscape and Reborrowed. The resulting liveness at
11371137
// least covers the known uses.

lib/SIL/Utils/PrunedLiveness.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,18 +275,15 @@ void PrunedLivenessBoundary::visitInsertionPoints(
275275
//===----------------------------------------------------------------------===//
276276

277277
template <typename LivenessWithDefs>
278-
SimpleLiveRangeSummary
279-
PrunedLiveRange<LivenessWithDefs>::updateForDef(SILValue def) {
278+
LiveRangeSummary PrunedLiveRange<LivenessWithDefs>::updateForDef(SILValue def) {
280279
ValueSet visited(def->getFunction());
281280
return recursivelyUpdateForDef(def, visited, def);
282281
}
283282

284283
template <typename LivenessWithDefs>
285-
SimpleLiveRangeSummary
286-
PrunedLiveRange<LivenessWithDefs>::recursivelyUpdateForDef(SILValue initialDef,
287-
ValueSet &visited,
288-
SILValue value) {
289-
SimpleLiveRangeSummary summary;
284+
LiveRangeSummary PrunedLiveRange<LivenessWithDefs>::recursivelyUpdateForDef(
285+
SILValue initialDef, ValueSet &visited, SILValue value) {
286+
LiveRangeSummary summary;
290287

291288
if (!visited.insert(value))
292289
return summary;
@@ -620,10 +617,10 @@ void MultiDefPrunedLiveness::findBoundariesInBlock(
620617
&& "findBoundariesInBlock must be called on a live block");
621618
}
622619

623-
SimpleLiveRangeSummary MultiDefPrunedLiveness::computeSimple() {
620+
LiveRangeSummary MultiDefPrunedLiveness::computeSimple() {
624621
assert(isInitialized() && "defs uninitialized");
625622

626-
SimpleLiveRangeSummary summary;
623+
LiveRangeSummary summary;
627624
for (SILNode *defNode : defs) {
628625
if (auto *arg = dyn_cast<SILArgument>(defNode))
629626
summary.meet(updateForDef(arg));

lib/SILOptimizer/Mandatory/MoveKillsCopyableAddressesChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ void ClosureArgDataflowState::classifyUses(BasicBlockSet &initBlocks,
722722
for (auto *user : useState.inits) {
723723
if (upwardScanForInit(user, useState)) {
724724
LLVM_DEBUG(llvm::dbgs() << " Found init block at: " << *user);
725-
livenessForConsumes.initializeDef(cast<SILNode>(user));
725+
livenessForConsumes.initializeDef(user);
726726
initBlocks.insert(user->getParent());
727727
}
728728
}

lib/SILOptimizer/Mandatory/MoveKillsCopyableValuesChecker.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,8 @@ bool CheckerLivenessInfo::compute() {
165165
// just mark it as extending liveness and look through it.
166166
liveness.updateForUse(user, /*lifetimeEnding*/ false);
167167
ForwardingOperand(use).visitForwardedValues([&](SILValue result) {
168-
if (auto *arg = dyn_cast<SILPhiArgument>(result)) {
169-
if (arg->isTerminatorResult()) {
170-
return true;
171-
}
168+
if (SILArgument::isTerminatorResult(result)) {
169+
return true;
172170
}
173171
if (result->getOwnershipKind() == OwnershipKind::Guaranteed)
174172
defUseWorklist.insert(result);

lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ struct SSALivenessTest : UnitTest {
301301
SmallVector<SILBasicBlock *, 8> discoveredBlocks;
302302
SSAPrunedLiveness liveness(&discoveredBlocks);
303303
liveness.initializeDef(value);
304-
SimpleLiveRangeSummary summary = liveness.computeSimple();
304+
LiveRangeSummary summary = liveness.computeSimple();
305305
if (summary.innerBorrowKind == InnerBorrowKind::Reborrowed)
306306
llvm::outs() << "Incomplete liveness: Reborrowed inner scope\n";
307307

0 commit comments

Comments
 (0)