From patchwork Fri Oct 21 07:30:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 6541 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp543675wrr; Fri, 21 Oct 2022 00:32:03 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5keVluLiJamOl3tyJjWMyhFJ/IpMgVi94JtCwOhTNpG00YXexYVF6LiUYNUYJm9rvXs1fi X-Received: by 2002:a17:907:a065:b0:78d:c5dd:45cf with SMTP id ia5-20020a170907a06500b0078dc5dd45cfmr14583982ejc.117.1666337523351; Fri, 21 Oct 2022 00:32:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666337523; cv=none; d=google.com; s=arc-20160816; b=NzC4YLXlvfG8VQ2CGkiAhNF0CuD4yXZZcj58/R27C2abunO+3EnxG9VJW9o/6KaHBX 1yPWBgbGyDnfietoavnQQL+x6Tp2BBGzPNuPj667SpoXKBwWNtHrrSJAPzboppAqe+GX ZX1l8k/BWS3jY23T8MgN7ABmP1lSz1ZIezGNt8aL1ebcJUYWcUH4cbBAJ0dmxuVjUKyA GTB7lIP9H+UBgg1HJUtonVZUH0bBfmYyTQ0hpokU1WNnO9eApP3659l2EulGd0A+si/+ N2CXHshJL1/Lv3KqdC4QiEmO33bT9u9Vhp3OQo2WpytiKKYFXTgb9mB/ai9RMJxC+nLq LjrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:in-reply-to:mime-version:references:message-id :subject:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=M6r+dBIiSnK2m9Td2k/twDyXtnCjV98nCRow9InZdls=; b=QvXbgCv2Q+dingOHRYrqyc77ISnIOqX67LPFpYbIqJNsxSeygNXOxG14IBm9xTCmIk jso+zk65TXLMHvhC2esf+JnZwtJ0hlZyK9n6u0hQ/LRslh7BmIpQpTC+wTVHy7V9Smdl SOA7X7pQZ7JkEVEo9wvg8dzFvr6M6zC2gyjNvqxIPe49OFe7XOEX6MrhrkPOqHvpAUC5 FIIk237L84GWtmWSI1iRpsOZBXXTkslxOQiYVOGTFLmLWm1eadH8TJ751O5R3So00d8g hu8MZfBVdM6ywT1tM73l579mdKfOGaVN1jPpsDCPThmc2yPLjvgmKho1kUvLmHRzfwsR SnlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=dVLmnVPu; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id y18-20020a056402359200b0045d06ea5596si21086367edc.26.2022.10.21.00.32.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Oct 2022 00:32:03 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=dVLmnVPu; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 4CEA5385AC1C for ; Fri, 21 Oct 2022 07:32:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4CEA5385AC1C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666337522; bh=M6r+dBIiSnK2m9Td2k/twDyXtnCjV98nCRow9InZdls=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=dVLmnVPuLAQcq4Ko/t49JAmhT7tv3qM4mWwLMv42MeyPJ7yH++YuHp7wRbV3gRmEc ht3FtgkbBLKLuh4blNqPDgkN0WhNCrFL3cfqtGFSuudr6BLRsodToNB8gfrgqvDfu9 HIwJOgj6uHI33D7df1iljZd7CIbJ7v5fPcDJP4sg= 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 BA8F23857343 for ; Fri, 21 Oct 2022 07:31:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BA8F23857343 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-501-jI0bXde6PzibRLlZo_CD7Q-1; Fri, 21 Oct 2022 03:30:59 -0400 X-MC-Unique: jI0bXde6PzibRLlZo_CD7Q-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E5529185A79C for ; Fri, 21 Oct 2022 07:30:58 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.193.252]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A449749BB60; Fri, 21 Oct 2022 07:30:58 +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 29L7UubY3484870 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 21 Oct 2022 09:30:56 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 29L7UtJY3484869; Fri, 21 Oct 2022 09:30:55 +0200 Date: Fri, 21 Oct 2022 09:30:55 +0200 To: Jason Merrill Subject: [PATCH] c++, v2: Don't shortcut TREE_CONSTANT vector type CONSTRUCTORs in cxx_eval_constant_expression [PR107295] Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 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 Cc: gcc-patches@gcc.gnu.org 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?1747281535154550096?= X-GMAIL-MSGID: =?utf-8?q?1747281535154550096?= On Thu, Oct 20, 2022 at 10:51:14AM -0400, Jason Merrill wrote: > That seems like a bug; for VECTOR_TYPE we should fold even if !changed. > > > Also, the reason for the short-cutting is I think trying to avoid > > allocating a new CONSTRUCTOR when nothing changes and we just create > > GC garbage by it. > > We might limit the shortcut to non-vector types by hoisting the vector check > in reduced_constant_expression_p out of the CONSTRUCTOR_NO_CLEARING > condition: > > > if (CONSTRUCTOR_NO_CLEARING (t)) > > { > > if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) > > /* An initialized vector would have a VECTOR_CST. */ > > return false; > > then we could remove the fold in the shortcut. Ok, so like this? Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-10-21 Jakub Jelinek PR c++/107295 * constexpr.cc (reduced_constant_expression_p) : Return false for VECTOR_TYPE CONSTRUCTORs even without CONSTRUCTOR_NO_CLEARING set on them. (cxx_eval_bare_aggregate): If constant but !changed, fold before returning VECTOR_TYPE_P CONSTRUCTOR. (cxx_eval_constant_expression) : Don't fold TREE_CONSTANT CONSTRUCTOR, just return it. * g++.dg/ext/vector42.C: New test. Jakub --- gcc/cp/constexpr.cc.jj 2022-10-19 11:20:28.960225787 +0200 +++ gcc/cp/constexpr.cc 2022-10-20 18:43:42.952440364 +0200 @@ -3104,12 +3104,12 @@ reduced_constant_expression_p (tree t) case CONSTRUCTOR: /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ tree field; + if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) + /* An initialized vector would have a VECTOR_CST. */ + return false; if (CONSTRUCTOR_NO_CLEARING (t)) { - if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) - /* An initialized vector would have a VECTOR_CST. */ - return false; - else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) + if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) { /* There must be a valid constant initializer at every array index. */ @@ -4956,8 +4956,14 @@ cxx_eval_bare_aggregate (const constexpr TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; } } - if (*non_constant_p || !changed) + if (*non_constant_p) return t; + if (!changed) + { + if (VECTOR_TYPE_P (type)) + t = fold (t); + return t; + } t = ctx->ctor; if (!t) t = build_constructor (type, NULL); @@ -7387,11 +7393,10 @@ cxx_eval_constant_expression (const cons case CONSTRUCTOR: if (TREE_CONSTANT (t) && reduced_constant_expression_p (t)) { - /* Don't re-process a constant CONSTRUCTOR, but do fold it to - VECTOR_CST if applicable. */ + /* Don't re-process a constant CONSTRUCTOR. */ verify_constructor_flags (t); if (TREE_CONSTANT (t)) - return fold (t); + return t; } r = cxx_eval_bare_aggregate (ctx, t, lval, non_constant_p, overflow_p); --- gcc/testsuite/g++.dg/ext/vector42.C.jj 2022-10-20 17:57:42.767848544 +0200 +++ gcc/testsuite/g++.dg/ext/vector42.C 2022-10-20 17:57:42.767848544 +0200 @@ -0,0 +1,12 @@ +// PR c++/107295 +// { dg-do compile { target c++11 } } + +template struct A { + typedef T __attribute__((vector_size (sizeof (int)))) V; +}; +template using B = typename A::V; +template using V = B<4, T>; +using F = V; +constexpr F a = F () + 0.0f; +constexpr F b = F () + (float) 0.0; +constexpr F c = F () + (float) 0.0L;