From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1664473802; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=0/EqvCHpvs1HQ3Z4WVfCYCMFoPuYhqNEb6bkXTi2CAQ=; b=bIQiXqiPsAAWYw6V5DAqoREHmkyXmAJBYaGzRKPEZHmYbPVVQSk6br6qU+yt8adfFwUnDd 07MmpVrXQGFK84nB/P1YAOppWFDJ0NXuiUhbcbAldtbHXgpRYSDrCQheBJSqUcCuAQH3rB 531vuXtki+yMIh1K0OesLE8h8Op6hUs= 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-Google-Smtp-Source: AMsMyM5eQJgPQpTtaUFEohBBGl5u57NhTQUrZ3z4USoKamDDfDUgJSDDlWUZH47D7xrZS5TRA9qevw== 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) From: Jason Merrill 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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 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); +} base-commit: af9034827e8f06f10767064e9fc7443b94e08184 -- 2.31.1