Re: [PATCH 2/3] Implement CALL_EXPR_MUST_TAIL_CALL

2016-05-21 Thread Andreas Schwab
David Malcolm  writes:

> diff --git a/gcc/testsuite/gcc.dg/plugin/must-tail-call-2.c 
> b/gcc/testsuite/gcc.dg/plugin/must-tail-call-2.c
> new file mode 100644
> index 000..c5504f8
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/plugin/must-tail-call-2.c
> @@ -0,0 +1,58 @@
> +/* Allow nested functions.  */
> +/* { dg-options "-Wno-pedantic" } */
> +
> +struct box { char field[64]; int i; };
> +
> +struct box __attribute__((noinline,noclone))
> +returns_struct (int i)
> +{
> +  struct box b;
> +  b.i = i * i;
> +  return b;
> +}
> +
> +int __attribute__((noinline,noclone))
> +test_1 (int i)
> +{
> +  return returns_struct (i * 5).i; /* { dg-error "cannot tail-call: callee 
> returns a structure" } */
> +}
> +
> +int __attribute__((noinline,noclone))
> +test_2_callee (int i, struct box b)
> +{
> +  if (b.field[0])
> +return 5;
> +  return i * i;
> +}
> +
> +int __attribute__((noinline,noclone))
> +test_2_caller (int i)
> +{
> +  struct box b;
> +  return test_2_callee (i + 1, b); /* { dg-error "cannot tail-call: callee 
> required more stack slots than the caller" } */
> +}
> +
> +extern void setjmp (void);
> +void
> +test_3 (void)
> +{
> +  setjmp (); /* { dg-error "cannot tail-call: callee returns twice" } */
> +}
> +
> +void
> +test_4 (void)
> +{
> +  void nested (void)
> +  {
> +  }
> +  nested (); /* { dg-error "cannot tail-call: nested function" } */
> +}
> +
> +typedef void (fn_ptr_t) (void);
> +volatile fn_ptr_t fn_ptr;
> +
> +void
> +test_5 (void)
> +{
> +  fn_ptr (); /* { dg-error "cannot tail-call: callee does not return" } */
> +}

On aarch64:

FAIL: gcc.dg/plugin/must-tail-call-2.c -fplugin=./must_tail_call_plugin.so  
(test for errors, line 32)
FAIL: gcc.dg/plugin/must-tail-call-2.c -fplugin=./must_tail_call_plugin.so 
(test for excess errors)
Excess errors:
gcc.dg/plugin/must-tail-call-2.c:32:10: error: cannot tail-call: argument must 
be passed by copying

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."


Re: [PATCH 2/3] Implement CALL_EXPR_MUST_TAIL_CALL

2016-05-21 Thread Andreas Schwab
David Malcolm  writes:

> diff --git a/gcc/testsuite/gcc.dg/plugin/must-tail-call-2.c 
> b/gcc/testsuite/gcc.dg/plugin/must-tail-call-2.c
> new file mode 100644
> index 000..c5504f8
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/plugin/must-tail-call-2.c
> @@ -0,0 +1,58 @@
> +/* Allow nested functions.  */
> +/* { dg-options "-Wno-pedantic" } */
> +
> +struct box { char field[64]; int i; };
> +
> +struct box __attribute__((noinline,noclone))
> +returns_struct (int i)
> +{
> +  struct box b;
> +  b.i = i * i;
> +  return b;
> +}
> +
> +int __attribute__((noinline,noclone))
> +test_1 (int i)
> +{
> +  return returns_struct (i * 5).i; /* { dg-error "cannot tail-call: callee 
> returns a structure" } */
> +}
> +
> +int __attribute__((noinline,noclone))
> +test_2_callee (int i, struct box b)
> +{
> +  if (b.field[0])
> +return 5;
> +  return i * i;
> +}
> +
> +int __attribute__((noinline,noclone))
> +test_2_caller (int i)
> +{
> +  struct box b;
> +  return test_2_callee (i + 1, b); /* { dg-error "cannot tail-call: callee 
> required more stack slots than the caller" } */
> +}
> +
> +extern void setjmp (void);
> +void
> +test_3 (void)
> +{
> +  setjmp (); /* { dg-error "cannot tail-call: callee returns twice" } */
> +}
> +
> +void
> +test_4 (void)
> +{
> +  void nested (void)
> +  {
> +  }
> +  nested (); /* { dg-error "cannot tail-call: nested function" } */
> +}
> +
> +typedef void (fn_ptr_t) (void);
> +volatile fn_ptr_t fn_ptr;
> +
> +void
> +test_5 (void)
> +{
> +  fn_ptr (); /* { dg-error "cannot tail-call: callee does not return" } */
> +}

On ia64:

FAIL: gcc.dg/plugin/must-tail-call-2.c -fplugin=./must_tail_call_plugin.so  
(test for errors, line 39)
FAIL: gcc.dg/plugin/must-tail-call-2.c -fplugin=./must_tail_call_plugin.so  
(test for errors, line 57)
FAIL: gcc.dg/plugin/must-tail-call-2.c -fplugin=./must_tail_call_plugin.so 
(test for excess errors)
Excess errors:
gcc.dg/plugin/must-tail-call-2.c:39:3: error: cannot tail-call: target is not 
able to optimize the call into a sibling call
gcc.dg/plugin/must-tail-call-2.c:57:3: error: cannot tail-call: target is not 
able to optimize the call into a sibling call

On m68k:

FAIL: gcc.dg/plugin/must-tail-call-2.c -fplugin=./must_tail_call_plugin.so  
(test for errors, line 48)
FAIL: gcc.dg/plugin/must-tail-call-2.c -fplugin=./must_tail_call_plugin.so 
(test for excess errors)
Excess errors:
gcc.dg/plugin/must-tail-call-2.c:48:3: error: cannot tail-call: target is not 
able to optimize the call into a sibling call

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."


Re: [PATCH 2/3] Implement CALL_EXPR_MUST_TAIL_CALL

2016-05-19 Thread Jeff Law

On 05/17/2016 04:01 PM, David Malcolm wrote:

This patch implements support for marking CALL_EXPRs
as being mandatory for tail-call-optimization. expand_call
tries harder to perform the optimization on such CALL_EXPRs,
and issues an error if it fails.

Currently this flag isn't accessible from any frontend,
so the patch uses a plugin for testing the functionality.

Successfully bootstrapped on x86_64-pc-linux-gnu;
adds 8 PASS results to gcc.sum.

OK for trunk?

gcc/ChangeLog:
* calls.c (maybe_complain_about_tail_call): New function.
(initialize_argument_information): Call
maybe_complain_about_tail_call when clearing *may_tailcall.
(can_implement_as_sibling_call_p): Call
maybe_complain_about_tail_call when returning false.
(expand_call): Read CALL_EXPR_MUST_TAIL_CALL and, if set,
ensure try_tail_call is set.  Call maybe_complain_about_tail_call
if tail-call optimization fails.
* cfgexpand.c (expand_call_stmt): Initialize
CALL_EXPR_MUST_TAIL_CALL from gimple_call_must_tail_p.
* gimple-pretty-print.c (dump_gimple_call): Dump
gimple_call_must_tail_p.
* gimple.c (gimple_build_call_from_tree): Call
gimple_call_set_must_tail with the value of
CALL_EXPR_MUST_TAIL_CALL.
* gimple.h (enum gf_mask): Add GF_CALL_MUST_TAIL_CALL.
(gimple_call_set_must_tail): New function.
(gimple_call_must_tail_p): New function.
* print-tree.c (print_node): Update printing of TREE_STATIC
to reflect its use for CALL_EXPR_MUST_TAIL_CALL.
* tree-core.h (struct tree_base): Add MUST_TAIL_CALL to the
trailing comment listing applicable flags.
* tree.h (CALL_EXPR_MUST_TAIL_CALL): New macro.
It's actually simpler than it looks -- most of the changes are just 
getting better diagnostics when tail call fails, which I wholeheartedly 
support.


OK for the trunk.


Thanks,
Jeff



[PATCH 2/3] Implement CALL_EXPR_MUST_TAIL_CALL

2016-05-17 Thread David Malcolm
This patch implements support for marking CALL_EXPRs
as being mandatory for tail-call-optimization. expand_call
tries harder to perform the optimization on such CALL_EXPRs,
and issues an error if it fails.

Currently this flag isn't accessible from any frontend,
so the patch uses a plugin for testing the functionality.

Successfully bootstrapped on x86_64-pc-linux-gnu;
adds 8 PASS results to gcc.sum.

OK for trunk?

gcc/ChangeLog:
* calls.c (maybe_complain_about_tail_call): New function.
(initialize_argument_information): Call
maybe_complain_about_tail_call when clearing *may_tailcall.
(can_implement_as_sibling_call_p): Call
maybe_complain_about_tail_call when returning false.
(expand_call): Read CALL_EXPR_MUST_TAIL_CALL and, if set,
ensure try_tail_call is set.  Call maybe_complain_about_tail_call
if tail-call optimization fails.
* cfgexpand.c (expand_call_stmt): Initialize
CALL_EXPR_MUST_TAIL_CALL from gimple_call_must_tail_p.
* gimple-pretty-print.c (dump_gimple_call): Dump
gimple_call_must_tail_p.
* gimple.c (gimple_build_call_from_tree): Call
gimple_call_set_must_tail with the value of
CALL_EXPR_MUST_TAIL_CALL.
* gimple.h (enum gf_mask): Add GF_CALL_MUST_TAIL_CALL.
(gimple_call_set_must_tail): New function.
(gimple_call_must_tail_p): New function.
* print-tree.c (print_node): Update printing of TREE_STATIC
to reflect its use for CALL_EXPR_MUST_TAIL_CALL.
* tree-core.h (struct tree_base): Add MUST_TAIL_CALL to the
trailing comment listing applicable flags.
* tree.h (CALL_EXPR_MUST_TAIL_CALL): New macro.

gcc/testsuite/ChangeLog:
* gcc.dg/plugin/must-tail-call-1.c: New test case.
* gcc.dg/plugin/must-tail-call-2.c: New test case.
* gcc.dg/plugin/must_tail_call_plugin.c: New file.
* gcc.dg/plugin/plugin.exp (plugin_test_list): Add the above.
---
 gcc/calls.c| 123 ++---
 gcc/cfgexpand.c|   1 +
 gcc/gimple-pretty-print.c  |   2 +
 gcc/gimple.c   |   1 +
 gcc/gimple.h   |  20 
 gcc/print-tree.c   |   2 +-
 gcc/testsuite/gcc.dg/plugin/must-tail-call-1.c |  22 
 gcc/testsuite/gcc.dg/plugin/must-tail-call-2.c |  58 ++
 .../gcc.dg/plugin/must_tail_call_plugin.c  |  76 +
 gcc/testsuite/gcc.dg/plugin/plugin.exp |   3 +
 gcc/tree-core.h|   3 +
 gcc/tree.h |   5 +
 12 files changed, 299 insertions(+), 17 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/plugin/must-tail-call-1.c
 create mode 100644 gcc/testsuite/gcc.dg/plugin/must-tail-call-2.c
 create mode 100644 gcc/testsuite/gcc.dg/plugin/must_tail_call_plugin.c

diff --git a/gcc/calls.c b/gcc/calls.c
index ac8092c..1b12eca 100644
--- a/gcc/calls.c
+++ b/gcc/calls.c
@@ -1102,6 +1102,19 @@ store_unaligned_arguments_into_pseudos (struct arg_data 
*args, int num_actuals)
   }
 }
 
+/* Issue an error if CALL_EXPR was flagged as requiring
+   tall-call optimization.  */
+
+static void
+maybe_complain_about_tail_call (tree call_expr, const char *reason)
+{
+  gcc_assert (TREE_CODE (call_expr) == CALL_EXPR);
+  if (!CALL_EXPR_MUST_TAIL_CALL (call_expr))
+return;
+
+  error_at (EXPR_LOCATION (call_expr), "cannot tail-call: %s", reason);
+}
+
 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
CALL_EXPR EXP.
 
@@ -1343,7 +1356,13 @@ initialize_argument_information (int num_actuals 
ATTRIBUTE_UNUSED,
  /* We can't use sibcalls if a callee-copied argument is
 stored in the current function's frame.  */
  if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
-   *may_tailcall = false;
+   {
+ *may_tailcall = false;
+ maybe_complain_about_tail_call (exp,
+ "a callee-copied argument is"
+ " stored in the current "
+ " function's frame");
+   }
 
  args[i].tree_value = build_fold_addr_expr_loc (loc,
 args[i].tree_value);
@@ -1406,6 +1425,9 @@ initialize_argument_information (int num_actuals 
ATTRIBUTE_UNUSED,
= build_fold_addr_expr_loc (loc, make_tree (type, copy));
  type = TREE_TYPE (args[i].tree_value);
  *may_tailcall = false;
+ maybe_complain_about_tail_call (exp,
+ "argument must be passed"
+ " by copying");