From patchwork Sat Jan 29 16:23:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1586204 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=Yz4U2zGa; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JmKR556pvz9sRR for ; Sun, 30 Jan 2022 03:24:31 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2976F385840B for ; Sat, 29 Jan 2022 16:24:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2976F385840B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1643473465; bh=JkLf29fcn8KxETzm1AJBLuMs1Ya+X+NUAoo0goiPC9g=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=Yz4U2zGamaRXM8jjItkwvSsLbDSn9V98qGQEzaRboxgARvE/3DqsL2hEoOAkOpoHW 332i+fs7RqrjurPNR/30/R/xRtnIVgA7tHu4XilqILI/USJaDiaAE8jK7qbCjdLkXI zxJDw908VqfYdn0hYOV5xmfn+g+T3IDiGrmYUQ8A= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 98E0A3858402 for ; Sat, 29 Jan 2022 16:23:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 98E0A3858402 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-163-nQHFlNXiPl-P_OVe53M0pQ-1; Sat, 29 Jan 2022 11:23:40 -0500 X-MC-Unique: nQHFlNXiPl-P_OVe53M0pQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 74D6A2F25; Sat, 29 Jan 2022 16:23:39 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.125]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E360110A4B29; Sat, 29 Jan 2022 16:23:38 +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 20TGNZoF3404432 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 29 Jan 2022 17:23:36 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 20TGNZw53404431; Sat, 29 Jan 2022 17:23:35 +0100 Date: Sat, 29 Jan 2022 17:23:34 +0100 To: Richard Biener , Jeff Law Subject: [PATCH] match.pd: Fix up 1 / X for unsigned X optimization [PR104280] Message-ID: <20220129162334.GV2646553@tucnak> References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.2 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_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: Zhao Wei Liew , GCC Patches Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi! On Fri, Jan 28, 2022 at 11:38:23AM -0700, Jeff Law wrote: > Thanks.  Given the original submission and most of the review work was done > prior to stage3 closing, I went ahead and installed this on the trunk. Unfortunately this breaks quite a lot of things. The main problem is that GIMPLE allows EQ_EXPR etc. only with BOOLEAN_TYPE or with TYPE_PRECISION == 1 integral type (or vector boolean). Violating this causes verification failures in tree-cfg.cc in some cases, in other cases wrong-code issues because before it is verified we e.g. transform 1U / x into x == 1U and later into x (because we assume that == type must be one of the above cases and when it is the same type as the type of the first operand, for boolean-ish cases it should be equivalent). Fixed by changing that (eq @1 { build_one_cst (type); }) into (convert (eq:boolean_type_node @1 { build_one_cst (type); })) Note, I'm not 100% sure if :boolean_type_node is required in that case, I see some spots in match.pd that look exactly like this, while there is e.g. (convert (le ...)) that supposedly does the right thing too. The signed integer 1/X case doesn't need changes changes, for (cond (le ...) ...) le gets correctly boolean_type_node and cond should use type. I've also reformatted it, some lines were too long, match.pd uses indentation by 1 column instead of 2 etc. Bootstrapped/regtested on powerpc64le-linux and tested on the testcases on x86_64-linux, ok for trunk? 2022-01-29 Jakub Jelinek Andrew Pinski PR tree-optimization/104279 PR tree-optimization/104280 PR tree-optimization/104281 * match.pd (1 / X -> X == 1 for unsigned X): Build eq with boolean_type_node and convert to type. Formatting fixes. * gcc.dg/torture/pr104279.c: New test. * gcc.dg/torture/pr104280.c: New test. * gcc.dg/torture/pr104281.c: New test. Jakub --- gcc/match.pd.jj 2022-01-29 11:11:39.316628007 +0100 +++ gcc/match.pd 2022-01-29 12:19:46.662096678 +0100 @@ -435,18 +435,22 @@ (define_operator_list SYNC_FETCH_AND_AND && TYPE_UNSIGNED (type)) (trunc_divmod @0 @1)))) - /* 1 / X -> X == 1 for unsigned integer X. - 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X. - But not for 1 / 0 so that we can get proper warnings and errors, - and not for 1-bit integers as they are edge cases better handled elsewhere. */ +/* 1 / X -> X == 1 for unsigned integer X. + 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X. + But not for 1 / 0 so that we can get proper warnings and errors, + and not for 1-bit integers as they are edge cases better handled + elsewhere. */ (simplify - (trunc_div integer_onep@0 @1) - (if (INTEGRAL_TYPE_P (type) && !integer_zerop (@1) && TYPE_PRECISION (type) > 1) - (if (TYPE_UNSIGNED (type)) - (eq @1 { build_one_cst (type); }) - (with { tree utype = unsigned_type_for (type); } - (cond (le (plus (convert:utype @1) { build_one_cst (utype); }) { build_int_cst (utype, 2); }) - @1 { build_zero_cst (type); }))))) + (trunc_div integer_onep@0 @1) + (if (INTEGRAL_TYPE_P (type) + && !integer_zerop (@1) + && TYPE_PRECISION (type) > 1) + (if (TYPE_UNSIGNED (type)) + (convert (eq:boolean_type_node @1 { build_one_cst (type); })) + (with { tree utype = unsigned_type_for (type); } + (cond (le (plus (convert:utype @1) { build_one_cst (utype); }) + { build_int_cst (utype, 2); }) + @1 { build_zero_cst (type); }))))) /* Combine two successive divisions. Note that combining ceil_div and floor_div is trickier and combining round_div even more so. */ --- gcc/testsuite/gcc.dg/torture/pr104279.c.jj 2022-01-29 12:25:36.388174312 +0100 +++ gcc/testsuite/gcc.dg/torture/pr104279.c 2022-01-29 12:25:53.206937588 +0100 @@ -0,0 +1,12 @@ +/* PR tree-optimization/104279 */ +/* { dg-do compile } */ + +unsigned a, b; + +int +main () +{ + b = ~(0 || ~0); + a = ~b / ~a; + return 0; +} --- gcc/testsuite/gcc.dg/torture/pr104280.c.jj 2022-01-29 12:24:36.190021595 +0100 +++ gcc/testsuite/gcc.dg/torture/pr104280.c 2022-01-29 12:25:28.681282783 +0100 @@ -0,0 +1,16 @@ +/* PR tree-optimization/104280 */ +/* { dg-do run } */ + +int +foo (unsigned b, int c) +{ + return b / c; +} + +int +main () +{ + if (foo (1, 2) != 0) + __builtin_abort (); + return 0; +} --- gcc/testsuite/gcc.dg/torture/pr104281.c.jj 2022-01-29 12:27:29.840577473 +0100 +++ gcc/testsuite/gcc.dg/torture/pr104281.c 2022-01-29 12:27:24.526652267 +0100 @@ -0,0 +1,22 @@ +/* PR tree-optimization/104281 */ +/* { dg-do run } */ + +unsigned a = 1; +int b, c = 2; +long d; + +int +main () +{ + while (1) + { + int m = a; + L: + a = ~(-(m || b & d)); + b = ((1 ^ a) / c); + if (b) + goto L; + break; + } + return 0; +}