From patchwork Tue Jul 5 16:27:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1652601 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=TXVr+cMn; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Lcp4H6TnGz9s07 for ; Wed, 6 Jul 2022 02:27:42 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7EF8A3858292 for ; Tue, 5 Jul 2022 16:27:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7EF8A3858292 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1657038459; bh=zMd9ZIlxPsb6rs0pc9Ijyau+m/CgD+rBfzzPDQHTKzg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=TXVr+cMnwrmVxV37KtlR3QBut67dN9Qhpi237rSkPPhPnWhmjQ4cqeVgE/F07L8LJ mxihZXaECYSXOfcXIncniX4ImswcCm4VAVN99sCGg91gVsTOKMtj4WQRNG0LejbXAa Ufap+Oj/NZDmVxBgo712JYnqJDnA9iUvoxwezsfA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by sourceware.org (Postfix) with ESMTPS id AEFE83858280 for ; Tue, 5 Jul 2022 16:27:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AEFE83858280 Received: by mail-pj1-x1033.google.com with SMTP id o15so8183401pjh.1 for ; Tue, 05 Jul 2022 09:27:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=zMd9ZIlxPsb6rs0pc9Ijyau+m/CgD+rBfzzPDQHTKzg=; b=TeMUt1bWYscAp3zTdscxk4cVv4Y3lKA9PNqWTCy5cKaHDi4s8MhOYlatSdvT7xXMGT BEWsayAljyo5KOqTX7RZXap/VeAuJiWoKn6q1VDBy7VJJ8tzqIfZ7thL1bY1FH4KgNmG ujzNUyCq205etj8Eh3RxwlbhSMVl7QzL7K3aGMfR6TIhGFEyPphk/wJW8cPqgGlYq9r7 4JFwmNt8GEzozNzUkYipkECEBK5qjZPQCEuiUxsWFVn7vUU+U37e/AzOsbdCX1vtUZ8Z RgzIjVlEK8LoPhE0AjJ6VWpMdv/Qe8TfjHhEkVlo50QOqrpRjuHidn7Lq3rm6OYZ0Wkl 788Q== X-Gm-Message-State: AJIora8xEKx4dm0Sejn6kyUHQ2zhk64AFJk9oNechAkr/eKfcNiSFT3Z 5aK8dNLFxK4OZYX5DD6sZeKsJvEypVk= X-Google-Smtp-Source: AGRyM1uWJywVQF379NvrQlIPFQsH9jQLZBpz9JAtu0CX1lKhn2RPxjHQYYiFvqaOGGTXj85OzG0tgQ== X-Received: by 2002:a17:903:240b:b0:16b:8e84:f22d with SMTP id e11-20020a170903240b00b0016b8e84f22dmr42609905plo.128.1657038437326; Tue, 05 Jul 2022 09:27:17 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.58.37.183]) by smtp.gmail.com with ESMTPSA id u10-20020a170902714a00b001693bd7427asm23734198plm.170.2022.07.05.09.27.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Jul 2022 09:27:16 -0700 (PDT) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 9722AC02EE; Tue, 5 Jul 2022 09:27:15 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH v3] Enable __memcmpeq after seeing __memcmpeq prototype Date: Tue, 5 Jul 2022 09:27:15 -0700 Message-Id: <20220705162715.133539-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3027.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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-Patchwork-Original-From: "H.J. Lu via Gcc-patches" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "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 (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; + /* 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 + +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