From patchwork Wed May 8 23:07:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 1097280 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-500341-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=golang.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="sEsJ0QYa"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=golang-org.20150623.gappssmtp.com header.i=@golang-org.20150623.gappssmtp.com header.b="OhicPuat"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44zsb85xsxz9s5c for ; Thu, 9 May 2019 09:08:14 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=yWIwf9HLiEmbUCeqtWSoSe0Q1eYkX8E4CvCXQ4J39TUlUO 7gfmjkW7fCbp/mUJIwXyu4GzZoA74pbYsmNDQ3Ob3B86gjuPqKzt5nLPm61NR0pT EXQHL+Cd+9GboMWqtq2rkb2vYwKza0Dt4atleYWyfdv+nyXj+cZ55HBjM1zUI= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=pR8OHcOTr31MFtm9YQy73ahurII=; b=sEsJ0QYaneh51EzT0Rav 7axure05dp5y5Abz1tMHLH/7Id3IuwP+12B2IfyM2xRH7Oce3a//05uC82wS3QAa UFNoOGX0jBSBGLCi+nbqLbnoMK/RgdutTQ9arCBqL1VlaaNjfexNly/WT2i+HCnv KgkCg7pGPJAPGCKSz2Xd/I0= Received: (qmail 84548 invoked by alias); 8 May 2019 23:08:07 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 84487 invoked by uid 89); 8 May 2019 23:08:07 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-10.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mail-lf1-f52.google.com Received: from mail-lf1-f52.google.com (HELO mail-lf1-f52.google.com) (209.85.167.52) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 08 May 2019 23:08:04 +0000 Received: by mail-lf1-f52.google.com with SMTP id u27so118566lfg.10 for ; Wed, 08 May 2019 16:08:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=golang-org.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=fJcn6dBhETZM2hJaVPCbRNjl/no2OdE2K5Lr1nYwepE=; b=OhicPuatv3S2JemOxs1Q/OwVAD7DGjjTN/vMMjJFgk3gOssTEja2NR0rMbBYLl9Z3T vl1NV5Wzt9bpCieSS5MFhcH3OMri3qKPdpK0C3Wob07Af5MET5fiDcA3T6IQ0jabCj9k 7OC9ji0r8Ah/AZkfWX6Q35mBmT1wd1GPkBqFXouzN41Z3cuCDEfcyVq2rsdg6mj+DAH8 UytM+LOK8V/yc/02fLsqN32YpAWJlYRLo7wj4AYxup3QeQ3BcI7x4rAEfwbSHWAUzvs3 7MvMylHDOHEcn3cXr7w00tbu7448RKHYjNQY1y8DnBv6LXvBcRXi+OyYzzsX1Cysrl3V yDJg== MIME-Version: 1.0 From: Ian Lance Taylor Date: Wed, 8 May 2019 16:07:44 -0700 Message-ID: Subject: Go patch committed: Avoid copy for string([]byte) in string comparison To: gcc-patches , gofrontend-dev This Go frontend patch by Cherry Zhang avoids a copy for a string([]byte) conversion used in a string comparison. If a string([]byte) conversion is used immediately in a string comparison, we don't need to copy the backing store of the byte slice, as the string comparison doesn't hold any reference to it. Instead, just create a string header from the byte slice and pass it for comparison. A new type of expression, String_value_expression, is introduced, for constructing string headers. Bootstrapped and ran Go testsuite on x86_64-pc-linux-gnu. Committed to mainline. Ian 2019-05-08 Cherry Zhang * go.dg/cmpstring.go: New test. Index: gcc/go/gofrontend/MERGE =================================================================== --- gcc/go/gofrontend/MERGE (revision 271019) +++ gcc/go/gofrontend/MERGE (working copy) @@ -1,4 +1,4 @@ -f813c670deb8e0454c3f64de74bedb5dcedd10b4 +9c8581187b1c1a30036263728370f31cb846a274 The first line of this file holds the git revision number of the last merge done from the gofrontend repository. Index: gcc/go/gofrontend/expressions.cc =================================================================== --- gcc/go/gofrontend/expressions.cc (revision 271017) +++ gcc/go/gofrontend/expressions.cc (working copy) @@ -2031,6 +2031,90 @@ Expression::make_string_info(Expression* return new String_info_expression(string, string_info, location); } +// An expression that represents an string value: a struct with value pointer +// and length fields. + +class String_value_expression : public Expression +{ + public: + String_value_expression(Expression* valptr, Expression* len, Location location) + : Expression(EXPRESSION_STRING_VALUE, location), + valptr_(valptr), len_(len) + { } + + protected: + int + do_traverse(Traverse*); + + Type* + do_type() + { return Type::make_string_type(); } + + void + do_determine_type(const Type_context*) + { go_unreachable(); } + + Expression* + do_copy() + { + return new String_value_expression(this->valptr_->copy(), + this->len_->copy(), + this->location()); + } + + Bexpression* + do_get_backend(Translate_context* context); + + void + do_dump_expression(Ast_dump_context*) const; + + private: + // The value pointer. + Expression* valptr_; + // The length. + Expression* len_; +}; + +int +String_value_expression::do_traverse(Traverse* traverse) +{ + if (Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT + || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT) + return TRAVERSE_EXIT; + return TRAVERSE_CONTINUE; +} + +Bexpression* +String_value_expression::do_get_backend(Translate_context* context) +{ + std::vector vals(2); + vals[0] = this->valptr_->get_backend(context); + vals[1] = this->len_->get_backend(context); + + Gogo* gogo = context->gogo(); + Btype* btype = Type::make_string_type()->get_backend(gogo); + return gogo->backend()->constructor_expression(btype, vals, this->location()); +} + +void +String_value_expression::do_dump_expression( + Ast_dump_context* ast_dump_context) const +{ + ast_dump_context->ostream() << "stringvalue("; + ast_dump_context->ostream() << "value: "; + this->valptr_->dump_expression(ast_dump_context); + ast_dump_context->ostream() << ", length: "; + this->len_->dump_expression(ast_dump_context); + ast_dump_context->ostream() << ")"; +} + +Expression* +Expression::make_string_value(Expression* valptr, Expression* len, + Location location) +{ + return new String_value_expression(valptr, len, location); +} + // Make an integer expression. class Integer_expression : public Expression @@ -3702,9 +3786,11 @@ Type_conversion_expression::do_check_typ Expression* Type_conversion_expression::do_copy() { - return new Type_conversion_expression(this->type_->copy_expressions(), - this->expr_->copy(), - this->location()); + Expression* ret = new Type_conversion_expression(this->type_->copy_expressions(), + this->expr_->copy(), + this->location()); + ret->conversion_expression()->set_no_copy(this->no_copy_); + return ret; } // Get the backend representation for a type conversion. @@ -3764,7 +3850,22 @@ Type_conversion_expression::do_get_backe Runtime::Function code; if (e->integer_type()->is_byte()) - code = Runtime::SLICEBYTETOSTRING; + { + if (this->no_copy_) + { + if (gogo->debug_optimization()) + go_inform(loc, "no copy string([]byte)"); + Expression* ptr = Expression::make_slice_info(this->expr_, + SLICE_INFO_VALUE_POINTER, + loc); + Expression* len = Expression::make_slice_info(this->expr_, + SLICE_INFO_LENGTH, + loc); + Expression* str = Expression::make_string_value(ptr, len, loc); + return str->get_backend(context); + } + code = Runtime::SLICEBYTETOSTRING; + } else { go_assert(e->integer_type()->is_rune()); @@ -6805,6 +6906,15 @@ Expression::comparison(Translate_context if (left_type->is_string_type() && right_type->is_string_type()) { + // Mark string([]byte) operands to reuse the backing store. + // String comparison does not keep the reference, so it is safe. + Type_conversion_expression* lce = left->conversion_expression(); + if (lce != NULL && lce->expr()->type()->is_slice_type()) + lce->set_no_copy(true); + Type_conversion_expression* rce = right->conversion_expression(); + if (rce != NULL && rce->expr()->type()->is_slice_type()) + rce->set_no_copy(true); + if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) { left = Runtime::make_call(Runtime::EQSTRING, location, 2, Index: gcc/go/gofrontend/expressions.h =================================================================== --- gcc/go/gofrontend/expressions.h (revision 271014) +++ gcc/go/gofrontend/expressions.h (working copy) @@ -102,6 +102,7 @@ class Expression EXPRESSION_BOOLEAN, EXPRESSION_STRING, EXPRESSION_STRING_INFO, + EXPRESSION_STRING_VALUE, EXPRESSION_INTEGER, EXPRESSION_FLOAT, EXPRESSION_COMPLEX, @@ -248,6 +249,10 @@ class Expression static Expression* make_string_info(Expression* string, String_info, Location); + // Make an expression for a string value. + static Expression* + make_string_value(Expression* valptr, Expression* len, Location); + // Make a character constant expression. TYPE should be NULL for an // abstract type. static Expression* @@ -1668,7 +1673,8 @@ class Type_conversion_expression : publi Type_conversion_expression(Type* type, Expression* expr, Location location) : Expression(EXPRESSION_CONVERSION, location), - type_(type), expr_(expr), may_convert_function_types_(false) + type_(type), expr_(expr), may_convert_function_types_(false), + no_copy_(false) { } // Return the type to which we are converting. @@ -1689,6 +1695,12 @@ class Type_conversion_expression : publi this->may_convert_function_types_ = true; } + // Mark string([]byte) conversion to reuse the backing store + // without copying. + void + set_no_copy(bool b) + { this->no_copy_ = b; }; + // Import a type conversion expression. static Expression* do_import(Import_expression*, Location); @@ -1751,6 +1763,9 @@ class Type_conversion_expression : publi // True if this is permitted to convert function types. This is // used internally for method expressions. bool may_convert_function_types_; + // True if a string([]byte) conversion can reuse the backing store + // without copying. Only used in string([]byte) conversion. + bool no_copy_; }; // An unsafe type conversion, used to pass values to builtin functions. Index: gcc/testsuite/go.dg/cmpstring.go =================================================================== --- gcc/testsuite/go.dg/cmpstring.go (nonexistent) +++ gcc/testsuite/go.dg/cmpstring.go (working copy) @@ -0,0 +1,13 @@ +// { dg-do compile } +// { dg-options "-fgo-debug-optimization" } + +package p + +func F(x []byte, y string) bool { + return string(x) == y // { dg-error "no copy string\\(\\\[\\\]byte\\)" } +} + +func BytesEqual(x, y []byte) bool { + return string(x) == // { dg-error "no copy string\\(\\\[\\\]byte\\)" } + string(y) // { dg-error "no copy string\\(\\\[\\\]byte\\)" } +}