From patchwork Sat May 20 20:32:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 96818 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp551349vqo; Sat, 20 May 2023 13:33:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ70kuUIFSKwIBo/bLgC3KO9NhvQUwQ7faelSWtE5ayF2b2sODpbiXclvFyNMvIZ4Vfwqihj X-Received: by 2002:a17:906:7087:b0:965:fa3b:7478 with SMTP id b7-20020a170906708700b00965fa3b7478mr4813280ejk.53.1684614832877; Sat, 20 May 2023 13:33:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684614832; cv=none; d=google.com; s=arc-20160816; b=Fki8+JWHiVzzg+BhSgHw6oDq6wOmkU2SPBs/jIBmsdecyRSPMdJr1ZPrNkGqx8u5hU FHDf1SFA9J3/ALIGcVPJjVZhwuATrTYneXcIdn4OfbqPMzHMA7NVWGI2dNOwcJKdsEnq XNVY3Np1vcwanJlBUZJs4VsP8BJJQavEWqgP0yupoE3EsJLutmG+trKvxtkOMLk6GT4K 3K196lvOLVuJ5ojmwhAAr6aONnyow3N6owwudm5qkHTJuveY12XyKE5UFsIPd8IKCfxs l/sizuzO11FyCK8Ojs37MwGWtJJhbLF25oeeIJQ2o+GToIVie0P0Q6+Qem9/fAT1WIGk V13g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=48WH6RdD+dy9I1orD923H8PdsRYvCWmMj1AOC1uLsrQ=; b=pUa/dF88Y+ez43nZoJunHMmkN8SDQCCYCC1YoxKWbd5RUtzun8oIiVLWLBfqaf+saH k7AhqdYlAt0eZs3Mh/1vomo65JVLpYAMEMUjK5W8easq8RoMfl7Jjy/ukKYOVes5f/a4 kDRlb8oloymdf+wA9VMrtSek5Mv4lUOCljsVgPRLcVfB5bf3da7CXSqQ/x9i55PfMED3 gqqdXgx5e3LMYxbHV6P0qe3y1R4IhrYMDQ78umjudoknYsgdnFuIvfsQhCrdSueM7btT PrM67DPDIUvRpTtPXcrLOSGgO9uN8vKID5vOezrMPE7wqRIByyaVRGyVLIvb9jMNO4w/ N4Uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=jJix1iEN; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id bm21-20020a170906c05500b0096f7955a3a5si404304ejb.496.2023.05.20.13.33.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 May 2023 13:33:52 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=jJix1iEN; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 897403858408 for ; Sat, 20 May 2023 20:33:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 897403858408 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684614831; bh=48WH6RdD+dy9I1orD923H8PdsRYvCWmMj1AOC1uLsrQ=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=jJix1iENri+WEd5+UPI4zJw51FvRyJ0kBHBgmoyEr7QOMNMgRpxt5LeAI3Z+yAh81 8+kfuIJJsUl0aCbYAa9BwKWoM1I9Mru0jdUJ3GOgF3fKmdX5o3ALWmCj7imKMgSDv3 XJ7+OTu8lsifgqwReh2Zndjh9qS0hZeK+gAkIt0c= 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 1FD073858D20 for ; Sat, 20 May 2023 20:33:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1FD073858D20 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-617-2UNdvKOwPI6X1Cev4ItezA-1; Sat, 20 May 2023 16:33:02 -0400 X-MC-Unique: 2UNdvKOwPI6X1Cev4ItezA-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 8E3D329AA2D2; Sat, 20 May 2023 20:33:02 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 454C9140E954; Sat, 20 May 2023 20:33:02 +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 34KKWxRO1846901 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 20 May 2023 22:32:59 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 34KKWw1J1846900; Sat, 20 May 2023 22:32:58 +0200 Date: Sat, 20 May 2023 22:32:57 +0200 To: Richard Biener , Richard Sandiford Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Ensure (op CONSTANT_CLASS_P CONSTANT_CLASS_P) is simplified [PR109505] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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 Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766446683078983561?= X-GMAIL-MSGID: =?utf-8?q?1766446683078983561?= Hi! On the following testcase we hang, because POLY_INT_CST is CONSTANT_CLASS_P, but BIT_AND_EXPR with it and INTEGER_CST doesn't simplify and the (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) simplification actually relies on the (CST1 & CST2) simplification, otherwise it is a deoptimization, trading 2 ops for 3 and furthermore running into /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both operands are another bit-wise operation with a common input. If so, distribute the bit operations to save an operation and possibly two if constants are involved. For example, convert (A | B) & (A | C) into A | (B & C) Further simplification will occur if B and C are constants. */ simplification which simplifies that (x & CST2) | (CST1 & CST2) back to CST2 & (x | CST1). I went through all other places I could find where we have a simplification with 2 CONSTANT_CLASS_P operands and perform some operation on those two, while the other spots aren't that severe (just trade 2 operations for another 2 if the two constants don't simplify, rather than as in the above case trading 2 ops for 3), I still think all those spots really intend to optimize only if the 2 constants simplify. So, the following patch adds to those a ! modifier to ensure that, even at GENERIC that modifier means !EXPR_P which is exactly what we want IMHO. Bootstrapped/regtested on aarch64-linux, powerpc64le-linux, x86_64-linux and i686-linux, ok for trunk and later 13.2/12.4? 2023-05-20 Jakub Jelinek PR tree-optimization/109505 * match.pd ((x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2), Combine successive equal operations with constants, (A +- CST1) +- CST2 -> A + CST3, (CST1 - A) +- CST2 -> CST3 - A, CST1 - (CST2 - A) -> CST3 + A): Use ! on ops with 2 CONSTANT_CLASS_P operands. * gcc.target/aarch64/sve/pr109505.c: New test. Jakub --- gcc/match.pd.jj 2023-05-17 20:57:59.000000000 +0200 +++ gcc/match.pd 2023-05-20 09:51:27.094277249 +0200 @@ -1916,7 +1916,7 @@ (define_operator_list SYNC_FETCH_AND_AND /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */ (simplify (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) - (bit_ior (bit_and @0 @2) (bit_and @1 @2))) + (bit_ior (bit_and @0 @2) (bit_and! @1 @2))) /* Combine successive equal operations with constants. */ (for bitop (bit_and bit_ior bit_xor) @@ -1925,7 +1925,7 @@ (define_operator_list SYNC_FETCH_AND_AND (if (!CONSTANT_CLASS_P (@0)) /* This is the canonical form regardless of whether (bitop @1 @2) can be folded to a constant. */ - (bitop @0 (bitop @1 @2)) + (bitop @0 (bitop! @1 @2)) /* In this case we have three constants and (bitop @0 @1) doesn't fold to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if the values involved are such that the operation can't be decided at @@ -2998,13 +2998,13 @@ (define_operator_list SYNC_FETCH_AND_AND forever if something doesn't simplify into a constant. */ (if (!CONSTANT_CLASS_P (@0)) (if (outer_op == PLUS_EXPR) - (plus (view_convert @0) (inner_op @2 (view_convert @1))) - (minus (view_convert @0) (neg_inner_op @2 (view_convert @1))))) + (plus (view_convert @0) (inner_op! @2 (view_convert @1))) + (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1))))) (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) (if (outer_op == PLUS_EXPR) - (view_convert (plus @0 (inner_op (view_convert @2) @1))) - (view_convert (minus @0 (neg_inner_op (view_convert @2) @1)))) + (view_convert (plus @0 (inner_op! (view_convert @2) @1))) + (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1)))) /* If the constant operation overflows we cannot do the transform directly as we would introduce undefined overflow, for example with (a - 1) + INT_MIN. */ @@ -3035,10 +3035,10 @@ (define_operator_list SYNC_FETCH_AND_AND /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse forever if something doesn't simplify into a constant. */ (if (!CONSTANT_CLASS_P (@0)) - (minus (outer_op (view_convert @1) @2) (view_convert @0))) + (minus (outer_op! (view_convert @1) @2) (view_convert @0))) (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) - (view_convert (minus (outer_op @1 (view_convert @2)) @0)) + (view_convert (minus (outer_op! @1 (view_convert @2)) @0)) (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type)) (with { tree cst = const_binop (outer_op, type, @1, @2); } (if (cst && !TREE_OVERFLOW (cst)) @@ -3054,10 +3054,10 @@ (define_operator_list SYNC_FETCH_AND_AND /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse forever if something doesn't simplify into a constant. */ (if (!CONSTANT_CLASS_P (@0)) - (plus (view_convert @0) (minus @1 (view_convert @2)))) + (plus (view_convert @0) (minus! @1 (view_convert @2)))) (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) - (view_convert (plus @0 (minus (view_convert @1) @2))) + (view_convert (plus @0 (minus! (view_convert @1) @2))) (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type)) (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); } (if (cst && !TREE_OVERFLOW (cst)) --- gcc/testsuite/gcc.target/aarch64/sve/pr109505.c.jj 2023-05-20 09:58:15.646446786 +0200 +++ gcc/testsuite/gcc.target/aarch64/sve/pr109505.c 2023-05-20 09:58:06.295580225 +0200 @@ -0,0 +1,12 @@ +/* PR tree-optimization/109505 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=armv8.2-a+sve" } */ + +#pragma GCC aarch64 "arm_sve.h" + +unsigned long +foo (unsigned long x) +{ + unsigned long y = svcntb (); + return (x | 15) & y; +}