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.133.124]) by sourceware.org (Postfix) with ESMTP id 3BEB43857033 for ; Fri, 14 May 2021 17:26:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3BEB43857033 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-188-Lj0yKMATOr6_ak7YfE_CbA-1; Fri, 14 May 2021 13:26:42 -0400 X-MC-Unique: Lj0yKMATOr6_ak7YfE_CbA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4DE47801B14; Fri, 14 May 2021 17:26:41 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-114-59.ams2.redhat.com [10.36.114.59]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F0E125D6D1; Fri, 14 May 2021 17:26:40 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 14EHQdIj656251 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 14 May 2021 19:26:39 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 14EHQdd5656250; Fri, 14 May 2021 19:26:39 +0200 Date: Fri, 14 May 2021 19:26:39 +0200 From: Jakub Jelinek To: gcc-patches@gcc.gnu.org Cc: Richard Biener , Jonathan Wakely Subject: Re: [PATCH] phiopt: Optimize (x <=> y) cmp z [PR94589] Message-ID: <20210514172639.GW1179226@tucnak> Reply-To: Jakub Jelinek References: <20210504074413.GI1179226@tucnak> <20210505165227.GT1179226@tucnak> <20210506102230.GY1179226@tucnak> <4139aa5f-e3c3-c1ce-7fc3-a1e41a4d4c20@hippo.saclay.inria.fr> MIME-Version: 1.0 In-Reply-To: <4139aa5f-e3c3-c1ce-7fc3-a1e41a4d4c20@hippo.saclay.inria.fr> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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=-6.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Fri, 14 May 2021 17:26:45 -0000 On Thu, May 06, 2021 at 09:42:41PM +0200, Marc Glisse wrote: > We can probably do it in 2 steps, first something like > > (for cmp (eq ne) > (simplify > (cmp (bit_and:c @0 @1) @0) > (cmp (@0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))) > > to get rid of the double use, and then simplify X&C==0 to X<=~C if C is a > mask 111...000 (I thought we already had a function to detect such masks, or > the 000...111, but I can't find them anymore). > > I agree that the comparison seems preferable, although if X is signed, the > way GIMPLE represents types will add an inconvenient cast. And I think VRP > already manages to use the bit test to derive a range. I've tried the second step, but it unfortunately regresses +FAIL: gcc.dg/ipa/propbits-2.c scan-tree-dump-not optimized "fail_test" +FAIL: gcc.dg/tree-ssa/loop-42.c scan-tree-dump-not ivcanon "under assumptions " so maybe it is better to keep these cases as the users wrote them. So posting this patch just for archival purposes. 2021-05-13 Jakub Jelinek PR tree-optimization/94589 * match.pd ((X & (-1 << N)) == 0 -> X <= (1 << N) - 1U): New simplification. * gcc.dg/tree-ssa/pr94589-2.c: New test. --- gcc/match.pd.jj 2021-05-12 09:45:55.832976540 +0200 +++ gcc/match.pd 2021-05-13 19:51:17.458507854 +0200 @@ -4792,6 +4792,22 @@ (define_operator_list COND_TERNARY (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2)) (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); }))) +#if GIMPLE +/* (X & (-1 << N)) == 0 becomes X <= (1 << N) - 1. */ +(for cmp (eq ne) + ncmp (le gt) + (simplify + (cmp:c (bit_and:cs @0 INTEGER_CST@1) integer_zerop) + (with { tree utype = NULL_TREE; + int tz = wi::ctz (wi::to_wide (@1)); + int prec = TYPE_PRECISION (TREE_TYPE (@1)); + if (tz && wi::eq_p (wi::shifted_mask (tz, prec - tz, false, prec), + wi::to_wide (@1))) + utype = unsigned_type_for (TREE_TYPE (@0)); } + (if (utype) + (ncmp (convert:utype @0) (convert:utype (bit_not @1))))))) +#endif + /* (X < 0) != (Y < 0) into (X ^ Y) < 0. (X >= 0) != (Y >= 0) into (X ^ Y) < 0. (X < 0) == (Y < 0) into (X ^ Y) >= 0. --- gcc/testsuite/gcc.dg/tree-ssa/pr94589-2.c.jj 2021-05-13 19:55:21.201854003 +0200 +++ gcc/testsuite/gcc.dg/tree-ssa/pr94589-2.c 2021-05-13 19:54:59.804086977 +0200 @@ -0,0 +1,21 @@ +/* PR tree-optimization/94589 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int +foo (int x) +{ + return (x & (-1 << 7)) == 0; +/* { dg-final { scan-tree-dump " = \\\(unsigned int\\\) x_" "optimized" } } */ +/* { dg-final { scan-tree-dump-not " & -128" "optimized" } } */ +/* { dg-final { scan-tree-dump " <= 127" "optimized" } } */ +} + +int +bar (int y) +{ + return (y & (-1 << 12)) != 0; +/* { dg-final { scan-tree-dump " = \\\(unsigned int\\\) y_" "optimized" } } */ +/* { dg-final { scan-tree-dump-not " & -4096" "optimized" } } */ +/* { dg-final { scan-tree-dump " > 4095" "optimized" } } */ +} Jakub