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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 8A7D13959E51 for ; Tue, 9 Mar 2021 15:40:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8A7D13959E51 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-231-0o6L_c28OECRVf2DR15TNg-1; Tue, 09 Mar 2021 10:40:27 -0500 X-MC-Unique: 0o6L_c28OECRVf2DR15TNg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 52092881D52; Tue, 9 Mar 2021 15:40:26 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-113-238.ams2.redhat.com [10.36.113.238]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D9ED95D9CD; Tue, 9 Mar 2021 15:40:25 +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 129FeNih2902092 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 9 Mar 2021 16:40:23 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 129FeMia2902091; Tue, 9 Mar 2021 16:40:22 +0100 Date: Tue, 9 Mar 2021 16:40:22 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] phiopt: Fix up conditional_replacement [PR99305] Message-ID: <20210309154022.GR745611@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 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_H3, 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: Tue, 09 Mar 2021 15:40:31 -0000 Hi! Before my PR97690 changes, conditional_replacement would not set neg when the nonzero arg was boolean true. I've simplified the testing, so that it first finds the zero argument and then checks the other argument for all the handled cases (1, -1 and 1 << X, where the last case is what the patch added support for). But, unfortunately I've placed the integer_all_onesp test first. For unsigned precision 1 types such as bool integer_all_onesp, integer_onep and integer_pow2p can all be true and the code set neg to true in that case, which is undesirable. The following patch tests integer_pow2p first (which is trivially true for integer_onep too and tree_log2 in that case gives shift == 0) and only if that isn't the case, integer_all_onesp. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-03-09 Jakub Jelinek PR tree-optimization/99305 * tree-ssa-phiopt.c (conditional_replacement): Test integer_pow2p before integer_all_onesp instead of vice versa. * g++.dg/opt/pr99305.C: New test. --- gcc/tree-ssa-phiopt.c.jj 2021-01-22 11:41:38.078708425 +0100 +++ gcc/tree-ssa-phiopt.c 2021-03-09 13:15:02.649094949 +0100 @@ -808,14 +808,14 @@ conditional_replacement (basic_block con nonzero_arg = arg0; else return false; - if (integer_all_onesp (nonzero_arg)) - neg = true; - else if (integer_pow2p (nonzero_arg)) + if (integer_pow2p (nonzero_arg)) { shift = tree_log2 (nonzero_arg); if (shift && POINTER_TYPE_P (TREE_TYPE (nonzero_arg))) return false; } + else if (integer_all_onesp (nonzero_arg)) + neg = true; else return false; --- gcc/testsuite/g++.dg/opt/pr99305.C.jj 2021-03-09 13:39:11.040957563 +0100 +++ gcc/testsuite/g++.dg/opt/pr99305.C 2021-03-09 13:39:44.063589691 +0100 @@ -0,0 +1,26 @@ +// PR tree-optimization/99305 +// { dg-do compile } +// { dg-options "-O3 -fno-ipa-icf -fdump-tree-optimized" } +// { dg-final { scan-tree-dump-times " = \\\(unsigned char\\\) c_\[0-9]*\\\(D\\\);" 3 "optimized" } } +// { dg-final { scan-tree-dump-times " = \[^\n\r]* \\+ \[0-9]*;" 3 "optimized" } } +// { dg-final { scan-tree-dump-times " = \[^\n\r]* <= 9;" 3 "optimized" } } +// { dg-final { scan-tree-dump-not "if \\\(c_\[0-9]*\\\(D\\\) \[!=]= 0\\\)" "optimized" } } +// { dg-final { scan-tree-dump-not " = PHI <" "optimized" } } + +bool +foo (char c) +{ + return c >= 48 && c <= 57; +} + +bool +bar (char c) +{ + return c != 0 && foo (c); +} + +bool +baz (char c) +{ + return c != 0 && c >= 48 && c <= 57; +} Jakub