From patchwork Fri Nov 24 21:35:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 841176 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-467881-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="jJftKfsO"; dkim-atps=neutral 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 3yk8cR3cyYz9ryv for ; Sat, 25 Nov 2017 08:35:17 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; q=dns; s=default; b=jZlzEYGEaAnMfbokfPm2OjvbdjOk0 kYo6UdUiioYKtM/xakqEzhztSuXC1BPhi+0i/i6tZkfdWwEJ2RQ9Z0tSZ6L8NC3p 23ASBxALNYWSEMdOaFD3Mn8suoXIhIygjHvvQmmTbkeEK/1/VUnKv9MBwZx4ZtWA uqQRYmDenQjYd0= 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:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; s=default; bh=OnO1yDOJofL+C7XlwrTOeXusWhc=; b=jJf tKfsO3mzobEldyXmnN6nVYtQ4dxlk+Lv0EkIUaAFQ616OwEjnF7xWWrHpVEhjUM/ 23Vw3v05nz7MnIGEzocPCqH8iLR/YokmzL+5U/CE/p62lwOCUCbnHAATmwWOT/Ab G59RvQmUtrbQwdyiAnH5r5LLEkD5EDRj4ZBZM0Tc= 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-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 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 22:35:00 +0100 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-Disposition: inline User-Agent: Mutt/1.7.1 (2016-10-04) X-IsSubscribed: yes 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. Jakub --- 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; +}