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.
This commit is contained in:
John McCall
2016-02-05 11:00:36 -08:00
parent 375a7bfde8
commit e249fd680e
217 changed files with 5176 additions and 3220 deletions

View File

@@ -97,10 +97,10 @@ static bool isIdentifiedSourceValue(SILValue Def) {
return false;
// Check that the argument is passed as an in type. This means there are
// no aliases accessible within this function scope.
ParameterConvention Conv = Arg->getParameterInfo().getConvention();
SILArgumentConvention Conv = Arg->getArgumentConvention();
switch (Conv) {
case ParameterConvention::Indirect_In:
case ParameterConvention::Indirect_In_Guaranteed:
case SILArgumentConvention::Indirect_In:
case SILArgumentConvention::Indirect_In_Guaranteed:
return true;
default:
DEBUG(llvm::dbgs() << " Skipping Def: Not an @in argument!\n");
@@ -125,10 +125,10 @@ static bool isIdentifiedDestValue(SILValue Def) {
return false;
// Check that the argument is passed as an out type. This means there are
// no aliases accessible within this function scope.
ParameterConvention Conv = Arg->getParameterInfo().getConvention();
SILArgumentConvention Conv = Arg->getArgumentConvention();
switch (Conv) {
case ParameterConvention::Indirect_Inout:
case ParameterConvention::Indirect_Out:
case SILArgumentConvention::Indirect_Inout:
case SILArgumentConvention::Indirect_Out:
return true;
default:
DEBUG(llvm::dbgs() << " Skipping Def: Not an @in argument!\n");
@@ -145,20 +145,17 @@ static bool isIdentifiedDestValue(SILValue Def) {
/// indirectly via Address.
///
/// Set Oper to the Apply operand that passes Address.
static ParameterConvention getAddressArgConvention(ApplyInst *Apply,
SILValue Address,
Operand *&Oper) {
static SILArgumentConvention getAddressArgConvention(ApplyInst *Apply,
SILValue Address,
Operand *&Oper) {
Oper = nullptr;
ParameterConvention Conv;
auto Params = Apply->getSubstCalleeType()->getParameters();
SILArgumentConvention Conv;
auto Args = Apply->getArgumentOperands();
for (unsigned ArgIdx = 0, ArgE = Params.size(); ArgIdx != ArgE; ++ArgIdx) {
for (auto ArgIdx : indices(Args)) {
if (Args[ArgIdx].get() != Address)
continue;
Conv = Params[ArgIdx].getConvention();
assert(isIndirectParameter(Conv) && "Address not passed as an indirection");
Conv = Apply->getArgumentConvention(ArgIdx);
assert(!Oper && "Address can only be passed once as an indirection.");
Oper = &Args[ArgIdx];
#ifndef NDEBUG
@@ -208,14 +205,12 @@ public:
bool visitApplyInst(ApplyInst *Apply) {
switch (getAddressArgConvention(Apply, Address, Oper)) {
case ParameterConvention::Indirect_In:
case SILArgumentConvention::Indirect_In:
return true;
case ParameterConvention::Indirect_In_Guaranteed:
case ParameterConvention::Indirect_Inout:
case ParameterConvention::Indirect_InoutAliasable:
case SILArgumentConvention::Indirect_In_Guaranteed:
case SILArgumentConvention::Indirect_Inout:
case SILArgumentConvention::Indirect_InoutAliasable:
return false;
case ParameterConvention::Indirect_Out:
llvm_unreachable("copy_addr not released before reinitialization");
default:
llvm_unreachable("unexpected calling convention for copy_addr user");
}
@@ -305,13 +300,13 @@ public:
bool visitApplyInst(ApplyInst *Apply) {
switch (getAddressArgConvention(Apply, Address, Oper)) {
case ParameterConvention::Indirect_Out:
case SILArgumentConvention::Indirect_Out:
return true;
case ParameterConvention::Indirect_Inout:
case ParameterConvention::Indirect_InoutAliasable:
case ParameterConvention::Indirect_In_Guaranteed:
case SILArgumentConvention::Indirect_Inout:
case SILArgumentConvention::Indirect_InoutAliasable:
case SILArgumentConvention::Indirect_In_Guaranteed:
return false;
case ParameterConvention::Indirect_In:
case SILArgumentConvention::Indirect_In:
llvm_unreachable("copy_addr src destroyed without reinitialization");
default:
llvm_unreachable("unexpected calling convention for copy_addr user");
@@ -464,10 +459,10 @@ bool CopyForwarding::collectUsers() {
/// object. However, we can rely on a subsequent mark_dependent
/// instruction to take that object as an operand, causing it to escape
/// for the purpose of this analysis.
auto Params = Apply->getSubstCalleeType()->getParameters();
(void)Params;
assert(Params[UI->getOperandNumber() - Apply->getArgumentOperandNumber()]
.isIndirect() && "copy_addr location should be passed indirect");
assert(isIndirectConvention(
Apply->getSubstCalleeType()->getSILArgumentConvention(
UI->getOperandNumber() - Apply->getArgumentOperandNumber()))
&& "copy_addr location should be passed indirect");
SrcUserInsts.insert(Apply);
continue;
}
@@ -1109,11 +1104,7 @@ static bool canNRVO(CopyAddrInst *CopyInst) {
if (!OutArg)
return false;
if (!OutArg->isFunctionArg())
return false;
auto ArgConv = OutArg->getParameterInfo().getConvention();
if (ArgConv != ParameterConvention::Indirect_Out)
if (!OutArg->isFunctionArg() || !OutArg->isIndirectResult())
return false;
SILBasicBlock *BB = CopyInst->getParent();