From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 2BCEF3858C2C for ; Mon, 20 Jun 2022 17:29:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2BCEF3858C2C Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-633-bo0JsLv1Me-dkhSup0jSrA-1; Mon, 20 Jun 2022 13:29:41 -0400 X-MC-Unique: bo0JsLv1Me-dkhSup0jSrA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E974380B70F; Mon, 20 Jun 2022 17:29:40 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.14]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A265EC28115; Mon, 20 Jun 2022 17:29:40 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 25KHTbZ53761346 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 20 Jun 2022 19:29:38 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 25KHTaXP3761345; Mon, 20 Jun 2022 19:29:36 +0200 Date: Mon, 20 Jun 2022 19:29:36 +0200 From: Jakub Jelinek To: Noah Goldstein , "H.J. Lu" Cc: gcc-patches@gcc.gnu.org, carlos@systemhalted.org Subject: Re: [PATCH v1] tree-optimization/95821 - Convert strlen + strchr to memchr Message-ID: Reply-To: Jakub Jelinek References: <20220620163536.2653437-1-goldstein.w.n@gmail.com> MIME-Version: 1.0 In-Reply-To: <20220620163536.2653437-1-goldstein.w.n@gmail.com> X-Scanned-By: MIMEDefang 2.85 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Jun 2022 17:29:46 -0000 On Mon, Jun 20, 2022 at 09:35:36AM -0700, Noah Goldstein via Gcc-patches wrote: > This patch allows for strchr(x, c) to the replace with memchr(x, c, > strlen(x) + 1) if strlen(x) has already been computed earlier in the > tree. > > Handles PR95821: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95821 > > Since memchr doesn't need to re-find the null terminator it is faster > than strchr. Do you have a GCC Copyright assignment on file, or do you want to submit this under DCO ( https://gcc.gnu.org/dco.html )? If the latter, there should be a Signed-off-by: line, both in the mail and later commit. > > bootstrapped and tested on x86_64-linux. > > gcc/ > As it fixes a GCC bugzilla bug, the ChangeLog entry should start with PR tree-optimization/95821 line. > * tree-ssa-strlen.cc: Emit memchr instead of strchr if strlen > already computed. All the indented lines in ChangeLog should be indented by tab. You are modifying strlen_pass::handle_builtin_strchr function, so after tree-ssa-strlen.cc there should be that function name in parens: * tree-ssa-strlen.cc (strlen_pass::handle_builtin_strchr): Emit memchr ... > > gcc/testsuite/ > > * c-c++-common/pr95821-1.c > * c-c++-common/pr95821-2.c > * c-c++-common/pr95821-3.c > * c-c++-common/pr95821-4.c > * c-c++-common/pr95821-5.c > * c-c++-common/pr95821-6.c All the above lines should end with ": New test." after .c > --- a/gcc/tree-ssa-strlen.cc > +++ b/gcc/tree-ssa-strlen.cc How does the patch relate to the one that H.J. attached in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95821#c4 ? > @@ -2405,9 +2405,12 @@ strlen_pass::handle_builtin_strlen () > } > } > > -/* Handle a strchr call. If strlen of the first argument is known, replace > - the strchr (x, 0) call with the endptr or x + strlen, otherwise remember > - that lhs of the call is endptr and strlen of the argument is endptr - x. */ > +/* Handle a strchr call. If strlen of the first argument is known, > + replace the strchr (x, 0) call with the endptr or x + strlen, > + otherwise remember that lhs of the call is endptr and strlen of the > + argument is endptr - x. If strlen of x is not know but has been > + computed earlier in the tree then replace strchr(x, c) to > + memchr(x, c, strlen + 1). */ Space before ( even in comments. > void > strlen_pass::handle_builtin_strchr () > @@ -2418,8 +2421,8 @@ strlen_pass::handle_builtin_strchr () > if (lhs == NULL_TREE) > return; > > - if (!integer_zerop (gimple_call_arg (stmt, 1))) > - return; > + tree chr = gimple_call_arg (stmt, 1); > + bool is_strchr_zerop = integer_zerop (chr); > > tree src = gimple_call_arg (stmt, 0); > > @@ -2452,32 +2455,56 @@ strlen_pass::handle_builtin_strchr () > fprintf (dump_file, "Optimizing: "); > print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); > } > - if (si != NULL && si->endptr != NULL_TREE) > + if (!is_strchr_zerop) > { > - rhs = unshare_expr (si->endptr); > - if (!useless_type_conversion_p (TREE_TYPE (lhs), > - TREE_TYPE (rhs))) > - rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs); > + /* If its not strchr(s, zerop) then try and convert to > + memchr if strlen has already been computed. */ Again, space before (. The second line is weirdly formatted, should be indented below If. > + tree fn = builtin_decl_explicit (BUILT_IN_MEMCHR); > + tree one = build_int_cst (TREE_TYPE (rhs), 1); > + rhs = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (rhs), > + unshare_expr (rhs), one); > + tree size = make_ssa_name (TREE_TYPE (rhs)); > + gassign *size_stmt = gimple_build_assign (size, rhs); > + gsi_insert_before (&m_gsi, size_stmt, GSI_SAME_STMT); > + rhs = size; > + if (!update_gimple_call (&m_gsi, fn, 3, src, chr, rhs)) > + return; I think we should differentiate more. If integer_nonzerop (chr) or perhaps better tree_expr_nonzero_p (chr), then it is better to optimize t = strlen (x); ... p = strchr (x, c); to t = strlen (x); ... p = memchr (x, c, t); the t + 1 is only needed if c might be zero. > + /* Don't update strlen of lhs if search-char was non-zero. */ Wasn't known to be zero is the right thing. Jakub