From 2c28c3e49983e8054850e60704edc391146e376d Mon Sep 17 00:00:00 2001 From: Richard Sandiford Date: Fri, 25 Nov 2016 08:17:46 +0000 Subject: [PATCH] Tighten check for whether sibcall references local variables 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. 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. From-SVN: r242860 --- gcc/ChangeLog | 5 ++ gcc/testsuite/ChangeLog | 4 ++ gcc/testsuite/gcc.dg/tree-ssa/tailcall-8.c | 80 ++++++++++++++++++++++ gcc/tree-tailcall.c | 6 +- 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/tailcall-8.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index f2b48289d6b..efc20162e12 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2016-11-25 Richard Sandiford + + * tree-tailcall.c (find_tail_calls): Allow calls to reference + local variables if all references are known to be direct. + 2016-11-25 Jakub Jelinek Prathamesh Kulkarni diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 1028191731f..e18397f2a4c 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2016-11-25 Richard Sandiford + + * gcc.dg/tree-ssa/tailcall-8.c: New test. + 2016-11-25 Senthil Kumar Selvaraj * gcc.dg/pr64277.c: Use __INT32_TYPE__ for targets 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 00000000000..ffeabe54293 --- /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 f97541d35a5..66a0a4c4460 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; -- 2.30.2