From ea418485c700494c3efdc282854c5f5a08702416 Mon Sep 17 00:00:00 2001 From: Andrew Pinski Date: Mon, 31 May 2021 00:17:22 +0000 Subject: Fix PR 95481: tail call fails with empty struct types The problem here is we don't have an assignment type any more for empty structs as they were removed during gimplifcation. This adds a special case where the assignment var does not exist and the return decl is empty typed. OK? Tested on aarch64-linux-gnu with no regressions. Thanks, Andrew Pinski changes since v1: v2: Use is_empty_type instead of zero-sized type. gcc/ChangeLog: PR tree-optimization/95481 * tree-tailcall.c (find_tail_calls): Handle empty typed return decls. gcc/testsuite/ChangeLog: PR tree-optimization/95481 * gcc.dg/tree-ssa/tailcall-10.c: New test. * gcc.dg/tree-ssa/tailcall-11.c: New test. * gcc.dg/tree-ssa/tailcall-12.c: New test. * gcc.dg/tree-ssa/tailcall-13.c: New test. * gcc.dg/tree-ssa/tailrecursion-8.c: New test. --- gcc/tree-tailcall.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'gcc/tree-tailcall.c') diff --git a/gcc/tree-tailcall.c b/gcc/tree-tailcall.c index e866f72..a4d31c9 100644 --- a/gcc/tree-tailcall.c +++ b/gcc/tree-tailcall.c @@ -710,9 +710,11 @@ find_tail_calls (basic_block bb, struct tailcall **ret) ret_var = gimple_return_retval (as_a (stmt)); /* We may proceed if there either is no return value, or the return value - is identical to the call's return. */ + is identical to the call's return or if the return decl is an empty type + variable and the call's return was not assigned. */ if (ret_var - && (ret_var != ass_var)) + && (ret_var != ass_var + && !(is_empty_type (TREE_TYPE (ret_var)) && !ass_var))) return; /* If this is not a tail recursive call, we cannot handle addends or -- cgit v1.1 From 8819419ba1d397c0444d89079ec16657a09914fb Mon Sep 17 00:00:00 2001 From: Andrew Pinski Date: Tue, 20 Jul 2021 11:25:43 -0700 Subject: Fix PR 10153: tail recusion for vector types. The problem here is we try to an initialized value from a scalar constant. For vectors we need to do a vect_dup instead. This fixes that issue by using build_{one,zero}_cst instead of integer_{one,zero}_node when calling create_tailcall_accumulator. Changes from v1: * v2: Use build_{one,zero}_cst and get the correct type before. OK? Bootstrapped and tested on aarch64-linux-gnu with no regressions. gcc/ChangeLog: PR tree-optimization/10153 * tree-tailcall.c (create_tailcall_accumulator): Don't call fold_convert as the type should be correct already. (tree_optimize_tail_calls_1): Use build_{one,zero}_cst instead of integer_{one,zero}_node for the call of create_tailcall_accumulator. gcc/testsuite/ChangeLog: PR tree-optimization/10153 * gcc.c-torture/compile/pr10153-1.c: New test. * gcc.c-torture/compile/pr10153-2.c: New test. --- gcc/tree-tailcall.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'gcc/tree-tailcall.c') diff --git a/gcc/tree-tailcall.c b/gcc/tree-tailcall.c index a4d31c9..f2833d2 100644 --- a/gcc/tree-tailcall.c +++ b/gcc/tree-tailcall.c @@ -1079,8 +1079,7 @@ create_tailcall_accumulator (const char *label, basic_block bb, tree init) gphi *phi; phi = create_phi_node (tmp, bb); - /* RET_TYPE can be a float when -ffast-maths is enabled. */ - add_phi_arg (phi, fold_convert (ret_type, init), single_pred_edge (bb), + add_phi_arg (phi, init, single_pred_edge (bb), UNKNOWN_LOCATION); return PHI_RESULT (phi); } @@ -1157,14 +1156,17 @@ tree_optimize_tail_calls_1 (bool opt_tailcalls) } phis_constructed = true; } + tree ret_type = TREE_TYPE (DECL_RESULT (current_function_decl)); + if (POINTER_TYPE_P (ret_type)) + ret_type = sizetype; if (act->add && !a_acc) a_acc = create_tailcall_accumulator ("add_acc", first, - integer_zero_node); + build_zero_cst (ret_type)); if (act->mult && !m_acc) m_acc = create_tailcall_accumulator ("mult_acc", first, - integer_one_node); + build_one_cst (ret_type)); } if (a_acc || m_acc) -- cgit v1.1 From 4d562591018a51f155a2e5d8b9f3e5860111a327 Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Wed, 4 Aug 2021 09:22:51 +0200 Subject: tree-optimization/101769 - tail recursion creates possibly infinite loop This makes tail recursion optimization produce a loop structure manually rather than relying on loop fixup. That also allows the loop to be marked as finite (it would eventually blow the stack if it were not). 2021-08-04 Richard Biener PR tree-optimization/101769 * tree-tailcall.c (eliminate_tail_call): Add the created loop for the first recursion and return it via the new output parameter. (optimize_tail_call): Pass through new output param. (tree_optimize_tail_calls_1): After creating all latches, add the created loop to the loop tree. Do not mark loops for fixup. * g++.dg/tree-ssa/pr101769.C: New testcase. --- gcc/tree-tailcall.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'gcc/tree-tailcall.c') diff --git a/gcc/tree-tailcall.c b/gcc/tree-tailcall.c index f2833d2..f2f3a6b 100644 --- a/gcc/tree-tailcall.c +++ b/gcc/tree-tailcall.c @@ -131,9 +131,6 @@ static tree m_acc, a_acc; static bitmap tailr_arg_needs_copy; -static bool optimize_tail_call (struct tailcall *, bool); -static void eliminate_tail_call (struct tailcall *); - /* Returns false when the function is not suitable for tail call optimization from some reason (e.g. if it takes variable number of arguments). */ @@ -926,10 +923,11 @@ decrease_profile (basic_block bb, profile_count count) } /* Eliminates tail call described by T. TMP_VARS is a list of - temporary variables used to copy the function arguments. */ + temporary variables used to copy the function arguments. + Allocates *NEW_LOOP if not already done and initializes it. */ static void -eliminate_tail_call (struct tailcall *t) +eliminate_tail_call (struct tailcall *t, class loop *&new_loop) { tree param, rslt; gimple *stmt, *call; @@ -999,6 +997,16 @@ eliminate_tail_call (struct tailcall *t) gcc_assert (e); PENDING_STMT (e) = NULL; + /* Add the new loop. */ + if (!new_loop) + { + new_loop = alloc_loop (); + new_loop->header = first; + new_loop->finite_p = true; + } + else + gcc_assert (new_loop->header == first); + /* Add phi node entries for arguments. The ordering of the phi nodes should be the same as the ordering of the arguments. */ for (param = DECL_ARGUMENTS (current_function_decl), @@ -1037,11 +1045,12 @@ eliminate_tail_call (struct tailcall *t) mark the tailcalls for the sibcall optimization. */ static bool -optimize_tail_call (struct tailcall *t, bool opt_tailcalls) +optimize_tail_call (struct tailcall *t, bool opt_tailcalls, + class loop *&new_loop) { if (t->tail_recursion) { - eliminate_tail_call (t); + eliminate_tail_call (t, new_loop); return true; } @@ -1177,12 +1186,15 @@ tree_optimize_tail_calls_1 (bool opt_tailcalls) opt_tailcalls = false; } + class loop *new_loop = NULL; for (; tailcalls; tailcalls = next) { next = tailcalls->next; - changed |= optimize_tail_call (tailcalls, opt_tailcalls); + changed |= optimize_tail_call (tailcalls, opt_tailcalls, new_loop); free (tailcalls); } + if (new_loop) + add_loop (new_loop, loops_for_fn (cfun)->tree_root); if (a_acc || m_acc) { @@ -1198,11 +1210,7 @@ tree_optimize_tail_calls_1 (bool opt_tailcalls) } if (changed) - { - /* We may have created new loops. Make them magically appear. */ - loops_state_set (LOOPS_NEED_FIXUP); - free_dominance_info (CDI_DOMINATORS); - } + free_dominance_info (CDI_DOMINATORS); /* Add phi nodes for the virtual operands defined in the function to the header of the loop created by tail recursion elimination. Do so -- cgit v1.1