[v3] Enable __memcmpeq after seeing __memcmpeq prototype

Message ID 20220705162715.133539-1-hjl.tools@gmail.com
State New
Headers
Series [v3] Enable __memcmpeq after seeing __memcmpeq prototype |

Commit Message

H.J. Lu July 5, 2022, 4:27 p.m. UTC
  extern int __memcmpeq (const void *, const void *, size_t);

was was added to GLIBC 2.35.  Expand BUILT_IN_MEMCMP_EQ to __memcmpeq
after seeing __memcmpeq prototype

gcc/

	* builtins.cc (expand_builtin): Issue an error for
	BUILT_IN___MEMCMPEQ if there is no __memcmpeq prototype.  Expand
	BUILT_IN_MEMCMP_EQ to BUILT_IN___MEMCMP_EQ if there is __memcmpeq
	prototype.
	* builtins.def (BUILT_IN___MEMCMPEQ): New.

gcc/testsuite/

	* c-c++-common/memcmpeq-1.c: New test.
	* c-c++-common/memcmpeq-2.c: Likewise.
	* c-c++-common/memcmpeq-3.c: Likewise.
	* c-c++-common/memcmpeq-4.c: Likewise.
	* c-c++-common/memcmpeq-5.c: Likewise.
	* c-c++-common/memcmpeq-6.c: Likewise.
	* c-c++-common/memcmpeq.h: Likewise.
---
 gcc/builtins.cc                         | 14 +++++++++++++-
 gcc/builtins.def                        |  3 +++
 gcc/testsuite/c-c++-common/memcmpeq-1.c | 11 +++++++++++
 gcc/testsuite/c-c++-common/memcmpeq-2.c | 11 +++++++++++
 gcc/testsuite/c-c++-common/memcmpeq-3.c | 11 +++++++++++
 gcc/testsuite/c-c++-common/memcmpeq-4.c | 11 +++++++++++
 gcc/testsuite/c-c++-common/memcmpeq-5.c | 11 +++++++++++
 gcc/testsuite/c-c++-common/memcmpeq-6.c | 10 ++++++++++
 gcc/testsuite/c-c++-common/memcmpeq.h   | 11 +++++++++++
 9 files changed, 92 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/c-c++-common/memcmpeq-1.c
 create mode 100644 gcc/testsuite/c-c++-common/memcmpeq-2.c
 create mode 100644 gcc/testsuite/c-c++-common/memcmpeq-3.c
 create mode 100644 gcc/testsuite/c-c++-common/memcmpeq-4.c
 create mode 100644 gcc/testsuite/c-c++-common/memcmpeq-5.c
 create mode 100644 gcc/testsuite/c-c++-common/memcmpeq-6.c
 create mode 100644 gcc/testsuite/c-c++-common/memcmpeq.h
  

Comments

Richard Biener July 8, 2022, 12:28 p.m. UTC | #1
On Tue, Jul 5, 2022 at 6:27 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> extern int __memcmpeq (const void *, const void *, size_t);
>
> was was added to GLIBC 2.35.  Expand BUILT_IN_MEMCMP_EQ to __memcmpeq
> after seeing __memcmpeq prototype
>
> gcc/
>
>         * builtins.cc (expand_builtin): Issue an error for
>         BUILT_IN___MEMCMPEQ if there is no __memcmpeq prototype.  Expand
>         BUILT_IN_MEMCMP_EQ to BUILT_IN___MEMCMP_EQ if there is __memcmpeq
>         prototype.
>         * builtins.def (BUILT_IN___MEMCMPEQ): New.
>
> gcc/testsuite/
>
>         * c-c++-common/memcmpeq-1.c: New test.
>         * c-c++-common/memcmpeq-2.c: Likewise.
>         * c-c++-common/memcmpeq-3.c: Likewise.
>         * c-c++-common/memcmpeq-4.c: Likewise.
>         * c-c++-common/memcmpeq-5.c: Likewise.
>         * c-c++-common/memcmpeq-6.c: Likewise.
>         * c-c++-common/memcmpeq.h: Likewise.
> ---
>  gcc/builtins.cc                         | 14 +++++++++++++-
>  gcc/builtins.def                        |  3 +++
>  gcc/testsuite/c-c++-common/memcmpeq-1.c | 11 +++++++++++
>  gcc/testsuite/c-c++-common/memcmpeq-2.c | 11 +++++++++++
>  gcc/testsuite/c-c++-common/memcmpeq-3.c | 11 +++++++++++
>  gcc/testsuite/c-c++-common/memcmpeq-4.c | 11 +++++++++++
>  gcc/testsuite/c-c++-common/memcmpeq-5.c | 11 +++++++++++
>  gcc/testsuite/c-c++-common/memcmpeq-6.c | 10 ++++++++++
>  gcc/testsuite/c-c++-common/memcmpeq.h   | 11 +++++++++++
>  9 files changed, 92 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/c-c++-common/memcmpeq-1.c
>  create mode 100644 gcc/testsuite/c-c++-common/memcmpeq-2.c
>  create mode 100644 gcc/testsuite/c-c++-common/memcmpeq-3.c
>  create mode 100644 gcc/testsuite/c-c++-common/memcmpeq-4.c
>  create mode 100644 gcc/testsuite/c-c++-common/memcmpeq-5.c
>  create mode 100644 gcc/testsuite/c-c++-common/memcmpeq-6.c
>  create mode 100644 gcc/testsuite/c-c++-common/memcmpeq.h
>
> diff --git a/gcc/builtins.cc b/gcc/builtins.cc
> index e6816d5c865..2254a597bec 100644
> --- a/gcc/builtins.cc
> +++ b/gcc/builtins.cc
> @@ -7395,6 +7395,15 @@ expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
>         return target;
>        break;
>
> +    case BUILT_IN___MEMCMPEQ:
> +      if (!builtin_decl_declared_p (BUILT_IN___MEMCMPEQ))
> +       {
> +         error ("use of %<__builtin___memcmpeq ()%> without "
> +                "%<__memcmpeq%> prototype");
> +         return const0_rtx;
> +       }
> +      break;
> +

I think we don't need this - if the user manually calls
__builtin__memcmpeq () he
should know what he does.

>      /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
>         back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
>         when changing it to a strcmp call.  */
> @@ -7448,7 +7457,10 @@ expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
>         return target;
>        if (fcode == BUILT_IN_MEMCMP_EQ)
>         {
> -         tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
> +         tree newdecl = builtin_decl_explicit
> +           (builtin_decl_declared_p (BUILT_IN___MEMCMPEQ)
> +            ? BUILT_IN___MEMCMPEQ
> +            : BUILT_IN_MEMCMP);
>           TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
>         }
>        break;
> diff --git a/gcc/builtins.def b/gcc/builtins.def
> index 005976f34e9..95642c6acdf 100644
> --- a/gcc/builtins.def
> +++ b/gcc/builtins.def
> @@ -965,6 +965,9 @@ DEF_BUILTIN_STUB (BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX, "__builtin_alloca_with_ali
>     equality with zero.  */
>  DEF_BUILTIN_STUB (BUILT_IN_MEMCMP_EQ, "__builtin_memcmp_eq")
>
> +/* Similar to BUILT_IN_MEMCMP_EQ, but is mapped to __memcmpeq.  */
> +DEF_EXT_LIB_BUILTIN (BUILT_IN___MEMCMPEQ, "__memcmpeq", BT_FN_INT_CONST_PTR_CONST_PTR_SIZE, ATTR_PURE_NOTHROW_NONNULL_LEAF)
> +

it would be nice to not make __builtin___memcmpeq or __memcmpeq available to the
user without a __memcmpeq declaration.  I'm not sure this can be arranged for.
In principle we're just assigning an optional libfunc name to BUILT_IN_MEMCMP_EQ
when __memcmpeq is available.

Joseph, is there any convenient mechanism available I am missing?

Otherwise the builtin should probably be documented (as well as the
behavior wrt a __memcmpeq
declaration).

Thanks,
Richard.

>  /* An internal version of strcmp/strncmp, used when the result is only
>     tested for equality with zero.  */
>  DEF_BUILTIN_STUB (BUILT_IN_STRCMP_EQ, "__builtin_strcmp_eq")
> diff --git a/gcc/testsuite/c-c++-common/memcmpeq-1.c b/gcc/testsuite/c-c++-common/memcmpeq-1.c
> new file mode 100644
> index 00000000000..14622f0d765
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/memcmpeq-1.c
> @@ -0,0 +1,11 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2" } */
> +/* { dg-final { scan-assembler "__memcmpeq" } } */
> +
> +#include "memcmpeq.h"
> +
> +int
> +foo (const char *s1, const char *s2, size_t len)
> +{
> +  return __builtin_memcmp (s1, s2, len) != 0;
> +}
> diff --git a/gcc/testsuite/c-c++-common/memcmpeq-2.c b/gcc/testsuite/c-c++-common/memcmpeq-2.c
> new file mode 100644
> index 00000000000..f57f279f173
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/memcmpeq-2.c
> @@ -0,0 +1,11 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2" } */
> +/* { dg-final { scan-assembler "__memcmpeq" } } */
> +
> +#include "memcmpeq.h"
> +
> +int
> +foo (const char *s1, const char *s2, size_t len)
> +{
> +  return memcmp (s1, s2, len) == 0;
> +}
> diff --git a/gcc/testsuite/c-c++-common/memcmpeq-3.c b/gcc/testsuite/c-c++-common/memcmpeq-3.c
> new file mode 100644
> index 00000000000..2ca2131c23a
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/memcmpeq-3.c
> @@ -0,0 +1,11 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2" } */
> +/* { dg-final { scan-assembler-not "__memcmpeq" } } */
> +
> +#include "memcmpeq.h"
> +
> +int
> +foo (const char *s1, const char *s2, size_t len)
> +{
> +  return memcmp (s1, s2, len);
> +}
> diff --git a/gcc/testsuite/c-c++-common/memcmpeq-4.c b/gcc/testsuite/c-c++-common/memcmpeq-4.c
> new file mode 100644
> index 00000000000..d59765894e7
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/memcmpeq-4.c
> @@ -0,0 +1,11 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2" } */
> +/* { dg-final { scan-assembler-not "__memcmpeq" } } */
> +
> +#include <stddef.h>
> +
> +int
> +foo (const char *s1, const char *s2, size_t len)
> +{
> +  return __builtin_memcmp_eq (s1, s2, len) != 0;
> +}
> diff --git a/gcc/testsuite/c-c++-common/memcmpeq-5.c b/gcc/testsuite/c-c++-common/memcmpeq-5.c
> new file mode 100644
> index 00000000000..cb12358cd87
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/memcmpeq-5.c
> @@ -0,0 +1,11 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2" } */
> +/* { dg-final { scan-assembler "__memcmpeq" } } */
> +
> +#include "memcmpeq.h"
> +
> +int
> +foo (const char *s1, const char *s2, size_t len)
> +{
> +  return __builtin___memcmpeq (s1, s2, len) != 0;
> +}
> diff --git a/gcc/testsuite/c-c++-common/memcmpeq-6.c b/gcc/testsuite/c-c++-common/memcmpeq-6.c
> new file mode 100644
> index 00000000000..8f983c05a44
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/memcmpeq-6.c
> @@ -0,0 +1,10 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2" } */
> +
> +#include <stddef.h>
> +
> +int
> +foo (const char *s1, const char *s2, size_t len)
> +{
> +  return __builtin___memcmpeq (s1, s2, len) != 0; /* { dg-error "__builtin___memcmpeq" } */
> +}
> diff --git a/gcc/testsuite/c-c++-common/memcmpeq.h b/gcc/testsuite/c-c++-common/memcmpeq.h
> new file mode 100644
> index 00000000000..3f731a4806b
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/memcmpeq.h
> @@ -0,0 +1,11 @@
> +#include <string.h>
> +
> +#if __cplusplus
> +extern "C" {
> +#endif
> +
> +extern int __memcmpeq (const void *, const void *, size_t);
> +
> +#if __cplusplus
> +}
> +#endif
> --
> 2.36.1
>
  

Patch

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index e6816d5c865..2254a597bec 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -7395,6 +7395,15 @@  expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
 	return target;
       break;
 
+    case BUILT_IN___MEMCMPEQ:
+      if (!builtin_decl_declared_p (BUILT_IN___MEMCMPEQ))
+	{
+	  error ("use of %<__builtin___memcmpeq ()%> without "
+		 "%<__memcmpeq%> prototype");
+	  return const0_rtx;
+	}
+      break;
+
     /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
        back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
        when changing it to a strcmp call.  */
@@ -7448,7 +7457,10 @@  expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
 	return target;
       if (fcode == BUILT_IN_MEMCMP_EQ)
 	{
-	  tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
+	  tree newdecl = builtin_decl_explicit
+	    (builtin_decl_declared_p (BUILT_IN___MEMCMPEQ)
+	     ? BUILT_IN___MEMCMPEQ
+	     : BUILT_IN_MEMCMP);
 	  TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
 	}
       break;
diff --git a/gcc/builtins.def b/gcc/builtins.def
index 005976f34e9..95642c6acdf 100644
--- a/gcc/builtins.def
+++ b/gcc/builtins.def
@@ -965,6 +965,9 @@  DEF_BUILTIN_STUB (BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX, "__builtin_alloca_with_ali
    equality with zero.  */
 DEF_BUILTIN_STUB (BUILT_IN_MEMCMP_EQ, "__builtin_memcmp_eq")
 
+/* Similar to BUILT_IN_MEMCMP_EQ, but is mapped to __memcmpeq.  */
+DEF_EXT_LIB_BUILTIN (BUILT_IN___MEMCMPEQ, "__memcmpeq", BT_FN_INT_CONST_PTR_CONST_PTR_SIZE, ATTR_PURE_NOTHROW_NONNULL_LEAF)
+
 /* An internal version of strcmp/strncmp, used when the result is only 
    tested for equality with zero.  */
 DEF_BUILTIN_STUB (BUILT_IN_STRCMP_EQ, "__builtin_strcmp_eq")
diff --git a/gcc/testsuite/c-c++-common/memcmpeq-1.c b/gcc/testsuite/c-c++-common/memcmpeq-1.c
new file mode 100644
index 00000000000..14622f0d765
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/memcmpeq-1.c
@@ -0,0 +1,11 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-final { scan-assembler "__memcmpeq" } } */
+
+#include "memcmpeq.h"
+
+int
+foo (const char *s1, const char *s2, size_t len)
+{
+  return __builtin_memcmp (s1, s2, len) != 0;
+}
diff --git a/gcc/testsuite/c-c++-common/memcmpeq-2.c b/gcc/testsuite/c-c++-common/memcmpeq-2.c
new file mode 100644
index 00000000000..f57f279f173
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/memcmpeq-2.c
@@ -0,0 +1,11 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-final { scan-assembler "__memcmpeq" } } */
+
+#include "memcmpeq.h"
+
+int
+foo (const char *s1, const char *s2, size_t len)
+{
+  return memcmp (s1, s2, len) == 0;
+}
diff --git a/gcc/testsuite/c-c++-common/memcmpeq-3.c b/gcc/testsuite/c-c++-common/memcmpeq-3.c
new file mode 100644
index 00000000000..2ca2131c23a
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/memcmpeq-3.c
@@ -0,0 +1,11 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-final { scan-assembler-not "__memcmpeq" } } */
+
+#include "memcmpeq.h"
+
+int
+foo (const char *s1, const char *s2, size_t len)
+{
+  return memcmp (s1, s2, len);
+}
diff --git a/gcc/testsuite/c-c++-common/memcmpeq-4.c b/gcc/testsuite/c-c++-common/memcmpeq-4.c
new file mode 100644
index 00000000000..d59765894e7
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/memcmpeq-4.c
@@ -0,0 +1,11 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-final { scan-assembler-not "__memcmpeq" } } */
+
+#include <stddef.h>
+
+int
+foo (const char *s1, const char *s2, size_t len)
+{
+  return __builtin_memcmp_eq (s1, s2, len) != 0;
+}
diff --git a/gcc/testsuite/c-c++-common/memcmpeq-5.c b/gcc/testsuite/c-c++-common/memcmpeq-5.c
new file mode 100644
index 00000000000..cb12358cd87
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/memcmpeq-5.c
@@ -0,0 +1,11 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-final { scan-assembler "__memcmpeq" } } */
+
+#include "memcmpeq.h"
+
+int
+foo (const char *s1, const char *s2, size_t len)
+{
+  return __builtin___memcmpeq (s1, s2, len) != 0;
+}
diff --git a/gcc/testsuite/c-c++-common/memcmpeq-6.c b/gcc/testsuite/c-c++-common/memcmpeq-6.c
new file mode 100644
index 00000000000..8f983c05a44
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/memcmpeq-6.c
@@ -0,0 +1,10 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+#include <stddef.h>
+
+int
+foo (const char *s1, const char *s2, size_t len)
+{
+  return __builtin___memcmpeq (s1, s2, len) != 0; /* { dg-error "__builtin___memcmpeq" } */
+}
diff --git a/gcc/testsuite/c-c++-common/memcmpeq.h b/gcc/testsuite/c-c++-common/memcmpeq.h
new file mode 100644
index 00000000000..3f731a4806b
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/memcmpeq.h
@@ -0,0 +1,11 @@ 
+#include <string.h>
+
+#if __cplusplus
+extern "C" {
+#endif
+
+extern int __memcmpeq (const void *, const void *, size_t);
+
+#if __cplusplus
+}
+#endif