From patchwork Thu Dec 8 18:45:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1713845 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.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=) Authentication-Results: legolas.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=nWEq02bf; dkim-atps=neutral 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 ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NSjm81Sh0z23np for ; Fri, 9 Dec 2022 05:46:16 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2731E389BC8D for ; Thu, 8 Dec 2022 18:46:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2731E389BC8D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670525174; bh=CVEG0ScEQr0Y5G5DnOfcx7A7fCn5kV3/EaQMsCE8+to=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=nWEq02bfSL92yUw+Mjr6Jxf2KRCchHxFHGnfz2wvZ3U5TyWedx9LM+iNdeGb6l6vi AHvoY612AxO4VpvTwnXwn3SjmkCKfIH+1NnMXM0Qk+iyBoK1B6rK5LW+E0kJp1odnj QSqi6vYlynJhACyvu9i0QEmd7g1N3JGk/XZS0RIo= 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 6CF84389BC9E for ; Thu, 8 Dec 2022 18:45:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6CF84389BC9E Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-226-QNNbmyCqMNiNzr5_v2J5_g-1; Thu, 08 Dec 2022 13:45:34 -0500 X-MC-Unique: QNNbmyCqMNiNzr5_v2J5_g-1 Received: by mail-qk1-f197.google.com with SMTP id q20-20020a05620a0d9400b006fcaa1eac9bso2356505qkl.23 for ; Thu, 08 Dec 2022 10:45:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CVEG0ScEQr0Y5G5DnOfcx7A7fCn5kV3/EaQMsCE8+to=; b=iSGFCbZzNr1U3Mn+ftmgVCClh3OOqJRCCY5wDYxoTAU9dIs+PcjVgM3RwTouNWCJyG QALZZBG3Iw9dh1OMcnxrfKfQMafaBSEqTzJec1/+Z6YbAJ91xwUz2HgJUKW6KulzQj7e sFRGmaKhFZAvhIl7uth3rKiyEmOYdc1ZL7CCOWIF9HFush2UQlSqKgrHYQImcIIXNTzX CaAYa70Q21gqtiTGtPusvUk8+lE8OmumD24oGEBwtnMLB6TJIQeXw2hDjUcSrfBXvSBB 0dLU8mOwLsLRe+tN/+uVb9XzP4WqFInITpgGDm9lAix3sLJEe3QR5z47vJAABJbdVf2v YtSw== X-Gm-Message-State: ANoB5pkOeSj7ysksxT/KLw5gGnm0afGe46A4F18J2/GdFVSPgjTwd48b Cc6VVUXoxE2FHIDOXmgCfxfAjYBZYMuEP5/VDiY11uNA8tRxsn07eIRFSGYmydQb+qtqyBYWZnU mgZp9X4IVmlrxjkuSIHdo1sry1nSAO8zzSLIR6KFLD5Ltf8ARyE+6rWW95js+Nt4ZVQ== X-Received: by 2002:a05:622a:580d:b0:3a5:fba4:96e6 with SMTP id fg13-20020a05622a580d00b003a5fba496e6mr2877852qtb.1.1670525133392; Thu, 08 Dec 2022 10:45:33 -0800 (PST) X-Google-Smtp-Source: AA0mqf4kJn7fa3qJVJI9HElOfcsjzrhXY1uJQqE4oKXfAM2HaT/Bo3GEndmirGGL0iKWR6gEEbE8FQ== X-Received: by 2002:a05:622a:580d:b0:3a5:fba4:96e6 with SMTP id fg13-20020a05622a580d00b003a5fba496e6mr2877824qtb.1.1670525132904; Thu, 08 Dec 2022 10:45:32 -0800 (PST) Received: from jason.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id z10-20020ac86b8a000000b00398a7c860c2sm15347276qts.4.2022.12.08.10.45.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Dec 2022 10:45:32 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: build initializer_list in a loop [PR105838] Date: Thu, 8 Dec 2022 13:45:28 -0500 Message-Id: <20221208184528.1657376-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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_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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- The previous patch avoided building an initializer_list at all when building a vector, but in situations where that isn't possible, we could still build the initializer_list with a loop over a constant array. This is represented using a VEC_INIT_EXPR, which required adjusting a couple of places that expected the initializer array to have the same type as the target array and fixing build_vec_init not to undo our efforts. PR c++/105838 gcc/cp/ChangeLog: * call.cc (convert_like_internal) [ck_list]: Use maybe_init_list_as_array. * constexpr.cc (cxx_eval_vec_init_1): Init might have a different type. * tree.cc (build_vec_init_elt): Likewise. * init.cc (build_vec_init): Handle from_array from a TARGET_EXPR. Retain TARGET_EXPR of a different type. gcc/testsuite/ChangeLog: * g++.dg/tree-ssa/initlist-opt2.C: New test. --- gcc/cp/call.cc | 11 ++++++++- gcc/cp/constexpr.cc | 6 ++--- gcc/cp/init.cc | 13 ++++++++-- gcc/cp/tree.cc | 2 -- gcc/testsuite/g++.dg/tree-ssa/initlist-opt2.C | 24 +++++++++++++++++++ 5 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/g++.dg/tree-ssa/initlist-opt2.C base-commit: d081807d8d70e3e87eae41e1560e54d503f4d465 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 33b5e7f87f5..14aa96dd328 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -8501,7 +8501,16 @@ convert_like_internal (conversion *convs, tree expr, tree fn, int argnum, unsigned len = CONSTRUCTOR_NELTS (expr); tree array; - if (len) + if (tree init = maybe_init_list_as_array (elttype, expr)) + { + elttype = cp_build_qualified_type + (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST); + array = build_array_of_n_type (elttype, len); + array = build_vec_init_expr (array, init, complain); + array = get_target_expr (array); + array = cp_build_addr_expr (array, complain); + } + else if (len) { tree val; unsigned ix; diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index e43d92864f5..3f7892aa88a 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -5255,12 +5255,12 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, else { /* Copying an element. */ - gcc_assert (same_type_ignoring_top_level_qualifiers_p - (atype, TREE_TYPE (init))); eltinit = cp_build_array_ref (input_location, init, idx, complain); if (!lvalue_p (init)) eltinit = move (eltinit); - eltinit = force_rvalue (eltinit, complain); + eltinit = (perform_implicit_conversion_flags + (elttype, eltinit, complain, + LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING)); eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, non_constant_p, overflow_p); } diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index 428fac5621c..1ccde7cf8ad 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -4420,7 +4420,9 @@ build_vec_init (tree base, tree maxindex, tree init, /* Look through the TARGET_EXPR around a compound literal. */ if (init && TREE_CODE (init) == TARGET_EXPR && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR - && from_array != 2) + && from_array != 2 + && (same_type_ignoring_top_level_qualifiers_p + (TREE_TYPE (init), atype))) init = TARGET_EXPR_INITIAL (init); if (tree vi = get_vec_init_expr (init)) @@ -4546,7 +4548,14 @@ build_vec_init (tree base, tree maxindex, tree init, { if (lvalue_kind (init) & clk_rvalueref) xvalue = true; - base2 = decay_conversion (init, complain); + if (TREE_CODE (init) == TARGET_EXPR) + { + /* Avoid error in decay_conversion. */ + base2 = decay_conversion (TARGET_EXPR_SLOT (init), complain); + base2 = cp_build_compound_expr (init, base2, tf_none); + } + else + base2 = decay_conversion (init, complain); if (base2 == error_mark_node) return error_mark_node; itype = TREE_TYPE (base2); diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index 33bde16f128..a600178239c 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -785,8 +785,6 @@ build_vec_init_elt (tree type, tree init, tsubst_flags_t complain) releasing_vec argvec; if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)) { - gcc_assert (same_type_ignoring_top_level_qualifiers_p - (type, TREE_TYPE (init))); tree init_type = strip_array_types (TREE_TYPE (init)); tree dummy = build_dummy_object (init_type); if (!lvalue_p (init)) diff --git a/gcc/testsuite/g++.dg/tree-ssa/initlist-opt2.C b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt2.C new file mode 100644 index 00000000000..fc928bb5405 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt2.C @@ -0,0 +1,24 @@ +// PR c++/105838 +// { dg-additional-options -fdump-tree-gimple } +// { dg-do compile { target c++11 } } + +// Check that we call the basic_string constructor once (and define it once). +// { dg-final { scan-tree-dump-times {>::basic_string} 2 "gimple" } } + +#include + +void g (const void *); + +void f (const char *p) +{ + std::initializer_list lst = { + "aahing", "aaliis", "aarrgh", "abacas", "abacus", "abakas", "abamps", "abands", "abased", "abaser", "abases", "abasia", + "abated", "abater", "abates", "abatis", "abator", "abattu", "abayas", "abbacy", "abbess", "abbeys", "abbots", "abcees", + "abdabs", "abduce", "abduct", "abears", "abeigh", "abeles", "abelia", "abends", "abhors", "abided", "abider", "abides", + "abject", "abjure", "ablate", "ablaut", "ablaze", "ablest", "ablets", "abling", "ablins", "abloom", "ablush", "abmhos", + "aboard", "aboded", "abodes", "abohms", "abolla", "abomas", "aboral", "abords", "aborne", "aborts", "abound", "abouts", + "aboves", "abrade", "abraid", "abrash", "abrays", "abrazo", "abrege", "abrins", "abroad", "abrupt", "abseil", "absent", + }; + + g(&lst); +}