From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x112a.google.com (mail-yw1-x112a.google.com [IPv6:2607:f8b0:4864:20::112a]) by sourceware.org (Postfix) with ESMTPS id BBC423857BA9 for ; Mon, 20 Jun 2022 19:13:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BBC423857BA9 Received: by mail-yw1-x112a.google.com with SMTP id 00721157ae682-31772f8495fso108858517b3.4 for ; Mon, 20 Jun 2022 12:13:04 -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=p5BZI8pl2taeu8EcVZT7K9+7FfnP2dZ/twnIm8tixtI=; b=uf4qVErJtp5BkNVPetNb1lrA6RDov0KEu2rk/I/REeDtJJE1K8xVu9yh9k+Rfb/7mc vvq8kC8Y2Ny0UGrEAeTJvtfz6D8ILsTMjsFMZGl5SodI4CKR2bvcMheY7P6hnWxOr0lC LnY13G+6PTtE7c9B1aEUoUWA2aAFvyt8LZrFl4pVh5d1aS2OC/Q3i+o3EfPrczACH/6N nbUKTwRFmOkYiyrcT0ujA2q7qsG7QLQPPTdpZR8KBP+iPr0wCYIQWRt7kh2fCNWBOrd0 /YTObGf6Fk1HSIBEObtXMDvEYgMfn4JNLEK80ttpzb5zrE9K89FY9hfgnzpsNkDpdtOf 05Ng== X-Gm-Message-State: AJIora/iiNh9zNdvh/0VJ2AQ7HyVjEFqlkKkeEQ8Cxa8OhHLPLizIcF1 9A1Nnil4/Do44PwAC0l95g6nEh9XygNxJ5vp7NQ= X-Google-Smtp-Source: AGRyM1tkmy1ucPfGG2LX1Gn54fdXFsL/LetUqQOGsrErSnj0rjqcaLNZrBI8QjGB0xrHKi2gRy6nlJumQ6li1tQoyDA= X-Received: by 2002:a81:3602:0:b0:30d:6d92:19be with SMTP id d2-20020a813602000000b0030d6d9219bemr28328934ywa.422.1655752384062; Mon, 20 Jun 2022 12:13:04 -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 12:12:53 -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=-2.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, 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 19:13:06 -0000 On Mon, Jun 20, 2022 at 12:04 PM Jakub Jelinek wrote: > > On Mon, Jun 20, 2022 at 11:48:24AM -0700, Noah Goldstein wrote: > > > 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); > > What do you mean by differentiate more? More comments? Or > > seperate the logic more? > > Different code, don't add the 1 to the strlen value whenever you know > that chr can't be possibly 0 (either it is a non-zero constant, > or the compiler can prove it won't be zero at runtime otherwise). > Because if c is not 0, then memchr (x, c, strlen (x)) == memchr (x, c, strlen (x) + 1), > either c is among the first strlen (x) chars, or it will return NULL > because x[strlen (x)] == 0. > > It actually is slightly more complicated, strchr second argument is int, > but we just care about the low 8 bits. > For TREE_CODE (chr) == INTEGER_CST, it is still trivial, > say integer_nonzerop (fold_convert (char_type_node, chr)) > or equivalent using wide-int.h APIs. > For SSA_NAMEs, we'd need get_zero_bits API, but we only have > get_nonzero_bits, but we could say at least handle the case where > get_ssa_name_range_info gives a VR_RANGE or set of them where none of > the ranges include integral multiplies of 256. > But for start perhaps just handling INTEGER_CST chr would be good enough. Got it. Will have that in V2. We could also make the initial: bool is_strchr_zerop = integer_zerop (chr); Only check the lower 8 bits. > > Jakub >