From patchwork Thu Nov 24 07:18:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 83797 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp24105qgi; Wed, 23 Nov 2016 23:18:39 -0800 (PST) X-Received: by 10.84.216.28 with SMTP id m28mr2251125pli.107.1479971919152; Wed, 23 Nov 2016 23:18:39 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id q197si38053752pfq.263.2016.11.23.23.18.38 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Nov 2016 23:18:39 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-442499-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-442499-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-442499-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=XYGxQLOnI+qNGM9GEG9bH1hnENIMvotHGsYYopnSMDUyKC 9Ux3l/O7afGd9EAgvAEgRmX5W2ZmCNnaj1cjHgfUAM3Yd2U83w+fIwPeSVPe50er c+Gt8aFtFTcUSWuKwz0KXELQtdqyULlVLlvPeQ6jcaTB0mVkYT5iL8JEA/jCA= 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 :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=J8clvC0OAG0UU7+Xgq+UtuX5nyg=; b=gNYFpjZdduBErw64Pzah kHKzEJgvjkX7Ohufa9jXWa/0ANWdmg1G9uFuUsRovFegg6RsGqX7+dhESAbtPwe7 KECC5OKPG80FAiu1hIq6GXWNypwAk+Pt6ex93rPKfzkB4tUMwzR22i+YalK29zlH olp5w8vjM7Kp4LqKqkNJsEw= Received: (qmail 127565 invoked by alias); 24 Nov 2016 07:18:22 -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 127547 invoked by uid 89); 24 Nov 2016 07:18:21 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=Cross, UD:gimple.h, gimple.h, gimpleh X-HELO: mail-io0-f170.google.com Received: from mail-io0-f170.google.com (HELO mail-io0-f170.google.com) (209.85.223.170) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 24 Nov 2016 07:18:11 +0000 Received: by mail-io0-f170.google.com with SMTP id x94so65320415ioi.3 for ; Wed, 23 Nov 2016 23:18:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=o8fsM971/YPbWLuBy6QRCv1FAh3vgBzprtqfMjIHg9w=; b=eqgooY7JRA0Cv888lSo1WWKZphIszdyOxvpdFd5VvIBnjoVRuGBEYwikTFFVivu3gd WjUiEfZlJqDjI587ORQ/3bckZwgyA0UkxnLIiVHejqvZHfZi+kEsxu0TE/4xMGKEAutB gMdVeKuNH+VAeOczj8aE2kscys51Hfuz2InQVbpC2ILtcTCLs+bM7tzF70H8i53fRUtm MsG3PpOqqellE9xYsj2Mon5T6wmo540X0/m4sfhbuhSJhkEd1LSLKZuWCP9tbV1LWS/2 0QSFhmBYkmkaY1NM1TbyArRnSKF7+N9zvX8kpmiDNBdb8RMTexg6lRthmCzOW+kNHwYD lpLg== X-Gm-Message-State: AKaTC01yl64HTt4O9puryIdYh2jMWy+D0XB1oIpMU8+j6X1heelGBM1tPrCEX2oM2ctMBcBIVcQ+S99yrbOKZzc5 X-Received: by 10.36.194.70 with SMTP id i67mr806877itg.21.1479971889359; Wed, 23 Nov 2016 23:18:09 -0800 (PST) MIME-Version: 1.0 Received: by 10.107.136.156 with HTTP; Wed, 23 Nov 2016 23:18:08 -0800 (PST) From: Prathamesh Kulkarni Date: Thu, 24 Nov 2016 12:48:08 +0530 Message-ID: Subject: [tree-tailcall] Check if function returns it's argument To: gcc Patches , Richard Biener X-IsSubscribed: yes Hi, Consider following test-case: void *f(void *a1, void *a2, __SIZE_TYPE__ a3) { __builtin_memcpy (a1, a2, a3); return a1; } return a1 can be considered equivalent to return value of memcpy, and the call could be emitted as a tail-call. gcc doesn't emit the above call to memcpy as a tail-call, but if it is changed to: void *t1 = __builtin_memcpy (a1, a2, a3); return t1; Then memcpy is emitted as a tail-call. The attached patch tries to handle the former case. Bootstrapped+tested on x86_64-unknown-linux-gnu. Cross tested on arm*-*-*, aarch64*-*-* Does this patch look OK ? Thanks, Prathamesh 2016-11-24 Prathamesh Kulkarni * gimple.c (gimple_call_return_arg): New function. * gimple.h (gimple_call_return_arg): Declare. * tree-tailcall.c (find_tail_calls): Call gimple_call_return_arg. testsuite/ * gcc.dg/tree-ssa/tailcall-8.c: New test. diff --git a/gcc/gimple.c b/gcc/gimple.c index 0a3dc72..ec460fc 100644 --- a/gcc/gimple.c +++ b/gcc/gimple.c @@ -2561,6 +2561,23 @@ gimple_call_combined_fn (const gimple *stmt) return CFN_LAST; } +/* Return arg, if function returns it's argument or NULL if it doesn't. */ +tree +gimple_call_return_arg (gcall *call_stmt) +{ + unsigned rf = gimple_call_return_flags (call_stmt); + if (rf & ERF_RETURNS_ARG) + { + unsigned argnum = rf & ERF_RETURN_ARG_MASK; + if (argnum < gimple_call_num_args (call_stmt)) + { + tree arg = gimple_call_arg (call_stmt, argnum); + return arg; + } + } + return NULL_TREE; +} + /* Return true if STMT clobbers memory. STMT is required to be a GIMPLE_ASM. */ diff --git a/gcc/gimple.h b/gcc/gimple.h index 0d0296e..ebccbe1 100644 --- a/gcc/gimple.h +++ b/gcc/gimple.h @@ -1520,6 +1520,7 @@ extern combined_fn gimple_call_combined_fn (const gimple *); extern bool gimple_call_builtin_p (const gimple *); extern bool gimple_call_builtin_p (const gimple *, enum built_in_class); extern bool gimple_call_builtin_p (const gimple *, enum built_in_function); +extern tree gimple_call_return_arg (gcall *); extern bool gimple_asm_clobbers_memory_p (const gasm *); extern void dump_decl_set (FILE *, bitmap); extern bool nonfreeing_call_p (gimple *); 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..b3fdc6c --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/tailcall-8.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-tailc-details" } */ + +void *f(void *a1, void *a2, __SIZE_TYPE__ a3) +{ + __builtin_memcpy (a1, a2, a3); + return a1; +} + +/* { dg-final { scan-tree-dump-times "Found tail call" 1 "tailc" } } */ diff --git a/gcc/tree-tailcall.c b/gcc/tree-tailcall.c index f97541d..3396473 100644 --- a/gcc/tree-tailcall.c +++ b/gcc/tree-tailcall.c @@ -422,6 +422,8 @@ find_tail_calls (basic_block bb, struct tailcall **ret) { call = as_a (stmt); ass_var = gimple_call_lhs (call); + if (!ass_var) + ass_var = gimple_call_return_arg (call); break; }