Skip to content

Commit ef309b8

Browse files
authored
Merge pull request swiftlang#30179 from gottesmm/pr-413b415c765eb62debe5d09415f22e89952479af
[ownership] Clean up BorrowScopeIntroducingValue.
2 parents 92ba948 + 6fe7ad2 commit ef309b8

File tree

1 file changed

+17
-49
lines changed

1 file changed

+17
-49
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,6 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
364364
const BorrowScopeOperand &operand);
365365

366366
struct BorrowScopeIntroducingValueKind {
367-
using UnderlyingKindTy = std::underlying_type<ValueKind>::type;
368-
369367
/// Enum we use for exhaustive pattern matching over borrow scope introducers.
370368
enum Kind {
371369
LoadBorrow,
@@ -374,8 +372,10 @@ struct BorrowScopeIntroducingValueKind {
374372
Phi,
375373
};
376374

377-
static Optional<BorrowScopeIntroducingValueKind> get(ValueKind kind) {
378-
switch (kind) {
375+
static Optional<BorrowScopeIntroducingValueKind> get(SILValue value) {
376+
if (value.getOwnershipKind() != ValueOwnershipKind::Guaranteed)
377+
return None;
378+
switch (value->getKind()) {
379379
default:
380380
return None;
381381
case ValueKind::LoadBorrowInst:
@@ -384,9 +384,16 @@ struct BorrowScopeIntroducingValueKind {
384384
return BorrowScopeIntroducingValueKind(BeginBorrow);
385385
case ValueKind::SILFunctionArgument:
386386
return BorrowScopeIntroducingValueKind(SILFunctionArgument);
387-
case ValueKind::SILPhiArgument:
387+
case ValueKind::SILPhiArgument: {
388+
if (llvm::any_of(value->getParentBlock()->getPredecessorBlocks(),
389+
[](SILBasicBlock *block) {
390+
return !isa<BranchInst>(block->getTerminator());
391+
})) {
392+
return None;
393+
}
388394
return BorrowScopeIntroducingValueKind(Phi);
389395
}
396+
}
390397
}
391398

392399
Kind value;
@@ -441,53 +448,14 @@ struct BorrowScopeIntroducingValue {
441448
BorrowScopeIntroducingValueKind kind;
442449
SILValue value;
443450

444-
BorrowScopeIntroducingValue(LoadBorrowInst *lbi)
445-
: kind(BorrowScopeIntroducingValueKind::LoadBorrow), value(lbi) {}
446-
BorrowScopeIntroducingValue(BeginBorrowInst *bbi)
447-
: kind(BorrowScopeIntroducingValueKind::BeginBorrow), value(bbi) {}
448-
BorrowScopeIntroducingValue(SILFunctionArgument *arg)
449-
: kind(BorrowScopeIntroducingValueKind::SILFunctionArgument), value(arg) {
450-
assert(arg->getOwnershipKind() == ValueOwnershipKind::Guaranteed);
451-
}
452-
BorrowScopeIntroducingValue(SILPhiArgument *arg)
453-
: kind(BorrowScopeIntroducingValueKind::Phi), value(arg) {
454-
assert(llvm::all_of(arg->getParent()->getPredecessorBlocks(),
455-
[](SILBasicBlock *block) {
456-
return isa<BranchInst>(block->getTerminator());
457-
}) &&
458-
"Phi argument incoming values must come from branch insts!");
459-
assert(arg->isPhiArgument() && "Can only accept a true phi argument!");
460-
assert(arg->getOwnershipKind() == ValueOwnershipKind::Guaranteed);
461-
}
462-
463-
BorrowScopeIntroducingValue(SILValue v)
464-
: kind(*BorrowScopeIntroducingValueKind::get(v->getKind())), value(v) {
465-
// Validate that if we have a phi argument that all our predecessors have
466-
// branches as terminators.
467-
assert(!isa<SILPhiArgument>(v) ||
468-
(llvm::all_of(v->getParentBlock()->getPredecessorBlocks(),
469-
[](SILBasicBlock *block) {
470-
return isa<BranchInst>(block->getTerminator());
471-
}) &&
472-
"Phi argument incoming values must come from branch insts!"));
473-
474-
assert(v.getOwnershipKind() == ValueOwnershipKind::Guaranteed);
475-
}
476-
477451
/// If value is a borrow introducer return it after doing some checks.
452+
///
453+
/// This is the only way to construct a BorrowScopeIntroducingValue. We make
454+
/// the primary constructor private for this reason.
478455
static Optional<BorrowScopeIntroducingValue> get(SILValue value) {
479-
auto kind = BorrowScopeIntroducingValueKind::get(value->getKind());
480-
if (!kind || value.getOwnershipKind() != ValueOwnershipKind::Guaranteed)
481-
return None;
482-
// If kind is phi and we were not passed something with all branch
483-
// predecessors, return None.
484-
if ((*kind) == BorrowScopeIntroducingValueKind::Phi &&
485-
llvm::any_of(value->getParentBlock()->getPredecessorBlocks(),
486-
[](SILBasicBlock *block) {
487-
return !isa<BranchInst>(block->getTerminator());
488-
}))
456+
auto kind = BorrowScopeIntroducingValueKind::get(value);
457+
if (!kind)
489458
return None;
490-
// Otherwise, create our value directly.
491459
return BorrowScopeIntroducingValue(*kind, value);
492460
}
493461

0 commit comments

Comments
 (0)