From patchwork Fri Sep 20 16:40:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 97769 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5E177385C6C7 for ; Fri, 20 Sep 2024 16:41:56 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from buffalo.birch.relay.mailchannels.net (buffalo.birch.relay.mailchannels.net [23.83.209.24]) by sourceware.org (Postfix) with ESMTPS id 2C1FF385840B; Fri, 20 Sep 2024 16:40:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2C1FF385840B 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 2C1FF385840B Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.209.24 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1726850450; cv=pass; b=SnKL/0Szn3R/NtVKruuPGtF6S87F3qhHU6wm22nm/BC5c3JYlJSDojJ7obI8ekQoKq1/KT675SfMwrluz1Q77Im2xzSdv0IIkFrqnIFoa4MPlQMV6xp5Zv9xVksOsMHKSv0yVeqoavV+4/I+FhkUi9Iyg7PR2rPqV6P5y4jIs1E= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1726850450; c=relaxed/simple; bh=1Trc7CEiHbWZuTP0X1xJwbLWoxBTF+lOiM8XmZ2IgIQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=nTkcYDYhL4fAUAh21NqitViOX0jVMY9HqLevM690P7qygp3/ZX9ioh2ZdhWAlL9Xtp3K9gKBgnak53JiLdtWfH/o5a8CdOXnslY/dXLPLsCWaDgzlohWFeS+VrlKECBTipRHfmXks1n4jEpxSVjxr/q0Q3cbNF5+o5xalcuZBqI= 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 31CAE16473B; Fri, 20 Sep 2024 16:40:46 +0000 (UTC) Received: from pdx1-sub0-mail-a312.dreamhost.com (trex-9.trex.outbound.svc.cluster.local [100.99.97.159]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id D9BD6164D95; Fri, 20 Sep 2024 16:40:45 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1726850445; a=rsa-sha256; cv=none; b=B2rtGZ1OLZRYC8mKYJJyYq7rNYiHa6wS3ON4nQEcnaghm5wvokUbcOsyS2Lb9qgxP402lr YMo6+N/M2flFhZX5E+heldqmn9JpIld/Km4z7WSlDnJVAqbvjlN6B8pN0XAmrRjU4y/IcV 4Q9PHQmWht1uVY8WzZgljChQsQ1bjfDiWLi9vsKpzPdv9/GLztzhHXGg8DCqJPpOAQs9st 5P3whjhKDJESNbwtlYfXRr5u/OlNM2MRUhpUXss5KoB5tinL8VUlZdLSnyDrdC4YW7xQCa +dFzFFtHmj6qKjr/WaXE+RS93ztobSa26+GzF2Ew6hgIAymHLLF1m3Co1iDA5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1726850445; 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=xJGGB6Hwxom4go72+H3/PeMKWPHXY41ITwxQ2wEar3g=; b=KLvCzax7xpY7DtwWZv23v00FAZV9RQb0LkZjobTGOYz3oCV+s3cN2Y+n9Z8t5SNgvrDmsn GLp+4w1Ti/yNZG2hFIQt1rRdiqoiQh+3FfvqXmDIA1w1vc5sxU2xSn7dF79884pqq1hrvB G2M5WwXIPCmCTOtho03uc105q0c4UqOKCAyxvrY/lZNK5zePxp1Mskw9CpebjY7YhbmHL8 j+Z1+84APPcvYVpgQaV4cIUHibo+UAjGCEEmbBvqrRaDeYEtR6zro60ok670zMV30nbr9n RJbM8RKIGqCaP2Cp0ai5HU8bVeN1/tuNfkUJ6RO2DKgqllumSrdc6g1vkDyH4A== 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-Desert-Wiry: 11e14d13547b4f25_1726850446105_872672388 X-MC-Loop-Signature: 1726850446105:1526565235 X-MC-Ingress-Time: 1726850446105 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.97.159 (trex/7.0.2); Fri, 20 Sep 2024 16:40:46 +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 4X9J6P3rpyzTP; Fri, 20 Sep 2024 09:40:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gotplt.org; s=dreamhost; t=1726850445; bh=xJGGB6Hwxom4go72+H3/PeMKWPHXY41ITwxQ2wEar3g=; h=From:To:Cc:Subject:Date:Content-Transfer-Encoding; b=nFZBIZyMPAw+ve4710aFfo9Ratu8NRlclOrevrLoxWUgQUg8zQ1yr9xpkJ7k1TO0x YudCuv/twEUkxOhK/VtoYoXOayUPKMr9SPWqbvZ4JpII59MNVuQkWGtU3yeObQ/QQj sTWQyzaSKqCEHULJ2dz0Kf3t5TWlplVPsZ2hmCXyr+UvM2bTxFeVA+redBf/BOwLpX x+b9FmAVLB1TMiSKbesx4kkgluPzoA9qEnwbvohsdmyss3/0z9490pWf9j1f/DiMeg lE04oVitAdkY+PJzwi2AEnDCyFpTvOxt/D2FoussIW7Y6ishgJ89qtPPrZhiwlAgRQ hXZRZL6HqZpbA== From: Siddhesh Poyarekar To: gcc-patches@gcc.gnu.org Cc: pinskia@gcc.gnu.org Subject: [PATCH v2 1/4] tree-object-size: use size_for_offset in more cases Date: Fri, 20 Sep 2024 12:40:26 -0400 Message-ID: <20240920164029.63843-2-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.8 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.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~patchwork=sourceware.org@gcc.gnu.org When wholesize != size, there is a reasonable opportunity for static object sizes also to be computed using size_for_offset, so use that. gcc/ChangeLog: * tree-object-size.cc (plus_stmt_object_size): Call SIZE_FOR_OFFSET for some negative offset cases. * testsuite/gcc.dg/builtin-object-size-3.c (test9): Adjust test. * testsuite/gcc.dg/builtin-object-size-4.c (test8): Likewise. --- gcc/testsuite/gcc.dg/builtin-object-size-3.c | 6 +++--- gcc/testsuite/gcc.dg/builtin-object-size-4.c | 6 +++--- gcc/tree-object-size.cc | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-3.c b/gcc/testsuite/gcc.dg/builtin-object-size-3.c index 3f58da3d500..ec2c62c9640 100644 --- a/gcc/testsuite/gcc.dg/builtin-object-size-3.c +++ b/gcc/testsuite/gcc.dg/builtin-object-size-3.c @@ -574,7 +574,7 @@ test9 (unsigned cond) if (__builtin_object_size (&p[-4], 2) != (cond ? 6 : 10)) FAIL (); #else - if (__builtin_object_size (&p[-4], 2) != 0) + if (__builtin_object_size (&p[-4], 2) != 6) FAIL (); #endif @@ -585,7 +585,7 @@ test9 (unsigned cond) if (__builtin_object_size (p, 2) != ((cond ? 2 : 6) + cond)) FAIL (); #else - if (__builtin_object_size (p, 2) != 0) + if (__builtin_object_size (p, 2) != 2) FAIL (); #endif @@ -598,7 +598,7 @@ test9 (unsigned cond) != sizeof (y) - __builtin_offsetof (struct A, c) - 8 + cond) FAIL (); #else - if (__builtin_object_size (p, 2) != 0) + if (__builtin_object_size (p, 2) != sizeof (y) - __builtin_offsetof (struct A, c) - 8) FAIL (); #endif } diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-4.c b/gcc/testsuite/gcc.dg/builtin-object-size-4.c index b3eb36efb74..7bcd24c4150 100644 --- a/gcc/testsuite/gcc.dg/builtin-object-size-4.c +++ b/gcc/testsuite/gcc.dg/builtin-object-size-4.c @@ -482,7 +482,7 @@ test8 (unsigned cond) if (__builtin_object_size (&p[-4], 3) != (cond ? 6 : 10)) FAIL (); #else - if (__builtin_object_size (&p[-4], 3) != 0) + if (__builtin_object_size (&p[-4], 3) != 6) FAIL (); #endif @@ -493,7 +493,7 @@ test8 (unsigned cond) if (__builtin_object_size (p, 3) != ((cond ? 2 : 6) + cond)) FAIL (); #else - if (__builtin_object_size (p, 3) != 0) + if (__builtin_object_size (p, 3) != 2) FAIL (); #endif @@ -505,7 +505,7 @@ test8 (unsigned cond) if (__builtin_object_size (p, 3) != sizeof (y.c) - 8 + cond) FAIL (); #else - if (__builtin_object_size (p, 3) != 0) + if (__builtin_object_size (p, 3) != sizeof (y.c) - 8) FAIL (); #endif } diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 6544730e153..f8fae0cbc82 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -1527,7 +1527,7 @@ plus_stmt_object_size (struct object_size_info *osi, tree var, gimple *stmt) if (size_unknown_p (bytes, 0)) ; else if ((object_size_type & OST_DYNAMIC) - || compare_tree_int (op1, offset_limit) <= 0) + || bytes != wholesize || compare_tree_int (op1, offset_limit) <= 0) bytes = size_for_offset (bytes, op1, wholesize); /* In the static case, with a negative offset, the best estimate for minimum size is size_unknown but for maximum size, the wholesize is a From patchwork Fri Sep 20 16:40:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 97768 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1C3813858420 for ; Fri, 20 Sep 2024 16:41:35 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from beige.elm.relay.mailchannels.net (beige.elm.relay.mailchannels.net [23.83.212.16]) by sourceware.org (Postfix) with ESMTPS id A0E053858C66; Fri, 20 Sep 2024 16:40:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A0E053858C66 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 A0E053858C66 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.212.16 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1726850449; cv=pass; b=Aabxw3Dmd5XtJez6omUUzEZcV6s7GRM3LbGQzPFAyJSzYbtxz5wq21gHShLoQOZ/CRn23DO3h/nydKfZSCu31KTkK1DRFTrK6+98Kck748UmvVyIK8PPXiO1tXjnKUFI/Tt8KCksCQzDj+t70Qz+LacVpVv7yvO4WZp1+ruDZNw= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1726850449; c=relaxed/simple; bh=EGvKiW0fiyuqdPSoSvWgSNH5B+dlLWylFj+O+yWTi1E=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZU1/x1TqJMHmqqT6Zv3yp4CEmX4DeLxATyPkQB7vY6wlhg/I4RjyYzPADpsl4AA+X8t9gYO/4m6pi7yR9XJw4DgKmr5mIU6f0bRTe4gmmo1J0GFsL+THjIX0lvzldLhnHW8oadxrdqRTcvxI/aPPHm6wsyRTaIHIu8C14F1XKRk= 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 9CA691A6383; Fri, 20 Sep 2024 16:40:46 +0000 (UTC) Received: from pdx1-sub0-mail-a312.dreamhost.com (100-99-68-57.trex-nlb.outbound.svc.cluster.local [100.99.68.57]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 410691A236A; 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=6F+3R1X+FHYJuMEu5zPOSLIbHUvcnarVP4kxdJ74/XITyGpxlO6xEfmdfertEi3NXgkZsv oQSVzRV4w/+oco7HGQ+xo96SsruvrnQlAIqXzSGxQ6+LHIRAO+BuxhC/iIbYjHBMm1iSqv 3L/ktLM9WVbtnD6StcRc1N51TP7Wexy8hQDrbrVCsJQ4uDDtViANn2dj/T4dMkE+o853F9 S7VS109rag+GnWIXx1YYaOGM/97eZq2ZXo6gwR/H/fAUcspI53ToZCJ3cYfieMvGbLzaOd tcHmjbCV/O0ZT+820RZ/xi1rpqUXKu/GLFL/atVept3qqiquvucVlsLGgf9fIw== 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=o9TDWwqXoyPzTrjXb9xur51IlQFiX6wirNjEqlWR4dY=; b=IISlYxNa5B013dYw5xhcgRMMIfvb8tFayN4yK8Eu1/EbSphmsoTdYQxW5vJD1n6bVhaUO+ NIdpfMvSrpNXJyPW6NY4XX6REkytP43cNzrF1jde+FtMTWJdiLfNXMuF0XgVBQX9HLH7y+ Ifp8WZuAS8pxbDyCmUAOu/XwTCg7Lh4ajSQylPoSkTIlO5oK5zsZKCG74zyVGDTcKSHiiG 0shjhR225U19hL+aXcAjVlLDq3ORAT2PpY9YI0NGy0gj539U4UPZz/BKYSNoFqX+jTsZG3 bAh+efB0PQD7e4U5fT+IMkmzD0NvpT9Ta/SDZkDRluAMPjkjvLOm7JAZ+S1D/A== ARC-Authentication-Results: i=1; rspamd-5b46bcd97f-5x2s4; 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-Cellar-Illustrious: 4c0c190a42770ac4_1726850446545_658108161 X-MC-Loop-Signature: 1726850446545:3897719161 X-MC-Ingress-Time: 1726850446545 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:46 +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 4X9J6P6X75zS4; Fri, 20 Sep 2024 09:40:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gotplt.org; s=dreamhost; t=1726850446; bh=o9TDWwqXoyPzTrjXb9xur51IlQFiX6wirNjEqlWR4dY=; h=From:To:Cc:Subject:Date:Content-Transfer-Encoding; b=wKrreNv1NBJpp/UQjpqpSPtfU8sMMDi8KlOIJfzKp9oLYgGnzKW00HmG9sOEOgREf uSCytmFhv2R68mMg6FREnSFQZ+IkDnhwrHT727ZtET2RKZkwzDDyJkdeSYSy9B3wX0 cVpC1v11/Kn9PVbme0+N8iB747WXfihYR4WIfaHdYVADSfD8zOj42N4SKi95HxwuLG YuGYWX7DFyU9EYB9Y+Qt+ddb+3KBWLh9alkpRkKZzRRzK1HqmuSQfgSdHPE8oYEXvn ykKYJAtfAroNyZF7TazLAIrtQS0lZasLQaU0KWKguozOzMjoQXAwp+z1/bC/VQFpMK hFo+FrQMn7wTg== From: Siddhesh Poyarekar To: gcc-patches@gcc.gnu.org Cc: pinskia@gcc.gnu.org Subject: [PATCH v2 2/4] tree-object-size: Fold PHI node offsets with constants [PR116556] Date: Fri, 20 Sep 2024 12:40:27 -0400 Message-ID: <20240920164029.63843-3-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.8 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.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~patchwork=sourceware.org@gcc.gnu.org In PTR + OFFSET cases, try harder to see if the target offset could result in a constant. Specifically, if the offset is a PHI node with all constant branches, return the minimum (or maximum for OST_MINIMUM) of the possible values. gcc/ChangeLog: PR tree-optimization/116556 * tree-object-size.cc (try_collapsing_offset): New function. (plus_stmt_object_size): Use it. * gcc/testsuite/gcc.dg/builtin-object-size-1.c (test12, test13): New functions. (main): Call them. * gcc/testsuite/gcc.dg/builtin-object-size-3.c (test12, test13): New functions. (main): Call them. Signed-off-by: Siddhesh Poyarekar --- gcc/testsuite/gcc.dg/builtin-object-size-1.c | 63 ++++++++++++++++++++ gcc/testsuite/gcc.dg/builtin-object-size-3.c | 63 ++++++++++++++++++++ gcc/tree-object-size.cc | 60 +++++++++++++++++++ 3 files changed, 186 insertions(+) diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-1.c b/gcc/testsuite/gcc.dg/builtin-object-size-1.c index d6d13c5ef7a..1f5cd5d99a1 100644 --- a/gcc/testsuite/gcc.dg/builtin-object-size-1.c +++ b/gcc/testsuite/gcc.dg/builtin-object-size-1.c @@ -712,6 +712,65 @@ test11 (void) } #endif +void +__attribute__ ((noinline)) +test12 (unsigned cond) +{ + char *buf2 = malloc (10); + char *p; + size_t t; + + if (cond) + t = 8; + else + t = 4; + + p = &buf2[t]; + +#ifdef __builtin_object_size + if (__builtin_object_size (p, 0) != (cond ? 2 : 6)) + FAIL (); +#else + if (__builtin_object_size (p, 0) != 6) + FAIL (); +#endif +} + +void +__attribute__ ((noinline)) +test13 (unsigned cond) +{ + char *buf2 = malloc (10); + char *p = &buf2[4]; + int t; + + if (cond) + t = -1; + else + t = -2; + +#ifdef __builtin_object_size + if (__builtin_object_size (&p[t], 0) != (cond ? 7 : 8)) + FAIL (); +#else + if (__builtin_object_size (&p[t], 0) != 8) + FAIL (); +#endif + + if (cond) + t = 1; + else + t = -3; + +#ifdef __builtin_object_size + if (__builtin_object_size (&p[t], 0) != (cond ? 5 : 9)) + FAIL (); +#else + if (__builtin_object_size (&p[t], 0) != 9) + FAIL (); +#endif +} + int main (void) { @@ -729,6 +788,10 @@ main (void) test10 (); #ifndef SKIP_STRNDUP test11 (); + test12 (1); + test12 (0); + test13 (1); + test13 (0); #endif DONE (); } diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-3.c b/gcc/testsuite/gcc.dg/builtin-object-size-3.c index ec2c62c9640..7fad106fc27 100644 --- a/gcc/testsuite/gcc.dg/builtin-object-size-3.c +++ b/gcc/testsuite/gcc.dg/builtin-object-size-3.c @@ -720,6 +720,65 @@ test11 (void) } #endif +void +__attribute__ ((noinline)) +test12 (unsigned cond) +{ + char *buf2 = malloc (10); + char *p; + size_t t; + + if (cond) + t = 8; + else + t = 4; + + p = &buf2[t]; + +#ifdef __builtin_object_size + if (__builtin_object_size (p, 2) != (cond ? 2 : 6)) + FAIL (); +#else + if (__builtin_object_size (p, 2) != 2) + FAIL (); +#endif +} + +void +__attribute__ ((noinline)) +test13 (unsigned cond) +{ + char *buf2 = malloc (10); + char *p = &buf2[4]; + size_t t; + + if (cond) + t = -1; + else + t = -2; + +#ifdef __builtin_object_size + if (__builtin_object_size (&p[t], 2) != (cond ? 7 : 8)) + FAIL (); +#else + if (__builtin_object_size (&p[t], 2) != 7) + FAIL (); +#endif + + if (cond) + t = 1; + else + t = -3; + +#ifdef __builtin_object_size + if (__builtin_object_size (&p[t], 2) != (cond ? 5 : 9)) + FAIL (); +#else + if (__builtin_object_size (&p[t], 2) != 5) + FAIL (); +#endif +} + int main (void) { @@ -738,5 +797,9 @@ main (void) #ifndef SKIP_STRNDUP test11 (); #endif + test12 (1); + test12 (0); + test13 (1); + test13 (0); DONE (); } diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index f8fae0cbc82..2dfc28407ab 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -1468,6 +1468,63 @@ merge_object_sizes (struct object_size_info *osi, tree dest, tree orig) return bitmap_bit_p (osi->reexamine, SSA_NAME_VERSION (orig)); } +/* For constant sizes, try collapsing a non-constant offset to a constant if + possible. The case handled at the moment is when the offset is a PHI node + with all of its targets are constants. */ + +static tree +try_collapsing_offset (tree op, int object_size_type) +{ + gcc_assert (!(object_size_type & OST_DYNAMIC)); + + if (TREE_CODE (op) != SSA_NAME) + return op; + + gimple *stmt = SSA_NAME_DEF_STMT (op); + + switch (gimple_code (stmt)) + { + case GIMPLE_ASSIGN: + /* Peek through casts. */ + if (gimple_assign_rhs_code (stmt) == NOP_EXPR) + { + tree ret = try_collapsing_offset (gimple_assign_rhs1 (stmt), + object_size_type); + if (TREE_CODE (ret) == INTEGER_CST) + return ret; + } + break; + case GIMPLE_PHI: + { + tree off = ((object_size_type & OST_MINIMUM) + ? TYPE_MIN_VALUE (ptrdiff_type_node) + : TYPE_MAX_VALUE (ptrdiff_type_node)); + + for (unsigned i = 0; i < gimple_phi_num_args (stmt); i++) + { + tree rhs = gimple_phi_arg_def (stmt, i); + + if (TREE_CODE (rhs) != INTEGER_CST) + return op; + + /* 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); + + off = fold_build2 (code, ptrdiff_type_node, off, + fold_convert (ptrdiff_type_node, rhs)); + } + return fold_convert (sizetype, off); + } + default: + break; + } + + /* Nothing worked, so return OP untouched. */ + return op; +} /* Compute object_sizes for VAR, defined to the result of an assignment with operator POINTER_PLUS_EXPR. Return true if the object size might @@ -1500,6 +1557,9 @@ plus_stmt_object_size (struct object_size_info *osi, tree var, gimple *stmt) if (object_sizes_unknown_p (object_size_type, varno)) return false; + if (!(object_size_type & OST_DYNAMIC) && TREE_CODE (op1) != INTEGER_CST) + op1 = try_collapsing_offset (op1, object_size_type); + /* Handle PTR + OFFSET here. */ if (size_valid_p (op1, object_size_type) && (TREE_CODE (op0) == SSA_NAME || TREE_CODE (op0) == ADDR_EXPR)) 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: 97771 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 37FD63858D39 for ; Fri, 20 Sep 2024 16:43:35 +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~patchwork=sourceware.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) From patchwork Fri Sep 20 16:40:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 97770 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4A7353858410 for ; Fri, 20 Sep 2024 16:42:36 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from skyblue.cherry.relay.mailchannels.net (skyblue.cherry.relay.mailchannels.net [23.83.223.167]) by sourceware.org (Postfix) with ESMTPS id 48828385840F; Fri, 20 Sep 2024 16:40:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 48828385840F 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 48828385840F Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.223.167 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1726850450; cv=pass; b=F/b86rYEfdVz5txCRF6RC9fBw6qib5Sjk4ZMYS5J4f/5Z3UtPrzRpHEibQZ9SRQO0ho6UJlDj8/51yO08ckUCbvOOEtHjYODvX/1lWUXknBtoyMpJob7aEDTEP9hrJk+980G9zhCGcahlPOILaE2M64xXSzfutIqPaC5qqMbh9M= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1726850450; c=relaxed/simple; bh=VsG7OtiRL5oMEYsbPJBoy+pXx6O1R3457n4JvsvzFUQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=et35zr0DoNYvt3O0h65V8++0lnOmArqsqMYlfrffVud50DyMhRvXoG1KORbdgAGph7a54AIsJOUZKAMeC6UkLDQgLe8SrM/ebbIriLjX6lfFJIVZx4/ggIUog41mW3/2lkNUJEeAoT5NmGvfD3Uwq1b2hNVyXJMcUz7eQTY1gwM= 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 50CC64E5B82; Fri, 20 Sep 2024 16:40:47 +0000 (UTC) Received: from pdx1-sub0-mail-a312.dreamhost.com (100-99-96-38.trex-nlb.outbound.svc.cluster.local [100.99.96.38]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 04ECC4E5CC3; Fri, 20 Sep 2024 16:40:47 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1726850447; a=rsa-sha256; cv=none; b=771PCRnD3Ube2xzlKNdVP048f8eJ9yGhulb3axyjruCx5rAZ7h8xSUu8MRWXwiu0j+X3yq KJ7gLrVkb0OL8tLuZvisO5nMeIW0c+AO4FZadNxcL0PjDO83jHOVtGJVOQfjHNUoYevN/O uru3gbAQ+lXytFMRP3/+xNZxP8XZA7PCjt3/9zIKrmOfnFAu190iCIemgCJLqYnau4W655 FdCvoUUbhcTo/tn3YV0HtlCBEWYKzOG7p4/hDeBdGEx5HdRu2IJC7i24xdWq65D+SUsxP5 EeUkxkqOBVTRS2LiilnKg1K24CyHXAr73a3du13xwVo6hD+FNzB5BpB6SpHt9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1726850447; 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=4iXUX+fofrqTagFYElDZ27uTIMqz58nNW0JZc4nLKYs=; b=jsugSRiAWucxmOTKhgMyJWUSUElRU1CQrLH6qNRosuC8QB23Ol/s1ji34w0tKjNsli1N2O OtzUt1RUYftvg40ufHanfOhXetX7qY0CV2UorOlEbSWmqAUkRem7xb9P5WZRGRUtXIyDwr 5kMJ2pG2fKolfmOfJDbsEWIm1GX3TKES2W4t9xPO20besONkQwEccCjQLFHlljRYDjDj2u RsfF4nMiDpH3ZKKzvZFACMCptKesi58q/lMyvWMzzQ8AYxoHafpCKS0KC2WNqLGcJoPBc1 ZZv/gbr6p0+p1C0pCXcHt4yK4Vg+HBZ58VSGkErfp1qjRrYLRZ+053af6INR8w== ARC-Authentication-Results: i=1; rspamd-5b46bcd97f-7bkdm; 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-Suffer-Tasty: 1fb953685ec291b6_1726850447238_2835399218 X-MC-Loop-Signature: 1726850447238:2231977248 X-MC-Ingress-Time: 1726850447238 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.96.38 (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 4X9J6Q4pKBzS4; 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=4iXUX+fofrqTagFYElDZ27uTIMqz58nNW0JZc4nLKYs=; h=From:To:Cc:Subject:Date:Content-Transfer-Encoding; b=EvbtqsxiLFnjNYxMWkgJKLDvkeVdxbIMqzsciHGKwB81/p3+VhCostl0xzlmOh2rb KiDbULSw5VJLWDkfSBfvYpypxiPRTfmHAMq3l64vyAc1aKZSXO3QEkdoB9rN6HpAYX 6Y3TRjo7PnSfYcgldmMd4S1X2m5ab4qLt1uTGC99bnoqG3QkTGONdOwEYtdedtDtrL AZ4QCl2pE9DK8Kr+uyeIdyfTY+oeMYRXPQNyV2zY899wJ8chhQU3qZVQii6KrWcoTM hU+F3035AZcvB+NRnigJqKXMjvNuDw40/5skZGPsOABGXWiDAat1L8QM9U7XwLfE/j hOjVV/6ON+Q6g== From: Siddhesh Poyarekar To: gcc-patches@gcc.gnu.org Cc: pinskia@gcc.gnu.org Subject: [PATCH v2 4/4] tree-object-size: Fall back to wholesize for non-const offset Date: Fri, 20 Sep 2024 12:40:29 -0400 Message-ID: <20240920164029.63843-5-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.8 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.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~patchwork=sourceware.org@gcc.gnu.org Don't bail out early if the offset to a pointer in __builtin_object_size is a variable, return the wholesize instead since that is a better fallback for maximum estimate. This should keep checks in place for fortified functions to constrain overflows to at lesat some extent. gcc/ChangeLog: PR middle-end/77608 * tree-object-size.cc (plus_stmt_object_size): Drop check for constant offset. * testsuite/gcc.dg/builtin-object-size-1.c (test14): New test. Signed-off-by: Siddhesh Poyarekar --- gcc/testsuite/gcc.dg/builtin-object-size-1.c | 19 +++++++++++++++++++ gcc/tree-object-size.cc | 7 ++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-1.c b/gcc/testsuite/gcc.dg/builtin-object-size-1.c index 6ffe12be683..5a24087ae1e 100644 --- a/gcc/testsuite/gcc.dg/builtin-object-size-1.c +++ b/gcc/testsuite/gcc.dg/builtin-object-size-1.c @@ -791,6 +791,25 @@ test13 (unsigned cond) #endif } +void +__attribute__ ((noinline)) +test14 (unsigned off) +{ + char *buf2 = malloc (10); + char *p; + size_t t; + + p = &buf2[off]; + +#ifdef __builtin_object_size + if (__builtin_object_size (p, 0) != 10 - off) + FAIL (); +#else + if (__builtin_object_size (p, 0) != 10) + FAIL (); +#endif +} + int main (void) { diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 1b569c3d12b..ebd2a4650aa 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -1595,8 +1595,7 @@ plus_stmt_object_size (struct object_size_info *osi, tree var, gimple *stmt) op1 = try_collapsing_offset (op1, NULL_TREE, NOP_EXPR, object_size_type); /* Handle PTR + OFFSET here. */ - if (size_valid_p (op1, object_size_type) - && (TREE_CODE (op0) == SSA_NAME || TREE_CODE (op0) == ADDR_EXPR)) + if ((TREE_CODE (op0) == SSA_NAME || TREE_CODE (op0) == ADDR_EXPR)) { if (TREE_CODE (op0) == SSA_NAME) { @@ -1621,7 +1620,9 @@ plus_stmt_object_size (struct object_size_info *osi, tree var, gimple *stmt) if (size_unknown_p (bytes, 0)) ; else if ((object_size_type & OST_DYNAMIC) - || bytes != wholesize || compare_tree_int (op1, offset_limit) <= 0) + || bytes != wholesize + || (size_valid_p (op1, object_size_type) + && compare_tree_int (op1, offset_limit) <= 0)) bytes = size_for_offset (bytes, op1, wholesize); /* In the static case, with a negative offset, the best estimate for minimum size is size_unknown but for maximum size, the wholesize is a