From patchwork Thu Aug 6 17:50:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1341851 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: 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=ZY1JPuXK; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 ozlabs.org (Postfix) with ESMTPS id 4BMwyB3DZcz9sR4 for ; Fri, 7 Aug 2020 03:50:42 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A6082383F850; Thu, 6 Aug 2020 17:50:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A6082383F850 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1596736239; bh=jCuMvdW240GsCNZMOpDz3iXxXGSf5QryW5bO4AA88y0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ZY1JPuXKv9skw+drL0n6mPQz0A6q7Nt7xT8iXAyIdWQO1bsqirszYMPP9jlTmtX4F SgqPEUV8qgv6uOst2EVbyhVl1meUJ5RyyittQ4urou65GZVLkOj2iBRGAkFK9GuWkN +zfqTvZrF3dsxzBhuJKLH3/rohaFJjaQ4axAJ86U= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by sourceware.org (Postfix) with ESMTP id 02AE7383F850 for ; Thu, 6 Aug 2020 17:50:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 02AE7383F850 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-489-2YNR_gwKP3-t8-w_fcqobg-1; Thu, 06 Aug 2020 13:50:35 -0400 X-MC-Unique: 2YNR_gwKP3-t8-w_fcqobg-1 Received: by mail-qk1-f198.google.com with SMTP id c202so28184329qkg.12 for ; Thu, 06 Aug 2020 10:50:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=jCuMvdW240GsCNZMOpDz3iXxXGSf5QryW5bO4AA88y0=; b=Yi1RHtSIA9Uw0N++rjtw+ZauOGp6dpakGnZiC3MvebeabVjARIYeFClAZTluWjG+8W cKawIpXLJiQPMLPef+fJAZR1G7Spv2A8hwM2lPWfMry3kQbqOEkAKMMO8Y1TsWultGBH erZtAq4VB3vAFw+U0gFs/zDGNsQQPeSxySZF5LG+8EiYWX9opvnqQehRX8n8s6HwbKtR TeFM0/ZimX7rndlpw1oCCd9HwBQO+Q1dR6pj7KgMFLxTjjyqpf7plQwYfFfil4ffBtKd XuNxljQ1dCtPxTc50eD2zx+pmDzW04An1ncppdk5egIV5LkSsDHAch+XMl0CzFc7Ekf1 5BEw== X-Gm-Message-State: AOAM533bMYOgqzfVNPW4o4i9ogFidq+gxBB6sLdNa5EXZ+NUM7SyZ4pN 04Vm8d0PNdUyKYcoODOntk15SrGnqA3978hv1neqeIue4xbM5IijN4FJ8v6T/6bNSYgMISeWTp3 Mxclz7tIiSodG1dqIxw== X-Received: by 2002:a37:4c15:: with SMTP id z21mr9416133qka.334.1596736234408; Thu, 06 Aug 2020 10:50:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyjKNQHqsiEE3Br3M/C6CCBDCvCpc3XvrF1w66ejd+w2fc1e8QeDa/9br8eAhK2HEBXhmdQfQ== X-Received: by 2002:a37:4c15:: with SMTP id z21mr9416110qka.334.1596736234099; Thu, 06 Aug 2020 10:50:34 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id 141sm4870464qke.41.2020.08.06.10.50.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Aug 2020 10:50:33 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Improve RANGE_EXPR optimization in cxx_eval_vec_init Date: Thu, 6 Aug 2020 13:50:06 -0400 Message-Id: <20200806175006.3522650-1-ppalka@redhat.com> X-Mailer: git-send-email 2.28.0.97.gdc04167d37 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-16.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, 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.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" This patch eliminates an exponential dependence in cxx_eval_vec_init on the array dimension of a VEC_INIT_EXPR when the RANGE_EXPR optimization applies. This is achieved by using a single constructor_elt (with index RANGE_EXPR 0...max-1) per dimension instead of two constructor_elts (with index 0 and RANGE_EXPR 1...max-1 respectively). In doing so, we can also get rid of the call to unshare_constructor since the element initializer now gets used in exactly one spot. The patch also removes the 'eltinit = new_ctx.ctor' assignment within the RANGE_EXPR optimization since eltinit should already always be equal to new_ctx.ctor here (modulo encountering an error when computing eltinit). This was verified by running the testsuite against an appropriate assert. Finally, this patch reverses the sense of the ctx->quiet test that controls whether to short-circuit evaluation upon seeing an error. This should speed up speculative evaluation of non-constant VEC_INIT_EXPRs (since ctx->quiet is true then). I'm not sure why we were testing !ctx->quiet originally; it's inconsistent with how we short-circuit in other spots. I contrived the testcase array60.C below which verifies that we now short-circuit quickly. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK to commit? gcc/cp/ChangeLog: * constexpr.c (cxx_eval_vec_init_1): Move the i == 0 test to the if statement that guards the RANGE_EXPR optimization. Invert the ctx->quiet test. Apply the RANGE_EXPR optimization before we append the first element initializer. Truncate ctx->ctor when performing the RANGE_EXPR optimization. Make the built RANGE_EXPR start at index 0 instead of 1. Don't call unshare_constructor. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-array28.C: New test. * g++.dg/init/array60.C: New test. --- gcc/cp/constexpr.c | 34 ++++++++++--------- .../g++.dg/cpp0x/constexpr-array28.C | 14 ++++++++ gcc/testsuite/g++.dg/init/array60.C | 13 +++++++ 3 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-array28.C create mode 100644 gcc/testsuite/g++.dg/init/array60.C diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index ab747a58fa0..e67ce5da355 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -4205,7 +4205,7 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, if (value_init || init == NULL_TREE) { eltinit = NULL_TREE; - reuse = i == 0; + reuse = true; } else eltinit = cp_build_array_ref (input_location, init, idx, complain); @@ -4222,7 +4222,7 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, return ctx->ctor; eltinit = cxx_eval_constant_expression (&new_ctx, init, lval, non_constant_p, overflow_p); - reuse = i == 0; + reuse = true; } else { @@ -4236,35 +4236,37 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, non_constant_p, overflow_p); } - if (*non_constant_p && !ctx->quiet) + if (*non_constant_p && ctx->quiet) break; - if (new_ctx.ctor != ctx->ctor) - { - /* We appended this element above; update the value. */ - gcc_assert ((*p)->last().index == idx); - (*p)->last().value = eltinit; - } - else - CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit); + /* Reuse the result of cxx_eval_constant_expression call from the first iteration to all others if it is a constant initializer that doesn't require relocations. */ - if (reuse + if (i == 0 + && reuse && max > 1 && (eltinit == NULL_TREE || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) == null_pointer_node))) { - if (new_ctx.ctor != ctx->ctor) - eltinit = new_ctx.ctor; tree range = build2 (RANGE_EXPR, size_type_node, - build_int_cst (size_type_node, 1), + build_int_cst (size_type_node, 0), build_int_cst (size_type_node, max - 1)); - CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit)); + vec_safe_truncate (*p, 0); + CONSTRUCTOR_APPEND_ELT (*p, range, eltinit); break; } else if (i == 0) vec_safe_reserve (*p, max); + + if (new_ctx.ctor != ctx->ctor) + { + /* We appended this element above; update the value. */ + gcc_assert ((*p)->last().index == idx); + (*p)->last().value = eltinit; + } + else + CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit); } if (!*non_constant_p) diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-array28.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-array28.C new file mode 100644 index 00000000000..f844926e32b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-array28.C @@ -0,0 +1,14 @@ +// { dg-do compile { target c++11 } } + +struct A { int i = 42; }; + +struct B +{ + A a[2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2]; +}; + +void f() { + // Verify default initialization here does not scale exponentially + // with the number of array dimensions. + constexpr B b; +} diff --git a/gcc/testsuite/g++.dg/init/array60.C b/gcc/testsuite/g++.dg/init/array60.C new file mode 100644 index 00000000000..22bd750f8e6 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/array60.C @@ -0,0 +1,13 @@ +struct A { int i; }; + +struct B +{ + virtual void f(); + A a[10000000]; +}; + +extern B b; + +// Verify that speculative constexpr evaluation of this non-constant +// initializer does not scale with the size of the array member 'a'. +B b2 (b);