Re: [PATCH v2] Enable __memcmpeq after seeing __memcmpeq prototype

2022-07-05 Thread H.J. Lu via Gcc-patches
On Fri, Jul 1, 2022 at 12:51 AM Richard Biener
 wrote:
>
> On Mon, Jun 20, 2022 at 5:44 PM H.J. Lu  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
>
> Can you instead use builtin_decl_declared_p (), see how frontends
> set that via set_builtin_decl_declared_p?

The v3 patch is at

https://gcc.gnu.org/pipermail/gcc-patches/2022-July/597861.html

Thanks.

> > gcc/
> >
> > * builtins.cc (have_memcmpeq_prototype): New.
> > (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.
> > * builtins.h (have_memcmpeq_prototype): New.
> >
> > gcc/c/
> >
> > * c-decl.cc (diagnose_mismatched_decls): Set
> > have_memcmpeq_prototype to true after seeing __memcmpeq prototype.
> >
> > gcc/cp/
> >
> > *  decl.cc (duplicate_decls): Set have_memcmpeq_prototype to true
> > after seeing __memcmpeq prototype.
> >
> > 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 | 17 -
> >  gcc/builtins.def|  3 +++
> >  gcc/builtins.h  |  3 +++
> >  gcc/c/c-decl.cc | 25 ++---
> >  gcc/cp/decl.cc  |  5 +
> >  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 +++
> >  12 files changed, 121 insertions(+), 8 deletions(-)
> >  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 971b18c3745..96e283e5847 100644
> > --- a/gcc/builtins.cc
> > +++ b/gcc/builtins.cc
> > @@ -104,6 +104,9 @@ builtin_info_type builtin_info[(int)END_BUILTINS];
> >  /* Non-zero if __builtin_constant_p should be folded right away.  */
> >  bool force_folding_builtin_constant_p;
> >
> > +/* True if there is a __memcmpeq prototype.  */
> > +bool have_memcmpeq_prototype;
> > +
> >  static int target_char_cast (tree, char *);
> >  static int apply_args_size (void);
> >  static int apply_result_size (void);
> > @@ -7392,6 +7395,15 @@ expand_builtin (tree exp, rtx target, rtx subtarget, 
> > machine_mode mode,
> > return target;
> >break;
> >
> > +case BUILT_IN___MEMCMPEQ:
> > +  if (!have_memcmpeq_prototype)
> > +   {
> > + 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.  */
> > @@ -7445,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
> > +   (have_memcmpeq_prototype
> > +? 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. 

Re: [PATCH v2] Enable __memcmpeq after seeing __memcmpeq prototype

2022-07-01 Thread Richard Biener via Gcc-patches
On Mon, Jun 20, 2022 at 5:44 PM H.J. Lu  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

Can you instead use builtin_decl_declared_p (), see how frontends
set that via set_builtin_decl_declared_p?

> gcc/
>
> * builtins.cc (have_memcmpeq_prototype): New.
> (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.
> * builtins.h (have_memcmpeq_prototype): New.
>
> gcc/c/
>
> * c-decl.cc (diagnose_mismatched_decls): Set
> have_memcmpeq_prototype to true after seeing __memcmpeq prototype.
>
> gcc/cp/
>
> *  decl.cc (duplicate_decls): Set have_memcmpeq_prototype to true
> after seeing __memcmpeq prototype.
>
> 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 | 17 -
>  gcc/builtins.def|  3 +++
>  gcc/builtins.h  |  3 +++
>  gcc/c/c-decl.cc | 25 ++---
>  gcc/cp/decl.cc  |  5 +
>  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 +++
>  12 files changed, 121 insertions(+), 8 deletions(-)
>  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 971b18c3745..96e283e5847 100644
> --- a/gcc/builtins.cc
> +++ b/gcc/builtins.cc
> @@ -104,6 +104,9 @@ builtin_info_type builtin_info[(int)END_BUILTINS];
>  /* Non-zero if __builtin_constant_p should be folded right away.  */
>  bool force_folding_builtin_constant_p;
>
> +/* True if there is a __memcmpeq prototype.  */
> +bool have_memcmpeq_prototype;
> +
>  static int target_char_cast (tree, char *);
>  static int apply_args_size (void);
>  static int apply_result_size (void);
> @@ -7392,6 +7395,15 @@ expand_builtin (tree exp, rtx target, rtx subtarget, 
> machine_mode mode,
> return target;
>break;
>
> +case BUILT_IN___MEMCMPEQ:
> +  if (!have_memcmpeq_prototype)
> +   {
> + 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.  */
> @@ -7445,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
> +   (have_memcmpeq_prototype
> +? 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/builtins.h b/gcc/builtins.h
> index 

[PATCH v2] Enable __memcmpeq after seeing __memcmpeq prototype

2022-06-20 Thread H.J. Lu via Gcc-patches
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 (have_memcmpeq_prototype): New.
(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.
* builtins.h (have_memcmpeq_prototype): New.

gcc/c/

* c-decl.cc (diagnose_mismatched_decls): Set
have_memcmpeq_prototype to true after seeing __memcmpeq prototype.

gcc/cp/

*  decl.cc (duplicate_decls): Set have_memcmpeq_prototype to true
after seeing __memcmpeq prototype.

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 | 17 -
 gcc/builtins.def|  3 +++
 gcc/builtins.h  |  3 +++
 gcc/c/c-decl.cc | 25 ++---
 gcc/cp/decl.cc  |  5 +
 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 +++
 12 files changed, 121 insertions(+), 8 deletions(-)
 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 971b18c3745..96e283e5847 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -104,6 +104,9 @@ builtin_info_type builtin_info[(int)END_BUILTINS];
 /* Non-zero if __builtin_constant_p should be folded right away.  */
 bool force_folding_builtin_constant_p;
 
+/* True if there is a __memcmpeq prototype.  */
+bool have_memcmpeq_prototype;
+
 static int target_char_cast (tree, char *);
 static int apply_args_size (void);
 static int apply_result_size (void);
@@ -7392,6 +7395,15 @@ expand_builtin (tree exp, rtx target, rtx subtarget, 
machine_mode mode,
return target;
   break;
 
+case BUILT_IN___MEMCMPEQ:
+  if (!have_memcmpeq_prototype)
+   {
+ 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.  */
@@ -7445,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
+   (have_memcmpeq_prototype
+? 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/builtins.h b/gcc/builtins.h
index 5ad830c9fbf..e3e80b33f6d 100644
--- a/gcc/builtins.h
+++ b/gcc/builtins.h
@@ -49,6 +49,9 @@ extern struct target_builtins *this_target_builtins;
 /* Non-zero if __builtin_constant_p should be folded right away.  */
 extern bool force_folding_builtin_constant_p;
 
+/* True if there is a __memcmpeq prototype.  */
+extern bool have_memcmpeq_prototype;
+
 extern bool