From patchwork Fri Sep 20 16:40:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1987972 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gotplt.org header.i=@gotplt.org header.a=rsa-sha256 header.s=dreamhost header.b=Z6A23Yov; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X9J9X3PWJz1y1t for ; Sat, 21 Sep 2024 02:43:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4E15C3858408 for ; Fri, 20 Sep 2024 16:43:26 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from cyan.elm.relay.mailchannels.net (cyan.elm.relay.mailchannels.net [23.83.212.47]) by sourceware.org (Postfix) with ESMTPS id 2EFF23858C5F; Fri, 20 Sep 2024 16:40:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2EFF23858C5F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2EFF23858C5F Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.212.47 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1726850450; cv=pass; b=gtDfPCmfKyRGutKLJCzurzwGe81HDY6M9/4UzRrSqCQb3MIGRSR5/wpsEWVNDgoAuxjOAanEMsYUf8ENuNpm9T/ghKPfTYrGD6as+DVnrUoGH35v+OSJiCrnwBCYtZhy7jFAKBK5XQetYkqA3pSQb5HAtcsqYyTwEFyfPowh1yY= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1726850450; c=relaxed/simple; bh=PbrgRQ9maCeh1cOoQGss0rlAqHq667jpYVcPFmdMTuc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=jaFY4nDyBYw8TQZ+bPr0ZG2haUZu6zJTybp4UrPv4f0wPhXz2WZLUt/Q7/VUD5Y55abGnrArLkfMDaWfyfIVmFJAgMLFodwXEkCq1nNUFN8O+mTZATrjxe6+q7TG7/RmZuS5YNVkviKfwHU3R1AR+SPhRXkQUy34BTPrlu48wHE= ARC-Authentication-Results: i=2; server2.sourceware.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 35CDF45D90; Fri, 20 Sep 2024 16:40:47 +0000 (UTC) Received: from pdx1-sub0-mail-a312.dreamhost.com (trex-14.trex.outbound.svc.cluster.local [100.99.68.57]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id DA78A42C21; Fri, 20 Sep 2024 16:40:46 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1726850446; a=rsa-sha256; cv=none; b=3ak3KO2dDstL3vZtkUyHS5CVgPAeXSfpvkH1F4itO3NIH8ZTZDw2J3Y33Nk/gr8wdtZ4qv Zic78UG1T9catAWv++dEAiM8L1VAPLPXmN2cRtbOe8EV68BgUidGSRwXpa7+nV7HoFjDcN A4Td5IGYBb3Fev4hnlpU/etkxedHV0BjM6cRcT/cIg4Ba34yH0shjEjM8lYaaQBIfSJAR4 DLFvo1c86NWHX5Nzgm1vODyFwdAZNdLGcoA6DVJAko3JVapvZRDEk7HOXSPAGsxagy5PcA HNIQ1gWUGR8PhxhHKDc17CFEcecVMWLAcgRsQkAwAG2SfnqOFHgEgjC38rlwkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1726850446; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=chSczJzw2Qgiu/pXpGFWRsyl8snBqFAoJljQ4tO9jiM=; b=v80IWEB3cBLdW4v8HQCm3AIkrfo+J6JgKy16S/5rjUiu5OvnAa+p3aP8XuxUa3GeJipdQV DPimWnawkXKHVxV2yqDpdCg1a6IzSmmA35uwoo/7EnN8GrQ3bW9/9AFuaY7eE8xPq+7GKd aFzHDzPBfzwwZ/tDfliabErBGXXbhvXy8F2klHbQWwRKYDTBkOmQQrsRv/ioEp1JCMzU+8 NTHg5NeV6umHax34NJ+6WWB9N1z8zIvwy1c99o29fYokkuKTPMjmudaFioYZKfv4A4cL5P HZnuNaumuJVhQtsG1IqGOsk8I3RgQ6banlKqpGslJ/ly0AMQI6t9Tq5DZLseAg== ARC-Authentication-Results: i=1; rspamd-5b46bcd97f-lhx9s; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Inform-Duck: 3640c116574cf8ab_1726850447130_1118491798 X-MC-Loop-Signature: 1726850447130:3529821372 X-MC-Ingress-Time: 1726850447130 Received: from pdx1-sub0-mail-a312.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.99.68.57 (trex/7.0.2); Fri, 20 Sep 2024 16:40:47 +0000 Received: from fedora.redhat.com (bras-base-toroon4859w-grc-66-76-71-1-128.dsl.bell.ca [76.71.1.128]) (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) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a312.dreamhost.com (Postfix) with ESMTPSA id 4X9J6Q25dKzRH; Fri, 20 Sep 2024 09:40:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gotplt.org; s=dreamhost; t=1726850446; bh=chSczJzw2Qgiu/pXpGFWRsyl8snBqFAoJljQ4tO9jiM=; h=From:To:Cc:Subject:Date:Content-Transfer-Encoding; b=Z6A23YovFBA3QIFmNi0HKitLvg43+RZTX6AVJQ7rqmGetMaoEaRfyrYEEutdUOQBM vyV3UwgyxF9ep/ok7psh8ge2jSnuztCoBjNBzjVoy/NPygYINN6hSxjGRT53zh5H2X 0Vaw7X0fKoNoyAum9BtDboxXOBLWdJGfWiMc/jW03OYf0mLHDQtCav6IPHMULDUaO+ 3WHwijHNxFBDqqR+puc0HsdItDSuVGmrFNq3sHWCLiDJbzZ1DaaFtzWXIsb6GK85o+ C0eqL6je6f5GEqIuJFljiB0JURSPYwGsXq5fuxybC/u8JQyUsrsNBS7GeubSclq9Xf tAFU4OL4BblyQ== From: Siddhesh Poyarekar To: gcc-patches@gcc.gnu.org Cc: pinskia@gcc.gnu.org Subject: [PATCH v2 3/4] tree-object-size: Handle PHI + CST type offsets Date: Fri, 20 Sep 2024 12:40:28 -0400 Message-ID: <20240920164029.63843-4-siddhesh@gotplt.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240920164029.63843-1-siddhesh@gotplt.org> References: <20240914123049.2746225-1-siddhesh@gotplt.org> <20240920164029.63843-1-siddhesh@gotplt.org> MIME-Version: 1.0 X-Spam-Status: No, score=-3036.3 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Propogate constant additions and subtractions to an offset when computing an estimate for a PHI of constants. gcc/ChangeLog: PR tree-optimization/PR116556 * tree-object-size.cc (try_collapsing_offset): New parameters CST and CODE. Use them to propagate PLUS_EXPR and MINUS_EXPR through recursively calling TRY_COLLAPSE_OFFSET. (plus_stmt_object_size): Adjust call to TRY_COLLAPSE_OFFSET. * testsuite/gcc.dg/builtin-object-size-1.c (test13): Test constant addition and subtraction. * testsuite/gcc.dg/builtin-object-size-3.c (test13): Likewise. Signed-off-by: Siddhesh Poyarekar --- gcc/testsuite/gcc.dg/builtin-object-size-1.c | 20 +++++++ gcc/testsuite/gcc.dg/builtin-object-size-3.c | 20 +++++++ gcc/tree-object-size.cc | 58 ++++++++++++++++---- 3 files changed, 86 insertions(+), 12 deletions(-) diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-1.c b/gcc/testsuite/gcc.dg/builtin-object-size-1.c index 1f5cd5d99a1..6ffe12be683 100644 --- a/gcc/testsuite/gcc.dg/builtin-object-size-1.c +++ b/gcc/testsuite/gcc.dg/builtin-object-size-1.c @@ -769,6 +769,26 @@ test13 (unsigned cond) if (__builtin_object_size (&p[t], 0) != 9) FAIL (); #endif + + t += 4; + +#ifdef __builtin_object_size + if (__builtin_object_size (&p[t], 0) != (cond ? 1 : 5)) + FAIL (); +#else + if (__builtin_object_size (&p[t], 0) != 5) + FAIL (); +#endif + + t -= 5; + +#ifdef __builtin_object_size + if (__builtin_object_size (&p[t], 0) != (cond ? 6 : 10)) + FAIL (); +#else + if (__builtin_object_size (&p[t], 0) != 10) + FAIL (); +#endif } int diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-3.c b/gcc/testsuite/gcc.dg/builtin-object-size-3.c index 7fad106fc27..c51cb69c01b 100644 --- a/gcc/testsuite/gcc.dg/builtin-object-size-3.c +++ b/gcc/testsuite/gcc.dg/builtin-object-size-3.c @@ -777,6 +777,26 @@ test13 (unsigned cond) if (__builtin_object_size (&p[t], 2) != 5) FAIL (); #endif + + t += 4; + +#ifdef __builtin_object_size + if (__builtin_object_size (&p[t], 2) != (cond ? 1 : 5)) + FAIL (); +#else + if (__builtin_object_size (&p[t], 2) != 1) + FAIL (); +#endif + + t -= 5; + +#ifdef __builtin_object_size + if (__builtin_object_size (&p[t], 2) != (cond ? 6 : 10)) + FAIL (); +#else + if (__builtin_object_size (&p[t], 2) != 6) + FAIL (); +#endif } int diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 2dfc28407ab..1b569c3d12b 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -1473,7 +1473,7 @@ merge_object_sizes (struct object_size_info *osi, tree dest, tree orig) with all of its targets are constants. */ static tree -try_collapsing_offset (tree op, int object_size_type) +try_collapsing_offset (tree op, tree cst, tree_code code, int object_size_type) { gcc_assert (!(object_size_type & OST_DYNAMIC)); @@ -1485,13 +1485,41 @@ try_collapsing_offset (tree op, int object_size_type) switch (gimple_code (stmt)) { case GIMPLE_ASSIGN: - /* Peek through casts. */ - if (gimple_assign_rhs_code (stmt) == NOP_EXPR) + switch (gimple_assign_rhs_code (stmt)) { - tree ret = try_collapsing_offset (gimple_assign_rhs1 (stmt), - object_size_type); - if (TREE_CODE (ret) == INTEGER_CST) - return ret; + /* Peek through casts. */ + case NOP_EXPR: + { + tree ret = try_collapsing_offset (gimple_assign_rhs1 (stmt), + NULL_TREE, NOP_EXPR, + object_size_type); + if (TREE_CODE (ret) == INTEGER_CST) + return ret; + break; + } + /* Propagate constant offsets to PHI expressions. */ + case PLUS_EXPR: + case MINUS_EXPR: + { + tree rhs1 = gimple_assign_rhs1 (stmt); + tree rhs2 = gimple_assign_rhs2 (stmt); + tree ret = NULL_TREE; + + if (TREE_CODE (rhs1) == INTEGER_CST) + ret = try_collapsing_offset (rhs2, rhs1, + gimple_assign_rhs_code (stmt), + object_size_type); + else if (TREE_CODE (rhs2) == INTEGER_CST) + ret = try_collapsing_offset (rhs1, rhs2, + gimple_assign_rhs_code (stmt), + object_size_type); + + if (ret && TREE_CODE (ret) == INTEGER_CST) + return ret; + break; + } + default: + break; } break; case GIMPLE_PHI: @@ -1507,14 +1535,20 @@ try_collapsing_offset (tree op, int object_size_type) if (TREE_CODE (rhs) != INTEGER_CST) return op; + if (cst) + rhs = fold_build2 (code, sizetype, + fold_convert (ptrdiff_type_node, rhs), + fold_convert (ptrdiff_type_node, cst)); + else + rhs = fold_convert (ptrdiff_type_node, rhs); + /* Note that this is the *opposite* of what we usually do with sizes, because the maximum offset estimate here will give us a minimum size estimate and vice versa. */ - enum tree_code code = (object_size_type & OST_MINIMUM - ? MAX_EXPR : MIN_EXPR); + enum tree_code selection_code = (object_size_type & OST_MINIMUM + ? MAX_EXPR : MIN_EXPR); - off = fold_build2 (code, ptrdiff_type_node, off, - fold_convert (ptrdiff_type_node, rhs)); + off = fold_build2 (selection_code, ptrdiff_type_node, off, rhs); } return fold_convert (sizetype, off); } @@ -1558,7 +1592,7 @@ plus_stmt_object_size (struct object_size_info *osi, tree var, gimple *stmt) return false; if (!(object_size_type & OST_DYNAMIC) && TREE_CODE (op1) != INTEGER_CST) - op1 = try_collapsing_offset (op1, object_size_type); + op1 = try_collapsing_offset (op1, NULL_TREE, NOP_EXPR, object_size_type); /* Handle PTR + OFFSET here. */ if (size_valid_p (op1, object_size_type)