From patchwork Wed Feb 3 17:51:40 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 578264 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 79E85140BCB for ; Thu, 4 Feb 2016 04:52:04 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=yCXc6fwR; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :date:to:cc:subject:in-reply-to:message-id:references :mime-version:content-type; q=dns; s=default; b=HdoPA3foE3xnFvtd 8eoTLRv9oU0N8qandIqF0xMshu4HWWya9M+C3nEojf/nljHqk5f+QhD1mc/rmJaA 6GdKBTaCSFZD6jV53LEOxdQAsfus5LFY6cp5RuS/0lNgavTSk+n8eSq/0WEjUpVz ZZ+XL3q4excOfsPzPcFSWcreDU0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :date:to:cc:subject:in-reply-to:message-id:references :mime-version:content-type; s=default; bh=iFjmnZoGmExi3NjvsuxtUR ptOxg=; b=yCXc6fwRrejNwzZhzuirP6ezVuu+NoxBharWZoxxg9LjsaeX3oR1u8 1l9WIjpT/2c0amwPswcI3DlKmka41veyv+BBqYnaU0C2P1e80U0B8vxfBaEczami s5y0TdTh9R48Yd/kU6MKggODbrkYA4bO6IaTCW2F6sANORM47sXmU= Received: (qmail 14858 invoked by alias); 3 Feb 2016 17:51:51 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 14798 invoked by uid 89); 3 Feb 2016 17:51:51 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 spammy=sides, PTRMEM_CST, sk:CONSTAN, ne_expr X-HELO: mail-qg0-f41.google.com Received: from mail-qg0-f41.google.com (HELO mail-qg0-f41.google.com) (209.85.192.41) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Wed, 03 Feb 2016 17:51:49 +0000 Received: by mail-qg0-f41.google.com with SMTP id u30so21502701qge.1 for ; Wed, 03 Feb 2016 09:51:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:date:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version:content-type; bh=ZbChe2fTLu4EgygHVoQvVbQ0pkEIF0rqiolqLjwpSMY=; b=AKxGKrB+zhADosj8EtQDFxhgcysTw3LXLtrbQ6Qt8SAuGjKKnj8hKPrQet7hbtfWXy V0IzfVVTqGFk+FbOZC59LH7/DvyrUup/F+2RfWiEFdAFhfvM/jkiHg6/nso2ACzAXNXP IfKj1Q54GbYWBFVZsf1Fc/FrD8AFbIeXUn2mUI2Aa6wAaGygVQCWEUdErW/LQuZEwvXc lZm8hpucgKoP9W1hD8Shw7fYipN0upgSOTdJ7JyfKuQuI32N6KYZ7Dqgl/jGJTrl0QmQ h8JyfbdEDcSIswycgR/ILYZdCt7l633vgsyZHYt/mLnsBhcnHcYMe4/5kJ5PzgBGoZg6 2GkA== X-Gm-Message-State: AG10YOQr+u//Vg5/nuugQ5BQPiDhCeu80ycPxxP0rMfIL0YRp0fcGFVfG0NIThbLaaglDA== X-Received: by 10.140.82.11 with SMTP id g11mr3257908qgd.77.1454521906981; Wed, 03 Feb 2016 09:51:46 -0800 (PST) Received: from [192.168.1.130] (ool-4353abbc.dyn.optonline.net. [67.83.171.188]) by smtp.gmail.com with ESMTPSA id w71sm3091250qha.30.2016.02.03.09.51.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 Feb 2016 09:51:45 -0800 (PST) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Wed, 3 Feb 2016 12:51:40 -0500 (EST) To: Jason Merrill cc: Patrick Palka , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] Fix constexpr evaluation of comparisons involving pointer-to-members In-Reply-To: <56B0FAA2.3080804@redhat.com> Message-ID: References: <1450739227-18825-1-git-send-email-patrick@parcs.ath.cx> <56B0FAA2.3080804@redhat.com> User-Agent: Alpine 2.20.9 (DEB 106 2015-09-22) MIME-Version: 1.0 On Tue, 2 Feb 2016, Jason Merrill wrote: > On 12/22/2015 12:07 AM, Patrick Palka wrote: >> + if (code == EQ_EXPR || code == NE_EXPR) >> + { >> + if (TREE_CODE (lhs) == PTRMEM_CST && CONSTANT_CLASS_P (rhs)) >> + lhs = cplus_expand_constant (lhs); >> + if (TREE_CODE (rhs) == PTRMEM_CST && CONSTANT_CLASS_P (lhs)) >> + rhs = cplus_expand_constant (rhs); >> + } > > If both sides are PTRMEM_CST, we should be able to compare them without > expanding, using cp_tree_equal. Ah, okay. Here's an updated patch that uses cp_tree_equal and avoids calling cplus_expand_constant altogether, by assuming that we should only expect to compare a PTRMEM_CST against another PTRMEM_CST or against the constant -1. I also improved the coverage of the test case. Does this look reasonable? gcc/cp/ChangeLog: * constexpr.c (cxx_eval_binary_expression): Fold equality comparisons involving PTRMEM_CSTs. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-ptrmem5.C: New test. --- gcc/cp/constexpr.c | 21 +++++++++++++++++++-- gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index b076991..c5e6642 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -1593,7 +1593,7 @@ cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, bool /*lval*/, bool *non_constant_p, bool *overflow_p) { - tree r; + tree r = NULL_TREE; tree orig_lhs = TREE_OPERAND (t, 0); tree orig_rhs = TREE_OPERAND (t, 1); tree lhs, rhs; @@ -1612,7 +1612,24 @@ cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, location_t loc = EXPR_LOCATION (t); enum tree_code code = TREE_CODE (t); tree type = TREE_TYPE (t); - r = fold_binary_loc (loc, code, type, lhs, rhs); + + if ((code == EQ_EXPR || code == NE_EXPR)) + { + bool is_code_eq = (code == EQ_EXPR); + + if (TREE_CODE (lhs) == PTRMEM_CST + && TREE_CODE (rhs) == PTRMEM_CST) + r = constant_boolean_node (cp_tree_equal (lhs, rhs) == is_code_eq, + type); + else if ((TREE_CODE (lhs) == PTRMEM_CST + || TREE_CODE (rhs) == PTRMEM_CST) + && (integer_minus_onep (lhs) + || integer_minus_onep (rhs))) + r = constant_boolean_node (code == NE_EXPR, type); + } + + if (r == NULL_TREE) + r = fold_binary_loc (loc, code, type, lhs, rhs); if (r == NULL_TREE) { if (lhs == orig_lhs && rhs == orig_rhs) diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C new file mode 100644 index 0000000..b1318c4 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C @@ -0,0 +1,17 @@ +// { dg-do compile { target c++11 } } + +#define SA(x) static_assert ((x), #x) + +struct X { int a, b; }; + +void +foo () +{ + SA (&X::a); + SA (&X::a == &X::a); + SA (!(&X::a != &X::a)); + SA (&X::a != &X::b); + SA (!(&X::a == &X::b)); + SA ((!&X::b) == 0); + SA (!(&X::b == 0)); +}