From patchwork Thu Oct 7 22:14:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1538030 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=gotplt.org header.i=@gotplt.org header.a=rsa-sha1 header.s=gotplt.org header.b=ne4eAbAp; 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 (ip-8-43-85-97.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 (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HQQcr5Kpmz9sRN for ; Fri, 8 Oct 2021 09:15:40 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E002E3857813 for ; Thu, 7 Oct 2021 22:15:37 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from quail.birch.relay.mailchannels.net (quail.birch.relay.mailchannels.net [23.83.209.151]) by sourceware.org (Postfix) with ESMTPS id 404893858428 for ; Thu, 7 Oct 2021 22:14:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 404893858428 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 95D4D542399; Thu, 7 Oct 2021 22:14:47 +0000 (UTC) Received: from pdx1-sub0-mail-a17.g.dreamhost.com (100-96-17-22.trex.outbound.svc.cluster.local [100.96.17.22]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 148755421D1; Thu, 7 Oct 2021 22:14:47 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a17.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.17.22 (trex/6.4.3); Thu, 07 Oct 2021 22:14:47 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Madly-Plucky: 3009b32121dc4f30_1633644887334_1358701832 X-MC-Loop-Signature: 1633644887334:2400994570 X-MC-Ingress-Time: 1633644887333 Received: from pdx1-sub0-mail-a17.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a17.g.dreamhost.com (Postfix) with ESMTP id 6DB4583497; Thu, 7 Oct 2021 15:14:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gotplt.org; h=from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=gotplt.org; bh=uyjD330FVgNMM6xjoK9 tA++HnDw=; b=ne4eAbApuBFMh67xqvgsRxwdK5Gc6dsTC22kTBvXaVFnw1gK6l/ 4cd5PIqP2U7zlxqY3aHX813BF1yfkMnGToikEqliqnKIANY7lKg+q6e3jdTLp+Kj pYX1024yt4Ut/obn/8dj5QMl5pCzSAnke7A+h+T6X5ERrzGFdogTgErY= Received: from rhbox.redhat.com (unknown [1.186.223.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a17.g.dreamhost.com (Postfix) with ESMTPSA id 901F07F2D7; Thu, 7 Oct 2021 15:14:43 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a17 From: Siddhesh Poyarekar To: gcc-patches@gcc.gnu.org Subject: [PATCH 1/8] __builtin_dynamic_object_size: Recognize builtin name Date: Fri, 8 Oct 2021 03:44:25 +0530 Message-Id: <20211007221432.1029249-2-siddhesh@gotplt.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211007221432.1029249-1-siddhesh@gotplt.org> References: <20211007221432.1029249-1-siddhesh@gotplt.org> MIME-Version: 1.0 X-Spam-Status: No, score=-3038.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , Cc: jakub@redhat.com Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Recognize the __builtin_dynamic_object_size builtin, but simply replace it with -1 or 0 for now. gcc/ChangeLog: * builtins.c (expand_builtin, fold_builtin_2): Add BUILT_IN_DYN_OBJECT_SIZE. (fold_builtin_dyn_object_size): New function. (valid_object_size_args): New function. (fold_builtin_object_size): Use it. * builtins.def (BUILT_IN_DYN_OBJECT_SIZE): New builtin. --- gcc/builtins.c | 63 +++++++++++++++++++++++++++++++++++++++--------- gcc/builtins.def | 1 + 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/gcc/builtins.c b/gcc/builtins.c index 3e57eb03af0..894d62359b4 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -180,6 +180,7 @@ static rtx expand_builtin_memory_chk (tree, rtx, machine_mode, static void maybe_emit_chk_warning (tree, enum built_in_function); static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function); static tree fold_builtin_object_size (tree, tree); +static tree fold_builtin_dyn_object_size (tree, tree); unsigned HOST_WIDE_INT target_newline; unsigned HOST_WIDE_INT target_percent; @@ -7910,6 +7911,7 @@ expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode, return const0_rtx; case BUILT_IN_OBJECT_SIZE: + case BUILT_IN_DYN_OBJECT_SIZE: return expand_builtin_object_size (exp); case BUILT_IN_MEMCPY_CHK: @@ -9318,6 +9320,9 @@ fold_builtin_2 (location_t loc, tree expr, tree fndecl, tree arg0, tree arg1) case BUILT_IN_OBJECT_SIZE: return fold_builtin_object_size (arg0, arg1); + case BUILT_IN_DYN_OBJECT_SIZE: + return fold_builtin_dyn_object_size (arg0, arg1); + case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE: return fold_builtin_atomic_always_lock_free (arg0, arg1); @@ -9964,7 +9969,10 @@ fold_builtin_next_arg (tree exp, bool va_start_p) } -/* Expand a call EXP to __builtin_object_size. */ +/* Expand a call EXP to __builtin_object_size or + __builtin_dynamic_object_size. If the builtin survived up to this point + then it means we have failed to get an object size, so replace the call with + 0 or -1 depending on the object size type argument. */ static rtx expand_builtin_object_size (tree exp) @@ -10250,27 +10258,40 @@ maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode) access_write_only); } -/* Fold a call to __builtin_object_size with arguments PTR and OST, - if possible. */ +/* Validate arguments to __builtin_object_size and + __builtin_dynamic_object_size. If both arguments are valid, return the + object size type in OSTP. */ -static tree -fold_builtin_object_size (tree ptr, tree ost) +static bool +valid_object_size_args (const tree ptr, tree ost, int *ostp) { - unsigned HOST_WIDE_INT bytes; - int object_size_type; - if (!validate_arg (ptr, POINTER_TYPE) || !validate_arg (ost, INTEGER_TYPE)) - return NULL_TREE; + return false; STRIP_NOPS (ost); if (TREE_CODE (ost) != INTEGER_CST || tree_int_cst_sgn (ost) < 0 || compare_tree_int (ost, 3) > 0) - return NULL_TREE; + return false; - object_size_type = tree_to_shwi (ost); + *ostp = tree_to_shwi (ost); + + return true; +} + +/* Fold a call to __builtin_object_size with arguments PTR and OST, + if possible. */ + +static tree +fold_builtin_object_size (tree ptr, tree ost) +{ + unsigned HOST_WIDE_INT bytes; + int object_size_type; + + if (!valid_object_size_args (ptr, ost, &object_size_type)) + return NULL_TREE; /* __builtin_object_size doesn't evaluate side-effects in its arguments; if there are any side-effects, it returns (size_t) -1 for types 0 and 1 @@ -10297,6 +10318,26 @@ fold_builtin_object_size (tree ptr, tree ost) return NULL_TREE; } +/* Fold a call to __builtin_dynamic_object_size with arguments PTR and OST, + if possible. */ + +static tree +fold_builtin_dyn_object_size (tree ptr, tree ost) +{ + int object_size_type; + + if (!valid_object_size_args (ptr, ost, &object_size_type)) + return NULL_TREE; + + /* __builtin_dynamic_object_size doesn't evaluate side-effects in its + arguments; if there are any side-effects, it returns (size_t) -1 for types + 0 and 1 and (size_t) 0 for types 2 and 3. */ + if (TREE_SIDE_EFFECTS (ptr)) + return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0); + + return NULL_TREE; +} + /* Builtins with folding operations that operate on "..." arguments need special handling; we need to store the arguments in a convenient data structure before attempting any folding. Fortunately there are diff --git a/gcc/builtins.def b/gcc/builtins.def index 45a09b4d42d..ae94caab921 100644 --- a/gcc/builtins.def +++ b/gcc/builtins.def @@ -972,6 +972,7 @@ DEF_BUILTIN_STUB (BUILT_IN_STRNCMP_EQ, "__builtin_strncmp_eq") /* Object size checking builtins. */ DEF_GCC_BUILTIN (BUILT_IN_OBJECT_SIZE, "object_size", BT_FN_SIZE_CONST_PTR_INT, ATTR_CONST_NOTHROW_LEAF_LIST) +DEF_GCC_BUILTIN (BUILT_IN_DYN_OBJECT_SIZE, "dynamic_object_size", BT_FN_SIZE_CONST_PTR_INT, ATTR_NOTHROW_LEAF_LIST) DEF_EXT_LIB_BUILTIN (BUILT_IN_MEMCPY_CHK, "__memcpy_chk", BT_FN_PTR_PTR_CONST_PTR_SIZE_SIZE, ATTR_NOTHROW_NONNULL_LEAF) DEF_EXT_LIB_BUILTIN (BUILT_IN_MEMMOVE_CHK, "__memmove_chk", BT_FN_PTR_PTR_CONST_PTR_SIZE_SIZE, ATTR_NOTHROW_NONNULL_LEAF) DEF_EXT_LIB_BUILTIN (BUILT_IN_MEMPCPY_CHK, "__mempcpy_chk", BT_FN_PTR_PTR_CONST_PTR_SIZE_SIZE, ATTR_RETNONNULL_NOTHROW_LEAF)