From patchwork Mon Oct 24 07:27:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 8184 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp302035wru; Mon, 24 Oct 2022 00:28:35 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7K7kOT+arora1r+Ghp3EI3w211Gibi18P7p+F+tYIixedkcsd5MXHOwHOcvpyhcMmE7hTq X-Received: by 2002:a17:906:8457:b0:78d:b793:5511 with SMTP id e23-20020a170906845700b0078db7935511mr26076856ejy.393.1666596514862; Mon, 24 Oct 2022 00:28:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666596514; cv=none; d=google.com; s=arc-20160816; b=vtUhJjqLqmuPZ3xfzCS5+f8Zkhe/UbjnFuKKeL/C6K7cXmZxRbY64vnPQ4ThxOuiCG jVQ+iGePwj6+VWtoV8SIq4NYA1VnJ1RQS7KqboFtRhQUr8zHHq2vrE5FiMdFhX8291GO qVd7+snYqzvduaiwNhGe5y+Riz1e7A/KIYZZp37SViEle2uhkwkyvgRkg8cq1+EOUW2S xpF+tASKL7kQk95oxcepN07VkPxrluenLuczxuh/NHI34hIQ3NiQ19CabxudKB+TXCO7 OloyNb3xUzTh3U76Zi7OAP1DmuDHgAv+8M9Y4+dO6KwJzWk4jbd20uxy78L716tU686b y58Q== 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:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=9WDcj4GulQCci1C9CQxnwVMM2CvfcCKYGE71bFeHqao=; b=0oiXPjRS/r5VvHOT+iPgwEGPrFq87TjMvh9WoOY0C9ulkdRjZjBT8wdOGAPMY68ITU Cqi/TaVStzvMo7sxDLW8Gc7nEoDwZ+FWCoeO6sdkHkXRi0MR8Vh/QqQftpwyhpi+vWzZ gON3qXR5xShcI1z2goZuov8telptEdUs0ioOreLjRvz1XmZ0jqTrtCJNe1rwr31c3++v aQiZG7XN0ZWp6ZfgCEqP26/Q3bd/AFqHaOJC60etJv49uybqaDKex64n9mTDNI2Xa4nj /TEZ4yCJPHBxZ54lcpbAa5z2yyCFVVKPQJ3l9SKq0oLf3kHfCYOEJOaJV5UEjTzT1Aql +KNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=WYpRw4Nb; 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 ae8-20020a17090725c800b0078dce38525asi29649815ejc.896.2022.10.24.00.28.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Oct 2022 00:28:34 -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=WYpRw4Nb; 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 E7A453857342 for ; Mon, 24 Oct 2022 07:28:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E7A453857342 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666596513; bh=9WDcj4GulQCci1C9CQxnwVMM2CvfcCKYGE71bFeHqao=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=WYpRw4NbhQRFEwXaVyRDuer4pV4+Bv9dqfiMtUJR74qQVYdbohqG9woDxYVQNvu+H Fy0eOq4z9nyzHqY9ofkK/eyKQhhRnq4NQLLC+d8i1qPJLsTtt3wnOjb2f1Z29KErUq YpssacoQ2mQ4XtKeX0dZh/MCrNbduu4w7HG6Jygg= 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 820AB385840A for ; Mon, 24 Oct 2022 07:27:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 820AB385840A Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-135-d9TtLcl7PL-BhOS26aXMQA-1; Mon, 24 Oct 2022 03:27:31 -0400 X-MC-Unique: d9TtLcl7PL-BhOS26aXMQA-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 D30111C07552 for ; Mon, 24 Oct 2022 07:27:30 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.193.252]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8B4CC39DB3; Mon, 24 Oct 2022 07:27:28 +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 29O7RQmd3042803 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 24 Oct 2022 09:27:26 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 29O7RPQQ3042802; Mon, 24 Oct 2022 09:27:25 +0200 Date: Mon, 24 Oct 2022 09:27:25 +0200 To: Jason Merrill Subject: [PATCH] c++: Fix up constexpr handling of char/signed char/short pre/post inc/decrement [PR105774] 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=-4.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, 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?1747553107242836336?= X-GMAIL-MSGID: =?utf-8?q?1747553107242836336?= Hi! signed char, char or short int pre/post inc/decrement are represented by normal {PRE,POST}_{INC,DEC}REMENT_EXPRs in the FE and only gimplification ensures that the {PLUS,MINUS}_EXPR is done in unsigned version of those types: case PREINCREMENT_EXPR: case PREDECREMENT_EXPR: case POSTINCREMENT_EXPR: case POSTDECREMENT_EXPR: { tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 0)); if (INTEGRAL_TYPE_P (type) && c_promoting_integer_type_p (type)) { if (!TYPE_OVERFLOW_WRAPS (type)) type = unsigned_type_for (type); return gimplify_self_mod_expr (expr_p, pre_p, post_p, 1, type); } break; } This means during constant evaluation we need to do it similarly (either using unsigned_type_for or using widening to integer_type_node). The following patch does the latter. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-10-24 Jakub Jelinek PR c++/105774 * constexpr.cc (cxx_eval_increment_expression): For signed types that promote to int, evaluate PLUS_EXPR or MINUS_EXPR in int type. * g++.dg/cpp1y/constexpr-105774.C: New test. Jakub --- gcc/cp/constexpr.cc.jj 2022-10-21 18:04:47.869797312 +0200 +++ gcc/cp/constexpr.cc 2022-10-23 18:43:27.003390282 +0200 @@ -6234,6 +6234,18 @@ cxx_eval_increment_expression (const con offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); } + else if (c_promoting_integer_type_p (type) + && !TYPE_UNSIGNED (type) + && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) + { + offset = fold_convert (integer_type_node, offset); + mod = fold_convert (integer_type_node, val); + tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node, + mod, offset); + mod = fold_convert (type, t); + if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t)) + TREE_OVERFLOW (mod) = false; + } else mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); if (!ptr) --- gcc/testsuite/g++.dg/cpp1y/constexpr-105774.C.jj 2022-10-23 18:44:15.587729613 +0200 +++ gcc/testsuite/g++.dg/cpp1y/constexpr-105774.C 2022-10-23 18:33:54.754170726 +0200 @@ -0,0 +1,15 @@ +// PR c++/105774 +// { dg-do compile { target c++14 } } + +constexpr signed char +foo () +{ +#if __SCHAR_MAX__ < __INT_MAX__ + signed char x = __SCHAR_MAX__; +#else + signed char x = 0; +#endif + return ++x; +} + +constexpr auto a = foo ();