Files
swift-mirror/lib/SIL/SILValue.cpp
John McCall ab3f77baf2 Make SILInstruction no longer a subclass of ValueBase and
introduce a common superclass, SILNode.

This is in preparation for allowing instructions to have multiple
results.  It is also a somewhat more elegant representation for
instructions that have zero results.  Instructions that are known
to have exactly one result inherit from a class, SingleValueInstruction,
that subclasses both ValueBase and SILInstruction.  Some care must be
taken when working with SILNode pointers and testing for equality;
please see the comment on SILNode for more information.

A number of SIL passes needed to be updated in order to handle this
new distinction between SIL values and SIL instructions.

Note that the SIL parser is now stricter about not trying to assign
a result value from an instruction (like 'return' or 'strong_retain')
that does not produce any.
2017-09-25 02:06:26 -04:00

248 lines
8.4 KiB
C++

//===--- SILValue.cpp - Implementation for SILValue -----------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/SILValue.h"
#include "ValueOwnershipKindClassifier.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILBuiltinVisitor.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILVisitor.h"
#include "llvm/ADT/StringSwitch.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// Check SILValue Type Properties
//===----------------------------------------------------------------------===//
/// These are just for performance and verification. If one needs to make
/// changes that cause the asserts the fire, please update them. The purpose is
/// to prevent these predicates from changing values by mistake.
static_assert(std::is_standard_layout<SILValue>::value,
"Expected SILValue to be standard layout");
static_assert(sizeof(SILValue) == sizeof(uintptr_t),
"SILValue should be pointer sized");
//===----------------------------------------------------------------------===//
// Utility Methods
//===----------------------------------------------------------------------===//
void ValueBase::replaceAllUsesWith(ValueBase *RHS) {
assert(this != RHS && "Cannot RAUW a value with itself");
while (!use_empty()) {
Operand *Op = *use_begin();
Op->set(RHS);
}
}
void ValueBase::replaceAllUsesWithUndef() {
SILModule *Mod = getModule();
if (!Mod) {
llvm_unreachable("replaceAllUsesWithUndef can only be used on ValueBase "
"that have access to the parent module.");
}
while (!use_empty()) {
Operand *Op = *use_begin();
Op->set(SILUndef::get(Op->get()->getType(), Mod));
}
}
SILInstruction *ValueBase::getDefiningInstruction() {
if (auto inst = dyn_cast<SingleValueInstruction>(this))
return inst;
// TODO: MultiValueInstruction
return nullptr;
}
Optional<ValueBase::DefiningInstructionResult>
ValueBase::getDefiningInstructionResult() {
if (auto inst = dyn_cast<SingleValueInstruction>(this))
return DefiningInstructionResult{ inst, 0 };
// TODO: MultiValueInstruction
return None;
}
SILBasicBlock *SILNode::getParentBlock() const {
auto *NonConstThis = const_cast<SILNode *>(this);
if (auto *Inst = dyn_cast<SILInstruction>(NonConstThis))
return Inst->getParent();
// TODO: MultiValueInstruction
if (auto *Arg = dyn_cast<SILArgument>(NonConstThis))
return Arg->getParent();
return nullptr;
}
SILFunction *SILNode::getFunction() const {
auto *NonConstThis = const_cast<SILNode *>(this);
if (auto *Inst = dyn_cast<SILInstruction>(NonConstThis))
return Inst->getFunction();
// TODO: MultiValueInstruction
if (auto *Arg = dyn_cast<SILArgument>(NonConstThis))
return Arg->getFunction();
return nullptr;
}
SILModule *SILNode::getModule() const {
auto *NonConstThis = const_cast<SILNode *>(this);
if (auto *Inst = dyn_cast<SILInstruction>(NonConstThis))
return &Inst->getModule();
// TODO: MultiValueInstruction
if (auto *Arg = dyn_cast<SILArgument>(NonConstThis))
return &Arg->getModule();
return nullptr;
}
const SILNode *SILNode::getCanonicalSILNodeSlowPath() const {
assert(getStorageLoc() != SILNodeStorageLocation::Instruction &&
hasMultipleSILNodes(getKind()));
return &static_cast<const SILInstruction &>(
static_cast<const SingleValueInstruction &>(
static_cast<const ValueBase &>(*this)));
}
//===----------------------------------------------------------------------===//
// ValueOwnershipKind
//===----------------------------------------------------------------------===//
ValueOwnershipKind::ValueOwnershipKind(SILModule &M, SILType Type,
SILArgumentConvention Convention)
: Value() {
// Trivial types can be passed using a variety of conventions. They always
// have trivial ownership.
if (Type.isTrivial(M)) {
Value = ValueOwnershipKind::Trivial;
return;
}
switch (Convention) {
case SILArgumentConvention::Indirect_In:
case SILArgumentConvention::Indirect_In_Constant:
Value = SILModuleConventions(M).useLoweredAddresses()
? ValueOwnershipKind::Trivial
: ValueOwnershipKind::Owned;
break;
case SILArgumentConvention::Indirect_In_Guaranteed:
Value = SILModuleConventions(M).useLoweredAddresses()
? ValueOwnershipKind::Trivial
: ValueOwnershipKind::Guaranteed;
break;
case SILArgumentConvention::Indirect_Inout:
case SILArgumentConvention::Indirect_InoutAliasable:
case SILArgumentConvention::Indirect_Out:
Value = ValueOwnershipKind::Trivial;
return;
case SILArgumentConvention::Direct_Owned:
Value = ValueOwnershipKind::Owned;
return;
case SILArgumentConvention::Direct_Unowned:
Value = ValueOwnershipKind::Unowned;
return;
case SILArgumentConvention::Direct_Guaranteed:
Value = ValueOwnershipKind::Guaranteed;
return;
case SILArgumentConvention::Direct_Deallocating:
llvm_unreachable("Not handled");
}
}
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::Any:
return os << "any";
}
llvm_unreachable("Unhandled ValueOwnershipKind in switch.");
}
Optional<ValueOwnershipKind>
ValueOwnershipKind::merge(ValueOwnershipKind RHS) const {
auto LHSVal = Value;
auto RHSVal = RHS.Value;
// Any merges with anything.
if (LHSVal == ValueOwnershipKind::Any) {
return ValueOwnershipKind(RHSVal);
}
// Any merges with anything.
if (RHSVal == ValueOwnershipKind::Any) {
return ValueOwnershipKind(LHSVal);
}
return (LHSVal == RHSVal) ? Optional<ValueOwnershipKind>(*this) : None;
}
ValueOwnershipKind::ValueOwnershipKind(StringRef S) {
auto Result = llvm::StringSwitch<Optional<ValueOwnershipKind::innerty>>(S)
.Case("trivial", ValueOwnershipKind::Trivial)
.Case("unowned", ValueOwnershipKind::Unowned)
.Case("owned", ValueOwnershipKind::Owned)
.Case("guaranteed", ValueOwnershipKind::Guaranteed)
.Case("any", ValueOwnershipKind::Any)
.Default(None);
if (!Result.hasValue())
llvm_unreachable("Invalid string representation of ValueOwnershipKind");
Value = Result.getValue();
}
ValueOwnershipKind SILValue::getOwnershipKind() const {
// Once we have multiple return values, this must be changed.
sil::ValueOwnershipKindClassifier Classifier;
return Classifier.visit(const_cast<ValueBase *>(Value));
}
#if 0
/// Map a SILValue mnemonic name to its ValueKind.
ValueKind swift::getSILValueKind(StringRef Name) {
#define SINGLE_VALUE_INST(Id, TextualName, Parent, MemoryBehavior, ReleasingBehavior) \
if (Name == #TextualName) \
return ValueKind::Id;
#define VALUE(Id, Parent) \
if (Name == #Id) \
return ValueKind::Id;
#include "swift/SIL/SILNodes.def"
#ifdef NDEBUG
llvm::errs()
<< "Unknown SILValue name\n";
abort();
#endif
llvm_unreachable("Unknown SILValue name");
}
/// Map ValueKind to a corresponding mnemonic name.
StringRef swift::getSILValueName(ValueKind Kind) {
switch (Kind) {
#define SINGLE_VALUE_INST(Id, TextualName, Parent, MemoryBehavior, ReleasingBehavior) \
case ValueKind::Id: \
return #TextualName;
#define VALUE(Id, Parent) \
case ValueKind::Id: \
return #Id;
#include "swift/SIL/SILNodes.def"
}
}
#endif