Branch data Line data Source code
1 : : /* Tail call optimization on trees.
2 : : Copyright (C) 2003-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify
7 : : it under the terms of the GNU General Public License as published by
8 : : the Free Software Foundation; either version 3, or (at your option)
9 : : any later version.
10 : :
11 : : GCC is distributed in the hope that it will be useful,
12 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : GNU General Public License for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : #include "config.h"
21 : : #include "system.h"
22 : : #include "coretypes.h"
23 : : #include "backend.h"
24 : : #include "rtl.h"
25 : : #include "tree.h"
26 : : #include "gimple.h"
27 : : #include "cfghooks.h"
28 : : #include "tree-pass.h"
29 : : #include "ssa.h"
30 : : #include "cgraph.h"
31 : : #include "gimple-pretty-print.h"
32 : : #include "fold-const.h"
33 : : #include "stor-layout.h"
34 : : #include "gimple-iterator.h"
35 : : #include "gimplify-me.h"
36 : : #include "tree-cfg.h"
37 : : #include "tree-into-ssa.h"
38 : : #include "tree-dfa.h"
39 : : #include "except.h"
40 : : #include "tree-eh.h"
41 : : #include "dbgcnt.h"
42 : : #include "cfgloop.h"
43 : : #include "intl.h"
44 : : #include "common/common-target.h"
45 : : #include "ipa-utils.h"
46 : : #include "tree-ssa-live.h"
47 : : #include "diagnostic-core.h"
48 : : #include "gimple-range.h"
49 : : #include "alloc-pool.h"
50 : : #include "sreal.h"
51 : : #include "symbol-summary.h"
52 : : #include "ipa-cp.h"
53 : : #include "ipa-prop.h"
54 : : #include "attribs.h"
55 : : #include "asan.h"
56 : :
57 : : /* The file implements the tail recursion elimination. It is also used to
58 : : analyze the tail calls in general, passing the results to the rtl level
59 : : where they are used for sibcall optimization.
60 : :
61 : : In addition to the standard tail recursion elimination, we handle the most
62 : : trivial cases of making the call tail recursive by creating accumulators.
63 : : For example the following function
64 : :
65 : : int sum (int n)
66 : : {
67 : : if (n > 0)
68 : : return n + sum (n - 1);
69 : : else
70 : : return 0;
71 : : }
72 : :
73 : : is transformed into
74 : :
75 : : int sum (int n)
76 : : {
77 : : int acc = 0;
78 : :
79 : : while (n > 0)
80 : : acc += n--;
81 : :
82 : : return acc;
83 : : }
84 : :
85 : : To do this, we maintain two accumulators (a_acc and m_acc) that indicate
86 : : when we reach the return x statement, we should return a_acc + x * m_acc
87 : : instead. They are initially initialized to 0 and 1, respectively,
88 : : so the semantics of the function is obviously preserved. If we are
89 : : guaranteed that the value of the accumulator never change, we
90 : : omit the accumulator.
91 : :
92 : : There are three cases how the function may exit. The first one is
93 : : handled in adjust_return_value, the other two in adjust_accumulator_values
94 : : (the second case is actually a special case of the third one and we
95 : : present it separately just for clarity):
96 : :
97 : : 1) Just return x, where x is not in any of the remaining special shapes.
98 : : We rewrite this to a gimple equivalent of return m_acc * x + a_acc.
99 : :
100 : : 2) return f (...), where f is the current function, is rewritten in a
101 : : classical tail-recursion elimination way, into assignment of arguments
102 : : and jump to the start of the function. Values of the accumulators
103 : : are unchanged.
104 : :
105 : : 3) return a + m * f(...), where a and m do not depend on call to f.
106 : : To preserve the semantics described before we want this to be rewritten
107 : : in such a way that we finally return
108 : :
109 : : a_acc + (a + m * f(...)) * m_acc = (a_acc + a * m_acc) + (m * m_acc) * f(...).
110 : :
111 : : I.e. we increase a_acc by a * m_acc, multiply m_acc by m and
112 : : eliminate the tail call to f. Special cases when the value is just
113 : : added or just multiplied are obtained by setting a = 0 or m = 1.
114 : :
115 : : TODO -- it is possible to do similar tricks for other operations. */
116 : :
117 : : /* A structure that describes the tailcall. */
118 : :
119 : : struct tailcall
120 : : {
121 : : /* The iterator pointing to the call statement. */
122 : : gimple_stmt_iterator call_gsi;
123 : :
124 : : /* True if it is a call to the current function. */
125 : : bool tail_recursion;
126 : :
127 : : /* True if there is __tsan_func_exit call after the call. */
128 : : bool has_tsan_func_exit;
129 : :
130 : : /* The return value of the caller is mult * f + add, where f is the return
131 : : value of the call. */
132 : : tree mult, add;
133 : :
134 : : /* Next tailcall in the chain. */
135 : : struct tailcall *next;
136 : : };
137 : :
138 : : /* The variables holding the value of multiplicative and additive
139 : : accumulator. */
140 : : static tree m_acc, a_acc;
141 : :
142 : : /* Bitmap with a bit for each function parameter which is set to true if we
143 : : have to copy the parameter for conversion of tail-recursive calls. */
144 : :
145 : : static bitmap tailr_arg_needs_copy;
146 : :
147 : : static void maybe_error_musttail (gcall *call, const char *err, bool);
148 : :
149 : : /* Returns false when the function is not suitable for tail call optimization
150 : : from some reason (e.g. if it takes variable number of arguments). CALL
151 : : is call to report for. */
152 : :
153 : : static bool
154 : 1540099 : suitable_for_tail_opt_p (gcall *call, bool diag_musttail)
155 : : {
156 : 1540099 : if (cfun->stdarg)
157 : : {
158 : 18382 : maybe_error_musttail (call, _("caller uses stdargs"), diag_musttail);
159 : 18382 : return false;
160 : : }
161 : :
162 : : return true;
163 : : }
164 : :
165 : : /* Returns false when the function is not suitable for tail call optimization
166 : : for some reason (e.g. if it takes variable number of arguments).
167 : : This test must pass in addition to suitable_for_tail_opt_p in order to make
168 : : tail call discovery happen. CALL is call to report error for. */
169 : :
170 : : static bool
171 : 1521717 : suitable_for_tail_call_opt_p (gcall *call, bool diag_musttail)
172 : : {
173 : : /* alloca (until we have stack slot life analysis) inhibits
174 : : sibling call optimizations, but not tail recursion. */
175 : 1521717 : if (cfun->calls_alloca)
176 : : {
177 : 15022 : maybe_error_musttail (call, _("caller uses alloca"), diag_musttail);
178 : 15022 : return false;
179 : : }
180 : :
181 : : /* If we are using sjlj exceptions, we may need to add a call to
182 : : _Unwind_SjLj_Unregister at exit of the function. Which means
183 : : that we cannot do any sibcall transformations. */
184 : 1506695 : if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ
185 : 1506695 : && current_function_has_exception_handlers ())
186 : : {
187 : 0 : maybe_error_musttail (call, _("caller uses sjlj exceptions"),
188 : : diag_musttail);
189 : 0 : return false;
190 : : }
191 : :
192 : : /* Any function that calls setjmp might have longjmp called from
193 : : any called function. ??? We really should represent this
194 : : properly in the CFG so that this needn't be special cased. */
195 : 1506695 : if (cfun->calls_setjmp)
196 : : {
197 : 144 : maybe_error_musttail (call, _("caller uses setjmp"), diag_musttail);
198 : 144 : return false;
199 : : }
200 : :
201 : : /* Various targets don't handle tail calls correctly in functions
202 : : that call __builtin_eh_return. */
203 : 1506551 : if (cfun->calls_eh_return)
204 : : {
205 : 8 : maybe_error_musttail (call, _("caller uses __builtin_eh_return"),
206 : : diag_musttail);
207 : 8 : return false;
208 : : }
209 : :
210 : 1506543 : if (diag_musttail
211 : 364962 : && gimple_call_must_tail_p (call)
212 : 1507139 : && warn_musttail_local_addr)
213 : 1371 : for (unsigned int i = 0; i < gimple_call_num_args (call); i++)
214 : : {
215 : 775 : tree arg = gimple_call_arg (call, i);
216 : 775 : if (!POINTER_TYPE_P (TREE_TYPE (arg)))
217 : 458 : continue;
218 : 317 : if (TREE_CODE (arg) == ADDR_EXPR)
219 : : {
220 : 131 : arg = get_base_address (TREE_OPERAND (arg, 0));
221 : 131 : if (auto_var_in_fn_p (arg, current_function_decl))
222 : : {
223 : 131 : if (TREE_CODE (arg) == LABEL_DECL)
224 : 16 : warning_at (gimple_location (call), OPT_Wmusttail_local_addr,
225 : : "address of label passed to %<musttail%> "
226 : : "call argument");
227 : 115 : else if (TREE_CODE (arg) == PARM_DECL)
228 : 25 : warning_at (gimple_location (call), OPT_Wmusttail_local_addr,
229 : : "address of parameter %qD passed to "
230 : : "%<musttail%> call argument", arg);
231 : 90 : else if (!DECL_ARTIFICIAL (arg) && DECL_NAME (arg))
232 : 81 : warning_at (gimple_location (call), OPT_Wmusttail_local_addr,
233 : : "address of automatic variable %qD passed to "
234 : : "%<musttail%> call argument", arg);
235 : : else
236 : 9 : warning_at (gimple_location (call), OPT_Wmusttail_local_addr,
237 : : "address of local variable passed to "
238 : : "%<musttail%> call argument");
239 : 131 : suppress_warning (call, OPT_Wmaybe_musttail_local_addr);
240 : : }
241 : : }
242 : : }
243 : :
244 : : return true;
245 : : }
246 : :
247 : : /* Return single successor edge ignoring EDGE_EH edges. */
248 : :
249 : : static edge
250 : 527155 : single_non_eh_succ_edge (basic_block bb)
251 : : {
252 : 527155 : edge e, ret = NULL;
253 : 527155 : edge_iterator ei;
254 : 1054336 : FOR_EACH_EDGE (e, ei, bb->succs)
255 : 527181 : if ((e->flags & EDGE_EH) == 0)
256 : : {
257 : 527155 : gcc_assert (ret == NULL);
258 : : ret = e;
259 : : }
260 : 527155 : gcc_assert (ret);
261 : 527155 : return ret;
262 : : }
263 : :
264 : : /* Checks whether the expression EXPR in stmt AT is independent of the
265 : : statement pointed to by GSI (in a sense that we already know EXPR's value
266 : : at GSI). We use the fact that we are only called from the chain of
267 : : basic blocks that have only single successor. Returns the expression
268 : : containing the value of EXPR at GSI. */
269 : :
270 : : static tree
271 : 23158 : independent_of_stmt_p (tree expr, gimple *at, gimple_stmt_iterator gsi,
272 : : bitmap to_move)
273 : : {
274 : 23158 : basic_block bb, call_bb, at_bb;
275 : 23158 : edge e;
276 : 23158 : edge_iterator ei;
277 : :
278 : 23158 : if (is_gimple_min_invariant (expr))
279 : : return expr;
280 : :
281 : 6103 : if (TREE_CODE (expr) != SSA_NAME)
282 : : return NULL_TREE;
283 : :
284 : 6103 : if (bitmap_bit_p (to_move, SSA_NAME_VERSION (expr)))
285 : : return expr;
286 : :
287 : : /* Mark the blocks in the chain leading to the end. */
288 : 6082 : at_bb = gimple_bb (at);
289 : 6082 : call_bb = gimple_bb (gsi_stmt (gsi));
290 : 6882 : for (bb = call_bb; bb != at_bb; bb = single_non_eh_succ_edge (bb)->dest)
291 : 800 : bb->aux = &bb->aux;
292 : 6082 : bb->aux = &bb->aux;
293 : :
294 : 6189 : while (1)
295 : : {
296 : 6189 : at = SSA_NAME_DEF_STMT (expr);
297 : 6189 : bb = gimple_bb (at);
298 : :
299 : : /* The default definition or defined before the chain. */
300 : 6189 : if (!bb || !bb->aux)
301 : : break;
302 : :
303 : 3074 : if (bb == call_bb)
304 : : {
305 : 18510 : for (; !gsi_end_p (gsi); gsi_next (&gsi))
306 : 15731 : if (gsi_stmt (gsi) == at)
307 : : break;
308 : :
309 : 2945 : if (!gsi_end_p (gsi))
310 : 166 : expr = NULL_TREE;
311 : : break;
312 : : }
313 : :
314 : 129 : if (gimple_code (at) != GIMPLE_PHI)
315 : : {
316 : : expr = NULL_TREE;
317 : : break;
318 : : }
319 : :
320 : 142 : FOR_EACH_EDGE (e, ei, bb->preds)
321 : 142 : if (e->src->aux)
322 : : break;
323 : 129 : gcc_assert (e);
324 : :
325 : 129 : expr = PHI_ARG_DEF_FROM_EDGE (at, e);
326 : 129 : if (TREE_CODE (expr) != SSA_NAME)
327 : : {
328 : : /* The value is a constant. */
329 : : break;
330 : : }
331 : : }
332 : :
333 : : /* Unmark the blocks. */
334 : 6882 : for (bb = call_bb; bb != at_bb; bb = single_non_eh_succ_edge (bb)->dest)
335 : 800 : bb->aux = NULL;
336 : 6082 : bb->aux = NULL;
337 : :
338 : 6082 : return expr;
339 : : }
340 : :
341 : : enum par { FAIL, OK, TRY_MOVE };
342 : :
343 : : /* Simulates the effect of an assignment STMT on the return value of the tail
344 : : recursive CALL passed in ASS_VAR. M and A are the multiplicative and the
345 : : additive factor for the real return value. */
346 : :
347 : : static par
348 : 113552 : process_assignment (gassign *stmt,
349 : : gimple_stmt_iterator call, tree *m,
350 : : tree *a, tree *ass_var, bitmap to_move)
351 : : {
352 : 113552 : tree op0, op1 = NULL_TREE, non_ass_var = NULL_TREE;
353 : 113552 : tree dest = gimple_assign_lhs (stmt);
354 : 113552 : enum tree_code code = gimple_assign_rhs_code (stmt);
355 : 113552 : enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
356 : 113552 : tree src_var = gimple_assign_rhs1 (stmt);
357 : :
358 : : /* See if this is a simple copy operation of an SSA name to the function
359 : : result. In that case we may have a simple tail call. Ignore type
360 : : conversions that can never produce extra code between the function
361 : : call and the function return. */
362 : 54827 : if ((rhs_class == GIMPLE_SINGLE_RHS || gimple_assign_cast_p (stmt))
363 : 134055 : && src_var == *ass_var)
364 : : {
365 : : /* Reject a tailcall if the type conversion might need
366 : : additional code. */
367 : 23728 : if (gimple_assign_cast_p (stmt))
368 : : {
369 : 18402 : if (TYPE_MODE (TREE_TYPE (dest)) != TYPE_MODE (TREE_TYPE (src_var)))
370 : : return FAIL;
371 : :
372 : : /* Even if the type modes are the same, if the precision of the
373 : : type is smaller than mode's precision,
374 : : reduce_to_bit_field_precision would generate additional code. */
375 : 16898 : if (INTEGRAL_TYPE_P (TREE_TYPE (dest))
376 : 16391 : && !type_has_mode_precision_p (TREE_TYPE (dest)))
377 : : return FAIL;
378 : : }
379 : :
380 : 13742 : *ass_var = dest;
381 : 13742 : return OK;
382 : : }
383 : :
384 : 89824 : switch (rhs_class)
385 : : {
386 : 31591 : case GIMPLE_BINARY_RHS:
387 : 31591 : op1 = gimple_assign_rhs2 (stmt);
388 : :
389 : : /* Fall through. */
390 : :
391 : 36054 : case GIMPLE_UNARY_RHS:
392 : 36054 : op0 = gimple_assign_rhs1 (stmt);
393 : 36054 : break;
394 : :
395 : : default:
396 : : return FAIL;
397 : : }
398 : :
399 : : /* Accumulator optimizations will reverse the order of operations.
400 : : We can only do that for floating-point types if we're assuming
401 : : that addition and multiplication are associative. */
402 : 36054 : if (!flag_associative_math)
403 : 35706 : if (FLOAT_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
404 : : return FAIL;
405 : :
406 : : /* We at least cannot build -1 for all fixed point types. */
407 : 32051 : if (FIXED_POINT_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
408 : : return FAIL;
409 : :
410 : 32051 : if (rhs_class == GIMPLE_UNARY_RHS
411 : 4139 : && op0 == *ass_var)
412 : : ;
413 : 29995 : else if (op0 == *ass_var
414 : 29995 : && (non_ass_var = independent_of_stmt_p (op1, stmt, call,
415 : : to_move)))
416 : : ;
417 : 13437 : else if (*ass_var
418 : 6761 : && op1 == *ass_var
419 : 19899 : && (non_ass_var = independent_of_stmt_p (op0, stmt, call,
420 : : to_move)))
421 : : ;
422 : : else
423 : 7057 : return TRY_MOVE;
424 : :
425 : 24994 : switch (code)
426 : : {
427 : 6508 : case PLUS_EXPR:
428 : 6508 : *a = non_ass_var;
429 : 6508 : *ass_var = dest;
430 : 6508 : return OK;
431 : :
432 : 324 : case POINTER_PLUS_EXPR:
433 : 324 : if (op0 != *ass_var)
434 : : return FAIL;
435 : 83 : *a = non_ass_var;
436 : 83 : *ass_var = dest;
437 : 83 : return OK;
438 : :
439 : 376 : case MULT_EXPR:
440 : 376 : *m = non_ass_var;
441 : 376 : *ass_var = dest;
442 : 376 : return OK;
443 : :
444 : 110 : case NEGATE_EXPR:
445 : 110 : *m = build_minus_one_cst (TREE_TYPE (op0));
446 : 110 : *ass_var = dest;
447 : 110 : return OK;
448 : :
449 : 1785 : case MINUS_EXPR:
450 : 1785 : if (*ass_var == op0)
451 : 114 : *a = fold_build1 (NEGATE_EXPR, TREE_TYPE (non_ass_var), non_ass_var);
452 : : else
453 : : {
454 : 1671 : *m = build_minus_one_cst (TREE_TYPE (non_ass_var));
455 : 1671 : *a = fold_build1 (NEGATE_EXPR, TREE_TYPE (non_ass_var), non_ass_var);
456 : : }
457 : :
458 : 1785 : *ass_var = dest;
459 : 1785 : return OK;
460 : :
461 : : default:
462 : : return FAIL;
463 : : }
464 : : }
465 : :
466 : : /* Propagate VAR through phis on edge E. */
467 : :
468 : : static tree
469 : 523436 : propagate_through_phis (tree var, edge e)
470 : : {
471 : 523436 : basic_block dest = e->dest;
472 : 523436 : gphi_iterator gsi;
473 : :
474 : 993840 : for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
475 : : {
476 : 534795 : gphi *phi = gsi.phi ();
477 : 534795 : if (PHI_ARG_DEF_FROM_EDGE (phi, e) == var)
478 : 64391 : return PHI_RESULT (phi);
479 : : }
480 : : return var;
481 : : }
482 : :
483 : : /* Report an error for failing to tail convert must call CALL
484 : : with error message ERR. Also clear the flag to prevent further
485 : : errors. */
486 : :
487 : : static void
488 : 746407 : maybe_error_musttail (gcall *call, const char *err, bool diag_musttail)
489 : : {
490 : 746407 : if (gimple_call_must_tail_p (call) && diag_musttail)
491 : : {
492 : 39 : error_at (gimple_location (call), "cannot tail-call: %s", err);
493 : : /* Avoid another error. ??? If there are multiple reasons why tail
494 : : calls fail it might be useful to report them all to avoid
495 : : whack-a-mole for the user. But currently there is too much
496 : : redundancy in the reporting, so keep it simple. */
497 : 39 : gimple_call_set_must_tail (call, false); /* Avoid another error. */
498 : 39 : gimple_call_set_tail (call, false);
499 : : }
500 : 746407 : if (dump_file && (dump_flags & TDF_DETAILS))
501 : : {
502 : 13 : fprintf (dump_file, "Cannot tail-call: %s: ", err);
503 : 13 : print_gimple_stmt (dump_file, call, 0, TDF_SLIM);
504 : : }
505 : 746407 : }
506 : :
507 : : /* Return true if there is no real work performed in the exception
508 : : path starting at BB and it will in the end result in external exception.
509 : : Search at most CNT basic blocks (so that we don't need to do trivial
510 : : loop discovery). */
511 : : static bool
512 : 82 : empty_eh_cleanup (basic_block bb, int *eh_has_tsan_func_exit, int cnt)
513 : : {
514 : 88 : if (EDGE_COUNT (bb->succs) > 1)
515 : : return false;
516 : :
517 : 147 : for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);
518 : 59 : gsi_next (&gsi))
519 : : {
520 : 141 : gimple *g = gsi_stmt (gsi);
521 : 141 : if (is_gimple_debug (g) || gimple_clobber_p (g))
522 : 55 : continue;
523 : 90 : if (eh_has_tsan_func_exit
524 : 17 : && !*eh_has_tsan_func_exit
525 : 13 : && sanitize_flags_p (SANITIZE_THREAD)
526 : 90 : && gimple_call_builtin_p (g, BUILT_IN_TSAN_FUNC_EXIT))
527 : : {
528 : 4 : *eh_has_tsan_func_exit = 1;
529 : 4 : continue;
530 : : }
531 : 82 : if (is_gimple_resx (g) && stmt_can_throw_external (cfun, g))
532 : 82 : return true;
533 : : return false;
534 : : }
535 : 6 : if (!single_succ_p (bb))
536 : : return false;
537 : 6 : if (cnt == 1)
538 : : return false;
539 : 6 : return empty_eh_cleanup (single_succ (bb), eh_has_tsan_func_exit, cnt - 1);
540 : : }
541 : :
542 : : /* Argument for compute_live_vars/live_vars_at_stmt and what compute_live_vars
543 : : returns. Computed lazily, but just once for the function. */
544 : : static live_vars_map *live_vars;
545 : : static vec<bitmap_head> live_vars_vec;
546 : :
547 : : /* Finds tailcalls falling into basic block BB. The list of found tailcalls is
548 : : added to the start of RET. When ONLY_MUSTTAIL is set only handle musttail.
549 : : Update OPT_TAILCALLS as output parameter. If DIAG_MUSTTAIL, diagnose
550 : : failures for musttail calls. RETRY_TSAN_FUNC_EXIT is initially 0 and
551 : : in that case the last call is attempted to be tail called, including
552 : : __tsan_func_exit with -fsanitize=thread. It is set to -1 if we
553 : : detect __tsan_func_exit call and in that case tree_optimize_tail_calls_1
554 : : will retry with it set to 1 (regardless of whether turning the
555 : : __tsan_func_exit was successfully detected as tail call or not) and that
556 : : will allow turning musttail calls before that call into tail calls as well
557 : : by adding __tsan_func_exit call before the call. */
558 : :
559 : : static void
560 : 7224255 : find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail,
561 : : bool &opt_tailcalls, bool diag_musttail,
562 : : int &retry_tsan_func_exit)
563 : : {
564 : 7224255 : tree ass_var = NULL_TREE, ret_var, func, param;
565 : 7224255 : gimple *stmt;
566 : 7224255 : gcall *call = NULL;
567 : 7224255 : gimple_stmt_iterator gsi, agsi;
568 : 7224255 : bool tail_recursion;
569 : 7224255 : struct tailcall *nw;
570 : 7224255 : edge e;
571 : 7224255 : tree m, a;
572 : 7224255 : basic_block abb;
573 : 7224255 : size_t idx;
574 : 7224255 : tree var;
575 : 7224255 : bool only_tailr = false;
576 : 7224255 : bool has_tsan_func_exit = false;
577 : 7224255 : int eh_has_tsan_func_exit = -1;
578 : :
579 : 7224255 : if (!single_succ_p (bb)
580 : 7224255 : && (EDGE_COUNT (bb->succs) || !cfun->has_musttail || !diag_musttail))
581 : : {
582 : : /* If there is an abnormal edge assume it's the only extra one.
583 : : Tolerate that case so that we can give better error messages
584 : : for musttail later. */
585 : 1809098 : if (!has_abnormal_or_eh_outgoing_edge_p (bb))
586 : : {
587 : 1733484 : if (dump_file)
588 : 108 : fprintf (dump_file, "Basic block %d has extra exit edges\n",
589 : : bb->index);
590 : 6387467 : return;
591 : : }
592 : 75614 : if (!cfun->has_musttail)
593 : : return;
594 : : }
595 : :
596 : 5415245 : bool bad_stmt = false;
597 : 5415245 : gimple *last_stmt = nullptr;
598 : 23511789 : for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
599 : : {
600 : 15384643 : stmt = gsi_stmt (gsi);
601 : :
602 : : /* Ignore labels, returns, nops, clobbers and debug stmts. */
603 : 15384643 : if (gimple_code (stmt) == GIMPLE_LABEL
604 : : || gimple_code (stmt) == GIMPLE_RETURN
605 : : || gimple_code (stmt) == GIMPLE_NOP
606 : : || gimple_code (stmt) == GIMPLE_PREDICT
607 : 11605642 : || gimple_clobber_p (stmt)
608 : 10577288 : || is_gimple_debug (stmt))
609 : 11701432 : continue;
610 : :
611 : 3683211 : if (cfun->has_musttail
612 : 1632 : && sanitize_flags_p (SANITIZE_THREAD)
613 : 36 : && gimple_call_builtin_p (stmt, BUILT_IN_TSAN_FUNC_EXIT)
614 : 3683227 : && diag_musttail)
615 : : {
616 : 16 : if (retry_tsan_func_exit == 0)
617 : 8 : retry_tsan_func_exit = -1;
618 : 8 : else if (retry_tsan_func_exit == 1)
619 : 8 : continue;
620 : : }
621 : :
622 : 3683203 : if (!last_stmt)
623 : 3051927 : last_stmt = stmt;
624 : : /* Check for a call. */
625 : 3683203 : if (is_gimple_call (stmt))
626 : : {
627 : 1540108 : call = as_a <gcall *> (stmt);
628 : : /* Handle only musttail calls when not optimizing. */
629 : 1540108 : if (only_musttail && !gimple_call_must_tail_p (call))
630 : : return;
631 : 1540100 : if (bad_stmt)
632 : : {
633 : 1 : maybe_error_musttail (call, _("memory reference or volatile "
634 : : "after call"), diag_musttail);
635 : 1 : return;
636 : : }
637 : 1540099 : ass_var = gimple_call_lhs (call);
638 : 1540099 : break;
639 : : }
640 : :
641 : : /* Allow simple copies between local variables, even if they're
642 : : aggregates. */
643 : 2143095 : if (is_gimple_assign (stmt)
644 : 2122326 : && auto_var_in_fn_p (gimple_assign_lhs (stmt), cfun->decl)
645 : 2205394 : && auto_var_in_fn_p (gimple_assign_rhs1 (stmt), cfun->decl))
646 : 31718 : continue;
647 : :
648 : : /* If the statement references memory or volatile operands, fail. */
649 : 2111377 : if (gimple_references_memory_p (stmt)
650 : 14577400 : || gimple_has_volatile_ops (stmt))
651 : : {
652 : 1163318 : if (dump_file)
653 : : {
654 : 26 : fprintf (dump_file, "Cannot handle ");
655 : 26 : print_gimple_stmt (dump_file, stmt, 0);
656 : : }
657 : 1163318 : bad_stmt = true;
658 : 1163318 : if (!cfun->has_musttail)
659 : : break;
660 : : }
661 : : }
662 : :
663 : 5415236 : if (bad_stmt)
664 : : return;
665 : :
666 : 4251959 : if (gsi_end_p (gsi))
667 : : {
668 : 2711860 : edge_iterator ei;
669 : : /* Recurse to the predecessors. */
670 : 6516310 : FOR_EACH_EDGE (e, ei, bb->preds)
671 : 3804450 : find_tail_calls (e->src, ret, only_musttail, opt_tailcalls,
672 : : diag_musttail, retry_tsan_func_exit);
673 : :
674 : 2711860 : return;
675 : : }
676 : :
677 : 1540099 : if (!suitable_for_tail_opt_p (call, diag_musttail))
678 : : return;
679 : :
680 : 1521717 : if (!suitable_for_tail_call_opt_p (call, diag_musttail))
681 : 15174 : opt_tailcalls = false;
682 : :
683 : : /* ??? It is OK if the argument of a function is taken in some cases,
684 : : but not in all cases. See PR15387 and PR19616. Revisit for 4.1. */
685 : 1521717 : if (!diag_musttail || !gimple_call_must_tail_p (call))
686 : 1521121 : for (param = DECL_ARGUMENTS (current_function_decl);
687 : 4520152 : param; param = DECL_CHAIN (param))
688 : 2999031 : if (TREE_ADDRESSABLE (param))
689 : : {
690 : 28000 : maybe_error_musttail (call, _("address of caller arguments taken"),
691 : : diag_musttail);
692 : : /* If current function has musttail calls, we can't disable tail
693 : : calls altogether for the whole caller, because those might be
694 : : actually fine. So just punt if this exact call is not
695 : : a tail recursion. */
696 : 28000 : if (cfun->has_musttail)
697 : : only_tailr = true;
698 : : else
699 : 27928 : opt_tailcalls = false;
700 : : }
701 : :
702 : : /* If the LHS of our call is not just a simple register or local
703 : : variable, we can't transform this into a tail or sibling call.
704 : : This situation happens, in (e.g.) "*p = foo()" where foo returns a
705 : : struct. In this case we won't have a temporary here, but we need
706 : : to carry out the side effect anyway, so tailcall is impossible.
707 : :
708 : : ??? In some situations (when the struct is returned in memory via
709 : : invisible argument) we could deal with this, e.g. by passing 'p'
710 : : itself as that argument to foo, but it's too early to do this here,
711 : : and expand_call() will not handle it anyway. If it ever can, then
712 : : we need to revisit this here, to allow that situation. */
713 : 1521717 : if (ass_var
714 : 431454 : && !is_gimple_reg (ass_var)
715 : 1582057 : && !auto_var_in_fn_p (ass_var, cfun->decl))
716 : : {
717 : 7917 : maybe_error_musttail (call, _("return value in memory"), diag_musttail);
718 : 7917 : return;
719 : : }
720 : :
721 : 1513800 : if (cfun->calls_setjmp)
722 : : {
723 : 164 : maybe_error_musttail (call, _("caller uses setjmp"), diag_musttail);
724 : 164 : return;
725 : : }
726 : :
727 : : /* If the call might throw an exception that wouldn't propagate out of
728 : : cfun, we can't transform to a tail or sibling call (82081). */
729 : 1513636 : if ((stmt_could_throw_p (cfun, stmt)
730 : 1513636 : && !stmt_can_throw_external (cfun, stmt)) || EDGE_COUNT (bb->succs) > 1)
731 : : {
732 : 19003 : if (stmt != last_stmt)
733 : : {
734 : 164 : maybe_error_musttail (call, _("code between call and return"),
735 : : diag_musttail);
736 : 19091 : return;
737 : : }
738 : :
739 : 18839 : edge e;
740 : 18839 : edge_iterator ei;
741 : 37608 : FOR_EACH_EDGE (e, ei, bb->succs)
742 : 18851 : if (e->flags & EDGE_EH)
743 : : break;
744 : :
745 : 18839 : if (!e)
746 : : {
747 : 18757 : maybe_error_musttail (call, _("call may throw exception that does not "
748 : : "propagate"), diag_musttail);
749 : 18757 : return;
750 : : }
751 : :
752 : 82 : if (diag_musttail && gimple_call_must_tail_p (call))
753 : 13 : eh_has_tsan_func_exit = 0;
754 : 82 : if (!gimple_call_must_tail_p (call)
755 : 82 : || !empty_eh_cleanup (e->dest,
756 : 82 : eh_has_tsan_func_exit
757 : : ? NULL : &eh_has_tsan_func_exit, 20)
758 : 158 : || EDGE_COUNT (bb->succs) > 2)
759 : : {
760 : 6 : maybe_error_musttail (call, _("call may throw exception caught "
761 : : "locally or perform cleanups"),
762 : : diag_musttail);
763 : 6 : return;
764 : : }
765 : : }
766 : :
767 : : /* If the function returns a value, then at present, the tail call
768 : : must return the same type of value. There is conceptually a copy
769 : : between the object returned by the tail call candidate and the
770 : : object returned by CFUN itself.
771 : :
772 : : This means that if we have:
773 : :
774 : : lhs = f (&<retval>); // f reads from <retval>
775 : : // (lhs is usually also <retval>)
776 : :
777 : : there is a copy between the temporary object returned by f and lhs,
778 : : meaning that any use of <retval> in f occurs before the assignment
779 : : to lhs begins. Thus the <retval> that is live on entry to the call
780 : : to f is really an independent local variable V that happens to be
781 : : stored in the RESULT_DECL rather than a local VAR_DECL.
782 : :
783 : : Turning this into a tail call would remove the copy and make the
784 : : lifetimes of the return value and V overlap. The same applies to
785 : : tail recursion, since if f can read from <retval>, we have to assume
786 : : that CFUN might already have written to <retval> before the call.
787 : :
788 : : The problem doesn't apply when <retval> is passed by value, but that
789 : : isn't a case we handle anyway. */
790 : 1494709 : tree result_decl = DECL_RESULT (cfun->decl);
791 : 1494709 : if (result_decl
792 : 1494709 : && may_be_aliased (result_decl)
793 : 1499327 : && ref_maybe_used_by_stmt_p (call, result_decl, false))
794 : : {
795 : 1560 : maybe_error_musttail (call, _("return value used after call"),
796 : : diag_musttail);
797 : 1560 : return;
798 : : }
799 : :
800 : : /* We found the call, check whether it is suitable. */
801 : 1493149 : tail_recursion = false;
802 : 1493149 : func = gimple_call_fndecl (call);
803 : 1493149 : if (func
804 : 1418211 : && !fndecl_built_in_p (func)
805 : 1087640 : && recursive_call_p (current_function_decl, func)
806 : 1496019 : && !only_musttail)
807 : : {
808 : 2853 : tree arg;
809 : :
810 : 2853 : for (param = DECL_ARGUMENTS (current_function_decl), idx = 0;
811 : 9845 : param && idx < gimple_call_num_args (call);
812 : 6992 : param = DECL_CHAIN (param), idx ++)
813 : : {
814 : 7057 : arg = gimple_call_arg (call, idx);
815 : 7057 : if (param != arg)
816 : : {
817 : : /* Make sure there are no problems with copying. The parameter
818 : : have a copyable type and the two arguments must have reasonably
819 : : equivalent types. The latter requirement could be relaxed if
820 : : we emitted a suitable type conversion statement. */
821 : 6873 : if (TREE_ADDRESSABLE (TREE_TYPE (param))
822 : 13746 : || !useless_type_conversion_p (TREE_TYPE (param),
823 : 6873 : TREE_TYPE (arg)))
824 : : break;
825 : :
826 : 13355 : if (is_gimple_reg_type (TREE_TYPE (param))
827 : 6837 : ? !is_gimple_reg (param)
828 : 319 : : (!is_gimple_variable (param)
829 : 319 : || TREE_THIS_VOLATILE (param)
830 : 638 : || may_be_aliased (param)))
831 : : break;
832 : : }
833 : : }
834 : 2853 : if (idx == gimple_call_num_args (call) && !param)
835 : 1493149 : tail_recursion = true;
836 : : }
837 : :
838 : 1493149 : if (only_tailr && !tail_recursion)
839 : : return;
840 : :
841 : : /* Compute live vars if not computed yet. */
842 : 1493077 : if (live_vars == NULL)
843 : : {
844 : 1448995 : unsigned int cnt = 0;
845 : 13340441 : FOR_EACH_LOCAL_DECL (cfun, idx, var)
846 : 10638447 : if (VAR_P (var)
847 : 10638447 : && auto_var_in_fn_p (var, cfun->decl)
848 : 21154511 : && may_be_aliased (var))
849 : : {
850 : 891674 : if (live_vars == NULL)
851 : 213427 : live_vars = new live_vars_map;
852 : 891674 : live_vars->put (DECL_UID (var), cnt++);
853 : : }
854 : 1448995 : if (live_vars)
855 : 213427 : live_vars_vec = compute_live_vars (cfun, live_vars);
856 : : }
857 : :
858 : : /* Determine a bitmap of variables which are still in scope after the
859 : : call. */
860 : 1493077 : bitmap local_live_vars = NULL;
861 : 1493077 : if (live_vars)
862 : 257509 : local_live_vars = live_vars_at_stmt (live_vars_vec, live_vars, call);
863 : :
864 : : /* Make sure the tail invocation of this function does not indirectly
865 : : refer to local variables. (Passing variables directly by value
866 : : is OK.) */
867 : 13622096 : FOR_EACH_LOCAL_DECL (cfun, idx, var)
868 : : {
869 : 11025625 : if (TREE_CODE (var) != PARM_DECL
870 : 11025625 : && auto_var_in_fn_p (var, cfun->decl)
871 : 10948182 : && may_be_aliased (var)
872 : 11767233 : && (ref_maybe_used_by_stmt_p (call, var, false)
873 : 331298 : || call_may_clobber_ref_p (call, var, false)))
874 : : {
875 : 470680 : if (!VAR_P (var))
876 : : {
877 : 0 : if (diag_musttail && gimple_call_must_tail_p (call))
878 : : {
879 : 0 : auto opt = OPT_Wmaybe_musttail_local_addr;
880 : 0 : if (!warning_suppressed_p (call,
881 : : opt))
882 : : {
883 : 0 : warning_at (gimple_location (call), opt,
884 : : "address of local variable can escape to "
885 : : "%<musttail%> call");
886 : 0 : suppress_warning (call, opt);
887 : : }
888 : 0 : continue;
889 : 0 : }
890 : 0 : if (local_live_vars)
891 : 0 : BITMAP_FREE (local_live_vars);
892 : 0 : maybe_error_musttail (call, _("call invocation refers to "
893 : : "locals"), diag_musttail);
894 : 0 : return;
895 : : }
896 : : else
897 : : {
898 : 470680 : unsigned int *v = live_vars->get (DECL_UID (var));
899 : 470680 : if (bitmap_bit_p (local_live_vars, *v))
900 : : {
901 : 193863 : if (diag_musttail && gimple_call_must_tail_p (call))
902 : : {
903 : 176 : auto opt = OPT_Wmaybe_musttail_local_addr;
904 : 176 : if (!warning_suppressed_p (call, opt))
905 : : {
906 : 83 : if (!DECL_ARTIFICIAL (var) && DECL_NAME (var))
907 : 82 : warning_at (gimple_location (call), opt,
908 : : "address of automatic variable %qD "
909 : : "can escape to %<musttail%> call",
910 : : var);
911 : : else
912 : 1 : warning_at (gimple_location (call), opt,
913 : : "address of local variable can escape "
914 : : "to %<musttail%> call");
915 : 83 : suppress_warning (call, opt);
916 : : }
917 : 176 : continue;
918 : 176 : }
919 : 193687 : BITMAP_FREE (local_live_vars);
920 : 193687 : maybe_error_musttail (call, _("call invocation refers to "
921 : : "locals"), diag_musttail);
922 : 193687 : return;
923 : : }
924 : : }
925 : : }
926 : : }
927 : 1299390 : if (diag_musttail
928 : 307971 : && gimple_call_must_tail_p (call)
929 : 1299977 : && !warning_suppressed_p (call, OPT_Wmaybe_musttail_local_addr))
930 : 373 : for (tree param = DECL_ARGUMENTS (current_function_decl);
931 : 734 : param; param = DECL_CHAIN (param))
932 : 385 : if (may_be_aliased (param)
933 : 385 : && (ref_maybe_used_by_stmt_p (call, param, false)
934 : 0 : || call_may_clobber_ref_p (call, param, false)))
935 : : {
936 : 24 : auto opt = OPT_Wmaybe_musttail_local_addr;
937 : 24 : warning_at (gimple_location (call), opt,
938 : : "address of parameter %qD can escape to "
939 : : "%<musttail%> call", param);
940 : 24 : suppress_warning (call, opt);
941 : 24 : break;
942 : : }
943 : :
944 : 1299390 : if (local_live_vars)
945 : 63822 : BITMAP_FREE (local_live_vars);
946 : :
947 : : /* Now check the statements after the call. None of them has virtual
948 : : operands, so they may only depend on the call through its return
949 : : value. The return value should also be dependent on each of them,
950 : : since we are running after dce. */
951 : 1299390 : m = NULL_TREE;
952 : 1299390 : a = NULL_TREE;
953 : 1299390 : auto_bitmap to_move_defs;
954 : 1299390 : auto_vec<gimple *> to_move_stmts;
955 : 1299390 : bool is_noreturn = gimple_call_noreturn_p (call);
956 : 1299390 : auto_vec<edge> edges;
957 : :
958 : 1299390 : abb = bb;
959 : 1299390 : agsi = gsi;
960 : 3074888 : while (!is_noreturn)
961 : : {
962 : 3074222 : tree tmp_a = NULL_TREE;
963 : 3074222 : tree tmp_m = NULL_TREE;
964 : 3074222 : gsi_next (&agsi);
965 : :
966 : 3597658 : while (gsi_end_p (agsi))
967 : : {
968 : 523436 : edge e = single_non_eh_succ_edge (abb);
969 : 523436 : ass_var = propagate_through_phis (ass_var, e);
970 : 523436 : if (!ass_var)
971 : 432783 : edges.safe_push (e);
972 : 523436 : abb = e->dest;
973 : 1046872 : agsi = gsi_start_bb (abb);
974 : : }
975 : :
976 : 3074222 : stmt = gsi_stmt (agsi);
977 : 3074222 : if (gimple_code (stmt) == GIMPLE_RETURN)
978 : : break;
979 : :
980 : 3619536 : if (gimple_code (stmt) == GIMPLE_LABEL
981 : 1841239 : || gimple_code (stmt) == GIMPLE_NOP
982 : 1841239 : || gimple_code (stmt) == GIMPLE_PREDICT
983 : 1821728 : || gimple_clobber_p (stmt)
984 : 3519499 : || is_gimple_debug (stmt))
985 : 3505753 : continue;
986 : :
987 : 113826 : if (cfun->has_musttail
988 : 249 : && sanitize_flags_p (SANITIZE_THREAD)
989 : 10 : && retry_tsan_func_exit == 1
990 : 10 : && gimple_call_builtin_p (stmt, BUILT_IN_TSAN_FUNC_EXIT)
991 : 8 : && !has_tsan_func_exit
992 : 113826 : && gimple_call_must_tail_p (call))
993 : : {
994 : 8 : has_tsan_func_exit = true;
995 : 8 : continue;
996 : : }
997 : :
998 : 113810 : if (gimple_code (stmt) != GIMPLE_ASSIGN)
999 : : {
1000 : 258 : maybe_error_musttail (call, _("unhandled code after call"),
1001 : : diag_musttail);
1002 : 91437 : return;
1003 : : }
1004 : :
1005 : : /* This is a gimple assign. */
1006 : 113552 : par ret = process_assignment (as_a <gassign *> (stmt), gsi,
1007 : : &tmp_m, &tmp_a, &ass_var, to_move_defs);
1008 : 113552 : if (ret == FAIL || (ret == TRY_MOVE && !tail_recursion))
1009 : : {
1010 : 90908 : maybe_error_musttail (call, _("return value changed after call"),
1011 : : diag_musttail);
1012 : 90908 : return;
1013 : : }
1014 : 22644 : else if (ret == TRY_MOVE)
1015 : : {
1016 : : /* Do not deal with checking dominance, the real fix is to
1017 : : do path isolation for the transform phase anyway, removing
1018 : : the need to compute the accumulators with new stmts. */
1019 : 40 : if (abb != bb)
1020 : : return;
1021 : 81 : for (unsigned opno = 1; opno < gimple_num_ops (stmt); ++opno)
1022 : : {
1023 : 54 : tree op = gimple_op (stmt, opno);
1024 : 54 : if (independent_of_stmt_p (op, stmt, gsi, to_move_defs) != op)
1025 : : return;
1026 : : }
1027 : 54 : bitmap_set_bit (to_move_defs,
1028 : 27 : SSA_NAME_VERSION (gimple_assign_lhs (stmt)));
1029 : 27 : to_move_stmts.safe_push (stmt);
1030 : 27 : continue;
1031 : 27 : }
1032 : :
1033 : 22604 : if (tmp_a)
1034 : : {
1035 : 8376 : tree type = TREE_TYPE (tmp_a);
1036 : 8376 : if (a)
1037 : 296 : a = fold_build2 (PLUS_EXPR, type, fold_convert (type, a), tmp_a);
1038 : : else
1039 : : a = tmp_a;
1040 : : }
1041 : 22604 : if (tmp_m)
1042 : : {
1043 : 2157 : tree type = TREE_TYPE (tmp_m);
1044 : 2157 : if (m)
1045 : 76 : m = fold_build2 (MULT_EXPR, type, fold_convert (type, m), tmp_m);
1046 : : else
1047 : : m = tmp_m;
1048 : :
1049 : 2157 : if (a)
1050 : 1681 : a = fold_build2 (MULT_EXPR, type, fold_convert (type, a), tmp_m);
1051 : : }
1052 : : }
1053 : :
1054 : : /* See if this is a tail call we can handle. */
1055 : 1208211 : if (is_noreturn)
1056 : : {
1057 : 666 : if (gimple_call_internal_p (call))
1058 : : {
1059 : 654 : maybe_error_musttail (call, _("internal call"), diag_musttail);
1060 : 654 : return;
1061 : : }
1062 : 12 : tree rettype = TREE_TYPE (TREE_TYPE (current_function_decl));
1063 : 12 : tree calltype = TREE_TYPE (gimple_call_fntype (call));
1064 : 12 : if (!VOID_TYPE_P (rettype)
1065 : 12 : && !useless_type_conversion_p (rettype, calltype))
1066 : : {
1067 : 0 : maybe_error_musttail (call, _("call and return value are different"),
1068 : : diag_musttail);
1069 : 0 : return;
1070 : : }
1071 : : ret_var = NULL_TREE;
1072 : : }
1073 : : else
1074 : 1207545 : ret_var = gimple_return_retval (as_a <greturn *> (stmt));
1075 : :
1076 : : /* We may proceed if there either is no return value, or the return value
1077 : : is identical to the call's return or if the return decl is an empty type
1078 : : variable and the call's return was not assigned. */
1079 : 1207545 : if (ret_var
1080 : 1207545 : && (ret_var != ass_var
1081 : 370100 : && !(is_empty_type (TREE_TYPE (ret_var)) && !ass_var)))
1082 : : {
1083 : 369594 : bool ok = false;
1084 : 369594 : value_range val;
1085 : 369594 : if (ass_var == NULL_TREE && !tail_recursion)
1086 : : {
1087 : 369490 : tree other_value = NULL_TREE;
1088 : : /* If we have a function call that we know the return value is the same
1089 : : as the argument, try the argument too. */
1090 : 369490 : int flags = gimple_call_return_flags (call);
1091 : 369490 : if ((flags & ERF_RETURNS_ARG) != 0
1092 : 369490 : && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (call))
1093 : : {
1094 : 4309 : tree arg = gimple_call_arg (call, flags & ERF_RETURN_ARG_MASK);
1095 : 4309 : if (useless_type_conversion_p (TREE_TYPE (ret_var), TREE_TYPE (arg) ))
1096 : : other_value = arg;
1097 : : }
1098 : : /* If IPA-VRP proves called function always returns a singleton range,
1099 : : the return value is replaced by the only value in that range.
1100 : : For tail call purposes, pretend such replacement didn't happen. */
1101 : 365181 : else if (tree type = gimple_range_type (call))
1102 : 81811 : if (tree callee = gimple_call_fndecl (call))
1103 : : {
1104 : 81248 : tree valr;
1105 : 81248 : if ((INTEGRAL_TYPE_P (type)
1106 : : || SCALAR_FLOAT_TYPE_P (type)
1107 : 81248 : || POINTER_TYPE_P (type))
1108 : 81248 : && useless_type_conversion_p (TREE_TYPE (TREE_TYPE (callee)),
1109 : : type)
1110 : 81240 : && useless_type_conversion_p (TREE_TYPE (ret_var), type)
1111 : 21169 : && ipa_return_value_range (val, callee)
1112 : 90972 : && val.singleton_p (&valr))
1113 : 6441 : other_value = valr;
1114 : : }
1115 : :
1116 : 83731 : if (other_value)
1117 : : {
1118 : 8924 : tree rv = ret_var;
1119 : 8924 : unsigned int i = edges.length ();
1120 : : /* If ret_var is equal to other_value, we can tail optimize. */
1121 : 8924 : if (operand_equal_p (ret_var, other_value, 0))
1122 : : ok = true;
1123 : : else
1124 : : /* Otherwise, if ret_var is a PHI result, try to find out
1125 : : if other_value isn't propagated through PHIs on the path from
1126 : : call's bb to SSA_NAME_DEF_STMT (ret_var)'s bb. */
1127 : 3890 : while (TREE_CODE (rv) == SSA_NAME
1128 : 3890 : && gimple_code (SSA_NAME_DEF_STMT (rv)) == GIMPLE_PHI)
1129 : : {
1130 : 1465 : tree nrv = NULL_TREE;
1131 : : gimple *g = SSA_NAME_DEF_STMT (rv);
1132 : 1465 : for (; i; --i)
1133 : : {
1134 : 1465 : if (edges[i - 1]->dest == gimple_bb (g))
1135 : : {
1136 : 1465 : nrv = gimple_phi_arg_def_from_edge (g,
1137 : 1465 : edges[i - 1]);
1138 : 1465 : --i;
1139 : 1465 : break;
1140 : : }
1141 : : }
1142 : 1465 : if (nrv == NULL_TREE)
1143 : : break;
1144 : 1465 : if (operand_equal_p (nrv, other_value, 0))
1145 : : {
1146 : : ok = true;
1147 : : break;
1148 : : }
1149 : : rv = nrv;
1150 : : }
1151 : : }
1152 : : }
1153 : 2790 : if (!ok)
1154 : : {
1155 : 363095 : maybe_error_musttail (call, _("call and return value are different"),
1156 : : diag_musttail);
1157 : 363095 : return;
1158 : : }
1159 : 369594 : }
1160 : :
1161 : : /* If this is not a tail recursive call, we cannot handle addends or
1162 : : multiplicands. */
1163 : 844462 : if (!tail_recursion && (m || a))
1164 : : {
1165 : 7674 : maybe_error_musttail (call, _("operations after non tail recursive "
1166 : : "call"), diag_musttail);
1167 : 7674 : return;
1168 : : }
1169 : :
1170 : : /* For pointers only allow additions. */
1171 : 2129 : if (m && POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
1172 : : {
1173 : 0 : maybe_error_musttail (call, _("tail recursion with pointers can only "
1174 : : "use additions"), diag_musttail);
1175 : 0 : return;
1176 : : }
1177 : :
1178 : 836788 : if (eh_has_tsan_func_exit != -1
1179 : 13 : && eh_has_tsan_func_exit != has_tsan_func_exit)
1180 : : {
1181 : 0 : if (eh_has_tsan_func_exit)
1182 : 0 : maybe_error_musttail (call, _("call may throw exception caught "
1183 : : "locally or perform cleanups"),
1184 : : diag_musttail);
1185 : : else
1186 : 0 : maybe_error_musttail (call, _("exception cleanups omit "
1187 : : "__tsan_func_exit call"), diag_musttail);
1188 : 0 : return;
1189 : : }
1190 : :
1191 : : /* Move queued defs. */
1192 : 836788 : if (tail_recursion)
1193 : : {
1194 : : unsigned i;
1195 : 2132 : FOR_EACH_VEC_ELT (to_move_stmts, i, stmt)
1196 : : {
1197 : 3 : gimple_stmt_iterator mgsi = gsi_for_stmt (stmt);
1198 : 3 : gsi_move_before (&mgsi, &gsi);
1199 : : }
1200 : 2129 : if (!tailr_arg_needs_copy)
1201 : 1202 : tailr_arg_needs_copy = BITMAP_ALLOC (NULL);
1202 : 2129 : for (param = DECL_ARGUMENTS (current_function_decl), idx = 0;
1203 : 7944 : param;
1204 : 5815 : param = DECL_CHAIN (param), idx++)
1205 : : {
1206 : 5815 : tree ddef, arg = gimple_call_arg (call, idx);
1207 : 5815 : if (!is_gimple_reg (param)
1208 : 5815 : || ((ddef = ssa_default_def (cfun, param))
1209 : 5279 : && arg != ddef))
1210 : 2820 : bitmap_set_bit (tailr_arg_needs_copy, idx);
1211 : : }
1212 : : }
1213 : :
1214 : 836788 : nw = XNEW (struct tailcall);
1215 : :
1216 : 836788 : nw->call_gsi = gsi;
1217 : :
1218 : 836788 : nw->tail_recursion = tail_recursion;
1219 : 836788 : nw->has_tsan_func_exit = has_tsan_func_exit;
1220 : :
1221 : 836788 : nw->mult = m;
1222 : 836788 : nw->add = a;
1223 : :
1224 : 836788 : nw->next = *ret;
1225 : 836788 : *ret = nw;
1226 : 1299390 : }
1227 : :
1228 : : /* Helper to insert PHI_ARGH to the phi of VAR in the destination of edge E. */
1229 : :
1230 : : static void
1231 : 212 : add_successor_phi_arg (edge e, tree var, tree phi_arg)
1232 : : {
1233 : 212 : gphi_iterator gsi;
1234 : :
1235 : 450 : for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
1236 : 450 : if (PHI_RESULT (gsi.phi ()) == var)
1237 : : break;
1238 : :
1239 : 212 : gcc_assert (!gsi_end_p (gsi));
1240 : 212 : add_phi_arg (gsi.phi (), phi_arg, e, UNKNOWN_LOCATION);
1241 : 212 : }
1242 : :
1243 : : /* Creates a GIMPLE statement which computes the operation specified by
1244 : : CODE, ACC and OP1 to a new variable with name LABEL and inserts the
1245 : : statement in the position specified by GSI. Returns the
1246 : : tree node of the statement's result. */
1247 : :
1248 : : static tree
1249 : 178 : adjust_return_value_with_ops (enum tree_code code, const char *label,
1250 : : tree acc, tree op1, gimple_stmt_iterator gsi)
1251 : : {
1252 : :
1253 : 178 : tree ret_type = TREE_TYPE (DECL_RESULT (current_function_decl));
1254 : 178 : tree result = make_temp_ssa_name (ret_type, NULL, label);
1255 : 178 : gassign *stmt;
1256 : :
1257 : 178 : if (POINTER_TYPE_P (ret_type))
1258 : : {
1259 : 6 : gcc_assert (code == PLUS_EXPR && TREE_TYPE (acc) == sizetype);
1260 : : code = POINTER_PLUS_EXPR;
1261 : : }
1262 : 178 : if (types_compatible_p (TREE_TYPE (acc), TREE_TYPE (op1))
1263 : 178 : && code != POINTER_PLUS_EXPR)
1264 : 167 : stmt = gimple_build_assign (result, code, acc, op1);
1265 : : else
1266 : : {
1267 : 11 : tree tem;
1268 : 11 : if (code == POINTER_PLUS_EXPR)
1269 : 6 : tem = fold_build2 (code, TREE_TYPE (op1), op1, acc);
1270 : : else
1271 : 5 : tem = fold_build2 (code, TREE_TYPE (op1),
1272 : : fold_convert (TREE_TYPE (op1), acc), op1);
1273 : 11 : tree rhs = fold_convert (ret_type, tem);
1274 : 11 : rhs = force_gimple_operand_gsi (&gsi, rhs,
1275 : : false, NULL, true, GSI_SAME_STMT);
1276 : 11 : stmt = gimple_build_assign (result, rhs);
1277 : : }
1278 : :
1279 : 178 : gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
1280 : 178 : return result;
1281 : : }
1282 : :
1283 : : /* Creates a new GIMPLE statement that adjusts the value of accumulator ACC by
1284 : : the computation specified by CODE and OP1 and insert the statement
1285 : : at the position specified by GSI as a new statement. Returns new SSA name
1286 : : of updated accumulator. */
1287 : :
1288 : : static tree
1289 : 209 : update_accumulator_with_ops (enum tree_code code, tree acc, tree op1,
1290 : : gimple_stmt_iterator gsi)
1291 : : {
1292 : 209 : gassign *stmt;
1293 : 209 : tree var = copy_ssa_name (acc);
1294 : 209 : if (types_compatible_p (TREE_TYPE (acc), TREE_TYPE (op1)))
1295 : 195 : stmt = gimple_build_assign (var, code, acc, op1);
1296 : : else
1297 : : {
1298 : 14 : tree rhs = fold_convert (TREE_TYPE (acc),
1299 : : fold_build2 (code,
1300 : : TREE_TYPE (op1),
1301 : : fold_convert (TREE_TYPE (op1), acc),
1302 : : op1));
1303 : 14 : rhs = force_gimple_operand_gsi (&gsi, rhs,
1304 : : false, NULL, false, GSI_CONTINUE_LINKING);
1305 : 14 : stmt = gimple_build_assign (var, rhs);
1306 : : }
1307 : 209 : gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
1308 : 209 : return var;
1309 : : }
1310 : :
1311 : : /* Adjust the accumulator values according to A and M after GSI, and update
1312 : : the phi nodes on edge BACK. */
1313 : :
1314 : : static void
1315 : 2123 : adjust_accumulator_values (gimple_stmt_iterator gsi, tree m, tree a, edge back)
1316 : : {
1317 : 2123 : tree var, a_acc_arg, m_acc_arg;
1318 : :
1319 : 2123 : if (m)
1320 : 115 : m = force_gimple_operand_gsi (&gsi, m, true, NULL, true, GSI_SAME_STMT);
1321 : 2123 : if (a)
1322 : 94 : a = force_gimple_operand_gsi (&gsi, a, true, NULL, true, GSI_SAME_STMT);
1323 : :
1324 : 2123 : a_acc_arg = a_acc;
1325 : 2123 : m_acc_arg = m_acc;
1326 : 2123 : if (a)
1327 : : {
1328 : 94 : if (m_acc)
1329 : : {
1330 : 18 : if (integer_onep (a))
1331 : 1 : var = m_acc;
1332 : : else
1333 : 17 : var = adjust_return_value_with_ops (MULT_EXPR, "acc_tmp", m_acc,
1334 : : a, gsi);
1335 : : }
1336 : : else
1337 : : var = a;
1338 : :
1339 : 94 : a_acc_arg = update_accumulator_with_ops (PLUS_EXPR, a_acc, var, gsi);
1340 : : }
1341 : :
1342 : 2123 : if (m)
1343 : 115 : m_acc_arg = update_accumulator_with_ops (MULT_EXPR, m_acc, m, gsi);
1344 : :
1345 : 2123 : if (a_acc)
1346 : 97 : add_successor_phi_arg (back, a_acc, a_acc_arg);
1347 : :
1348 : 2123 : if (m_acc)
1349 : 115 : add_successor_phi_arg (back, m_acc, m_acc_arg);
1350 : 2123 : }
1351 : :
1352 : : /* Adjust value of the return at the end of BB according to M and A
1353 : : accumulators. */
1354 : :
1355 : : static void
1356 : 148 : adjust_return_value (basic_block bb, tree m, tree a)
1357 : : {
1358 : 148 : tree retval;
1359 : 296 : greturn *ret_stmt = as_a <greturn *> (gimple_seq_last_stmt (bb_seq (bb)));
1360 : 148 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
1361 : :
1362 : 148 : gcc_assert (gimple_code (ret_stmt) == GIMPLE_RETURN);
1363 : :
1364 : 148 : retval = gimple_return_retval (ret_stmt);
1365 : 148 : if (!retval || retval == error_mark_node)
1366 : 0 : return;
1367 : :
1368 : 148 : if (m)
1369 : 86 : retval = adjust_return_value_with_ops (MULT_EXPR, "mul_tmp", m_acc, retval,
1370 : : gsi);
1371 : 148 : if (a)
1372 : 75 : retval = adjust_return_value_with_ops (PLUS_EXPR, "acc_tmp", a_acc, retval,
1373 : : gsi);
1374 : 148 : gimple_return_set_retval (ret_stmt, retval);
1375 : 148 : update_stmt (ret_stmt);
1376 : : }
1377 : :
1378 : : /* Subtract COUNT and FREQUENCY from the basic block and it's
1379 : : outgoing edge. */
1380 : : static void
1381 : 6104 : decrease_profile (basic_block bb, profile_count count)
1382 : : {
1383 : 0 : bb->count = bb->count - count;
1384 : 0 : }
1385 : :
1386 : : /* Eliminates tail call described by T. TMP_VARS is a list of
1387 : : temporary variables used to copy the function arguments.
1388 : : Allocates *NEW_LOOP if not already done and initializes it. */
1389 : :
1390 : : static void
1391 : 2123 : eliminate_tail_call (struct tailcall *t, class loop *&new_loop)
1392 : : {
1393 : 2123 : tree param, rslt;
1394 : 2123 : gimple *stmt, *call;
1395 : 2123 : tree arg;
1396 : 2123 : size_t idx;
1397 : 2123 : basic_block bb, first;
1398 : 2123 : edge e;
1399 : 2123 : gphi *phi;
1400 : 2123 : gphi_iterator gpi;
1401 : 2123 : gimple_stmt_iterator gsi;
1402 : 2123 : gimple *orig_stmt;
1403 : :
1404 : 2123 : stmt = orig_stmt = gsi_stmt (t->call_gsi);
1405 : 2123 : bb = gsi_bb (t->call_gsi);
1406 : :
1407 : 2123 : if (dump_file && (dump_flags & TDF_DETAILS))
1408 : : {
1409 : 8 : fprintf (dump_file, "Eliminated tail recursion in bb %d : ",
1410 : : bb->index);
1411 : 8 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1412 : 8 : fprintf (dump_file, "\n");
1413 : : }
1414 : :
1415 : 2123 : gcc_assert (is_gimple_call (stmt));
1416 : :
1417 : 2123 : first = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1418 : :
1419 : : /* Remove the code after call_gsi that will become unreachable. The
1420 : : possibly unreachable code in other blocks is removed later in
1421 : : cfg cleanup. */
1422 : 2123 : gsi = t->call_gsi;
1423 : 2123 : gimple_stmt_iterator gsi2 = gsi_last_bb (gimple_bb (gsi_stmt (gsi)));
1424 : 3473 : while (gsi_stmt (gsi2) != gsi_stmt (gsi))
1425 : : {
1426 : 1350 : gimple *t = gsi_stmt (gsi2);
1427 : : /* Do not remove the return statement, so that redirect_edge_and_branch
1428 : : sees how the block ends. */
1429 : 1350 : if (gimple_code (t) != GIMPLE_RETURN)
1430 : : {
1431 : 1097 : gimple_stmt_iterator gsi3 = gsi2;
1432 : 1097 : gsi_prev (&gsi2);
1433 : 1097 : gsi_remove (&gsi3, true);
1434 : 1097 : release_defs (t);
1435 : : }
1436 : : else
1437 : 3726 : gsi_prev (&gsi2);
1438 : : }
1439 : :
1440 : 2123 : if (gimple_call_noreturn_p (as_a <gcall *> (stmt)))
1441 : : {
1442 : 4 : e = make_edge (gsi_bb (t->call_gsi), first, EDGE_FALLTHRU);
1443 : 4 : e->probability = profile_probability::always ();
1444 : : }
1445 : : else
1446 : : {
1447 : : /* Number of executions of function has reduced by the tailcall. */
1448 : 2119 : e = single_non_eh_succ_edge (gsi_bb (t->call_gsi));
1449 : :
1450 : 2119 : profile_count count = e->count ();
1451 : :
1452 : : /* When profile is inconsistent and the recursion edge is more frequent
1453 : : than number of executions of functions, scale it down, so we do not
1454 : : end up with 0 executions of entry block. */
1455 : 2119 : if (count >= ENTRY_BLOCK_PTR_FOR_FN (cfun)->count)
1456 : 38 : count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.apply_scale (7, 8);
1457 : 2119 : decrease_profile (EXIT_BLOCK_PTR_FOR_FN (cfun), count);
1458 : 2119 : decrease_profile (ENTRY_BLOCK_PTR_FOR_FN (cfun), count);
1459 : 2119 : if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1460 : 1866 : decrease_profile (e->dest, count);
1461 : :
1462 : : /* Replace the call by a jump to the start of function. */
1463 : 2119 : e = redirect_edge_and_branch (e, first);
1464 : : }
1465 : 2123 : gcc_assert (e);
1466 : 2123 : PENDING_STMT (e) = NULL;
1467 : :
1468 : : /* Add the new loop. */
1469 : 2123 : if (!new_loop)
1470 : : {
1471 : 1200 : new_loop = alloc_loop ();
1472 : 1200 : new_loop->header = first;
1473 : 1200 : new_loop->finite_p = true;
1474 : : }
1475 : : else
1476 : 923 : gcc_assert (new_loop->header == first);
1477 : :
1478 : : /* Add phi node entries for arguments. The ordering of the phi nodes should
1479 : : be the same as the ordering of the arguments. */
1480 : 2123 : auto_vec<tree> copies;
1481 : 2123 : for (param = DECL_ARGUMENTS (current_function_decl),
1482 : 2123 : idx = 0, gpi = gsi_start_phis (first);
1483 : 7926 : param;
1484 : 5803 : param = DECL_CHAIN (param), idx++)
1485 : : {
1486 : 5803 : if (!bitmap_bit_p (tailr_arg_needs_copy, idx))
1487 : 2980 : continue;
1488 : :
1489 : 2823 : if (!is_gimple_reg_type (TREE_TYPE (param)))
1490 : : {
1491 : 498 : if (param == gimple_call_arg (stmt, idx))
1492 : 184 : continue;
1493 : : /* First check if param isn't used by any of the following
1494 : : call arguments. If it is, we need to copy first to
1495 : : a temporary and only after doing all the assignments copy it
1496 : : to param. */
1497 : 314 : size_t idx2 = idx + 1;
1498 : 314 : tree param2 = DECL_CHAIN (param);
1499 : 1709 : for (; param2; param2 = DECL_CHAIN (param2), idx2++)
1500 : 1396 : if (!is_gimple_reg_type (TREE_TYPE (param)))
1501 : : {
1502 : 1396 : tree base = get_base_address (gimple_call_arg (stmt, idx2));
1503 : 1396 : if (base == param)
1504 : : break;
1505 : : }
1506 : 314 : tree tmp = param;
1507 : 314 : if (param2)
1508 : : {
1509 : 1 : tmp = create_tmp_var (TREE_TYPE (param));
1510 : 1 : copies.safe_push (param);
1511 : 1 : copies.safe_push (tmp);
1512 : : }
1513 : 314 : gimple *g = gimple_build_assign (tmp, gimple_call_arg (stmt, idx));
1514 : 314 : gsi_insert_before (&t->call_gsi, g, GSI_SAME_STMT);
1515 : 314 : continue;
1516 : 314 : }
1517 : :
1518 : 2325 : arg = gimple_call_arg (stmt, idx);
1519 : 2325 : phi = gpi.phi ();
1520 : 2325 : gcc_assert (param == SSA_NAME_VAR (PHI_RESULT (phi)));
1521 : :
1522 : 2325 : add_phi_arg (phi, arg, e, gimple_location (stmt));
1523 : 2325 : gsi_next (&gpi);
1524 : : }
1525 : 2124 : for (unsigned i = 0; i < copies.length (); i += 2)
1526 : : {
1527 : 1 : gimple *g = gimple_build_assign (copies[i], copies[i + 1]);
1528 : 1 : gsi_insert_before (&t->call_gsi, g, GSI_SAME_STMT);
1529 : : }
1530 : :
1531 : : /* Update the values of accumulators. */
1532 : 2123 : adjust_accumulator_values (t->call_gsi, t->mult, t->add, e);
1533 : :
1534 : 2123 : call = gsi_stmt (t->call_gsi);
1535 : 2123 : rslt = gimple_call_lhs (call);
1536 : 2123 : if (rslt != NULL_TREE && TREE_CODE (rslt) == SSA_NAME)
1537 : : {
1538 : : /* Result of the call will no longer be defined. So adjust the
1539 : : SSA_NAME_DEF_STMT accordingly. */
1540 : 512 : SSA_NAME_DEF_STMT (rslt) = gimple_build_nop ();
1541 : : }
1542 : :
1543 : 2123 : gsi_remove (&t->call_gsi, true);
1544 : 2123 : release_defs (call);
1545 : 2123 : }
1546 : :
1547 : : /* Optimizes the tailcall described by T. If OPT_TAILCALLS is true, also
1548 : : mark the tailcalls for the sibcall optimization. */
1549 : :
1550 : : static bool
1551 : 836782 : optimize_tail_call (struct tailcall *t, bool opt_tailcalls,
1552 : : class loop *&new_loop)
1553 : : {
1554 : 836782 : if (t->has_tsan_func_exit && (t->tail_recursion || opt_tailcalls))
1555 : : {
1556 : 8 : tree builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_FUNC_EXIT);
1557 : 8 : gimple *g = gimple_build_call (builtin_decl, 0);
1558 : 8 : gimple_set_location (g, cfun->function_end_locus);
1559 : 8 : gsi_insert_before (&t->call_gsi, g, GSI_SAME_STMT);
1560 : : }
1561 : :
1562 : 836782 : if (t->tail_recursion)
1563 : : {
1564 : 2123 : eliminate_tail_call (t, new_loop);
1565 : 2123 : return true;
1566 : : }
1567 : :
1568 : 834659 : if (opt_tailcalls)
1569 : : {
1570 : 173389 : gcall *stmt = as_a <gcall *> (gsi_stmt (t->call_gsi));
1571 : :
1572 : 173389 : gimple_call_set_tail (stmt, true);
1573 : 173389 : cfun->tail_call_marked = true;
1574 : 173389 : if (dump_file && (dump_flags & TDF_DETAILS))
1575 : : {
1576 : 25 : fprintf (dump_file, "Found tail call ");
1577 : 25 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1578 : 25 : fprintf (dump_file, " in bb %i\n", (gsi_bb (t->call_gsi))->index);
1579 : : }
1580 : 173389 : return t->has_tsan_func_exit;
1581 : : }
1582 : :
1583 : : return false;
1584 : : }
1585 : :
1586 : : /* Creates a tail-call accumulator of the same type as the return type of the
1587 : : current function. LABEL is the name used to creating the temporary
1588 : : variable for the accumulator. The accumulator will be inserted in the
1589 : : phis of a basic block BB with single predecessor with an initial value
1590 : : INIT converted to the current function return type. */
1591 : :
1592 : : static tree
1593 : 194 : create_tailcall_accumulator (const char *label, basic_block bb, tree init)
1594 : : {
1595 : 194 : tree ret_type = TREE_TYPE (DECL_RESULT (current_function_decl));
1596 : 194 : if (POINTER_TYPE_P (ret_type))
1597 : 6 : ret_type = sizetype;
1598 : :
1599 : 194 : tree tmp = make_temp_ssa_name (ret_type, NULL, label);
1600 : 194 : gphi *phi;
1601 : :
1602 : 194 : phi = create_phi_node (tmp, bb);
1603 : 194 : add_phi_arg (phi, init, single_pred_edge (bb),
1604 : : UNKNOWN_LOCATION);
1605 : 194 : return PHI_RESULT (phi);
1606 : : }
1607 : :
1608 : : /* Optimizes tail calls in the function, turning the tail recursion
1609 : : into iteration. When ONLY_MUSTTAIL is true only optimize musttail
1610 : : marked calls. When DIAG_MUSTTAIL, diagnose if musttail calls can't
1611 : : be tail call optimized. */
1612 : :
1613 : : static unsigned int
1614 : 3496880 : tree_optimize_tail_calls_1 (bool opt_tailcalls, bool only_musttail,
1615 : : bool diag_musttail)
1616 : : {
1617 : 3496880 : edge e;
1618 : 3496880 : bool phis_constructed = false;
1619 : 3496880 : struct tailcall *tailcalls = NULL, *act, *next;
1620 : 3496880 : bool changed = false;
1621 : 3496880 : basic_block first = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1622 : 3496880 : tree param;
1623 : 3496880 : edge_iterator ei;
1624 : :
1625 : 6917515 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
1626 : : {
1627 : : /* Only traverse the normal exits, i.e. those that end with return
1628 : : statement. */
1629 : 10261055 : if (safe_is_a <greturn *> (*gsi_last_bb (e->src)))
1630 : : {
1631 : 3419785 : int retry_tsan_func_exit = 0;
1632 : 3419785 : find_tail_calls (e->src, &tailcalls, only_musttail, opt_tailcalls,
1633 : : diag_musttail, retry_tsan_func_exit);
1634 : 3419785 : if (retry_tsan_func_exit == -1)
1635 : : {
1636 : 8 : retry_tsan_func_exit = 1;
1637 : 8 : find_tail_calls (e->src, &tailcalls, only_musttail,
1638 : : opt_tailcalls, diag_musttail,
1639 : : retry_tsan_func_exit);
1640 : : }
1641 : : }
1642 : : }
1643 : 3496880 : if (cfun->has_musttail && diag_musttail)
1644 : : {
1645 : 347 : basic_block bb;
1646 : 347 : int retry_tsan_func_exit = 0;
1647 : 1874 : FOR_EACH_BB_FN (bb, cfun)
1648 : 1527 : if (EDGE_COUNT (bb->succs) == 0
1649 : 1521 : || (single_succ_p (bb)
1650 : 1153 : && (single_succ_edge (bb)->flags & EDGE_EH)))
1651 : 31 : if (gimple *c = last_nondebug_stmt (bb))
1652 : 31 : if (is_gimple_call (c)
1653 : 12 : && gimple_call_must_tail_p (as_a <gcall *> (c))
1654 : 43 : && gimple_call_noreturn_p (as_a <gcall *> (c)))
1655 : 12 : find_tail_calls (bb, &tailcalls, only_musttail, opt_tailcalls,
1656 : : diag_musttail, retry_tsan_func_exit);
1657 : : }
1658 : :
1659 : 3496880 : if (live_vars)
1660 : : {
1661 : 213427 : destroy_live_vars (live_vars_vec);
1662 : 426854 : delete live_vars;
1663 : 213427 : live_vars = NULL;
1664 : : }
1665 : :
1666 : 3496880 : if (cfun->has_musttail)
1667 : : {
1668 : : /* We can't mix non-recursive must tail calls with tail recursive
1669 : : calls which require accumulators, because in that case we have to
1670 : : emit code in between the musttail calls and return, which prevent
1671 : : calling them as tail calls. So, in that case give up on the
1672 : : tail recursion. */
1673 : 707 : for (act = tailcalls; act; act = act->next)
1674 : 538 : if (!act->tail_recursion)
1675 : : {
1676 : 508 : gcall *call = as_a <gcall *> (gsi_stmt (act->call_gsi));
1677 : 508 : if (gimple_call_must_tail_p (call))
1678 : : break;
1679 : : }
1680 : 668 : if (act)
1681 : 1444 : for (struct tailcall **p = &tailcalls; *p; )
1682 : : {
1683 : 945 : if ((*p)->tail_recursion && ((*p)->add || (*p)->mult))
1684 : : {
1685 : 6 : struct tailcall *a = *p;
1686 : 6 : *p = (*p)->next;
1687 : 6 : gcall *call = as_a <gcall *> (gsi_stmt (a->call_gsi));
1688 : 6 : maybe_error_musttail (call, _("tail recursion with "
1689 : : "accumulation mixed with "
1690 : : "musttail non-recursive call"),
1691 : : diag_musttail);
1692 : 6 : free (a);
1693 : 6 : }
1694 : : else
1695 : 939 : p = &(*p)->next;
1696 : : }
1697 : : }
1698 : : /* Construct the phi nodes and accumulators if necessary. */
1699 : 3496880 : a_acc = m_acc = NULL_TREE;
1700 : 4333662 : for (act = tailcalls; act; act = act->next)
1701 : : {
1702 : 836782 : if (!act->tail_recursion)
1703 : 834659 : continue;
1704 : :
1705 : 2123 : if (!phis_constructed)
1706 : : {
1707 : : /* Ensure that there is only one predecessor of the block
1708 : : or if there are existing degenerate PHI nodes. */
1709 : 1200 : if (!single_pred_p (first)
1710 : 1200 : || !gimple_seq_empty_p (phi_nodes (first)))
1711 : 0 : first
1712 : 0 : = split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1713 : :
1714 : : /* Copy the args if needed. */
1715 : 1200 : unsigned idx;
1716 : 1200 : for (param = DECL_ARGUMENTS (current_function_decl), idx = 0;
1717 : 4332 : param;
1718 : 3132 : param = DECL_CHAIN (param), idx++)
1719 : 3132 : if (bitmap_bit_p (tailr_arg_needs_copy, idx))
1720 : : {
1721 : 1874 : if (!is_gimple_reg_type (TREE_TYPE (param)))
1722 : 498 : continue;
1723 : 1376 : tree name = ssa_default_def (cfun, param);
1724 : 1376 : tree new_name = make_ssa_name (param, SSA_NAME_DEF_STMT (name));
1725 : 1376 : gphi *phi;
1726 : :
1727 : 1376 : set_ssa_default_def (cfun, param, new_name);
1728 : 1376 : phi = create_phi_node (name, first);
1729 : 1376 : add_phi_arg (phi, new_name, single_pred_edge (first),
1730 : 1376 : EXPR_LOCATION (param));
1731 : : }
1732 : : phis_constructed = true;
1733 : : }
1734 : 2123 : tree ret_type = TREE_TYPE (DECL_RESULT (current_function_decl));
1735 : 2123 : if (POINTER_TYPE_P (ret_type))
1736 : 141 : ret_type = sizetype;
1737 : :
1738 : 2123 : if (act->add && !a_acc)
1739 : 88 : a_acc = create_tailcall_accumulator ("add_acc", first,
1740 : : build_zero_cst (ret_type));
1741 : :
1742 : 2123 : if (act->mult && !m_acc)
1743 : 106 : m_acc = create_tailcall_accumulator ("mult_acc", first,
1744 : : build_one_cst (ret_type));
1745 : : }
1746 : :
1747 : 3496880 : if (a_acc || m_acc)
1748 : : {
1749 : : /* When the tail call elimination using accumulators is performed,
1750 : : statements adding the accumulated value are inserted at all exits.
1751 : : This turns all other tail calls to non-tail ones. */
1752 : 176 : opt_tailcalls = false;
1753 : : }
1754 : :
1755 : 3496880 : class loop *new_loop = NULL;
1756 : 4333662 : for (; tailcalls; tailcalls = next)
1757 : : {
1758 : 836782 : next = tailcalls->next;
1759 : 836782 : changed |= optimize_tail_call (tailcalls, opt_tailcalls, new_loop);
1760 : 836782 : free (tailcalls);
1761 : : }
1762 : 3496880 : if (new_loop)
1763 : 1200 : add_loop (new_loop, loops_for_fn (cfun)->tree_root);
1764 : :
1765 : 3496880 : if (a_acc || m_acc)
1766 : : {
1767 : : /* Modify the remaining return statements. */
1768 : 324 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
1769 : : {
1770 : 444 : if (safe_is_a <greturn *> (*gsi_last_bb (e->src)))
1771 : 148 : adjust_return_value (e->src, m_acc, a_acc);
1772 : : }
1773 : : }
1774 : :
1775 : 3496880 : if (changed)
1776 : 1206 : free_dominance_info (CDI_DOMINATORS);
1777 : :
1778 : : /* Add phi nodes for the virtual operands defined in the function to the
1779 : : header of the loop created by tail recursion elimination. Do so
1780 : : by triggering the SSA renamer. */
1781 : 3496880 : if (phis_constructed)
1782 : 1200 : mark_virtual_operands_for_renaming (cfun);
1783 : :
1784 : 3496880 : if (tailr_arg_needs_copy)
1785 : 1202 : BITMAP_FREE (tailr_arg_needs_copy);
1786 : :
1787 : 3496880 : if (diag_musttail)
1788 : 708717 : cfun->has_musttail = false;
1789 : :
1790 : 3496880 : if (changed)
1791 : 1206 : return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
1792 : : return 0;
1793 : : }
1794 : :
1795 : : static bool
1796 : 4484482 : gate_tail_calls (void)
1797 : : {
1798 : 4484482 : return flag_optimize_sibling_calls != 0 && dbg_cnt (tail_call);
1799 : : }
1800 : :
1801 : : static unsigned int
1802 : 708515 : execute_tail_calls (void)
1803 : : {
1804 : 0 : return tree_optimize_tail_calls_1 (true, false, true);
1805 : : }
1806 : :
1807 : : namespace {
1808 : :
1809 : : const pass_data pass_data_tail_recursion =
1810 : : {
1811 : : GIMPLE_PASS, /* type */
1812 : : "tailr", /* name */
1813 : : OPTGROUP_NONE, /* optinfo_flags */
1814 : : TV_NONE, /* tv_id */
1815 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
1816 : : 0, /* properties_provided */
1817 : : 0, /* properties_destroyed */
1818 : : 0, /* todo_flags_start */
1819 : : 0, /* todo_flags_finish */
1820 : : };
1821 : :
1822 : : class pass_tail_recursion : public gimple_opt_pass
1823 : : {
1824 : : public:
1825 : 570162 : pass_tail_recursion (gcc::context *ctxt)
1826 : 1140324 : : gimple_opt_pass (pass_data_tail_recursion, ctxt)
1827 : : {}
1828 : :
1829 : : /* opt_pass methods: */
1830 : 285081 : opt_pass * clone () final override
1831 : : {
1832 : 285081 : return new pass_tail_recursion (m_ctxt);
1833 : : }
1834 : 3462900 : bool gate (function *) final override { return gate_tail_calls (); }
1835 : 2788163 : unsigned int execute (function *) final override
1836 : : {
1837 : 2788163 : return tree_optimize_tail_calls_1 (false, false, false);
1838 : : }
1839 : :
1840 : : }; // class pass_tail_recursion
1841 : :
1842 : : } // anon namespace
1843 : :
1844 : : gimple_opt_pass *
1845 : 285081 : make_pass_tail_recursion (gcc::context *ctxt)
1846 : : {
1847 : 285081 : return new pass_tail_recursion (ctxt);
1848 : : }
1849 : :
1850 : : namespace {
1851 : :
1852 : : const pass_data pass_data_tail_calls =
1853 : : {
1854 : : GIMPLE_PASS, /* type */
1855 : : "tailc", /* name */
1856 : : OPTGROUP_NONE, /* optinfo_flags */
1857 : : TV_NONE, /* tv_id */
1858 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
1859 : : 0, /* properties_provided */
1860 : : 0, /* properties_destroyed */
1861 : : 0, /* todo_flags_start */
1862 : : 0, /* todo_flags_finish */
1863 : : };
1864 : :
1865 : : class pass_tail_calls : public gimple_opt_pass
1866 : : {
1867 : : public:
1868 : 285081 : pass_tail_calls (gcc::context *ctxt)
1869 : 570162 : : gimple_opt_pass (pass_data_tail_calls, ctxt)
1870 : : {}
1871 : :
1872 : : /* opt_pass methods: */
1873 : 1021582 : bool gate (function *) final override { return gate_tail_calls (); }
1874 : 708515 : unsigned int execute (function *) final override
1875 : : {
1876 : 708515 : return execute_tail_calls ();
1877 : : }
1878 : :
1879 : : }; // class pass_tail_calls
1880 : :
1881 : : } // anon namespace
1882 : :
1883 : : gimple_opt_pass *
1884 : 285081 : make_pass_tail_calls (gcc::context *ctxt)
1885 : : {
1886 : 285081 : return new pass_tail_calls (ctxt);
1887 : : }
1888 : :
1889 : : namespace {
1890 : :
1891 : : const pass_data pass_data_musttail =
1892 : : {
1893 : : GIMPLE_PASS, /* type */
1894 : : "musttail", /* name */
1895 : : OPTGROUP_NONE, /* optinfo_flags */
1896 : : TV_NONE, /* tv_id */
1897 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
1898 : : 0, /* properties_provided */
1899 : : 0, /* properties_destroyed */
1900 : : 0, /* todo_flags_start */
1901 : : 0, /* todo_flags_finish */
1902 : : };
1903 : :
1904 : : class pass_musttail : public gimple_opt_pass
1905 : : {
1906 : : public:
1907 : 285081 : pass_musttail (gcc::context *ctxt)
1908 : 570162 : : gimple_opt_pass (pass_data_musttail, ctxt)
1909 : : {}
1910 : :
1911 : : /* opt_pass methods: */
1912 : : /* This pass is only used when the other tail call pass
1913 : : doesn't run to make [[musttail]] still work. But only
1914 : : run it when there is actually a musttail in the function. */
1915 : 1450636 : bool gate (function *f) final override
1916 : : {
1917 : 1450636 : return f->has_musttail;
1918 : : }
1919 : 202 : unsigned int execute (function *) final override
1920 : : {
1921 : 202 : return tree_optimize_tail_calls_1 (true, true, true);
1922 : : }
1923 : :
1924 : : }; // class pass_musttail
1925 : :
1926 : : } // anon namespace
1927 : :
1928 : : gimple_opt_pass *
1929 : 285081 : make_pass_musttail (gcc::context *ctxt)
1930 : : {
1931 : 285081 : return new pass_musttail (ctxt);
1932 : : }
|