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 C6DEE3857C4C for ; Thu, 22 Sep 2022 12:22:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C6DEE3857C4C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663849344; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=8N6LY3vlBFMa4vYAIVuxuMSk2NB+m4y/iGRXvOmNU8Q=; b=XM27BpS3AdRMy+JdaKv5J8uWHQdf//nVbLUd0bH4yQwoLmK/ORb9RjJy8/1aMq3GvmBjOu JzMWCxyiR1zLvqA7QWT2+kxn0YwIeZoa1ERiqKypDj3lRWGPR/hYu6nkUG1Z8aGTeBeFMh Dr4GVRyKb40X2t3I1KjzThtwgG7vAqY= 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-323-A3F5BbH9Nserg_X5J62Tkg-1; Thu, 22 Sep 2022 08:22:21 -0400 X-MC-Unique: A3F5BbH9Nserg_X5J62Tkg-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 D7CB73C1068B; Thu, 22 Sep 2022 12:22:20 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.194]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 96783140EBF4; Thu, 22 Sep 2022 12:22:20 +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 28MCMItL2547175 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 22 Sep 2022 14:22:18 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 28MCMHjF2547174; Thu, 22 Sep 2022 14:22:17 +0200 Date: Thu, 22 Sep 2022 14:22:17 +0200 From: Jakub Jelinek To: Noah Goldstein Cc: gcc-patches@gcc.gnu.org, hjl.tools@gmail.com Subject: Re: [PATCH v3] tree-optimization/95821 - Convert strlen + strchr to memchr Message-ID: Reply-To: Jakub Jelinek References: <20220620163536.2653437-1-goldstein.w.n@gmail.com> <20220621181215.2942551-1-goldstein.w.n@gmail.com> MIME-Version: 1.0 In-Reply-To: <20220621181215.2942551-1-goldstein.w.n@gmail.com> X-Scanned-By: MIMEDefang 3.1 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.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,KAM_SHORT,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Tue, Jun 21, 2022 at 11:12:15AM -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 > > 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. Sorry for the delay. > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/pr95821-1.c > @@ -0,0 +1,15 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2" } */ > +/* { dg-final { scan-assembler "memchr" } } */ Please don't scan assembler, whether memchr will expand to a call or be expanded inline etc. is not known. Better use "-O2 -fdump-tree-optimize" in dg-options and scan the optimized dump for "memchr \\\(". Ditto for other tests. > @@ -2452,32 +2459,96 @@ 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) > + /* Three potential optimizations assume t=strlen (s) has already been > + computed: > + 1. strchr (s, chr) where chr is known to be zero -> t -> s + t rather than -> t actually. > + 2. strchr (s, chr) where chr is known not to be zero -> > + memchr (s, chr, t) > + 3. strchr (s, chr) where chr is not known to be zero or nor instead of or? > + non-zero -> memchr (s, chr, t + 1). */ > + 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 since strlen has already been computed. */ > + 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. */ > + 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 must be always non-zero ? > + have the INT_TYPE_SIZE - CHAR_TYPE_SIZE bits > + the same on lower and upper bounds. */ That is actually not enough, see below. > + if (get_range_query (cfun)->range_of_expr (r, chr, stmt) > + && r.kind () == VR_RANGE) > + { > + wide_int mask > + = wi::mask (CHAR_TYPE_SIZE, true, INT_TYPE_SIZE); Wrong indentation, = should be 2 columns left of wide_int. > + for (unsigned i = 0; i < r.num_pairs (); ++i) > + if ((r.lower_bound (i) & mask) > + != (r.upper_bound (i) & mask)) > + { > + chr_nonzero = false; > + break; > + } This else if actually can't do what it indends to, because chr_nonzero is initialized to false at the start and in the loop you also just set it to false, so it is always false. You need to add chr_nonzero = true; before the for loop above. With that, all the above test proves is that there is no range like [15, 257] where it would include 256 in the middle of the range or at the end. But the above doesn't clear chr_nonzero on ranges like [0, 32] or [256, 511] where (char) chr can still be zero. So, the test should be: if ((r.lower_bound (i) & mask) != (r.upper_bound (i) & mask) || (r.lower_bound (i) & ~mask) == 0) or so, that will rule out also the above ranges and if one just has ranges like: [1, 32] U [48, 56] U [257, 511] all is fine, (char) chr is non-zero. But this also shows that the testsuite coverage is insufficient because nothing caught this. I don't see almost any tests where the second argument to strchr would be constant (ideally check for all of 0, ~0 & ~(unsigned char) ~0, ' ', (~0 & ~(unsigned char) ~0) + ' ') - I see you have one test with if (c != 0x100) return else strchr which effectively is strchr (, 0x100) and one if (c != 0) return else strchr which has c range of ~[0, 0] with which you can't do much (just can verify that we don't treat that as (char) c can't be zero). Beyond the tests with constant strchr arguments (and I think you want to check in each case if there is "= slen\[a-zA-Z.0-9_]* \\\+ 1;" or not (and how many times if you e.g. stick more tests into one source file, ideally all where you want the + 1 and in another one all that should not have it)) it would be nice to have at least some tests where you test the above problematic cases, say something like: if (c < 256) { if (c < 1 || c > 64) return ...; } else { if (c < 257 || c > 511) return ...; } ... strchr (..., c); c above should be (needs to be verified in the debugger) [1, 64] U [257, 511] and so chr_nonzero. Similarly construct cases like [1, 32] U [48, 56] U [257, 511] (chr_nonzero) or [0, 32] U [256, 511] (unknown whether c is zero or non-zero) or [15, 257] (unknown too). Jakub