Files
swift-mirror/lib/SILGen/ArgumentSource.cpp
John McCall e249fd680e Destructure result types in SIL function types.
Similarly to how we've always handled parameter types, we
now recursively expand tuples in result types and separately
determine a result convention for each result.

The most important code-generation change here is that
indirect results are now returned separately from each
other and from any direct results.  It is generally far
better, when receiving an indirect result, to receive it
as an independent result; the caller is much more likely
to be able to directly receive the result in the address
they want to initialize, rather than having to receive it
in temporary memory and then copy parts of it into the
target.

The most important conceptual change here that clients and
producers of SIL must be aware of is the new distinction
between a SILFunctionType's *parameters* and its *argument
list*.  The former is just the formal parameters, derived
purely from the parameter types of the original function;
indirect results are no longer in this list.  The latter
includes the indirect result arguments; as always, all
the indirect results strictly precede the parameters.
Apply instructions and entry block arguments follow the
argument list, not the parameter list.

A relatively minor change is that there can now be multiple
direct results, each with its own result convention.
This is a minor change because I've chosen to leave
return instructions as taking a single operand and
apply instructions as producing a single result; when
the type describes multiple results, they are implicitly
bound up in a tuple.  It might make sense to split these
up and allow e.g. return instructions to take a list
of operands; however, it's not clear what to do on the
caller side, and this would be a major change that can
be separated out from this already over-large patch.

Unsurprisingly, the most invasive changes here are in
SILGen; this requires substantial reworking of both call
emission and reabstraction.  It also proved important
to switch several SILGen operations over to work with
RValue instead of ManagedValue, since otherwise they
would be forced to spuriously "implode" buffers.
2016-02-18 01:26:28 -08:00

180 lines
6.3 KiB
C++

//===--- ArgumentSource.cpp - Latent value representation -----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// A structure for holding a r-value or l-value
//
//===----------------------------------------------------------------------===//
#include "ArgumentSource.h"
#include "Initialization.h"
using namespace swift;
using namespace Lowering;
RValue &ArgumentSource::forceAndPeekRValue(SILGenFunction &gen) & {
if (isRValue()) {
return peekRValue();
}
auto expr = asKnownExpr();
StoredKind = Kind::RValue;
new (&Storage.TheRV.Value) RValue(gen.emitRValue(expr));
Storage.TheRV.Loc = expr;
return Storage.TheRV.Value;
}
RValue &ArgumentSource::peekRValue() & {
assert(isRValue() && "Undefined behavior to call this method without the "
"ArgumentSource actually being an RValue");
return Storage.TheRV.Value;
}
void ArgumentSource::rewriteType(CanType newType) & {
assert(!isLValue());
if (isRValue()) {
Storage.TheRV.Value.rewriteType(newType);
} else {
Expr *expr = Storage.TheExpr;
if (expr->getType()->isEqual(newType)) return;
llvm_unreachable("unimplemented! hope it doesn't happen");
}
}
RValue ArgumentSource::getAsRValue(SILGenFunction &gen, SGFContext C) && {
assert(!isLValue());
if (isRValue())
return std::move(*this).asKnownRValue();
return gen.emitRValue(std::move(*this).asKnownExpr(), C);
}
ManagedValue ArgumentSource::getAsSingleValue(SILGenFunction &gen,
SGFContext C) && {
if (isRValue()) {
auto loc = getKnownRValueLocation();
return std::move(*this).asKnownRValue().getAsSingleValue(gen, loc);
}
if (isLValue()) {
auto loc = getKnownLValueLocation();
return gen.emitAddressOfLValue(loc, std::move(*this).asKnownLValue(),
AccessKind::ReadWrite);
}
auto e = std::move(*this).asKnownExpr();
if (e->getType()->is<InOutType>()) {
return gen.emitAddressOfLValue(e, gen.emitLValue(e, AccessKind::ReadWrite),
AccessKind::ReadWrite);
} else {
return gen.emitRValueAsSingleValue(e, C);
}
}
ManagedValue ArgumentSource::getAsSingleValue(SILGenFunction &gen,
AbstractionPattern origFormalType,
SGFContext C) && {
auto loc = getLocation();
auto substFormalType = getSubstType();
ManagedValue outputValue = std::move(*this).getAsSingleValue(gen);
return gen.emitSubstToOrigValue(loc,
outputValue, origFormalType,
substFormalType, C);
}
void ArgumentSource::forwardInto(SILGenFunction &gen, Initialization *dest) && {
assert(!isLValue());
if (isRValue()) {
auto loc = getKnownRValueLocation();
return std::move(*this).asKnownRValue().forwardInto(gen, loc, dest);
}
auto e = std::move(*this).asKnownExpr();
return gen.emitExprInto(e, dest);
}
ManagedValue ArgumentSource::materialize(SILGenFunction &gen) && {
assert(!isLValue());
if (isRValue()) {
auto loc = getKnownRValueLocation();
return std::move(*this).asKnownRValue().materialize(gen, loc);
}
auto expr = std::move(*this).asKnownExpr();
auto temp = gen.emitTemporary(expr, gen.getTypeLowering(expr->getType()));
gen.emitExprInto(expr, temp.get());
return temp->getManagedAddress();
}
ManagedValue ArgumentSource::materialize(SILGenFunction &SGF,
AbstractionPattern origFormalType,
SILType destType) && {
auto substFormalType = CanType(getSubstType()->getInOutObjectType());
assert(!destType || destType.getObjectType() ==
SGF.SGM.Types.getLoweredType(origFormalType,
substFormalType).getObjectType());
// Fast path: if the types match exactly, no abstraction difference
// is possible and we can just materialize as normal.
if (origFormalType.isExactType(substFormalType))
return std::move(*this).materialize(SGF);
auto &destTL =
(destType ? SGF.getTypeLowering(destType)
: SGF.getTypeLowering(origFormalType, substFormalType));
if (!destType) destType = destTL.getLoweredType();
// If there's no abstraction difference, we can just materialize as normal.
if (destTL.getLoweredType() == SGF.getLoweredType(substFormalType)) {
return std::move(*this).materialize(SGF);
}
// Emit a temporary at the given address.
auto temp = SGF.emitTemporary(getLocation(), destTL);
// Forward into it.
std::move(*this).forwardInto(SGF, origFormalType, temp.get(), destTL);
return temp->getManagedAddress();
}
void ArgumentSource::forwardInto(SILGenFunction &SGF,
AbstractionPattern origFormalType,
Initialization *dest,
const TypeLowering &destTL) && {
auto substFormalType = getSubstType();
assert(destTL.getLoweredType() ==
SGF.getLoweredType(origFormalType, substFormalType));
// If there are no abstraction changes, we can just forward
// normally.
if (origFormalType.isExactType(substFormalType) ||
destTL.getLoweredType() == SGF.getLoweredType(substFormalType)) {
std::move(*this).forwardInto(SGF, dest);
return;
}
// Otherwise, emit as a single independent value.
SILLocation loc = getLocation();
ManagedValue outputValue =
std::move(*this).getAsSingleValue(SGF, origFormalType,
SGFContext(dest));
if (outputValue.isInContext()) return;
// Use RValue's forward-into-initialization code. We have to lie to
// RValue about the formal type (by using the lowered type) because
// we're emitting into an abstracted value, which RValue doesn't
// really handle.
auto substLoweredType = destTL.getLoweredType().getSwiftRValueType();
RValue(SGF, loc, substLoweredType, outputValue).forwardInto(SGF, loc, dest);
}