Skip to content

Commit 2095c0c

Browse files
committed
OSSA: Use a public explicit constructor for BorrowedValue.
1 parent caefb9a commit 2095c0c

File tree

5 files changed

+13
-27
lines changed

5 files changed

+13
-27
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -473,20 +473,13 @@ struct BorrowedValue {
473473

474474
BorrowedValue() = default;
475475

476-
/// If \p value is a borrow introducer construct a valid BorrowedValue.
477-
BorrowedValue(SILValue value) {
476+
/// If value is a borrow introducer, create a valid BorrowedValue.
477+
explicit BorrowedValue(SILValue value) {
478478
kind = BorrowedValueKind::get(value);
479-
if (!kind)
480-
return;
481-
this->value = value;
479+
if (kind)
480+
this->value = value;
482481
}
483482

484-
/// If value is a borrow introducer return it after doing some checks.
485-
///
486-
/// This is the only way to construct a BorrowScopeIntroducingValue. We make
487-
/// the primary constructor private for this reason.
488-
static BorrowedValue get(SILValue value) { return BorrowedValue(value); }
489-
490483
operator bool() const { return kind != BorrowedValueKind::Invalid && value; }
491484

492485
/// If this value is introducing a local scope, gather all local end scope
@@ -567,12 +560,6 @@ struct BorrowedValue {
567560
SILValue operator->() const { return value; }
568561
SILValue operator*() { return value; }
569562
SILValue operator*() const { return value; }
570-
571-
private:
572-
/// Internal constructor for failable static constructor. Please do not expand
573-
/// its usage since it assumes the code passed in is well formed.
574-
BorrowedValue(SILValue value, BorrowedValueKind kind)
575-
: value(value), kind(kind) {}
576563
};
577564

578565
llvm::raw_ostream &operator<<(llvm::raw_ostream &os,

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,13 @@ bool BorrowingOperand::visitBorrowIntroducingUserResults(
414414
case BorrowingOperandKind::Yield:
415415
llvm_unreachable("Never has borrow introducer results!");
416416
case BorrowingOperandKind::BeginBorrow: {
417-
auto value = BorrowedValue::get(cast<BeginBorrowInst>(op->getUser()));
417+
auto value = BorrowedValue(cast<BeginBorrowInst>(op->getUser()));
418418
assert(value);
419419
return visitor(value);
420420
}
421421
case BorrowingOperandKind::Branch: {
422422
auto *bi = cast<BranchInst>(op->getUser());
423-
auto value = BorrowedValue::get(
423+
auto value = BorrowedValue(
424424
bi->getDestBB()->getArgument(op->getOperandNumber()));
425425
assert(value && "guaranteed-to-unowned conversion not allowed on branches");
426426
return visitor(value);
@@ -820,7 +820,7 @@ bool swift::getAllBorrowIntroducingValues(SILValue inputValue,
820820
SILValue value = worklist.pop_back_val();
821821

822822
// First check if v is an introducer. If so, stash it and continue.
823-
if (auto scopeIntroducer = BorrowedValue::get(value)) {
823+
if (auto scopeIntroducer = BorrowedValue(value)) {
824824
out.push_back(scopeIntroducer);
825825
continue;
826826
}
@@ -868,7 +868,7 @@ BorrowedValue swift::getSingleBorrowIntroducingValue(SILValue inputValue) {
868868
while (true) {
869869
// First check if our initial value is an introducer. If we have one, just
870870
// return it.
871-
if (auto scopeIntroducer = BorrowedValue::get(currentValue)) {
871+
if (auto scopeIntroducer = BorrowedValue(currentValue)) {
872872
return scopeIntroducer;
873873
}
874874

lib/SIL/Verifier/ReborrowVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void ReborrowVerifier::verifyReborrows(BorrowingOperand initialScopedOperand,
8383

8484
// Find the scope ending uses of the guaranteed phi arg and add it to the
8585
// worklist.
86-
auto scopedValue = BorrowedValue::get(phiArg);
86+
auto scopedValue = BorrowedValue(phiArg);
8787
assert(scopedValue);
8888
scopedValue.visitLocalScopeEndingUses([&](Operand *op) {
8989
addVisitedOp(op, newBaseVal);

lib/SILOptimizer/Utils/CanonicalOSSALifetime.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ SILValue CanonicalizeOSSALifetime::getCanonicalCopiedDef(SILValue v) {
6767
v = def;
6868
continue;
6969
}
70-
if (auto borrowedVal = BorrowedValue::get(def)) {
70+
if (auto borrowedVal = BorrowedValue(def)) {
7171
// Any def's that aren't filtered out here must be handled by
7272
// computeBorrowLiveness.
7373
switch (borrowedVal.kind) {
@@ -121,7 +121,7 @@ llvm::cl::opt<bool>
121121
llvm::cl::desc("Enable rewriting borrow scopes"));
122122

123123
bool CanonicalizeOSSALifetime::computeBorrowLiveness() {
124-
auto borrowedVal = BorrowedValue::get(currentDef);
124+
auto borrowedVal = BorrowedValue(currentDef);
125125
if (!borrowedVal) {
126126
return false;
127127
}
@@ -261,8 +261,7 @@ bool CanonicalizeOSSALifetime::consolidateBorrowScope() {
261261

262262
auto *beginBorrow = cast<BeginBorrowInst>(currentDef);
263263
SmallVector<SILInstruction *, 1> scopeEndingInst;
264-
BorrowedValue::get(beginBorrow)
265-
.getLocalScopeEndingInstructions(scopeEndingInst);
264+
BorrowedValue(beginBorrow).getLocalScopeEndingInstructions(scopeEndingInst);
266265
assert(scopeEndingInst.size() == 1 && "expected single-block borrow");
267266
// Remove outer uses that occur before the end of the borrow scope by
268267
// forward iterating from begin_borrow to end_borrow.

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ SILBasicBlock::iterator OwnershipRAUWUtility::handleGuaranteed() {
734734
// this formulation is that it ensures that we are always working with a
735735
// non-dominating copy value, allowing us to force our borrowing value to
736736
// need a base phi argument (the one of our choosing).
737-
if (auto oldValueBorrowedVal = BorrowedValue::get(oldValue)) {
737+
if (auto oldValueBorrowedVal = BorrowedValue(oldValue)) {
738738
SmallVector<std::pair<SILBasicBlock *, unsigned>, 8> foundReborrows;
739739
if (oldValueBorrowedVal.gatherReborrows(foundReborrows)) {
740740
rewriteReborrows(newBorrowedValue, foundReborrows, ctx.callbacks);

0 commit comments

Comments
 (0)