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: 38814 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp5045523wrt; Wed, 4 Jan 2023 01:14:33 -0800 (PST) X-Google-Smtp-Source: AMrXdXs0fWzIauHcLhxkp0v91G4Eb9WaIRWQq9GotbO5cpRqkRHa3nplPiSe8tzV8FK3hrs7W4CU X-Received: by 2002:a17:907:20e7:b0:7c0:969a:ca7f with SMTP id rh7-20020a17090720e700b007c0969aca7fmr37451060ejb.76.1672823673638; Wed, 04 Jan 2023 01:14:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672823673; cv=none; d=google.com; s=arc-20160816; b=VR0B+tFyrSkXvt4Z/axxBNcBNW57KeL9QqD8qclSFuuE5rMYxCHitU+hMSuqKsXYnh Us/j+t2wOwlad86AZOXiPqMQdJbC99tMgRDapACrJogP8pSoYZOlEP8ZNWCtmxH+Gppc Zqxl5b2JQuB9BqzKhzmmaXbqbz9/1tu5yBJPzeLD8IXOJiYZLonZTR4SP0q4zKtMFbUT 5GCeYAe1T7OVHqsfJdgWrd2MC1paygkQuQ8niNHlLhFyLlIbE3elUkRuMhvisiTIwXUj 0LfMqsXQ+RphSIwMhk1ZvZdO+VoRuCBVtmLXM3beKz4/OgVn5VfABq0238y1Z9TIb4Lb iufQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=YemHnXsu6fcm1jTbpdsk/hYYjI2d/TqYZ5GpLOG6gso=; b=o+WVCxuy7NwMhO7ufxkmQKBNB8u7dZ8eEJfOGeab60QvuCdovaEVPQRX0do4JAEUf4 rgIeUpGsnqtjn0gKIOWgnc5IVYjjgwG6xBeU6KNfhqrzxOsGm0L2gU8MygB+JL7eSCiy q3J66E1B+jpzlFPszNLXOOCFporCoMWd6/LXB+tYCsA75WY7WOY1cEEqnmOanYB/lBtf FzF5xVAso8C7McdcCdsnheV6xHDZOsZTdopiZtmvyCjG3wmIVtD5+Rm98BDnUyOTExO5 YjYAZlHrhK+LOneUqeXjjcBQJA0T6JHGQ3ibH3GpC/2f9kGx4Sar5lvTh0w33QkaPsa8 ghHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=HNamqZAb; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id dt12-20020a170907728c00b00817f698091csi28954418ejc.301.2023.01.04.01.14.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 01:14:33 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=HNamqZAb; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 721473858C2B for ; Wed, 4 Jan 2023 09:14:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 721473858C2B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1672823672; 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=HNamqZAbgrclQP1MsEWc+BtLNVJXgX6TDOfCt3dQKpMvq47cp1NRGl+kT4/nJ3Mxg 7LSQH0uA6suzq7M0f7ctnHlamJiiUo9xRoH8XTRi3RO3FeCCIB6TbgWrBiasZohEld dmH7DOzzYYNsgkUCqCdbZtClIYBR7WnblUVbvilY= 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754082756244039900?= X-GMAIL-MSGID: =?utf-8?q?1754082756244039900?= 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); +}