Skip to content

[NFC] Add LoadBorrow::getEndBorrows() and minor cleanup. #24581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions include/swift/SIL/OwnershipUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,27 +126,6 @@ class LinearLifetimeError {
}
};

struct UseToEndBorrow {
Optional<EndBorrowInst *> operator()(Operand *use) const {
if (auto *ebi = dyn_cast<EndBorrowInst>(use->getUser())) {
return ebi;
}
return None;
}
};

using EndBorrowRange =
OptionalTransformRange<ValueBase::use_range, UseToEndBorrow,
ValueBase::use_iterator>;

/// Given a value \p v that is a "borrow" introducer, return its associated
/// end_borrow users.
inline auto makeEndBorrowRange(SILValue v) -> EndBorrowRange {
assert((isa<BeginBorrowInst>(v) || isa<LoadBorrowInst>(v)) &&
"Unhandled borrow introducer");
return EndBorrowRange(v->getUses(), UseToEndBorrow());
}

/// Returns true if:
///
/// 1. No consuming uses are reachable from any other consuming use, from any
Expand Down
44 changes: 26 additions & 18 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -3231,19 +3231,40 @@ class StoreInst
}
};

class EndBorrowInst;

struct UseToEndBorrow {
Optional<EndBorrowInst *> operator()(Operand *use) const {
if (auto endBorrow = dyn_cast<EndBorrowInst>(use->getUser())) {
return endBorrow;
} else {
return None;
}
}
};

/// Represents a load of a borrowed value. Must be paired with an end_borrow
/// instruction in its use-def list.
class LoadBorrowInst :
public UnaryInstructionBase<SILInstructionKind::LoadBorrowInst,
SingleValueInstruction> {
friend class SILBuilder;

public:
LoadBorrowInst(SILDebugLocation DebugLoc, SILValue LValue)
: UnaryInstructionBase(DebugLoc, LValue,
LValue->getType().getObjectType()) {}

using EndBorrowRange =
OptionalTransformRange<use_range, UseToEndBorrow, use_iterator>;

/// Return a range over all EndBorrow instructions for this BeginBorrow.
EndBorrowRange getEndBorrows() const;
};

class EndBorrowInst;
inline auto LoadBorrowInst::getEndBorrows() const -> EndBorrowRange {
return EndBorrowRange(getUses(), UseToEndBorrow());
}

/// Represents the begin scope of a borrowed value. Must be paired with an
/// end_borrow instruction in its use-def list.
Expand All @@ -3252,9 +3273,6 @@ class BeginBorrowInst
SingleValueInstruction> {
friend class SILBuilder;

/// Predicate used to filter EndBorrowRange.
struct UseToEndBorrow;

BeginBorrowInst(SILDebugLocation DebugLoc, SILValue LValue)
: UnaryInstructionBase(DebugLoc, LValue,
LValue->getType().getObjectType()) {}
Expand All @@ -3275,6 +3293,10 @@ class BeginBorrowInst
Operand *getSingleNonEndingUse() const;
};

inline auto BeginBorrowInst::getEndBorrows() const -> EndBorrowRange {
return EndBorrowRange(getUses(), UseToEndBorrow());
}

/// Represents a store of a borrowed value into an address. Returns the borrowed
/// address. Must be paired with an end_borrow in its use-def list.
class StoreBorrowInst
Expand Down Expand Up @@ -3364,20 +3386,6 @@ class EndBorrowInst
}
};

struct BeginBorrowInst::UseToEndBorrow {
Optional<EndBorrowInst *> operator()(Operand *use) const {
if (auto borrow = dyn_cast<EndBorrowInst>(use->getUser())) {
return borrow;
} else {
return None;
}
}
};

inline auto BeginBorrowInst::getEndBorrows() const -> EndBorrowRange {
return EndBorrowRange(getUses(), UseToEndBorrow());
}

/// Different kinds of access.
enum class SILAccessKind : uint8_t {
/// An access which takes uninitialized memory and initializes it.
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/SILOwnershipVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ bool SILValueOwnershipChecker::gatherUsers(
for (unsigned i : indices(nonLifetimeEndingUsers)) {
if (auto *bbi = dyn_cast<BeginBorrowInst>(
nonLifetimeEndingUsers[i].getInst())) {
copy(makeEndBorrowRange(bbi),
copy(bbi->getEndBorrows(),
std::back_inserter(implicitRegularUsers));
}
}
Expand Down