From patchwork Thu Sep 29 17:49:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1554 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp98878wrs; Thu, 29 Sep 2022 10:50:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7w/1VoFip7ZUzP5s4q/bW4HgCkrYdb/VvnbZNfMtG9078BGJK3JbTYwqTyNihslot2GvdV X-Received: by 2002:aa7:c050:0:b0:453:4427:a947 with SMTP id k16-20020aa7c050000000b004534427a947mr4495470edo.172.1664473848051; Thu, 29 Sep 2022 10:50:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664473848; cv=none; d=google.com; s=arc-20160816; b=Ap7F3Ps1X+oJ1QWApdqFtU4Z/GkTa5SxKXjF2JIUtrmvacGX5Gx7fyDe8PcOwbPmQo 84/+ZGV3kRITRARb/gttpgUzbS8HLxnpVn6oOp4GLvyxAnAerT8wDxbLOs5YZSWyKaOF CUQQKe4Ln19NXE6+Iw4YMZpDLscQDnlmaao4DP6XoeYnoU2AnPxlf+Y8VzrGRwoj8EIp lRTt6U0BcPWKDnFtQc3fhFxk98T54a+bwa6h6tatV4EghcGXkjNf8e19VTZA2weTPU6n XOGvDl5J8rOhqvHaGQN83Uuoyrz86HYpHWi8i5VXq0FeJ9hSTBP0CIZNY0b4HYLzfKex J7FQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=0/EqvCHpvs1HQ3Z4WVfCYCMFoPuYhqNEb6bkXTi2CAQ=; b=EWhzV912zXmbqG0TIq0+16w/lrAqtEv/HcQU0KNDqH8d/NkV0avqDLOVbNBeZrYMjd dvi7qcYPFYXCoaX+erSYpVbV8YhyL/DmFHsqcstW9u1xzNXOzkWz8vAb9smqj4ye9Id/ TNPU8EI5BPEabUhfiEFbg+028A2p2Xcg1LYtO+gwyi5KRadsn/5ENJhHJKBHPzlK1gmm PYdVD/eeM71APt02HBGEhecvTlCaN4gK+e2WBrDyxDnoUoJElilbqEqdHW32JFFzcDJT 0SFXwc9KbfdNzpbNuHOxMY2pozgHTMq3WIQxT6hsvgge6jLgHsOjGNgW2/SYEYjU8o35 zoHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=BrPPeC5V; 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 i10-20020a05640242ca00b00456e52165bdsi86740edc.577.2022.09.29.10.50.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Sep 2022 10:50:48 -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=BrPPeC5V; 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 22D43385C400 for ; Thu, 29 Sep 2022 17:50:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 22D43385C400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664473847; bh=0/EqvCHpvs1HQ3Z4WVfCYCMFoPuYhqNEb6bkXTi2CAQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=BrPPeC5VBltNYL7t9UtSZwmK+1a/WDqY7b46CJoH+LgReYb62VCBkfhnB14igZkP0 C7P6K68Z6q04iVTcFF6vZGG+5Mzs7jtYvKtE/s/qLBeZXrgz0bcYlnh2XIvB8NK/UF ZLmGjZd/fGFMjCvgb1gj/FsCxe75yNX39tvxgs+Y= 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 8C6D7385C421 for ; Thu, 29 Sep 2022 17:50:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8C6D7385C421 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-453-v5hKnEj8O7KCPDjKVlUpEQ-1; Thu, 29 Sep 2022 13:49:59 -0400 X-MC-Unique: v5hKnEj8O7KCPDjKVlUpEQ-1 Received: by mail-qk1-f200.google.com with SMTP id r14-20020a05620a298e00b006be796b6164so1738680qkp.19 for ; Thu, 29 Sep 2022 10:49:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date; bh=0/EqvCHpvs1HQ3Z4WVfCYCMFoPuYhqNEb6bkXTi2CAQ=; b=d7vd+mCSp6ubaA4rkHTGVfaabUOoe/2omsxWOpncsvpC9cz65uu+uP1GSFyA+OCwV9 jLCiB92Yx+QO5su4CixyvVwFd72VfYU/zCDZoGKO/Ubf2pIIUCrDLfrehJqvnYDDZeGx qVOMf+/us1aDsG8oEBDxmJAwWBxElokcOQnLPtk8UZhxS0rXb9ZfxrxEeHa2+PbLoylD Kw7zbwvaykHu7Q7M7uV/dJBH/QexmGUpuWiG9J3tz8xOIfiKh1cko8X91yfOwyS/qdq2 vt82nSfOdq6gArQI55yrxyv7wyzj/g0wKp1Fn4VOs8yp816onhaUzw8qKcjGnRX5UmGe qHsA== X-Gm-Message-State: ACrzQf1LrYhQbRHjyo2ZlqjLyx/gezcx/RBVaQGSDj9Qu9Fg9shi+sm8 eEmccU+iTHoiQKzc5/tvtqZVprpOW+bTAk6mJEH/ZmZWe/ApsZoj3JSL/xMaEi4rWWvJAm/bn4c P9tNsJNSI1ybZwKI+3YpJ55Y/tz3JEaoQlzFB+tkZrSM1x+szdErUXfbXK0RGkwmJGA== X-Received: by 2002:a05:6214:e66:b0:4af:6d29:93ca with SMTP id jz6-20020a0562140e6600b004af6d2993camr3578954qvb.13.1664473799007; Thu, 29 Sep 2022 10:49:59 -0700 (PDT) X-Received: by 2002:a05:6214:e66:b0:4af:6d29:93ca with SMTP id jz6-20020a0562140e6600b004af6d2993camr3578933qvb.13.1664473798679; Thu, 29 Sep 2022 10:49:58 -0700 (PDT) Received: from barrymore.redhat.com (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 z25-20020ac81019000000b0035d432f5ba3sm5802850qti.17.2022.09.29.10.49.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Sep 2022 10:49:58 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: reduce temporaries in ?: Date: Thu, 29 Sep 2022 13:49:56 -0400 Message-Id: <20220929174956.1915381-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill 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?1745327329836844950?= X-GMAIL-MSGID: =?utf-8?q?1745327329836844950?= Tested x86_64-pc-linux-gnu, applying to trunk. -- >8 -- When the sides of ?: are class prvalues, we wrap the COND_EXPR in a TARGET_EXPR so that both sides will initialize the same temporary. But in this case we were stripping the outer TARGET_EXPR and conditionally creating different temporaries, unnecessarily using extra stack. The recently added TARGET_EXPR_NO_ELIDE flag avoids this. gcc/cp/ChangeLog: * call.cc (build_conditional_expr): Set TARGET_EXPR_NO_ELIDE on the outer TARGET_EXPR. gcc/testsuite/ChangeLog: * g++.dg/tree-ssa/cond-temp1.C: New test. --- gcc/cp/call.cc | 8 +++++++- gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C base-commit: af9034827e8f06f10767064e9fc7443b94e08184 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 3506b0fcfbb..9fad3cb950b 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -6009,7 +6009,13 @@ build_conditional_expr (const op_location_t &loc, but now we sometimes wrap them in NOP_EXPRs so the test would fail. */ if (CLASS_TYPE_P (TREE_TYPE (result))) - result = get_target_expr (result, complain); + { + result = get_target_expr (result, complain); + /* Tell gimplify_modify_expr_rhs not to strip this in + assignment context: we want both arms to initialize + the same temporary. */ + TARGET_EXPR_NO_ELIDE (result) = true; + } /* If this expression is an rvalue, but might be mistaken for an lvalue, we must add a NON_LVALUE_EXPR. */ result = rvalue (result); diff --git a/gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C b/gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C new file mode 100644 index 00000000000..b15635853f2 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C @@ -0,0 +1,16 @@ +// Test that the ?: only creates one temporary. +// { dg-additional-options "-fdump-tree-gimple" } +// { dg-final { scan-tree-dump-times "struct A" 2 "gimple" } } + +struct A +{ + int i; + A(int); +}; + +bool b; +int main() +{ + A a = 1; + a = b ? A(2) : A(3); +}