From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x32.google.com (mail-oa1-x32.google.com [IPv6:2001:4860:4864:20::32]) by sourceware.org (Postfix) with ESMTPS id 940DF38582B0 for ; Thu, 7 Jul 2022 16:26:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 940DF38582B0 Received: by mail-oa1-x32.google.com with SMTP id 586e51a60fabf-10bd4812c29so20210487fac.11 for ; Thu, 07 Jul 2022 09:26:34 -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=JCoJnDOWfxGA1+Giavito9ZNRpJDvNP8LKxaRyyoDnA=; b=vOUDSdkn+p6A24cz6kisctq+QkIbLlnD+42K5UpCla7dndJTV6eRgO+VyRO+t7HhO2 LlE/Dkpj/004r26/S3IyraJsrBuDKsCqjz0Q/Ttrzijg79k8Lr/o678aw4D9cvm9GcFT nNh5z61N6IbUQrtTeRcXD5QN9F7gKe0ctly7/Mq0caw1Ms8xUBJ3h0ctQSwG/atfozpw jt883LhJ8Dp/GDCdUG7CnNKIppwLsxprYm/USHUgrqnaXyfTPaQCNww1bGB6LmzR6B3D b6xuMSg8gRkmIE2r9wDX3eAQV78MyMobyTwxGICUq977PHxj6QmFs7p1QtVvkR5IsFu7 XguA== X-Gm-Message-State: AJIora/BVXjqpbL+iqZBlSrk6cvIkP0yYtjj17uxD2hzXxG2XY9CgVXb eWXhvaA2V9E47G+gaHUI5wS9j3FFl5LPDDMrOW4= X-Google-Smtp-Source: AGRyM1uKhvNn5KoKBB+kmQawQU4dOBQALiTtxh/1oraiplA/Tf7nmvGT0b9rqjEKweimbXsCnJq3QT82BjDG3wH8neQ= X-Received: by 2002:a05:6870:ea01:b0:f1:e631:ed6f with SMTP id g1-20020a056870ea0100b000f1e631ed6fmr3285012oap.89.1657211193774; Thu, 07 Jul 2022 09:26:33 -0700 (PDT) MIME-Version: 1.0 References: <20220620163536.2653437-1-goldstein.w.n@gmail.com> <20220620214220.2776648-1-goldstein.w.n@gmail.com> In-Reply-To: From: Noah Goldstein Date: Thu, 7 Jul 2022 09:26:22 -0700 Message-ID: Subject: Re: [PATCH v2] tree-optimization/95821 - Convert strlen + strchr to memchr To: Jakub Jelinek Cc: gcc-patches List , "H.J. Lu" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.7 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: Thu, 07 Jul 2022 16:26:36 -0000 On Tue, Jun 21, 2022 at 11:13 AM Noah Goldstein wrote: > > On Tue, Jun 21, 2022 at 5:01 AM Jakub Jelinek wrote: > > > > On Mon, Jun 20, 2022 at 02:42:20PM -0700, Noah Goldstein 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. > > > > > > bootstrapped and tested on x86_64-linux. > > > > > > PR tree-optimization/95821 > > > > This should be indented by a single tab, not two. > > Fixed in V3 > > > > > > gcc/ > > > > > > * tree-ssa-strlen.cc (strlen_pass::handle_builtin_strchr): Emit > > > memchr instead of strchr if strlen already computed. > > > > > > gcc/testsuite/ > > > > > > * c-c++-common/pr95821-1.c: New test. > > > * c-c++-common/pr95821-2.c: New test. > > > * c-c++-common/pr95821-3.c: New test. > > > * c-c++-common/pr95821-4.c: New test. > > > * c-c++-common/pr95821-5.c: New test. > > > * c-c++-common/pr95821-6.c: New test. > > > * c-c++-common/pr95821-7.c: New test. > > > * c-c++-common/pr95821-8.c: New test. > > > --- a/gcc/tree-ssa-strlen.cc > > > +++ b/gcc/tree-ssa-strlen.cc > > > @@ -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 > > > > Still missing space before ( above. > > Sorry, fixed that in V3. > > > > > + memchr (x, c, strlen + 1). */ > > > > > > void > > > strlen_pass::handle_builtin_strchr () > > > @@ -2418,8 +2421,12 @@ 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); > > > + /* strchr only uses the lower char of input so to check if its > > > + strchr (s, zerop) only take into account the lower char. */ > > > + bool is_strchr_zerop > > > + = (TREE_CODE (chr) == INTEGER_CST > > > + && integer_zerop (fold_convert (char_type_node, chr))); > > > > The indentation rule is that = should be 2 columns to the right from bool, > > so > > > > Fixed in V3. > > bool is_strchr_zerop > > = (TREE_CODE (chr) == INTEGER_CST > > && integer_zerop (fold_convert (char_type_node, chr))); > > > > > + /* If its not strchr (s, zerop) then try and convert to > > > + memchr since strlen has already been computed. */ > > > > This comment still has the second line weirdly indented. > > Sorry, have emacs with 4-space tabs so things that look right arent > as they seem :/ > > Fixed in V3 I believe. > > > > > + tree fn = builtin_decl_explicit (BUILT_IN_MEMCHR); > > > + > > > + /* Only need to check length strlen (s) + 1 if chr may be zero. > > > + Otherwise the last chr (which is known to be zero) can never > > > + be a match. NB: We don't need to test if chr is a non-zero > > > + integer const with zero char bits because that is taken into > > > + account with is_strchr_zerop. */ > > > + if (!tree_expr_nonzero_p (chr)) > > > > The above is unsafe though. tree_expr_nonzero_p (chr) will return true > > if say VRP can prove it is not zero, but because of the implicit > > (char) chr cast done by the function we need something different. > > Say if VRP determines that chr is in [1, INT_MAX] or even just [255, 257] > > it doesn't mean (char) chr won't be 0. > > So, as I've tried to explain in the previous mail, it can be done e.g. with > > Added your code in V3. Thanks for the help. > > bool chr_nonzero = false; > > if (TREE_CODE (chr) == INTEGER_CST > > && integer_nonzerop (fold_convert (char_type_node, chr))) > > chr_nonzero = true; > > else if (TREE_CODE (chr) == SSA_NAME > > && CHAR_TYPE_SIZE < INT_TYPE_SIZE) > > { > > value_range r; > > /* Try to determine using ranges if (char) chr must > > be always 0. That is true e.g. if all the subranges > > have the INT_TYPE_SIZE - CHAR_TYPE_SIZE bits > > the same on lower and upper bounds. */ > > if (get_range_query (cfun)->range_of_expr (r, chr, stmt) > > && r.kind () == VR_RANGE) > > { > > chr_nonzero = true; > > wide_int mask = wi::mask (CHAR_TYPE_SIZE, true, > > INT_TYPE_SIZE); > > for (int i = 0; i < r.num_pairs (); ++i) > > if ((r.lower_bound (i) & mask) > > != (r.upper_bound (i) & mask)) > > { > > chr_nonzero = false; > > break; > > } > > } > > } > > if (!chr_nonzero) > > > > Jakub > > Ping 1