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 322903857BA9 for ; Mon, 20 Jun 2022 19:04:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 322903857BA9 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-43-gheLeK8UNZecnwQf0dpikQ-1; Mon, 20 Jun 2022 15:04:25 -0400 X-MC-Unique: gheLeK8UNZecnwQf0dpikQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 277AF3816851; Mon, 20 Jun 2022 19:04:25 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.14]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D4056141510C; Mon, 20 Jun 2022 19:04:24 +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 25KJ4Gxt3761544 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 20 Jun 2022 21:04:17 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 25KJ4FL73761543; Mon, 20 Jun 2022 21:04:15 +0200 Date: Mon, 20 Jun 2022 21:04:15 +0200 From: Jakub Jelinek To: Noah Goldstein Cc: "H.J. Lu" , gcc-patches List , "Carlos O'Donell" 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: X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 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.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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 19:04:28 -0000 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. Jakub