From patchwork Fri Apr 12 21:24:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1084929 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-499207-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="lnlU0Zh6"; 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 44grWy0jkzz9s0W for ; Sat, 13 Apr 2019 07:24:59 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version :content-transfer-encoding; q=dns; s=default; b=FndcaHWv6d6YggBm m/H4MkHd1e3OYrQfW071HcPlta8p1h+nQfA98vxghSlCDtjAXrHA+zrzflL0lAih j4A/d2zkBf78pZ6Q6xhUHmpYuMl0k1ib2GTRkDMi42cn1AMgE84mzWUp3nYym0UV jogAlU65mGkD9bSlV7hTxprh3tM= 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 :to:subject:date:message-id:mime-version :content-transfer-encoding; s=default; bh=9shXF+dlS0010HTEnixz9k S09Mw=; b=lnlU0Zh6Tx4DbdZWYKthhXiMdSEJw2Gi7T1EelFUmCfpzXk1mPfh4O r6w5JfB5KEo6P5cgLYVIqYGRQhs5JpNBi+dR6gZvIlHVSPsmo+XsXVqwourTZuJS bZrY+jtluBc6bA7V28fDU5N90iftjRdacrNPpk7Ov5H4fhRrx9rJM= Received: (qmail 56022 invoked by alias); 12 Apr 2019 21:24:49 -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 39762 invoked by uid 89); 12 Apr 2019 21:24:34 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-19.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=dbgcnth, dbgcnt.h, UD:dbgcnt.h, stmt X-HELO: mail-qk1-f175.google.com Received: from mail-qk1-f175.google.com (HELO mail-qk1-f175.google.com) (209.85.222.175) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 12 Apr 2019 21:24:32 +0000 Received: by mail-qk1-f175.google.com with SMTP id b74so6544745qkg.9 for ; Fri, 12 Apr 2019 14:24:30 -0700 (PDT) Received: from orpheus.redhat.com (209-6-216-142.s141.c3-0.smr-cbr1.sbo-smr.ma.cable.rcncustomer.com. [209.6.216.142]) by smtp.gmail.com with ESMTPSA id y13sm23372075qkj.30.2019.04.12.14.24.27 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 12 Apr 2019 14:24:27 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [PATCH] (RFA tree-tailcall) PR c++/82081 - tail call optimization breaks noexcept Date: Fri, 12 Apr 2019 17:24:26 -0400 Message-Id: <20190412212426.3411-1-jason@redhat.com> MIME-Version: 1.0 X-IsSubscribed: yes If a noexcept function calls a function that might throw, doing the tail call optimization means that an exception thrown in the called function will propagate out, breaking the noexcept specification. So we need to prevent the optimization in that case. Tested x86_64-pc-linux-gnu. OK for trunk or hold for GCC 10? This isn't a regression, but it is a straightforward fix for a wrong-code bug. * tree-tailcall.c (find_tail_calls): Don't turn a call from a nothrow function to a might-throw function into a tail call. --- gcc/tree-tailcall.c | 7 +++++++ gcc/testsuite/g++.dg/tree-ssa/tail-call-1.C | 11 +++++++++++ gcc/ChangeLog | 6 ++++++ 3 files changed, 24 insertions(+) create mode 100644 gcc/testsuite/g++.dg/tree-ssa/tail-call-1.C base-commit: b7a39acf193fd19e41502d5e2cf6b3fa8b44dbac diff --git a/gcc/tree-tailcall.c b/gcc/tree-tailcall.c index afe8931b5f0..e0265b22dd5 100644 --- a/gcc/tree-tailcall.c +++ b/gcc/tree-tailcall.c @@ -37,6 +37,7 @@ along with GCC; see the file COPYING3. If not see #include "tree-into-ssa.h" #include "tree-dfa.h" #include "except.h" +#include "tree-eh.h" #include "dbgcnt.h" #include "cfgloop.h" #include "common/common-target.h" @@ -472,6 +473,12 @@ find_tail_calls (basic_block bb, struct tailcall **ret) && !auto_var_in_fn_p (ass_var, cfun->decl)) return; + /* If the call might throw an exception that wouldn't propagate out of + cfun, we can't transform to a tail or sibling call (82081). */ + if (stmt_could_throw_p (cfun, stmt) + && !stmt_can_throw_external (cfun, stmt)) + return; + /* We found the call, check whether it is suitable. */ tail_recursion = false; func = gimple_call_fndecl (call); diff --git a/gcc/testsuite/g++.dg/tree-ssa/tail-call-1.C b/gcc/testsuite/g++.dg/tree-ssa/tail-call-1.C new file mode 100644 index 00000000000..c67af6e41c7 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/tail-call-1.C @@ -0,0 +1,11 @@ +// PR c++/82081 +// { dg-do compile { target c++11 } } +// { dg-additional-options "-O2 -fdump-tree-optimized" } +// { dg-final { scan-tree-dump-not "tail call" "optimized" } } + +int g(int) ; + +int f() noexcept { + int i = 42, j = 43; + return g(i+j); +} diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 02d3d07d0e4..77ab20929ff 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2019-04-11 Jason Merrill + + PR c++/82081 - tail call optimization breaks noexcept + * tree-tailcall.c (find_tail_calls): Don't turn a call from a + nothrow function to a might-throw function into a tail call. + 2019-04-12 Jakub Jelinek PR rtl-optimization/89965