From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 126132 invoked by alias); 24 Nov 2017 21:35:10 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 126110 invoked by uid 89); 24 Nov 2017 21:35:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.7 required=5.0 tests=BAYES_00,GIT_PATCH_2,GIT_PATCH_3,KB_WAM_FROM_NAME_SINGLEWORD,SPF_HELO_PASS,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 24 Nov 2017 21:35:07 +0000 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 40F86C0587E2; Fri, 24 Nov 2017 21:35:06 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-116-247.ams2.redhat.com [10.36.116.247]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D88555C88A; Fri, 24 Nov 2017 21:35:05 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id vAOLZ28h010344; Fri, 24 Nov 2017 22:35:03 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id vAOLZ08q010343; Fri, 24 Nov 2017 22:35:00 +0100 Date: Fri, 24 Nov 2017 21:42:00 -0000 From: Jakub Jelinek To: Jason Merrill , Nathan Sidwell Cc: gcc-patches@gcc.gnu.org Subject: [C++ PATCH] Optimize void COND_EXPRs during cp_fold (PR c++/81675) Message-ID: <20171124213500.GA14653@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.7.1 (2016-10-04) X-IsSubscribed: yes X-SW-Source: 2017-11/txt/msg02224.txt.bz2 Hi! The comment says that trying to fold VOID_TYPE_P COND_EXPRs is not worth bothering, but as the following testcase shows, that is not the case. fold_ternary can optimize COND_EXPRs where the condition is constant and the unused branch doesn't have any labels, and not folding it early means bogus warnings afterwards. Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2017-11-24 Jakub Jelinek PR c++/81675 * cp-gimplify.c (cp_fold) : Don't return immediately for VOID_TYPE_P COND_EXPRs, instead fold the operands and if op0 is INTEGER_CST, ensure that both op1 and op2 are non-NULL and fall through into normal folding, otherwise just rebuild x if any op changed. * g++.dg/warn/pr81675.C: New test. --- gcc/cp/cp-gimplify.c.jj 2017-11-21 20:23:01.000000000 +0100 +++ gcc/cp/cp-gimplify.c 2017-11-24 11:09:51.879647584 +0100 @@ -2299,13 +2299,6 @@ cp_fold (tree x) case VEC_COND_EXPR: case COND_EXPR: - - /* Don't bother folding a void condition, since it can't produce a - constant value. Also, some statement-level uses of COND_EXPR leave - one of the branches NULL, so folding would crash. */ - if (VOID_TYPE_P (TREE_TYPE (x))) - return x; - loc = EXPR_LOCATION (x); op0 = cp_fold_rvalue (TREE_OPERAND (x, 0)); op1 = cp_fold (TREE_OPERAND (x, 1)); @@ -2319,6 +2312,29 @@ cp_fold (tree x) if (!VOID_TYPE_P (TREE_TYPE (op2))) op2 = cp_truthvalue_conversion (op2); } + else if (VOID_TYPE_P (TREE_TYPE (x))) + { + if (TREE_CODE (op0) == INTEGER_CST) + { + /* If the condition is constant, fold can fold away + the COND_EXPR. If some statement-level uses of COND_EXPR + have one of the branches NULL, avoid folding crash. */ + if (!op1) + op1 = build_empty_stmt (loc); + if (!op2) + op2 = build_empty_stmt (loc); + } + else + { + /* Otherwise, don't bother folding a void condition, since + it can't produce a constant value. */ + if (op0 != TREE_OPERAND (x, 0) + || op1 != TREE_OPERAND (x, 1) + || op2 != TREE_OPERAND (x, 2)) + x = build3_loc (loc, code, TREE_TYPE (x), op0, op1, op2); + break; + } + } if (op0 != TREE_OPERAND (x, 0) || op1 != TREE_OPERAND (x, 1) --- gcc/testsuite/g++.dg/warn/pr81675.C.jj 2017-11-24 11:12:13.962912829 +0100 +++ gcc/testsuite/g++.dg/warn/pr81675.C 2017-11-24 11:11:56.000000000 +0100 @@ -0,0 +1,15 @@ +// PR c++/81675 +// { dg-do compile } +// { dg-options "-Wall" } + +struct S +{ + ~S () __attribute__((noreturn)); + int a; +}; + +int +foo () +{ + false ? 5 : S ().a; +} Jakub