From patchwork Wed Nov 17 09:32:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1556123 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=SnHZCFiC; 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 (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HvHlY13jxz9s5P for ; Wed, 17 Nov 2021 20:32:40 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 12906385AC32 for ; Wed, 17 Nov 2021 09:32:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 12906385AC32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1637141557; bh=1bivMWv9maPETRYQa5siB7EeNrwGirPV+FKfe3x22kY=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=SnHZCFiCuT0c2CAqTeiWxEouriLWGrc5Ti5hnFnyQ/l7Q4Vn0IPS18LgmWC9ZdNtd v0qzyeTH5TgwM+CNqQZpEPxHMhKT0uCwaQDRleBoH9lIxNslSRNoMOB5r957XLOQtJ hjOZU7ingZ0W8zvfS4CG2RFdm8979Vm46k3isx6I= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 7D9843858C27 for ; Wed, 17 Nov 2021 09:32:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7D9843858C27 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-102-40a-7jFuPhGPDiC1wZoucw-1; Wed, 17 Nov 2021 04:32:13 -0500 X-MC-Unique: 40a-7jFuPhGPDiC1wZoucw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EA4671006AA2 for ; Wed, 17 Nov 2021 09:32:12 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.54]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0EA765C3E0; Wed, 17 Nov 2021 09:32:03 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 1AH9W1T51391492 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 17 Nov 2021 10:32:01 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 1AH9W0oE1391491; Wed, 17 Nov 2021 10:32:00 +0100 Date: Wed, 17 Nov 2021 10:32:00 +0100 To: Andrew MacLeod , Aldy Hernandez Subject: [PATCH] ranger: Fix up fold_using_range::range_of_address [PR103255] Message-ID: <20211117093200.GK2710@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi! If on &base->member the offset isn't constant or isn't zero and -fdelete-null-pointer-checks and not -fwrapv-pointer and base has a range that doesn't include NULL, we return the range of the base. Usually it isn't a big deal, because for most pointers we just use varying, range_zero and range_nonzero ranges and nothing beyond that, but if a pointer is initialized from a constant, we actually track the exact range and in that case this causes miscompilation. As discussed on IRC, I think doing something like: offset_int off2; if (off_cst && off.is_constant (&off2)) { tree cst = wide_int_to_tree (sizetype, off2 / BITS_PER_UNIT); // adjust range r with POINTER_PLUS_EXPR cst if (!range_includes_zero_p (&r)) return true; } // Fallback r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt))); return true; could work, given that most of the pointer ranges are just the simple ones perhaps it is too much for little benefit. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-11-17 Jakub Jelinek PR tree-optimization/103255 * gimple-range-fold.cc (fold_using_range::range_of_address): Return range_nonzero rather than unadjusted base's range. Formatting fixes. * gcc.c-torture/execute/pr103255.c: New test. Jakub --- gcc/gimple-range-fold.cc.jj 2021-11-04 12:27:02.341298923 +0100 +++ gcc/gimple-range-fold.cc 2021-11-16 22:10:44.453974329 +0100 @@ -720,14 +720,20 @@ fold_using_range::range_of_address (iran } /* If &X->a is equal to X, the range of X is the result. */ if (off_cst && known_eq (off, 0)) - return true; + return true; else if (flag_delete_null_pointer_checks && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))) { - /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't - allow going from non-NULL pointer to NULL. */ - if(!range_includes_zero_p (&r)) - return true; + /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't + allow going from non-NULL pointer to NULL. */ + if (!range_includes_zero_p (&r)) + { + /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT + using POINTER_PLUS_EXPR if off_cst and just fall back to + this. */ + r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt))); + return true; + } } /* If MEM_REF has a "positive" offset, consider it non-NULL always, for -fdelete-null-pointer-checks also "negative" --- gcc/testsuite/gcc.c-torture/execute/pr103255.c.jj 2021-11-16 22:14:10.660118225 +0100 +++ gcc/testsuite/gcc.c-torture/execute/pr103255.c 2021-11-16 22:13:56.506314265 +0100 @@ -0,0 +1,41 @@ +/* PR tree-optimization/103255 */ + +struct H +{ + unsigned a; + unsigned b; + unsigned c; +}; + +#if __SIZEOF_POINTER__ >= 4 +#define ADDR 0x400000 +#else +#define ADDR 0x4000 +#endif +#define OFF 0x20 + +int +main () +{ + struct H *h = 0; + unsigned long o; + volatile int t = 1; + + for (o = OFF; o <= OFF; o += 0x1000) + { + struct H *u; + u = (struct H *) (ADDR + o); + if (t) + { + h = u; + break; + } + } + + if (h == 0) + return 0; + unsigned *tt = &h->b; + if ((__SIZE_TYPE__) tt != (ADDR + OFF + __builtin_offsetof (struct H, b))) + __builtin_abort (); + return 0; +}