From: Jakub Jelinek <jakub@redhat.com>
To: Noah Goldstein <goldstein.w.n@gmail.com>,
"H.J. Lu" <hjl.tools@gmail.com>
Cc: gcc-patches@gcc.gnu.org, carlos@systemhalted.org
Subject: Re: [PATCH v1] tree-optimization/95821 - Convert strlen + strchr to memchr
Date: Mon, 20 Jun 2022 19:29:36 +0200 [thread overview]
Message-ID: <YrCugNDtftu400fW@tucnak> (raw)
In-Reply-To: <20220620163536.2653437-1-goldstein.w.n@gmail.com>
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
next prev parent reply other threads:[~2022-06-20 17:29 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-20 16:35 Noah Goldstein
2022-06-20 17:29 ` Jakub Jelinek [this message]
2022-06-20 17:54 ` H.J. Lu
2022-06-20 18:48 ` Noah Goldstein
2022-06-20 19:04 ` Jakub Jelinek
2022-06-20 19:12 ` Noah Goldstein
2022-06-20 19:57 ` Jakub Jelinek
2022-06-20 20:59 ` Noah Goldstein
2022-06-20 21:42 ` [PATCH v2] " Noah Goldstein
2022-06-21 12:01 ` Jakub Jelinek
2022-06-21 18:13 ` Noah Goldstein
2022-07-07 16:26 ` Noah Goldstein
2022-06-21 18:12 ` [PATCH v3] " Noah Goldstein
2022-07-09 15:59 ` Jeff Law
2022-09-21 22:02 ` Noah Goldstein
2022-09-22 12:22 ` Jakub Jelinek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YrCugNDtftu400fW@tucnak \
--to=jakub@redhat.com \
--cc=carlos@systemhalted.org \
--cc=gcc-patches@gcc.gnu.org \
--cc=goldstein.w.n@gmail.com \
--cc=hjl.tools@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).