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 [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id 6CA8D3851C2A for ; Sat, 5 Dec 2020 09:14:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6CA8D3851C2A 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-136-oVZ2AhLoN7OddEhiRUo8Vg-1; Sat, 05 Dec 2020 04:14:54 -0500 X-MC-Unique: oVZ2AhLoN7OddEhiRUo8Vg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 85427809DC3; Sat, 5 Dec 2020 09:14:53 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-112-11.ams2.redhat.com [10.36.112.11]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2291060C13; Sat, 5 Dec 2020 09:14:52 +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 0B59Eotd1214175 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 5 Dec 2020 10:14:50 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 0B59Eogs1214174; Sat, 5 Dec 2020 10:14:50 +0100 Date: Sat, 5 Dec 2020 10:14:49 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] phiopt: Handle bool in two_value_replacement [PR796232] Message-ID: <20201205091449.GY3788@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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=-5.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, 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: Sat, 05 Dec 2020 09:14:57 -0000 Hi! The following patch improves code generation on the included testcase by enabling two_value_replacement on booleans. It does that only for arg0/arg1 values that conditional_replacement doesn't handle, and only does it if not in the early phiopt pass, because conditional_replacement isn't done early either. I must say I'm not sure about that, in PR87105 / PR87608 you've added the early phiopt pass and specifically excluded conditional_replacement and a few others from being optimized early, but then 3 months later I've added two_value_replacement and didn't restrict it to !early_p. Shall we instead do two_value_replacement only in late phiopt? Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2020-12-05 Jakub Jelinek PR tree-optimization/96232 * tree-ssa-phiopt.c (two_value_replacement): Add early_p argument. If false, optimize even boolean lhs cases as long as arg0 has wider precision and conditional_replacement doesn't handle that case. * gcc.dg/tree-ssa/pr96232-2.c: New test. --- gcc/tree-ssa-phiopt.c.jj 2020-12-04 17:27:53.472837921 +0100 +++ gcc/tree-ssa-phiopt.c 2020-12-04 17:54:56.070689584 +0100 @@ -51,7 +51,7 @@ along with GCC; see the file COPYING3. static unsigned int tree_ssa_phiopt_worker (bool, bool, bool); static bool two_value_replacement (basic_block, basic_block, edge, gphi *, - tree, tree); + tree, tree, bool); static bool conditional_replacement (basic_block, basic_block, edge, edge, gphi *, tree, tree); static gphi *factor_out_conditional_conversion (edge, edge, gphi *, tree, tree, @@ -337,7 +337,7 @@ tree_ssa_phiopt_worker (bool do_store_el } /* Do the replacement of conditional if it can be done. */ - if (two_value_replacement (bb, bb1, e2, phi, arg0, arg1)) + if (two_value_replacement (bb, bb1, e2, phi, arg0, arg1, early_p)) cfgchanged = true; else if (!early_p && conditional_replacement (bb, bb1, e1, e2, phi, @@ -614,7 +614,7 @@ factor_out_conditional_conversion (edge static bool two_value_replacement (basic_block cond_bb, basic_block middle_bb, - edge e1, gphi *phi, tree arg0, tree arg1) + edge e1, gphi *phi, tree arg0, tree arg1, bool early_p) { /* Only look for adjacent integer constants. */ if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)) @@ -635,7 +635,7 @@ two_value_replacement (basic_block cond_ if (TREE_CODE (lhs) != SSA_NAME || !INTEGRAL_TYPE_P (TREE_TYPE (lhs)) - || TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE + || (early_p && TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE) || TREE_CODE (rhs) != INTEGER_CST) return false; @@ -648,9 +648,25 @@ two_value_replacement (basic_block cond_ return false; } + /* Defer boolean x ? 0 : {1,-1} or x ? {1,-1} : 0 to + conditional_replacement. */ + if (TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE + && (integer_zerop (arg0) + || integer_zerop (arg1) + || TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE + || (TYPE_PRECISION (TREE_TYPE (arg0)) + <= TYPE_PRECISION (TREE_TYPE (lhs))))) + return false; + wide_int min, max; - if (get_range_info (lhs, &min, &max) != VR_RANGE - || min + 1 != max + if (TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE) + { + min = wi::to_wide (boolean_false_node); + max = wi::to_wide (boolean_true_node); + } + else if (get_range_info (lhs, &min, &max) != VR_RANGE) + return false; + if (min + 1 != max || (wi::to_wide (rhs) != min && wi::to_wide (rhs) != max)) return false; --- gcc/testsuite/gcc.dg/tree-ssa/pr96232-2.c.jj 2020-12-04 17:56:22.180730234 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr96232-2.c 2020-12-04 17:57:27.973997239 +0100 @@ -0,0 +1,18 @@ +/* PR tree-optimization/96232 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump " 38 - " "optimized" } } */ +/* { dg-final { scan-tree-dump " \\+ 97;" "optimized" } } */ +/* { dg-final { scan-tree-dump-not "PHI <" "optimized" } } */ + +int +foo (_Bool x) +{ + return x ? 37 : 38; +} + +int +bar (_Bool x) +{ + return x ? 98 : 97; +} Jakub