[semantic-sil] Define ValueOwnershipKind enum.

This enum models the various froms of ownership semantics that a specific
SILValue can have.

rdar://29671437
This commit is contained in:
Michael Gottesman
2016-12-16 16:24:40 -08:00
parent e7c4065987
commit 57d3f848a6
2 changed files with 99 additions and 4 deletions

View File

@@ -20,8 +20,9 @@
#include "swift/Basic/Range.h"
#include "swift/SIL/SILType.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/Support/raw_ostream.h"
namespace swift {
@@ -48,9 +49,63 @@ static inline llvm::hash_code hash_value(ValueKind K) {
return llvm::hash_value(size_t(K));
}
/// ValueBase - This is the base class of the SIL value hierarchy, which
/// represents a runtime computed value. Things like SILInstruction derive
/// from this.
/// A value representing the specific ownership semantics that a SILValue may
/// have.
enum class ValueOwnershipKind : uint8_t {
/// A SILValue with Trivial ownership kind is an independent value that can
/// not be owned. Ownership does not place any constraints on how a SILValue
/// with Trivial ownership kind can be used. Other side effects (e.g. Memory
/// dependencies) must still be respected. A SILValue with Trivial ownership
/// kind must be of Trivial SILType (i.e. SILType::isTrivial(SILModule &) must
/// return true).
///
/// Some examples of SIL types with Trivial ownership are: Builtin.Int32,
/// Builtin.RawPointer, aggregates containing all trivial types.
Trivial,
/// A SILValue with `Unowned` ownership kind is an independent value that has
/// a lifetime that is only guaranteed to last until the next program visible
/// side-effect. To maintain the lifetime of an unowned value, it must be
/// converted to an owned representation via a copy_value.
///
/// Unowned ownership kind occurs mainly along method/function boundaries in
/// between Swift and Objective-C code.
Unowned,
/// A SILValue with `Owned` ownership kind is an independent value that has an
/// ownership independent of any other ownership imbued within it. The
/// SILValue must be paired with a consuming operation that ends the SSA
/// value's lifetime exactly once along all paths through the program.
Owned,
/// A SILValue with `Guaranteed` ownership kind is an independent value that
/// is guaranteed to be live over a specific region of the program. This
/// region can come in several forms:
///
/// 1. @guaranteed function argument. This guarantees that a value will
/// outlive a function.
///
/// 2. A shared borrow region. This is a region denotated by a
/// begin_borrow/load_borrow instruction and an end_borrow instruction. The
/// SSA value must not be destroyed or taken inside the borrowed region.
///
/// Any value with guaranteed ownership must be paired with an end_borrow
/// instruction exactly once along any path through the program.
Guaranteed,
/// A SILValue with undefined ownership. This means that it could take on
/// /any/ ownership semantics. This is meant only to model SILUndef.
Undef,
};
Optional<ValueOwnershipKind>
ValueOwnershipKindMerge(Optional<ValueOwnershipKind> LHS,
Optional<ValueOwnershipKind> RHS);
llvm::raw_ostream &operator<<(llvm::raw_ostream &os, ValueOwnershipKind Kind);
/// This is the base class of the SIL value hierarchy, which represents a
/// runtime computed value. Things like SILInstruction derive from this.
class alignas(8) ValueBase : public SILAllocated<ValueBase> {
SILType Type;
Operand *FirstUse = nullptr;

View File

@@ -65,3 +65,43 @@ SILModule *ValueBase::getModule() const {
return &Arg->getModule();
return nullptr;
}
//===----------------------------------------------------------------------===//
// ValueOwnershipKind
//===----------------------------------------------------------------------===//
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
ValueOwnershipKind Kind) {
switch (Kind) {
case ValueOwnershipKind::Trivial:
return os << "Trivial";
case ValueOwnershipKind::Unowned:
return os << "Unowned";
case ValueOwnershipKind::Owned:
return os << "Owned";
case ValueOwnershipKind::Guaranteed:
return os << "Guaranteed";
case ValueOwnershipKind::Undef:
return os << "Undef";
}
}
Optional<ValueOwnershipKind>
swift::ValueOwnershipKindMerge(Optional<ValueOwnershipKind> LHS,
Optional<ValueOwnershipKind> RHS) {
if (!LHS.hasValue() || !RHS.hasValue())
return NoneType::None;
auto LHSVal = LHS.getValue();
auto RHSVal = RHS.getValue();
// Undef merges with anything.
if (LHSVal == ValueOwnershipKind::Undef) {
return RHSVal;
}
// Undef merges with anything.
if (RHSVal == ValueOwnershipKind::Undef) {
return LHSVal;
}
return (LHSVal == RHSVal) ? LHS : None;
}