From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 16B6C385C325 for ; Fri, 10 Jun 2022 17:27:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 16B6C385C325 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-526-XHMmBfw-MxOrXC_Qsn_PcQ-1; Fri, 10 Jun 2022 13:27:31 -0400 X-MC-Unique: XHMmBfw-MxOrXC_Qsn_PcQ-1 Received: by mail-qv1-f72.google.com with SMTP id kd24-20020a056214401800b0046d7fd4a421so4842066qvb.20 for ; Fri, 10 Jun 2022 10:27:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=OY4OcmJcnGNgMor0ztPfZoGz4Y993WVP4cN0c8A99W4=; b=nD49YhojlxxLUrQznSz+YYYcC7e9bKEDHngBxM0yshuFrxBX3GUiAdP8TRCoqAjM+A Ko4lFARw666qUJNXqLPjHGjMcBExL3Tcow/BeXFWZRIa36M83qM6g50Zd/H0AlqmAgQQ SAj6hizkmI2pgVbP4LQ0fGi0NFf+nRKNVEt01sHhBd2LB7uMgQeppCzT77jvwfp+8YOH VGNlOLyiHLTt+V1WQ4Qo4c1RyCTuWEap0m7smvYq+SqqxfmjFYs6ZDscFfpXtTwz1xEv LaUsJhgy12qlur6Uvytot8WvxZaj89pxxz03vcqSJ1kDBYueR9Y1Wb6VWfxE1WHMKVtf wQVQ== X-Gm-Message-State: AOAM531pnszcsO/Txn3QVKyt0KoYBbKYuDAVlY2ZKs9FZN893rz4e3Hc 9zYwbz1TG8XNjfNp8ad84jTNbcwA98LlZaPrUYFwyao59cNZ87FWFkN0pN7DaGVsN6EOCt46J4Q L1RBdIPzlK0I23hETwQ== X-Received: by 2002:a05:6214:c21:b0:443:a2d2:a685 with SMTP id a1-20020a0562140c2100b00443a2d2a685mr91897910qvd.52.1654882051002; Fri, 10 Jun 2022 10:27:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwaxCRSOeLeNCMKx9diihQ164MfaYSgV9DfXXHnMQZb2Hrd6nIfkVoY07UkKeBw3H7XLL/xw== X-Received: by 2002:a05:6214:c21:b0:443:a2d2:a685 with SMTP id a1-20020a0562140c2100b00443a2d2a685mr91897874qvd.52.1654882050531; Fri, 10 Jun 2022 10:27:30 -0700 (PDT) Received: from [192.168.1.100] (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 u5-20020a05620a454500b006a6cadd89efsm11385113qkp.82.2022.06.10.10.27.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 10 Jun 2022 10:27:29 -0700 (PDT) Message-ID: <4d9319d5-1890-7c99-6c1b-d940f873a590@redhat.com> Date: Fri, 10 Jun 2022 13:27:28 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: Re: [PATCH] c++: Add support for __real__/__imag__ modifications in constant expressions [PR88174] To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org References: From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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-List-Received-Date: Fri, 10 Jun 2022 17:27:35 -0000 On 6/9/22 04:37, Jakub Jelinek wrote: > Hi! > > We claim we support P0415R1 (constexpr complex), but e.g. > #include > > constexpr bool > foo () > { > std::complex a (1.0, 2.0); > a += 3.0; > a.real (6.0); > return a.real () == 6.0 && a.imag () == 2.0; > } > > static_assert (foo ()); > > fails with > test.C:12:20: error: non-constant condition for static assertion > 12 | static_assert (foo ()); > | ~~~~^~ > test.C:12:20: in ‘constexpr’ expansion of ‘foo()’ > test.C:8:10: in ‘constexpr’ expansion of ‘a.std::complex::real(6.0e+0)’ > test.C:12:20: error: modification of ‘__real__ a.std::complex::_M_value’ is not a constant expression > > The problem is we don't handle REALPART_EXPR and IMAGPART_EXPR > in cxx_eval_store_expression. > The following patch attempts to support it (with a requirement > that those are the outermost expressions, ARRAY_REF/COMPONENT_REF > etc. are just not possible on the result of these, BIT_FIELD_REF > would be theoretically possible if trying to extract some bits > from one part of a complex int, but I don't see how it could appear > in the FE trees. > > For these references, the code handles value being COMPLEX_CST, > COMPLEX_EXPR or CONSTRUCTOR_NO_CLEARING empty CONSTRUCTOR (what we use > to represent uninitialized values for C++20 and later) and the > code starts by rewriting it to COMPLEX_EXPR, so that we can freely > adjust the individual parts and later on possibly optimize it back > to COMPLEX_CST if both halves are constant. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? > > 2022-06-09 Jakub Jelinek > > PR c++/88174 > * constexpr.cc (cxx_eval_store_expression): Handle REALPART_EXPR > and IMAGPART_EXPR. > > * g++.dg/cpp1y/constexpr-complex1.C: New test. > > --- gcc/cp/constexpr.cc.jj 2022-06-08 08:21:02.973448193 +0200 > +++ gcc/cp/constexpr.cc 2022-06-08 17:13:04.986040449 +0200 > @@ -5707,6 +5707,20 @@ cxx_eval_store_expression (const constex > } > break; > > + case REALPART_EXPR: > + gcc_assert (probe == target); Doesn't this assert mean that complex_expr will always be == valp? > + vec_safe_push (refs, integer_zero_node); > + vec_safe_push (refs, TREE_TYPE (probe)); > + probe = TREE_OPERAND (probe, 0); > + break; > + > + case IMAGPART_EXPR: > + gcc_assert (probe == target); > + vec_safe_push (refs, integer_one_node); > + vec_safe_push (refs, TREE_TYPE (probe)); > + probe = TREE_OPERAND (probe, 0); > + break; > + > default: > if (evaluated) > object = probe; > @@ -5749,6 +5763,8 @@ cxx_eval_store_expression (const constex > auto_vec index_pos_hints; > bool activated_union_member_p = false; > bool empty_base = false; > + int complex_part = -1; > + tree *complex_expr = NULL; > while (!refs->is_empty ()) > { > if (*valp == NULL_TREE) > @@ -5785,14 +5801,36 @@ cxx_eval_store_expression (const constex > *valp = ary_ctor; > } > > - /* If the value of object is already zero-initialized, any new ctors for > - subobjects will also be zero-initialized. */ > - no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); > - > enum tree_code code = TREE_CODE (type); > tree reftype = refs->pop(); > tree index = refs->pop(); > > + if (code == COMPLEX_TYPE) > + { > + if (TREE_CODE (*valp) == COMPLEX_CST) > + *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp), > + TREE_IMAGPART (*valp)); > + else if (TREE_CODE (*valp) == CONSTRUCTOR > + && CONSTRUCTOR_NELTS (*valp) == 0 > + && CONSTRUCTOR_NO_CLEARING (*valp)) > + { > + tree r = build_constructor (reftype, NULL); > + CONSTRUCTOR_NO_CLEARING (r) = 1; > + *valp = build2 (COMPLEX_EXPR, type, r, r); > + } > + gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR); > + complex_expr = valp; > + valp = &TREE_OPERAND (*valp, index != integer_zero_node); > + gcc_checking_assert (refs->is_empty ()); > + type = reftype; > + complex_part = index != integer_zero_node; > + break; > + } > + > + /* If the value of object is already zero-initialized, any new ctors for > + subobjects will also be zero-initialized. */ > + no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); > + > if (code == RECORD_TYPE && is_empty_field (index)) > /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they > have no data and might have an offset lower than previously declared > @@ -5946,6 +5984,24 @@ cxx_eval_store_expression (const constex > = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]); > valp = &cep->value; > } > + if (complex_part != -1) > + { > + if (TREE_CODE (*valp) == COMPLEX_CST) > + *valp = build2 (COMPLEX_EXPR, TREE_TYPE (*valp), > + TREE_REALPART (*valp), > + TREE_IMAGPART (*valp)); > + else if (TREE_CODE (*valp) == CONSTRUCTOR > + && CONSTRUCTOR_NELTS (*valp) == 0 > + && CONSTRUCTOR_NO_CLEARING (*valp)) > + { > + tree r = build_constructor (TREE_TYPE (TREE_TYPE (*valp)), NULL); > + CONSTRUCTOR_NO_CLEARING (r) = 1; > + *valp = build2 (COMPLEX_EXPR, TREE_TYPE (*valp), r, r); > + } > + gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR); > + complex_expr = valp; > + valp = &TREE_OPERAND (*valp, complex_part); I don't understand this block; shouldn't valp point to the real or imag part of the complex number at this point? How could complex_part be set without us handling the complex case in the loop already? > + } > } > > if (*non_constant_p) > @@ -6016,6 +6072,22 @@ cxx_eval_store_expression (const constex > if (TREE_CODE (TREE_TYPE (elt)) == UNION_TYPE) > CONSTRUCTOR_NO_CLEARING (elt) = false; > } > + if (complex_expr) I might have added the COMPLEX_EXPR to ctors instead of a separate variable, but this is fine too. > + { > + if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*complex_expr), > + TREE_OPERAND (*complex_expr, 0), > + TREE_OPERAND (*complex_expr, 1))) > + *complex_expr = c; > + else > + { > + TREE_CONSTANT (*complex_expr) > + = (TREE_CONSTANT (TREE_OPERAND (*complex_expr, 0)) > + & TREE_CONSTANT (TREE_OPERAND (*complex_expr, 1))); > + TREE_SIDE_EFFECTS (*complex_expr) > + = (TREE_SIDE_EFFECTS (TREE_OPERAND (*complex_expr, 0)) > + | TREE_SIDE_EFFECTS (TREE_OPERAND (*complex_expr, 1))); > + } > + } > > if (lval) > return target; > --- gcc/testsuite/g++.dg/cpp1y/constexpr-complex1.C.jj 2022-06-08 17:32:39.190148964 +0200 > +++ gcc/testsuite/g++.dg/cpp1y/constexpr-complex1.C 2022-06-08 17:29:04.413321741 +0200 > @@ -0,0 +1,24 @@ > +// PR c++/88174 > +// { dg-do compile { target c++14 } } > + > +constexpr bool > +foo (double x, double y, double z, double w) > +{ > + __complex__ double a = 0; > + __real__ a = x; > + __imag__ a = y; > +#if __cpp_constexpr >= 201907L > + __complex__ double b; > + __real__ b = z; > +#else > + __complex__ double b = z; > +#endif > + __imag__ b = w; > + a += b; > + a -= b; > + a *= b; > + a /= b; > + return __real__ a == x && __imag__ a == y; > +} > + > +static_assert (foo (1.0, 2.0, 3.0, 4.0), ""); > > Jakub >