From patchwork Fri Mar 11 23:48:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 51907 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F2020385DC09 for ; Fri, 11 Mar 2022 23:48:53 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id E887F3858C27 for ; Fri, 11 Mar 2022 23:48:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E887F3858C27 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=VEFYuDk61VDJndh9VNJ8X02CrU3+ZqhWjbfBvv46Ovo=; b=KOG/G0X4lGB95bqZNThRWeV4RN TRm3f58jOU1qZ2loOArreDeSv6RVRs1fowewb0gFvkp5zAgXQoHN9Mi8gHBfv7eA/yS58lVCU5F1V H1kbmK/9edrHX9rpai5jUWOZfIRtV3IAnS1NKQf9Jo/KOHgveNVziHwc7gMqdUcVxd2WeIN/8pf3i TPONXxb34txq7F/JhL0nxkZ8SpjknusMCuyC0r8XE+rJ43msc0KZx5XSnHuCcRmyAZW+EkwtIUp7c FPoPLS1eeMKt+FXXaqO/TIqe12+Gk4KUdxOrYj3LiJnaVHphPIk847EAK9/03wu88nyQrVF8s96vB m/e1tGNA==; Received: from host86-186-213-42.range86-186.btcentralplus.com ([86.186.213.42]:56677 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nSozs-00071q-CC for gcc-patches@gcc.gnu.org; Fri, 11 Mar 2022 18:48:36 -0500 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] PR tree-optimization/101895: Fold VEC_PERM to help recognize FMA. Date: Fri, 11 Mar 2022 23:48:34 -0000 Message-ID: <032301d835a2$86214110$9263c330$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adg1ohxhbt7glJ58QpSTrILFYKVjSw== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, 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: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This patch resolves PR tree-optimization/101895 a missed optimization regression, by adding a constant folding simplification to match.pd to simplify the transform "mult; vec_perm; plus" into "vec_perm; mult; plus" with the aim that keeping the multiplication and addition next to each other allows them to be recognized as fused-multiply-add on suitable targets. This transformation requires a tweak to match.pd's vec_same_elem_p predicate to handle CONSTRUCTOR_EXPRs using the same SSA_NAME_DEF_STMT idiom used for constructors elsewhere in match.pd. The net effect is that the following code example: void foo(float * __restrict__ a, float b, float *c) { a[0] = c[0]*b + a[0]; a[1] = c[2]*b + a[1]; a[2] = c[1]*b + a[2]; a[3] = c[3]*b + a[3]; } when compiled on x86_64-pc-linux-gnu with -O2 -march=cascadelake currently generates: vbroadcastss %xmm0, %xmm0 vmulps (%rsi), %xmm0, %xmm0 vpermilps $216, %xmm0, %xmm0 vaddps (%rdi), %xmm0, %xmm0 vmovups %xmm0, (%rdi) ret but with this patch now generates the improved: vpermilps $216, (%rsi), %xmm1 vbroadcastss %xmm0, %xmm0 vfmadd213ps (%rdi), %xmm0, %xmm1 vmovups %xmm1, (%rdi) ret This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check with no new failures. Ok for mainline? 2022-03-11 Roger Sayle gcc/ChangeLog PR tree-optimization/101895 * match.pd (vec_same_elem_p): Handle CONSTRUCTOR_EXPR def. (plus (vec_perm (mult ...) ...) ...): New reordering simplification. gcc/testsuite/ChangeLog * gcc.target/i386/pr101895.c: New test case. Thanks in advance, Roger diff --git a/gcc/match.pd b/gcc/match.pd index 97399e5..9184276 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -7695,10 +7695,22 @@ and, (match vec_same_elem_p (vec_duplicate @0)) +(match vec_same_elem_p + CONSTRUCTOR@0 + (if (uniform_vector_p (TREE_CODE (@0) == SSA_NAME + ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0)))) + (simplify (vec_perm vec_same_elem_p@0 @0 @1) @0) +/* Push VEC_PERM earlier if that may help FMA perception (PR101895). */ +(for plusminus (plus minus) + (simplify + (plusminus (vec_perm (mult@0 @1 vec_same_elem_p@2) @0 @3) @4) + (plusminus (mult (vec_perm @1 @1 @3) @2) @4))) + + /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop. The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic constant which when multiplied by a power of 2 contains a unique value diff --git a/gcc/testsuite/gcc.target/i386/pr101895.c b/gcc/testsuite/gcc.target/i386/pr101895.c new file mode 100644 index 0000000..4d0f1cb --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr101895.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=cascadelake" } */ + +void foo(float * __restrict__ a, float b, float *c) { + a[0] = c[0]*b + a[0]; + a[1] = c[2]*b + a[1]; + a[2] = c[1]*b + a[2]; + a[3] = c[3]*b + a[3]; +} + +/* { dg-final { scan-assembler "vfmadd" } } */