From 2202299c2aa69385ca5e7574914dabc84fb6a40a Mon Sep 17 00:00:00 2001
From: Kelvin Nilsen <kelvin@gcc.gnu.org>
Date: Mon, 11 May 2020 16:16:15 -0500
Subject: [PATCH] rs6000: Add pdepd and pextd

Add scalar instructions for parallel bit deposit and extract, with
built-in function support.

[gcc]

2020-05-11  Kelvin Nilsen  <kelvin@gcc.gnu.org>

	* config/rs6000/rs6000-builtin.def (__builtin_pdepd): New built-in
	function.
	(__builtin_pextd): Likewise.
	* config/rs6000/rs6000.md (UNSPEC_PDEPD): New constant.
	(UNSPEC_PEXTD): Likewise.
	(pdepd): New insn.
	(pextd): Likewise.
	* doc/extend.texi (Basic PowerPC Built-in Functions Available for
	a Future Architecture): Add descriptions of __builtin_pdepd and
	__builtin_pextd functions.

[gcc/testsuite]

2020-05-11  Kelvin Nilsen  <kelvin@gcc.gnu.org>

	* gcc.target/powerpc/pdep-0.c: New.
	* gcc.target/powerpc/pdep-1.c: New.
	* gcc.target/powerpc/pextd-0.c: New.
	* gcc.target/powerpc/pextd-1.c: New.
---
 gcc/ChangeLog                              | 13 ++++++
 gcc/config/rs6000/rs6000-builtin.def       |  2 +
 gcc/config/rs6000/rs6000.md                | 20 +++++++++
 gcc/doc/extend.texi                        | 16 +++++++
 gcc/testsuite/ChangeLog                    |  7 +++
 gcc/testsuite/gcc.target/powerpc/pdep-0.c  | 48 +++++++++++++++++++++
 gcc/testsuite/gcc.target/powerpc/pdep-1.c  | 48 +++++++++++++++++++++
 gcc/testsuite/gcc.target/powerpc/pextd-0.c | 50 ++++++++++++++++++++++
 gcc/testsuite/gcc.target/powerpc/pextd-1.c | 49 +++++++++++++++++++++
 9 files changed, 253 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/powerpc/pdep-0.c
 create mode 100644 gcc/testsuite/gcc.target/powerpc/pdep-1.c
 create mode 100644 gcc/testsuite/gcc.target/powerpc/pextd-0.c
 create mode 100644 gcc/testsuite/gcc.target/powerpc/pextd-1.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index f9ce18554905..02d745237698 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,16 @@
+2020-05-11  Kelvin Nilsen  <kelvin@gcc.gnu.org>
+
+	* config/rs6000/rs6000-builtin.def (__builtin_pdepd): New built-in
+	function.
+	(__builtin_pextd): Likewise.
+	* config/rs6000/rs6000.md (UNSPEC_PDEPD): New constant.
+	(UNSPEC_PEXTD): Likewise.
+	(pdepd): New insn.
+	(pextd): Likewise.
+	* doc/extend.texi (Basic PowerPC Built-in Functions Available for
+	a Future Architecture): Add descriptions of __builtin_pdepd and
+	__builtin_pextd functions.
+
 2020-05-11  Kelvin Nilsen  <kelvin@gcc.gnu.org>
 
 	* config/rs6000/altivec.h (vec_clrl): New #define.
diff --git a/gcc/config/rs6000/rs6000-builtin.def b/gcc/config/rs6000/rs6000-builtin.def
index 39e7da5fa506..4b06323a07fe 100644
--- a/gcc/config/rs6000/rs6000-builtin.def
+++ b/gcc/config/rs6000/rs6000-builtin.def
@@ -2577,6 +2577,8 @@ BU_P9_OVERLOAD_2 (CMPEQB,	"byte_in_set")
 BU_FUTURE_MISC_2 (CFUGED, "cfuged", CONST, cfuged)
 BU_FUTURE_MISC_2 (CNTLZDM, "cntlzdm", CONST, cntlzdm)
 BU_FUTURE_MISC_2 (CNTTZDM, "cnttzdm", CONST, cnttzdm)
+BU_FUTURE_MISC_2 (PDEPD, "pdepd", CONST, pdepd)
+BU_FUTURE_MISC_2 (PEXTD, "pextd", CONST, pextd)
 
 /* Future architecture vector built-ins.  */
 BU_FUTURE_V_2 (VCLRLB, "vclrlb", CONST, vclrlb)
diff --git a/gcc/config/rs6000/rs6000.md b/gcc/config/rs6000/rs6000.md
index 6c9bae934d11..4fcd6a940221 100644
--- a/gcc/config/rs6000/rs6000.md
+++ b/gcc/config/rs6000/rs6000.md
@@ -151,6 +151,8 @@
    UNSPEC_CFUGED
    UNSPEC_CNTLZDM
    UNSPEC_CNTTZDM
+   UNSPEC_PDEPD
+   UNSPEC_PEXTD
   ])
 
 ;;
@@ -2483,6 +2485,24 @@
    "cnttzdm %0,%1,%2"
    [(set_attr "type" "integer")])
 
+(define_insn "pdepd"
+  [(set (match_operand:DI 0 "register_operand" "=r")
+	(unspec:DI [(match_operand:DI 1 "gpc_reg_operand" "r")
+		    (match_operand:DI 2 "gpc_reg_operand" "r")]
+		   UNSPEC_PDEPD))]
+   "TARGET_FUTURE && TARGET_POWERPC64"
+   "pdepd %0,%1,%2"
+   [(set_attr "type" "integer")])
+
+(define_insn "pextd"
+  [(set (match_operand:DI 0 "register_operand" "=r")
+	(unspec:DI [(match_operand:DI 1 "gpc_reg_operand" "r")
+		    (match_operand:DI 2 "gpc_reg_operand" "r")]
+		   UNSPEC_PEXTD))]
+   "TARGET_FUTURE && TARGET_POWERPC64"
+   "pextd %0,%1,%2"
+   [(set_attr "type" "integer")])
+
 (define_insn "cmpb<mode>3"
   [(set (match_operand:GPR 0 "gpc_reg_operand" "=r")
 	(unspec:GPR [(match_operand:GPR 1 "gpc_reg_operand" "r")
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 104397823b01..9602a310cbb2 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -17572,6 +17572,22 @@ Perform a 64-bit count trailing zeros operation under mask, as if
 implemented by the future @code{cnttzdm} instruction.
 @findex __builtin_cnttzdm
 
+@smallexample
+@exdent unsigned long long int
+@exdent __builtin_pdepd (unsigned long long int, unsigned long long int)
+@end smallexample
+Perform a 64-bit parallel bits deposit operation, as if implemented by the
+Future @code{pdepd} instruction.
+@findex __builtin_pdepd
+
+@smallexample
+@exdent unsigned long long int
+@exdent __builtin_pextd (unsigned long long int, unsigned long long int)
+@end smallexample
+Perform a 64-bit parallel bits extract operation, as if implemented by the
+Future @code{pextd} instruction.
+@findex __builtin_pextd
+
 @node PowerPC AltiVec/VSX Built-in Functions
 @subsection PowerPC AltiVec/VSX Built-in Functions
 
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index e3f984ea9c27..74e83323e6e1 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,10 @@
+2020-05-11  Kelvin Nilsen  <kelvin@gcc.gnu.org>
+
+	* gcc.target/powerpc/pdep-0.c: New.
+	* gcc.target/powerpc/pdep-1.c: New.
+	* gcc.target/powerpc/pextd-0.c: New.
+	* gcc.target/powerpc/pextd-1.c: New.
+
 2020-05-11  Kelvin Nilsen  <kelvin@gcc.gnu.org>
 
 	* gcc.target/powerpc/vec-clrl-0.c: New.
diff --git a/gcc/testsuite/gcc.target/powerpc/pdep-0.c b/gcc/testsuite/gcc.target/powerpc/pdep-0.c
new file mode 100644
index 000000000000..5c6afb33169d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/powerpc/pdep-0.c
@@ -0,0 +1,48 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target powerpc64 } */
+/* { dg-options "-mcpu=future" } */
+
+extern void abort (void);
+
+unsigned long long int
+do_pdepd (unsigned long long int source, unsigned long long int mask) {
+  return __builtin_pdepd (source, mask);
+}
+
+int main (int argc, char *argv [])
+{
+  unsigned long long int sources [4], masks [4];
+  unsigned long long int results [4][4] = {
+    /* sources [0] with each of masks [0..3] */
+    { 0x7e3c0000ll, 0x00007e3cll, 0x070e030cll, 0x70e030c0ll },
+    /* sources [1] with each of masks [0..3] */
+    { 0xa5f00000ll, 0x0000a5f0ll, 0x0a050f00ll, 0xa050f000ll },
+    /* sources [2] with each of masks [0..3] */
+    { 0xf07e0000ll, 0x0000f07ell, 0x0f00070ell, 0xf00070e0ll },
+    /* sources [3] with each of masks [0..3] */
+    { 0xe7c30000ll, 0x0000e7c3ll, 0x0e070c03ll, 0xe070c030ll },
+  };
+
+  sources[0] = 0xa5f07e3cll;
+  sources[1] = 0x7e3ca5f0ll;
+  sources[2] = 0x3ca5f07ell;
+  sources[3] = 0x5a0fe7c3ll;
+
+  masks[0] = 0xffff0000ll;
+  masks[1] = 0x0000ffffll;
+  masks[2] = 0x0f0f0f0fll;
+  masks[3] = 0xf0f0f0f0ll;
+
+  for (int i = 0; i < 4; i++)
+    {
+      for (int j = 0; j < 4; j++)
+	{
+	  if (do_pdepd (sources[i], masks[j]) != results [i][j])
+	    abort ();
+	}
+    }
+
+  return 0;
+}
+
+/* { dg-final { scan-assembler {\mpdepd\M} } } */
diff --git a/gcc/testsuite/gcc.target/powerpc/pdep-1.c b/gcc/testsuite/gcc.target/powerpc/pdep-1.c
new file mode 100644
index 000000000000..40532ee11773
--- /dev/null
+++ b/gcc/testsuite/gcc.target/powerpc/pdep-1.c
@@ -0,0 +1,48 @@
+/* { dg-do run } */
+/* { dg-require-effective-target powerpc_future_hw } */
+/* { dg-require-effective-target powerpc64 } */
+/* { dg-options "-mcpu=future" } */
+
+extern void abort (void);
+
+unsigned long long int
+do_pdepd (unsigned long long int source, unsigned long long int mask) {
+  return __builtin_pdepd (source, mask);
+}
+
+int main (int argc, char *argv [])
+{
+  unsigned long long int sources [4], masks [4];
+  unsigned long long int results [4][4] = {
+    /* sources [0] with each of masks [0..3] */
+    { 0x7e3c0000ll, 0x00007e3cll, 0x070e030cll, 0x70e030c0ll },
+    /* sources [1] with each of masks [0..3] */
+    { 0xa5f00000ll, 0x0000a5f0ll, 0x0a050f00ll, 0xa050f000ll },
+    /* sources [2] with each of masks [0..3] */
+    { 0xf07e0000ll, 0x0000f07ell, 0x0f00070ell, 0xf00070e0ll },
+    /* sources [3] with each of masks [0..3] */
+    { 0xe7c30000ll, 0x0000e7c3ll, 0x0e070c03ll, 0xe070c030ll },
+  };
+
+  sources[0] = 0xa5f07e3cll;
+  sources[1] = 0x7e3ca5f0ll;
+  sources[2] = 0x3ca5f07ell;
+  sources[3] = 0x5a0fe7c3ll;
+
+  masks[0] = 0xffff0000ll;
+  masks[1] = 0x0000ffffll;
+  masks[2] = 0x0f0f0f0fll;
+  masks[3] = 0xf0f0f0f0ll;
+
+  for (int i = 0; i < 4; i++)
+    {
+      for (int j = 0; j < 4; j++)
+	{
+	  if (do_pdepd (sources[i], masks[j]) != results [i][j])
+	    abort ();
+	}
+    }
+
+  return 0;
+}
+
diff --git a/gcc/testsuite/gcc.target/powerpc/pextd-0.c b/gcc/testsuite/gcc.target/powerpc/pextd-0.c
new file mode 100644
index 000000000000..23a952dc2c69
--- /dev/null
+++ b/gcc/testsuite/gcc.target/powerpc/pextd-0.c
@@ -0,0 +1,50 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target powerpc64 } */
+/* { dg-options "-mcpu=future" } */
+
+extern void abort (void);
+
+unsigned long long int
+do_pextd (unsigned long long int source, unsigned long long int mask) {
+  return __builtin_pextd (source, mask);
+}
+
+int main (int argc, char *argv [])
+{
+  unsigned long long int sources [4], masks [4];
+  unsigned long long int results [4][4] = {
+    /* sources [0] with each of masks [0..3] */
+    { 0x0000a5f0ll, 0x00007e3cll, 0x000050ecll, 0x0000af73ll },
+    /* sources [1] with each of masks [0..3] */
+    { 0x00007e3cll, 0x0000a5f0ll, 0x0000ec50ll, 0x000073afll },
+    /* sources [2] with each of masks [0..3] */
+    { 0x00003ca5ll, 0x0000f07ell, 0x0000c50ell, 0x00003af7ll },
+    /* sources [3] with each of masks [0..3] */
+    { 0x00005a0fll, 0x0000e7c3ll, 0x0000af73ll, 0x000050ecll },
+  };
+
+  sources[0] = 0xa5f07e3cll;
+  sources[1] = 0x7e3ca5f0ll;
+  sources[2] = 0x3ca5f07ell;
+  sources[3] = 0x5a0fe7c3ll;
+
+  masks[0] = 0xffff0000ll;
+  masks[1] = 0x0000ffffll;
+  masks[2] = 0x0f0f0f0fll;
+  masks[3] = 0xf0f0f0f0ll;
+
+  unsigned long long int result;
+
+  for (int i = 0; i < 4; i++)
+    {
+      for (int j = 0; j < 4; j++)
+	{
+	  if (do_pextd (sources[i], masks[j]) != results [i][j])
+	    abort ();
+	}
+    }
+
+  return 0;
+}
+
+/* { dg-final { scan-assembler {\mpextd\M} } } */
diff --git a/gcc/testsuite/gcc.target/powerpc/pextd-1.c b/gcc/testsuite/gcc.target/powerpc/pextd-1.c
new file mode 100644
index 000000000000..814ce4325a03
--- /dev/null
+++ b/gcc/testsuite/gcc.target/powerpc/pextd-1.c
@@ -0,0 +1,49 @@
+/* { dg-do run } */
+/* { dg-require-effective-target powerpc_future_hw } */
+/* { dg-require-effective-target powerpc64 } */
+/* { dg-options "-mcpu=future" } */
+
+extern void abort (void);
+
+unsigned long long int
+do_pextd (unsigned long long int source, unsigned long long int mask) {
+  return __builtin_pextd (source, mask);
+}
+
+int main (int argc, char *argv [])
+{
+  unsigned long long int sources [4], masks [4];
+  unsigned long long int results [4][4] = {
+    /* sources [0] with each of masks [0..3] */
+    { 0x0000a5f0ll, 0x00007e3cll, 0x000050ecll, 0x0000af73ll },
+    /* sources [1] with each of masks [0..3] */
+    { 0x00007e3cll, 0x0000a5f0ll, 0x0000ec50ll, 0x000073afll },
+    /* sources [2] with each of masks [0..3] */
+    { 0x00003ca5ll, 0x0000f07ell, 0x0000c50ell, 0x00003af7ll },
+    /* sources [3] with each of masks [0..3] */
+    { 0x00005a0fll, 0x0000e7c3ll, 0x0000af73ll, 0x000050ecll },
+  };
+
+  sources[0] = 0xa5f07e3cll;
+  sources[1] = 0x7e3ca5f0ll;
+  sources[2] = 0x3ca5f07ell;
+  sources[3] = 0x5a0fe7c3ll;
+
+  masks[0] = 0xffff0000ll;
+  masks[1] = 0x0000ffffll;
+  masks[2] = 0x0f0f0f0fll;
+  masks[3] = 0xf0f0f0f0ll;
+
+  unsigned long long int result;
+
+  for (int i = 0; i < 4; i++)
+    {
+      for (int j = 0; j < 4; j++)
+	{
+	  if (do_pextd (sources[i], masks[j]) != results [i][j])
+	    abort ();
+	}
+    }
+
+  return 0;
+}
-- 
GitLab