public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PR tree-optimization/101895: Fold VEC_PERM to help recognize FMA.
@ 2022-03-11 23:48 Roger Sayle
  2022-03-12 23:39 ` Marc Glisse
  0 siblings, 1 reply; 5+ messages in thread
From: Roger Sayle @ 2022-03-11 23:48 UTC (permalink / raw)
  To: 'GCC Patches'

[-- Attachment #1: Type: text/plain, Size: 1723 bytes --]


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  <roger@nextmovesoftware.com>

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
--


[-- Attachment #2: patchvp.txt --]
[-- Type: text/plain, Size: 1413 bytes --]

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" } } */

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-03-15  7:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-11 23:48 [PATCH] PR tree-optimization/101895: Fold VEC_PERM to help recognize FMA Roger Sayle
2022-03-12 23:39 ` Marc Glisse
2022-03-14  7:37   ` Richard Biener
2022-03-15  7:25     ` Roger Sayle
2022-03-15  7:43       ` Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).