From b1403b691a24fae2a96fa7ce5b93c3395e7bfd05 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Fri, 15 Mar 2019 04:34:43 +0000 Subject: compiler: eliminate bound checks in append expression The compiler generates two array index expressions when lowering an append expression. Currently they generate bound checks. Bound checks are not necessary in this case, as we know the slice has, or will grow to, enough length and capacity. Eliminate them. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/166817 From-SVN: r269699 --- gcc/go/gofrontend/expressions.h | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'gcc/go/gofrontend/expressions.h') diff --git a/gcc/go/gofrontend/expressions.h b/gcc/go/gofrontend/expressions.h index 5d61b69..c521d9b 100644 --- a/gcc/go/gofrontend/expressions.h +++ b/gcc/go/gofrontend/expressions.h @@ -2854,7 +2854,7 @@ class Array_index_expression : public Expression Expression* end, Expression* cap, Location location) : Expression(EXPRESSION_ARRAY_INDEX, location), array_(array), start_(start), end_(end), cap_(cap), type_(NULL), - is_lvalue_(false) + is_lvalue_(false), needs_bounds_check_(true) { } // Return the array. @@ -2898,6 +2898,10 @@ class Array_index_expression : public Expression set_is_lvalue() { this->is_lvalue_ = true; } + void + set_needs_bounds_check(bool b) + { this->needs_bounds_check_ = b; } + protected: int do_traverse(Traverse*); @@ -2917,15 +2921,17 @@ class Array_index_expression : public Expression Expression* do_copy() { - return Expression::make_array_index(this->array_->copy(), - this->start_->copy(), - (this->end_ == NULL - ? NULL - : this->end_->copy()), - (this->cap_ == NULL - ? NULL - : this->cap_->copy()), - this->location()); + Expression* ret = Expression::make_array_index(this->array_->copy(), + this->start_->copy(), + (this->end_ == NULL + ? NULL + : this->end_->copy()), + (this->cap_ == NULL + ? NULL + : this->cap_->copy()), + this->location()); + ret->array_index_expression()->set_needs_bounds_check(this->needs_bounds_check_); + return ret; } bool @@ -2962,6 +2968,8 @@ class Array_index_expression : public Expression Type* type_; // Whether expr appears in an lvalue context. bool is_lvalue_; + // Whether bounds check is needed. + bool needs_bounds_check_; }; // A string index. This is used for both indexing and slicing. -- cgit v1.1