From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by sourceware.org (Postfix) with ESMTPS id 7931B3858C2F for ; Tue, 5 Jul 2022 16:28:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7931B3858C2F Received: by mail-pg1-x530.google.com with SMTP id 23so11805622pgc.8 for ; Tue, 05 Jul 2022 09:28:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=UQZgnD0dqyysEXd5PF0FcWXmiM3Of8eYoQSGL/WPTOk=; b=u0O8W/j3tXh9DDG0kWCvMyOh01GPL2GDK9slFM4OfHpO8LvnbhE9n857ARFnsVVHmR 8HtngCKonAUMxNDWl7E6iXhVLSDRcCwlwVMaKjaU05mw/rJNG5qnv1F7vzcReVNSsFCp yU6odJ6FftE4AXx/o8lArBxoK/K5/pqDzpIjoe24o7n+gvH+VFTUOq5xWSIPKwiD0NRd PBLZqlihdPH3OCecWA5m0aioyDQFCN2581fNhIljoyQiCjAzmh3ryYH1+nM8Lg8dsj9+ 9URvh9bWTs/wbS3lqmbb6rdJGu1pzhQ+pr13EYDMQWfOSPCes/v4SfW29R+H8zov7NCj 3nZw== X-Gm-Message-State: AJIora8uG/cdtgCcm96yKq53z1VaaQYVzDlyHFLvozzFETYkaK8CvrMM i3nAjoOPPJxj2SddyOuvm8W/+BAic9oTFKp/i7PeJqFuAoU= X-Google-Smtp-Source: AGRyM1tuRRq35zdTBcvA+DHWzNp2rFvJV+F/Kh8+2++AqSUfS38yP2FdFJDtx2YJ5A7aBCZv28HqHsKxzWPOlDh5w6o= X-Received: by 2002:a63:c15:0:b0:411:f92a:8ec7 with SMTP id b21-20020a630c15000000b00411f92a8ec7mr17865605pgl.86.1657038520365; Tue, 05 Jul 2022 09:28:40 -0700 (PDT) MIME-Version: 1.0 References: <20220620154421.2411443-1-hjl.tools@gmail.com> In-Reply-To: From: "H.J. Lu" Date: Tue, 5 Jul 2022 09:28:04 -0700 Message-ID: Subject: Re: [PATCH v2] Enable __memcmpeq after seeing __memcmpeq prototype To: Richard Biener Cc: GCC Patches Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3024.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: , X-List-Received-Date: Tue, 05 Jul 2022 16:28:44 -0000 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. */ > > +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 called_as_built_in (tree); > > extern bool get_object_alignment_1 (tree, unsigned int *, > > unsigned HOST_WIDE_INT *); > > diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc > > index ae8990c138f..cc66d254017 100644 > > --- a/gcc/c/c-decl.cc > > +++ b/gcc/c/c-decl.cc > > @@ -2140,13 +2140,24 @@ diagnose_mismatched_decls (tree newdecl, tree olddecl, > > return false; > > } > > } > > - /* Warn about enum/integer type mismatches. They are compatible types > > - (C2X 6.7.2.2/5), but may pose portability problems. */ > > - else if (enum_and_int_p && TREE_CODE (newdecl) != TYPE_DECL) > > - warned = warning_at (DECL_SOURCE_LOCATION (newdecl), > > - OPT_Wenum_int_mismatch, > > - "conflicting types for %q+D due to enum/integer " > > - "mismatch; have %qT", newdecl, newtype); > > + else > > + { > > + /* Enable __memcmpeq after seeing its prototype. */ > > + if (TREE_CODE (olddecl) == FUNCTION_DECL > > + && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL) > > + && !C_DECL_DECLARED_BUILTIN (olddecl) > > + && DECL_FUNCTION_CODE (olddecl) == BUILT_IN___MEMCMPEQ) > > + have_memcmpeq_prototype = true; > > + > > + /* Warn about enum/integer type mismatches. They are compatible > > + types (C2X 6.7.2.2/5), but may pose portability problems. */ > > + if (enum_and_int_p && TREE_CODE (newdecl) != TYPE_DECL) > > + warned = warning_at (DECL_SOURCE_LOCATION (newdecl), > > + OPT_Wenum_int_mismatch, > > + "conflicting types for %q+D due to " > > + "enum/integer mismatch; have %qT", > > + newdecl, newtype); > > + } > > > > /* Redeclaration of a type is a constraint violation (6.7.2.3p1), > > but silently ignore the redeclaration if either is in a system > > diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc > > index 29fc36534c2..051e83e3540 100644 > > --- a/gcc/cp/decl.cc > > +++ b/gcc/cp/decl.cc > > @@ -1719,6 +1719,11 @@ duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden) > > > > type = cp_build_type_attribute_variant (type, attribs); > > TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type; > > + > > + /* Enable __memcmpeq after seeing its prototype. */ > > + if (fndecl_built_in_p (olddecl, BUILT_IN_NORMAL) > > + && DECL_FUNCTION_CODE (olddecl) == BUILT_IN___MEMCMPEQ) > > + have_memcmpeq_prototype = true; > > } > > > > /* If a function is explicitly declared "throw ()", propagate that to > > 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 > > + > > +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 > > + > > +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 > > + > > +#if __cplusplus > > +extern "C" { > > +#endif > > + > > +extern int __memcmpeq (const void *, const void *, size_t); > > + > > +#if __cplusplus > > +} > > +#endif > > -- > > 2.36.1 > > -- H.J.