From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x1130.google.com (mail-yw1-x1130.google.com [IPv6:2607:f8b0:4864:20::1130]) by sourceware.org (Postfix) with ESMTPS id 82BB13858287 for ; Mon, 20 Jun 2022 20:59:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 82BB13858287 Received: by mail-yw1-x1130.google.com with SMTP id 00721157ae682-2ef5380669cso112028477b3.9 for ; Mon, 20 Jun 2022 13:59:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=8ReIJnji5MUfT3SDaYPFMOfsY4VoiwdTa1k+gEE30wY=; b=4Bwl1hyjfmx925T7E0V4VraUE/n32K49wShR2ZQ+Fd+eo0LlI8zwTZcjC7/8oLn40B 4sH8XEnnHgO+v2XLrNU5P/IipH6hGWS0NqsPpsWQ2CA/xx+T9oW235JOjL6Q/ZrscG/z 0DYoxJGIhTJNFd16Ek1OWIdA9OMDE4/my7TLhbTE1zYcVeEoAWcoPxi5sFyd2a8I6PJ9 YSD14gtXD1ciMHRLtPa0448z6qWSU9dA/sx/+L44guJs8ORAyN5Nl+8uWwxIPd4RPVLs QQBWMBDArrPmh7AS1kHpXSkaND1I1uoiU11T9wE5u+qYFE4L3ikwMIh/Mv0NIv2JHWzK 4LaA== X-Gm-Message-State: AJIora8PLk0ycHAsYRn9kv4sji8384IZgIkTJRbhnsyaUZzQ5rPwRbex /LURlNgWpROz77bbpR93d4bYj36xCTx7lB2igOI= X-Google-Smtp-Source: AGRyM1v+gbbhAOCr1JxOV1KNo/zG1hnWKLu44T3Up677yPPm7Z+L8IHNZIJGmbkbCwpHYUrTWECR0v+6/Q7YHQKnMWI= X-Received: by 2002:a0d:e28f:0:b0:317:89cb:b1ff with SMTP id l137-20020a0de28f000000b0031789cbb1ffmr20329287ywe.288.1655758758819; Mon, 20 Jun 2022 13:59:18 -0700 (PDT) MIME-Version: 1.0 References: <20220620163536.2653437-1-goldstein.w.n@gmail.com> In-Reply-To: From: Noah Goldstein Date: Mon, 20 Jun 2022 13:59:08 -0700 Message-ID: Subject: Re: [PATCH v1] tree-optimization/95821 - Convert strlen + strchr to memchr To: Jakub Jelinek Cc: "H.J. Lu" , gcc-patches List , "Carlos O'Donell" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 20:59:21 -0000 On Mon, Jun 20, 2022 at 10:29 AM Jakub Jelinek wrote: > > 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 ... Fixed in v2. > > > > > 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 Fixed in V2. > > > --- 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. Fixed in V2. > > > > > 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. Fixed in V2. > > > + 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. Done in V2. Also added the optimizations if chr has zero-char bits. Right now: t=strlen (s); strchr (s, 0) -> t; strchr (s, 256) -> t; strchr (s, 1234) -> memchr (s, 1234, t); strchr (s, non_zero) -> memchr (s, non_zero, t); strchr (s, unknown) -> memchr (s, unknown, t + 1); > > > + /* Don't update strlen of lhs if search-char was non-zero. */ > > Wasn't known to be zero is the right thing. Fixed in V2. > > Jakub >