From patchwork Wed Jan 4 09:13:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 62692 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 91B193858C60 for ; Wed, 4 Jan 2023 09:14:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 91B193858C60 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1672823657; bh=YemHnXsu6fcm1jTbpdsk/hYYjI2d/TqYZ5GpLOG6gso=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=jjNk1CT79A00kFfpdFa/cQ5ZGWYCfrM+Fqma0BIe3PFD1Ba3hDZXGB85yHoIwp8An CURk2KMCdVlGA+JhohJ31ETL6BpvkyN5iFd/SlCfZpQVzqAe/7mqphqqgo62WQ+Nl8 SQriICWi6BIrVQhM+OoYZBMotoIvLPE+K8IN8asQ= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 955D03858D1E for ; Wed, 4 Jan 2023 09:13:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 955D03858D1E Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-637-Z-UBJTSVP8-o4uS0f2qIgA-1; Wed, 04 Jan 2023 04:13:46 -0500 X-MC-Unique: Z-UBJTSVP8-o4uS0f2qIgA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 06D7F185A78B for ; Wed, 4 Jan 2023 09:13:46 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.223]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B463C51E5; Wed, 4 Jan 2023 09:13:45 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 3049DhV52397894 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 4 Jan 2023 10:13:43 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3049Dgl42397893; Wed, 4 Jan 2023 10:13:42 +0100 Date: Wed, 4 Jan 2023 10:13:42 +0100 To: Aldy Hernandez , Andrew MacLeod Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] vrp: Handle pointers in maybe_set_nonzero_bits [PR108253] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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.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 Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi! maybe_set_nonzero_bits calls set_nonzero_bits which asserts that var doesn't have pointer type. While we could punt for those cases, I think we can handle at least some easy cases. Earlier in maybe_set_nonzero_bits we've checked this is on (var & cst) == 0 edge and the other edge is __builtin_unreachable, so if cst is say 3 as in the testcase, we want to turn it into 4 byte alignment of the pointer. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-01-04 Jakub Jelinek PR tree-optimization/108253 * tree-vrp.cc (maybe_set_nonzero_bits): Handle var with pointer types. * g++.dg/opt/pr108253.C: New test. Jakub --- gcc/tree-vrp.cc.jj 2023-01-02 09:32:53.634833769 +0100 +++ gcc/tree-vrp.cc 2023-01-03 15:57:51.613239761 +0100 @@ -789,8 +789,22 @@ maybe_set_nonzero_bits (edge e, tree var return; } cst = gimple_assign_rhs2 (stmt); - set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), - wi::to_wide (cst))); + if (POINTER_TYPE_P (TREE_TYPE (var))) + { + struct ptr_info_def *pi = SSA_NAME_PTR_INFO (var); + if (pi && pi->misalign) + return; + wide_int w = wi::bit_not (wi::to_wide (cst)); + unsigned int bits = wi::ctz (w); + if (bits == 0 || bits >= HOST_BITS_PER_INT) + return; + unsigned int align = 1U << bits; + if (pi == NULL || pi->align < align) + set_ptr_info_alignment (get_ptr_info (var), align, 0); + } + else + set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), + wi::to_wide (cst))); } /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL --- gcc/testsuite/g++.dg/opt/pr108253.C.jj 2023-01-03 16:02:16.366438488 +0100 +++ gcc/testsuite/g++.dg/opt/pr108253.C 2023-01-03 16:02:33.549191780 +0100 @@ -0,0 +1,48 @@ +// PR tree-optimization/108253 +// { dg-do compile { target c++11 } } +// { dg-options "-O2" } + +struct S +{ + int *s; + S () : s (new int) {} + S (const S &r) noexcept : s (r.s) { __atomic_fetch_add (r.s, 1, 4); } +}; +struct T +{ + explicit T (const S &x) : t (x) {} + const S t; +}; +struct U +{ + operator int () const { new T (u); return 0; } + S u; +}; +bool foo (int matcher); +unsigned long bar (unsigned long pos, unsigned long end_pos); +struct V +{ + alignas (4) char v[4]; +}; +struct W +{ + void baz () + { + if (!w) __builtin_abort (); + if (reinterpret_cast <__UINTPTR_TYPE__> (w->v) % 4 != 0) __builtin_abort (); + __builtin_unreachable (); + } + [[gnu::noinline]] void qux (unsigned long) { if (!w) bar (0, x); } + V *w = nullptr; + unsigned x = 0; +}; + +void +test () +{ + W w; + U t; + if (!foo (t)) + w.baz (); + w.qux (0); +}