Commit 6303331c by Ian Lance Taylor

compiler: optimize append of make

    
    The gc compiler recognizes append(s, make([]T, n)...), and
    generates code to directly zero the tail instead of allocating a
    new slice and copying. This CL lets the Go frontend do basically
    the same.
    
    The difficulty is that at the point we handle append, there may
    already be temporaries introduced (e.g. in order_evaluations),
    which makes it hard to find the append-of-make pattern. The
    compiler could "see through" the value of a temporary, but it is
    only safe to do if the temporary is not assigned multiple times.
    For this, we add tracking of assignments and uses for temporaries.
    
    This also helps in optimizing non-escape slice make. We already
    optimize non-escape slice make with constant len/cap to stack
    allocation. But it failed to handle things like f(make([]T, n))
    (where the slice doesn't escape and n is constant), because of
    the temporary. With tracking of temporary assignments and uses,
    it can handle this now as well.
    
    Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/179597

From-SVN: r271822
parent 2b5360d7
e521123b23494148d534755e2f3d7806b42c96ad
52176566485e20968394a5cb67a89ac676182594
The first line of this file holds the git revision number of the last
merge done from the gofrontend repository.
......@@ -1068,6 +1068,11 @@ class Expression
static Expression*
unpack_direct_iface(Expression*, Location);
// Look through the expression of a Slice_value_expression's valmem to
// find an call to makeslice.
static std::pair<Call_expression*, Temporary_statement*>
find_makeslice_call(Expression*);
// Dump an expression to a dump constext.
void
dump_expression(Ast_dump_context*) const;
......
......@@ -3627,21 +3627,24 @@ static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
// locations.
static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
// Slice length or capacity out of bounds in make: negative or
// overflow or length greater than capacity.
static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
// Slice length out of bounds in make: negative or overflow
// or length greater than capacity.
static const int RUNTIME_ERROR_MAKE_SLICE_LEN_OUT_OF_BOUNDS = 7;
// Slice capacity out of bounds in make: negative.
static const int RUNTIME_ERROR_MAKE_SLICE_CAP_OUT_OF_BOUNDS = 8;
// Map capacity out of bounds in make: negative or overflow.
static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 9;
// Channel capacity out of bounds in make: negative or overflow.
static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 10;
// Division by zero.
static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 11;
// Go statement with nil function.
static const int RUNTIME_ERROR_GO_NIL = 11;
static const int RUNTIME_ERROR_GO_NIL = 12;
// This is used by some of the langhooks.
extern Gogo* go_get_gogo();
......
......@@ -1043,6 +1043,9 @@ Assignment_statement*
Statement::make_assignment(Expression* lhs, Expression* rhs,
Location location)
{
Temporary_reference_expression* tre = lhs->temporary_reference_expression();
if (tre != NULL)
tre->statement()->set_assigned();
return new Assignment_statement(lhs, rhs, location);
}
......
......@@ -675,7 +675,7 @@ class Temporary_statement : public Statement
Temporary_statement(Type* type, Expression* init, Location location)
: Statement(STATEMENT_TEMPORARY, location),
type_(type), init_(init), bvariable_(NULL), is_address_taken_(false),
value_escapes_(false)
value_escapes_(false), assigned_(false), uses_(0)
{ }
// Return the type of the temporary variable.
......@@ -687,6 +687,17 @@ class Temporary_statement : public Statement
init() const
{ return this->init_; }
// Set the initializer.
void
set_init(Expression* expr)
{ this->init_ = expr; }
// Whether something takes the address of this temporary
// variable.
bool
is_address_taken()
{ return this->is_address_taken_; }
// Record that something takes the address of this temporary
// variable.
void
......@@ -703,6 +714,26 @@ class Temporary_statement : public Statement
set_value_escapes()
{ this->value_escapes_ = true; }
// Whether this temporary variable is assigned (after initialization).
bool
assigned()
{ return this->assigned_; }
// Record that this temporary variable is assigned.
void
set_assigned()
{ this->assigned_ = true; }
// Number of uses of this temporary variable.
int
uses()
{ return this->uses_; }
// Add one use of this temporary variable.
void
add_use()
{ this->uses_++; }
// Return the temporary variable. This should not be called until
// after the statement itself has been converted.
Bvariable*
......@@ -745,6 +776,10 @@ class Temporary_statement : public Statement
// True if the value assigned to this temporary variable escapes.
// This is used for select statements.
bool value_escapes_;
// True if this temporary variable is assigned (after initialization).
bool assigned_;
// Number of uses of this temporary variable.
int uses_;
};
// A variable declaration. This marks the point in the code where a
......
......@@ -41,9 +41,6 @@ class Mark_address_taken : public Traverse
expression(Expression**);
private:
Call_expression*
find_makeslice_call(Expression*);
// General IR.
Gogo* gogo_;
// The function we are traversing.
......@@ -100,31 +97,6 @@ Mark_address_taken::statement(Block* block, size_t* pindex, Statement* s)
return TRAVERSE_CONTINUE;
}
// Look through the expression of a Slice_value_expression's valmem to
// find an call to makeslice.
Call_expression*
Mark_address_taken::find_makeslice_call(Expression* expr)
{
Unsafe_type_conversion_expression* utce =
expr->unsafe_conversion_expression();
if (utce != NULL)
expr = utce->expr();
Call_expression* call = expr->call_expression();
if (call == NULL)
return NULL;
Func_expression* fe = call->fn()->func_expression();
if (fe != NULL && fe->runtime_code() == Runtime::MAKESLICE)
return call;
// We don't worry about MAKESLICE64 bcause we don't want to use a
// stack allocation for a large slice anyhow.
return NULL;
}
// Mark variable addresses taken.
int
......@@ -173,7 +145,10 @@ Mark_address_taken::expression(Expression** pexpr)
Slice_value_expression* sve = expr->slice_value_expression();
if (sve != NULL)
{
Call_expression* call = this->find_makeslice_call(sve->valmem());
std::pair<Call_expression*, Temporary_statement*> p =
Expression::find_makeslice_call(sve);
Call_expression* call = p.first;
Temporary_statement* ts = p.second;
if (call != NULL
&& Node::make_node(call)->encoding() == Node::ESCAPE_NONE)
{
......@@ -203,6 +178,8 @@ Mark_address_taken::expression(Expression** pexpr)
Expression::make_slice_value(expr->type(), ptr, len_arg,
cap_arg, loc);
*pexpr = slice;
if (ts != NULL && ts->uses() == 1)
ts->set_init(Expression::make_nil(loc));
}
}
}
......
......@@ -38,21 +38,24 @@ enum
memory locations. */
NIL_DEREFERENCE = 6,
/* Slice length or capacity out of bounds in make: negative or
overflow or length greater than capacity. */
MAKE_SLICE_OUT_OF_BOUNDS = 7,
/* Slice length out of bounds in make: negative or overflow or length
greater than capacity. */
MAKE_SLICE_LEN_OUT_OF_BOUNDS = 7,
/* Slice capacity out of bounds in make: negative. */
MAKE_SLICE_CAP_OUT_OF_BOUNDS = 8,
/* Map capacity out of bounds in make: negative or overflow. */
MAKE_MAP_OUT_OF_BOUNDS = 8,
MAKE_MAP_OUT_OF_BOUNDS = 9,
/* Channel capacity out of bounds in make: negative or overflow. */
MAKE_CHAN_OUT_OF_BOUNDS = 9,
MAKE_CHAN_OUT_OF_BOUNDS = 10,
/* Integer division by zero. */
DIVISION_BY_ZERO = 10,
DIVISION_BY_ZERO = 11,
/* Go statement with nil function. */
GO_NIL = 11
GO_NIL = 12
};
extern void __go_runtime_error (int32) __attribute__ ((noreturn));
......@@ -88,8 +91,11 @@ __go_runtime_error (int32 i)
case NIL_DEREFERENCE:
runtime_panicstring ("nil pointer dereference");
case MAKE_SLICE_OUT_OF_BOUNDS:
runtime_panicstring ("make slice len or cap out of range");
case MAKE_SLICE_LEN_OUT_OF_BOUNDS:
runtime_panicstring ("make slice len out of range");
case MAKE_SLICE_CAP_OUT_OF_BOUNDS:
runtime_panicstring ("make slice cap out of range");
case MAKE_MAP_OUT_OF_BOUNDS:
runtime_panicstring ("make map len out of range");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment