From patchwork Tue Nov 22 09:00:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 697597 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 3tNKFb5n9Jz9s65 for ; Tue, 22 Nov 2016 20:01:07 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="pEFYqdr9"; dkim-atps=neutral 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-type; q=dns; s= default; b=ZGZNb41/2qQHsGpbeFk9zytmwZonFubTSNk1RVtOMb00Fqy6b2w90 UWYZ0Hjd1ePBFqVHaYtExPc8micGVkClyDpzeV6TFlwRBXn+R9zsn2jfaclOcude 8nzAQnPQuPzNfNpcQBdfbrIqiPt2LCniC3yZx9Dxr47lN4pRXQoro8= 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-type; s= default; bh=xPJp9FlKW7gkqNMCtJsEE8RUl4Y=; b=pEFYqdr966Yd3AaurFJp tTEDqeh+bNyMkedtO3eB5XQUixbmanMKGtX14g2pBGEpMBM+XVC2Khrq4ceaEsGD 7yctKF5h2SVwpmgifQniPMZ67zSV14FviSCdD1/aiYKNoj2VBx5fn4oXLiVM7UMr 0YHVqRMRbVH4EZQqxnZrzso= Received: (qmail 51831 invoked by alias); 22 Nov 2016 09:00:58 -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 51797 invoked by uid 89); 22 Nov 2016 09:00:57 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.9 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy= X-HELO: foss.arm.com Received: from foss.arm.com (HELO foss.arm.com) (217.140.101.70) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 22 Nov 2016 09:00:55 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 97CC51515; Tue, 22 Nov 2016 01:00:54 -0800 (PST) Received: from localhost (e105548-lin.manchester.arm.com [10.45.32.67]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 427143F483 for ; Tue, 22 Nov 2016 01:00:54 -0800 (PST) From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@arm.com Subject: Tighten check for whether a sibcall references local variables Date: Tue, 22 Nov 2016 09:00:52 +0000 Message-ID: <87polnq46z.fsf@e105548-lin.cambridge.arm.com> User-Agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 This loop: /* Make sure the tail invocation of this function does not refer to local variables. */ FOR_EACH_LOCAL_DECL (cfun, idx, var) { if (TREE_CODE (var) != PARM_DECL && auto_var_in_fn_p (var, cfun->decl) && (ref_maybe_used_by_stmt_p (call, var) || call_may_clobber_ref_p (call, var))) return; } triggered even for local variables that are passed by value. This meant that we didn't allow local aggregates to be passed to a sibling call but did (for example) allow global aggregates to be passed. I think the loop is really checking for indirect references, so should be able to skip any variables that never have their address taken. Tested on aarch64-linux-gnu and x86_64-linux-gnu. OK to install? Thanks, Richard gcc/ * tree-tailcall.c (find_tail_calls): Allow calls to reference local variables if all references are known to be direct. gcc/testsuite/ * gcc.dg/tree-ssa/tailcall-8.c: New test. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/tailcall-8.c b/gcc/testsuite/gcc.dg/tree-ssa/tailcall-8.c new file mode 100644 index 0000000..ffeabe5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/tailcall-8.c @@ -0,0 +1,80 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-tailc-details" } */ + +struct s { int x; }; +void f_direct (struct s); +void f_indirect (struct s *); +void f_void (void); + +/* Tail call. */ +void +g1 (struct s param) +{ + f_direct (param); +} + +/* Tail call. */ +void +g2 (struct s *param_ptr) +{ + f_direct (*param_ptr); +} + +/* Tail call. */ +void +g3 (struct s *param_ptr) +{ + f_indirect (param_ptr); +} + +/* Tail call. */ +void +g4 (struct s *param_ptr) +{ + f_indirect (param_ptr); + f_void (); +} + +/* Tail call. */ +void +g5 (struct s param) +{ + struct s local = param; + f_direct (local); +} + +/* Tail call. */ +void +g6 (struct s param) +{ + struct s local = param; + f_direct (local); + f_void (); +} + +/* Not a tail call. */ +void +g7 (struct s param) +{ + struct s local = param; + f_indirect (&local); +} + +/* Not a tail call. */ +void +g8 (struct s *param_ptr) +{ + struct s local = *param_ptr; + f_indirect (&local); +} + +/* Not a tail call. */ +void +g9 (struct s *param_ptr) +{ + struct s local = *param_ptr; + f_indirect (&local); + f_void (); +} + +/* { dg-final { scan-tree-dump-times "Found tail call" 6 "tailc" } } */ diff --git a/gcc/tree-tailcall.c b/gcc/tree-tailcall.c index f97541d..66a0a4c 100644 --- a/gcc/tree-tailcall.c +++ b/gcc/tree-tailcall.c @@ -504,12 +504,14 @@ find_tail_calls (basic_block bb, struct tailcall **ret) tail_recursion = true; } - /* Make sure the tail invocation of this function does not refer - to local variables. */ + /* Make sure the tail invocation of this function does not indirectly + refer to local variables. (Passing variables directly by value + is OK.) */ FOR_EACH_LOCAL_DECL (cfun, idx, var) { if (TREE_CODE (var) != PARM_DECL && auto_var_in_fn_p (var, cfun->decl) + && may_be_aliased (var) && (ref_maybe_used_by_stmt_p (call, var) || call_may_clobber_ref_p (call, var))) return;