Files
swift-mirror/lib/SILGen/RValue.h
John McCall bf75beeb7a Begin formal accesses on l-value arguments immediately before
the call instead of during the formal evaluation of the argument.

This is the last major chunk of the semantic changes proposed
in the accessors document.  It has two purposes, both related
to the fact that it shortens the duration of the formal access.

First, the change isolates later evaluations (as long as they
precede the call) from the formal access, preventing them from
spuriously seeing unspecified behavior.  For example::

  foo(&array[0], bar(array))

Here the value passed to bar is a proper copy of 'array',
and if bar() decides to stash it aside, any modifications
to 'array[0]' made by foo() will not spontaneously appear
in the copy.  (In contrast, if something caused a copy of
'array' during foo()'s execution, that copy would violate
our formal access rules and would therefore be allowed to
have an arbitrary value at index 0.)

Second, when a mutating access uses a pinning addressor, the
change limits the amount of arbitrary code that falls between
the pin and unpin.  For example::

  array[0] += countNodes(subtree)

Previously, we would begin the access to array[0] before the
call to countNodes().  To eliminate the pin and unpin, the
optimizer would have needed to prove that countNodes didn't
access the same array.  With this change, the call is evaluated
first, and the access instead begins immediately before the call
to +=.  Since that operator is easily inlined, it becomes
straightforward to eliminate the pin/unpin.

A number of other changes got bundled up with this in ways that
are hard to tease apart.  In particular:

  - RValueSource is now ArgumentSource and can now store LValues.

  - It is now illegal to use emitRValue to emit an l-value.

  - Call argument emission is now smart enough to emit tuple
    shuffles itself, applying abstraction patterns in reverse
    through the shuffle.  It also evaluates varargs elements
    directly into the array.

  - AllowPlusZero has been split in two.  AllowImmediatePlusZero
    is useful when you are going to immediately consume the value;
    this is good enough to avoid copies/retains when reading a 'var'.
    AllowGuaranteedPlusZero is useful when you need a stronger
    guarantee, e.g. when arbitrary code might intervene between
    evaluation and use; it's still good enough to avoid copies
    from a 'let'.  The upshot is that we're now a lot smarter
    about generally avoiding retains on lets, but we've also
    gotten properly paranoid about calling non-mutating methods
    on vars.

    (Note that you can't necessarily avoid a copy when passing
    something in a var to an @in_guaranteed parameter!  You
    first have to prove that nothing can assign to the var during
    the call.  That should be easy as long as the var hasn't
    escaped, but that does need to be proven first, so we can't
    do it in SILGen.)

Swift SVN r24709
2015-01-24 13:05:46 +00:00

215 lines
7.6 KiB
C++

//===--- RValue.h - Exploded RValue Representation --------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 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 storage structure for holding a destructured rvalue with an optional
// cleanup(s).
// Ownership of the rvalue can be "forwarded" to disable the associated
// cleanup(s).
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_LOWERING_RVALUE_H
#define SWIFT_LOWERING_RVALUE_H
#include "ManagedValue.h"
#include "SILGenFunction.h"
#include "llvm/ADT/SmallVector.h"
namespace swift {
namespace Lowering {
class Initialization;
/// An "exploded" SIL rvalue, in which tuple values are recursively
/// destructured. (In SILGen we don't try to explode structs, because doing so
/// would require considering resilience, a job we want to delegate to IRGen).
class RValue {
std::vector<ManagedValue> values;
CanType type;
unsigned elementsToBeAdded;
/// Flag value used to mark an rvalue as invalid, because it was
/// consumed or it was default-initialized.
enum : unsigned { Used = ~0U };
// Don't copy.
RValue(const RValue &) = delete;
RValue &operator=(const RValue &) = delete;
void makeUsed() {
elementsToBeAdded = Used;
values = {};
}
/// Private constructor used by copy().
RValue(const RValue &copied, SILGenFunction &gen, SILLocation l);
public:
/// Creates an invalid RValue object, in a "used" state.
RValue() : elementsToBeAdded(Used) {}
RValue(RValue &&rv)
: values(std::move(rv.values)),
type(rv.type),
elementsToBeAdded(rv.elementsToBeAdded)
{
assert((rv.isComplete() || rv.isUsed())
&& "moving rvalue that wasn't complete?!");
rv.elementsToBeAdded = Used;
}
RValue &operator=(RValue &&rv) {
assert(isUsed() && "reassigning an unused rvalue?!");
assert((rv.isComplete() || rv.isUsed())
&& "moving rvalue that wasn't complete?!");
values = std::move(rv.values);
type = rv.type;
elementsToBeAdded = rv.elementsToBeAdded;
rv.elementsToBeAdded = Used;
return *this;
}
/// Create a RValue from a single value. If the value is of tuple type, it
/// will be exploded.
///
/// \param expr - the expression which yielded this r-value; its type
/// will become the substituted formal type of this r-value
RValue(SILGenFunction &gen, Expr *expr, ManagedValue v);
/// Create a RValue from a single value. If the value is of tuple type, it
/// will be exploded.
RValue(SILGenFunction &gen, SILLocation l, CanType type, ManagedValue v);
/// Construct an RValue from a pre-exploded set of
/// ManagedValues. Used to implement the extractElement* methods.
RValue(ArrayRef<ManagedValue> values, CanType type);
/// Create an RValue to which values will be subsequently added using
/// addElement(). The RValue will not be complete until all the elements have
/// been added.
explicit RValue(CanType type);
/// True if the rvalue has been completely initialized by adding all its
/// elements.
bool isComplete() const & { return elementsToBeAdded == 0; }
/// True if this rvalue has been used.
bool isUsed() const & { return elementsToBeAdded == Used; }
explicit operator bool() const & { return !isUsed(); }
/// True if this represents an lvalue.
bool isLValue() const & {
return isa<InOutType>(type);
}
/// Add an element to the rvalue. The rvalue must not yet be complete.
void addElement(RValue &&element) &;
/// Add a ManagedValue element to the rvalue, exploding tuples if necessary.
/// The rvalue must not yet be complete.
void addElement(SILGenFunction &gen, ManagedValue element,
CanType formalType, SILLocation l) &;
/// Forward an rvalue into a single value, imploding tuples if necessary.
SILValue forwardAsSingleValue(SILGenFunction &gen, SILLocation l) &&;
/// Forward an rvalue into a single value, imploding tuples if necessary, and
/// introducing a potential conversion from semantic type to storage type.
SILValue forwardAsSingleStorageValue(SILGenFunction &gen,
SILType storageType,
SILLocation l) &&;
/// Get the rvalue as a single value, imploding tuples if necessary.
ManagedValue getAsSingleValue(SILGenFunction &gen, SILLocation l) &&;
/// Get the rvalue as a single unmanaged value, imploding tuples if necessary.
/// The values must not require any cleanups.
SILValue getUnmanagedSingleValue(SILGenFunction &gen, SILLocation l) const &;
/// Peek at the single scalar value backing this rvalue without consuming it.
/// The rvalue must not be of a tuple type.
SILValue peekScalarValue() const & {
assert(!isa<TupleType>(type) && "peekScalarValue of a tuple rvalue");
assert(values.size() == 1 && "exploded scalar value?!");
return values[0].getValue();
}
ManagedValue getScalarValue() && {
assert(!isa<TupleType>(type) && "getScalarValue of a tuple rvalue");
assert(values.size() == 1);
auto value = values[0];
makeUsed();
return value;
}
/// Use this rvalue to initialize an Initialization.
void forwardInto(SILGenFunction &gen, Initialization *I, SILLocation Loc) &&;
/// Copy this rvalue to initialize an Initialization without consuming the
/// rvalue.
void copyInto(SILGenFunction &gen, Initialization *I, SILLocation Loc) const&;
/// Forward the exploded SILValues into a SmallVector.
void forwardAll(SILGenFunction &gen,
SmallVectorImpl<SILValue> &values) &&;
ManagedValue materialize(SILGenFunction &gen, SILLocation loc) &&;
/// Take the ManagedValues from this RValue into a SmallVector.
void getAll(SmallVectorImpl<ManagedValue> &values) &&;
/// Store the unmanaged SILValues into a SmallVector. The values must not
/// require any cleanups.
void getAllUnmanaged(SmallVectorImpl<SILValue> &values) const &;
/// Extract a single tuple element from the rvalue.
RValue extractElement(unsigned element) &&;
/// Extract the tuple elements from the rvalue.
void extractElements(SmallVectorImpl<RValue> &elements) &&;
CanType getType() const & { return type; }
/// Rewrite the type of this r-value.
void rewriteType(CanType newType) & {
// We only allow a very modest set of changes to a type.
assert(newType == type ||
(isa<TupleType>(newType) &&
cast<TupleType>(newType)->getNumElements() == 1 &&
cast<TupleType>(newType).getElementType(0) == type));
type = newType;
}
/// Emit an equivalent value with independent ownership.
RValue copy(SILGenFunction &gen, SILLocation l) const & {
return RValue(*this, gen, l);
}
bool isObviouslyEqual(const RValue &rhs) const {
assert(isComplete() && rhs.isComplete() && "Comparing incomplete rvalues");
// Compare the count of elements instead of the type.
if (values.size() != rhs.values.size())
return false;
return std::equal(values.begin(), values.end(), rhs.values.begin(),
[](const ManagedValue &lhs, const ManagedValue &rhs) -> bool {
return lhs.getValue() == rhs.getValue() &&
lhs.getCleanup() == rhs.getCleanup();
});
}
};
} // end namespace Lowering
} // end namespace swift
#endif