Line data Source code
1 : /* Tail call optimization on trees.
2 : Copyright (C) 2003-2026 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 : #include "target.h"
57 :
58 : /* The file implements the tail recursion elimination. It is also used to
59 : analyze the tail calls in general, passing the results to the rtl level
60 : where they are used for sibcall optimization.
61 :
62 : In addition to the standard tail recursion elimination, we handle the most
63 : trivial cases of making the call tail recursive by creating accumulators.
64 : For example the following function
65 :
66 : int sum (int n)
67 : {
68 : if (n > 0)
69 : return n + sum (n - 1);
70 : else
71 : return 0;
72 : }
73 :
74 : is transformed into
75 :
76 : int sum (int n)
77 : {
78 : int acc = 0;
79 :
80 : while (n > 0)
81 : acc += n--;
82 :
83 : return acc;
84 : }
85 :
86 : To do this, we maintain two accumulators (a_acc and m_acc) that indicate
87 : when we reach the return x statement, we should return a_acc + x * m_acc
88 : instead. They are initially initialized to 0 and 1, respectively,
89 : so the semantics of the function is obviously preserved. If we are
90 : guaranteed that the value of the accumulator never change, we
91 : omit the accumulator.
92 :
93 : There are three cases how the function may exit. The first one is
94 : handled in adjust_return_value, the other two in adjust_accumulator_values
95 : (the second case is actually a special case of the third one and we
96 : present it separately just for clarity):
97 :
98 : 1) Just return x, where x is not in any of the remaining special shapes.
99 : We rewrite this to a gimple equivalent of return m_acc * x + a_acc.
100 :
101 : 2) return f (...), where f is the current function, is rewritten in a
102 : classical tail-recursion elimination way, into assignment of arguments
103 : and jump to the start of the function. Values of the accumulators
104 : are unchanged.
105 :
106 : 3) return a + m * f(...), where a and m do not depend on call to f.
107 : To preserve the semantics described before we want this to be rewritten
108 : in such a way that we finally return
109 :
110 : a_acc + (a + m * f(...)) * m_acc = (a_acc + a * m_acc) + (m * m_acc) * f(...).
111 :
112 : I.e. we increase a_acc by a * m_acc, multiply m_acc by m and
113 : eliminate the tail call to f. Special cases when the value is just
114 : added or just multiplied are obtained by setting a = 0 or m = 1.
115 :
116 : TODO -- it is possible to do similar tricks for other operations. */
117 :
118 : /* A structure that describes the tailcall. */
119 :
120 : struct tailcall
121 : {
122 : /* The iterator pointing to the call statement. */
123 : gimple_stmt_iterator call_gsi;
124 :
125 : /* True if it is a call to the current function. */
126 : bool tail_recursion;
127 :
128 : /* True if there is __tsan_func_exit call after the call. */
129 : bool has_tsan_func_exit;
130 :
131 : /* The return value of the caller is mult * f + add, where f is the return
132 : value of the call. */
133 : tree mult, add;
134 :
135 : /* Next tailcall in the chain. */
136 : struct tailcall *next;
137 : };
138 :
139 : /* The variables holding the value of multiplicative and additive
140 : accumulator. */
141 : static tree m_acc, a_acc;
142 :
143 : /* Bitmap with a bit for each function parameter which is set to true if we
144 : have to copy the parameter for conversion of tail-recursive calls. */
145 :
146 : static bitmap tailr_arg_needs_copy;
147 :
148 : static void maybe_error_musttail (gcall *call, const char *err, bool);
149 :
150 : /* Returns false when the function is not suitable for tail call optimization
151 : from some reason (e.g. if it takes variable number of arguments). CALL
152 : is call to report for. */
153 :
154 : static bool
155 1639036 : suitable_for_tail_opt_p (gcall *call, bool diag_musttail)
156 : {
157 1639036 : if (cfun->stdarg)
158 : {
159 18552 : maybe_error_musttail (call, _("caller uses stdargs"), diag_musttail);
160 18552 : return false;
161 : }
162 :
163 : return true;
164 : }
165 :
166 : /* Returns false when the function is not suitable for tail call optimization
167 : for some reason (e.g. if it takes variable number of arguments).
168 : This test must pass in addition to suitable_for_tail_opt_p in order to make
169 : tail call discovery happen. CALL is call to report error for. */
170 :
171 : static bool
172 1620484 : suitable_for_tail_call_opt_p (gcall *call, bool diag_musttail)
173 : {
174 : /* alloca (until we have stack slot life analysis) inhibits
175 : sibling call optimizations, but not tail recursion. */
176 1620484 : if (cfun->calls_alloca)
177 : {
178 16049 : maybe_error_musttail (call, _("caller uses alloca"), diag_musttail);
179 16049 : return false;
180 : }
181 :
182 : /* If we are using sjlj exceptions, we may need to add a call to
183 : _Unwind_SjLj_Unregister at exit of the function. Which means
184 : that we cannot do any sibcall transformations. */
185 1604435 : if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ
186 1604435 : && current_function_has_exception_handlers ())
187 : {
188 0 : maybe_error_musttail (call, _("caller uses sjlj exceptions"),
189 : diag_musttail);
190 0 : return false;
191 : }
192 :
193 : /* Any function that calls setjmp might have longjmp called from
194 : any called function. ??? We really should represent this
195 : properly in the CFG so that this needn't be special cased. */
196 1604435 : if (cfun->calls_setjmp)
197 : {
198 157 : maybe_error_musttail (call, _("caller uses setjmp"), diag_musttail);
199 157 : return false;
200 : }
201 :
202 : /* Various targets don't handle tail calls correctly in functions
203 : that call __builtin_eh_return. */
204 1604278 : if (cfun->calls_eh_return)
205 : {
206 12 : maybe_error_musttail (call, _("caller uses __builtin_eh_return"),
207 : diag_musttail);
208 12 : return false;
209 : }
210 :
211 1604266 : if (diag_musttail
212 376599 : && gimple_call_must_tail_p (call)
213 1605018 : && warn_musttail_local_addr)
214 1737 : for (unsigned int i = 0; i < gimple_call_num_args (call); i++)
215 : {
216 985 : tree arg = gimple_call_arg (call, i);
217 985 : if (!POINTER_TYPE_P (TREE_TYPE (arg)))
218 512 : continue;
219 473 : if (TREE_CODE (arg) == ADDR_EXPR)
220 : {
221 179 : arg = get_base_address (TREE_OPERAND (arg, 0));
222 179 : if (auto_var_in_fn_p (arg, current_function_decl))
223 : {
224 179 : if (TREE_CODE (arg) == LABEL_DECL)
225 16 : warning_at (gimple_location (call), OPT_Wmusttail_local_addr,
226 : "address of label passed to %<musttail%> "
227 : "call argument");
228 163 : else if (TREE_CODE (arg) == PARM_DECL)
229 25 : warning_at (gimple_location (call), OPT_Wmusttail_local_addr,
230 : "address of parameter %qD passed to "
231 : "%<musttail%> call argument", arg);
232 138 : else if (!DECL_ARTIFICIAL (arg) && DECL_NAME (arg))
233 129 : warning_at (gimple_location (call), OPT_Wmusttail_local_addr,
234 : "address of automatic variable %qD passed to "
235 : "%<musttail%> call argument", arg);
236 : else
237 9 : warning_at (gimple_location (call), OPT_Wmusttail_local_addr,
238 : "address of local variable passed to "
239 : "%<musttail%> call argument");
240 179 : suppress_warning (call, OPT_Wmaybe_musttail_local_addr);
241 : }
242 : }
243 : }
244 :
245 : return true;
246 : }
247 :
248 : /* Return single successor edge ignoring EDGE_EH edges. */
249 :
250 : static edge
251 523900 : single_non_eh_succ_edge (basic_block bb)
252 : {
253 523900 : edge e, ret = NULL;
254 523900 : edge_iterator ei;
255 1047967 : FOR_EACH_EDGE (e, ei, bb->succs)
256 524067 : if ((e->flags & EDGE_EH) == 0)
257 : {
258 523900 : gcc_assert (ret == NULL);
259 : ret = e;
260 : }
261 523900 : gcc_assert (ret);
262 523900 : return ret;
263 : }
264 :
265 : /* Checks whether the expression EXPR in stmt AT is independent of the
266 : statement pointed to by GSI (in a sense that we already know EXPR's value
267 : at GSI). We use the fact that we are only called from the chain of
268 : basic blocks that have only single successor. Returns the expression
269 : containing the value of EXPR at GSI. */
270 :
271 : static tree
272 25196 : independent_of_stmt_p (tree expr, gimple *at, gimple_stmt_iterator gsi,
273 : bitmap to_move)
274 : {
275 25196 : basic_block bb, call_bb, at_bb;
276 25196 : edge e;
277 25196 : edge_iterator ei;
278 :
279 25196 : if (is_gimple_min_invariant (expr))
280 : return expr;
281 :
282 6499 : if (TREE_CODE (expr) != SSA_NAME)
283 : return NULL_TREE;
284 :
285 6499 : if (bitmap_bit_p (to_move, SSA_NAME_VERSION (expr)))
286 : return expr;
287 :
288 : /* Mark the blocks in the chain leading to the end. */
289 6478 : at_bb = gimple_bb (at);
290 6478 : call_bb = gimple_bb (gsi_stmt (gsi));
291 7666 : for (bb = call_bb; bb != at_bb; bb = single_non_eh_succ_edge (bb)->dest)
292 1188 : bb->aux = &bb->aux;
293 6478 : bb->aux = &bb->aux;
294 :
295 6518 : while (1)
296 : {
297 6518 : at = SSA_NAME_DEF_STMT (expr);
298 6518 : bb = gimple_bb (at);
299 :
300 : /* The default definition or defined before the chain. */
301 6518 : if (!bb || !bb->aux)
302 : break;
303 :
304 3223 : if (bb == call_bb)
305 : {
306 19541 : for (; !gsi_end_p (gsi); gsi_next (&gsi))
307 16546 : if (gsi_stmt (gsi) == at)
308 : break;
309 :
310 3161 : if (!gsi_end_p (gsi))
311 166 : expr = NULL_TREE;
312 : break;
313 : }
314 :
315 62 : if (gimple_code (at) != GIMPLE_PHI)
316 : {
317 : expr = NULL_TREE;
318 : break;
319 : }
320 :
321 99 : FOR_EACH_EDGE (e, ei, bb->preds)
322 99 : if (e->src->aux)
323 : break;
324 62 : gcc_assert (e);
325 :
326 62 : expr = PHI_ARG_DEF_FROM_EDGE (at, e);
327 62 : if (TREE_CODE (expr) != SSA_NAME)
328 : {
329 : /* The value is a constant. */
330 : break;
331 : }
332 : }
333 :
334 : /* Unmark the blocks. */
335 7666 : for (bb = call_bb; bb != at_bb; bb = single_non_eh_succ_edge (bb)->dest)
336 1188 : bb->aux = NULL;
337 6478 : bb->aux = NULL;
338 :
339 6478 : return expr;
340 : }
341 :
342 : enum par { FAIL, OK, TRY_MOVE };
343 :
344 : /* Simulates the effect of an assignment STMT on the return value of the tail
345 : recursive CALL passed in ASS_VAR. M and A are the multiplicative and the
346 : additive factor for the real return value. */
347 :
348 : static par
349 118827 : process_assignment (gassign *stmt,
350 : gimple_stmt_iterator call, tree *m,
351 : tree *a, tree *ass_var, bitmap to_move)
352 : {
353 118827 : tree op0, op1 = NULL_TREE, non_ass_var = NULL_TREE;
354 118827 : tree dest = gimple_assign_lhs (stmt);
355 118827 : enum tree_code code = gimple_assign_rhs_code (stmt);
356 118827 : enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
357 118827 : tree src_var = gimple_assign_rhs1 (stmt);
358 :
359 : /* See if this is a simple copy operation of an SSA name to the function
360 : result. In that case we may have a simple tail call. Ignore type
361 : conversions that can never produce extra code between the function
362 : call and the function return. */
363 58064 : if ((rhs_class == GIMPLE_SINGLE_RHS || gimple_assign_cast_p (stmt))
364 140454 : && src_var == *ass_var)
365 : {
366 : /* Reject a tailcall if the type conversion might need
367 : additional code. */
368 21669 : if (gimple_assign_cast_p (stmt))
369 : {
370 19416 : if (TYPE_MODE (TREE_TYPE (dest)) != TYPE_MODE (TREE_TYPE (src_var)))
371 : return FAIL;
372 :
373 : /* While say unsigned short vs. unsigned _BitInt(16) can be
374 : returned the same on some targets, we don't have a guarantee
375 : that is the case on all targets, the ABI can differ, so just
376 : punt. */
377 17406 : if (BITINT_TYPE_P (TREE_TYPE (dest))
378 8703 : != BITINT_TYPE_P (TREE_TYPE (src_var)))
379 : return FAIL;
380 :
381 8652 : if (BITINT_TYPE_P (TREE_TYPE (dest)))
382 : {
383 : /* For _BitInt <-> _BitInt we have more target information.
384 : For BLKmode we need to punt if it has different size. */
385 60 : if (wi::to_widest (TYPE_SIZE (TREE_TYPE (dest)))
386 120 : != wi::to_widest (TYPE_SIZE (TREE_TYPE (src_var))))
387 24 : return FAIL;
388 60 : struct bitint_info info;
389 60 : unsigned dest_prec = TYPE_PRECISION (TREE_TYPE (dest));
390 60 : bool ok = targetm.c.bitint_type_info (dest_prec, &info);
391 60 : gcc_assert (ok);
392 60 : if (TYPE_PRECISION (TREE_TYPE (dest))
393 60 : <= TYPE_PRECISION (TREE_TYPE (src_var)))
394 : {
395 : /* Narrowing conversion or possibly same precision
396 : different sign. This is fine on bitint_ext_undef
397 : targets only. */
398 36 : if (info.extended != bitint_ext_undef)
399 : return FAIL;
400 : }
401 : /* Widening conversion. This is fine on bitint_ext_full
402 : target if signedness is the same. */
403 24 : else if (info.extended != bitint_ext_full
404 24 : || (TYPE_UNSIGNED (TREE_TYPE (dest))
405 0 : != TYPE_UNSIGNED (TREE_TYPE (src_var))))
406 : return FAIL;
407 : }
408 : /* Even if the type modes are the same, if the precision of the
409 : type is smaller than mode's precision,
410 : reduce_to_bit_field_precision would generate additional code. */
411 17182 : else if (INTEGRAL_TYPE_P (TREE_TYPE (dest))
412 16671 : && !type_has_mode_precision_p (TREE_TYPE (dest)))
413 : return FAIL;
414 : }
415 :
416 10830 : *ass_var = dest;
417 10830 : return OK;
418 : }
419 :
420 97158 : switch (rhs_class)
421 : {
422 34362 : case GIMPLE_BINARY_RHS:
423 34362 : op1 = gimple_assign_rhs2 (stmt);
424 :
425 : /* Fall through. */
426 :
427 38276 : case GIMPLE_UNARY_RHS:
428 38276 : op0 = gimple_assign_rhs1 (stmt);
429 38276 : break;
430 :
431 : default:
432 : return FAIL;
433 : }
434 :
435 : /* Accumulator optimizations will reverse the order of operations.
436 : We can only do that for floating-point types if we're assuming
437 : that addition and multiplication are associative. */
438 38276 : if (!flag_associative_math)
439 37849 : if (FLOAT_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
440 : return FAIL;
441 :
442 : /* We at least cannot build -1 for all fixed point types. */
443 33888 : if (FIXED_POINT_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
444 : return FAIL;
445 :
446 33888 : if (rhs_class == GIMPLE_UNARY_RHS
447 3450 : && op0 == *ass_var)
448 : ;
449 32641 : else if (op0 == *ass_var
450 32641 : && (non_ass_var = independent_of_stmt_p (op1, stmt, call,
451 : to_move)))
452 : ;
453 14054 : else if (*ass_var
454 6880 : && op1 == *ass_var
455 20525 : && (non_ass_var = independent_of_stmt_p (op0, stmt, call,
456 : to_move)))
457 : ;
458 : else
459 : return TRY_MOVE;
460 :
461 26223 : switch (code)
462 : {
463 7074 : case PLUS_EXPR:
464 7074 : *a = non_ass_var;
465 7074 : *ass_var = dest;
466 7074 : return OK;
467 :
468 344 : case POINTER_PLUS_EXPR:
469 344 : if (op0 != *ass_var)
470 : return FAIL;
471 78 : *a = non_ass_var;
472 78 : *ass_var = dest;
473 78 : return OK;
474 :
475 536 : case MULT_EXPR:
476 536 : *m = non_ass_var;
477 536 : *ass_var = dest;
478 536 : return OK;
479 :
480 139 : case NEGATE_EXPR:
481 139 : *m = build_minus_one_cst (TREE_TYPE (op0));
482 139 : *ass_var = dest;
483 139 : return OK;
484 :
485 1440 : case MINUS_EXPR:
486 1440 : if (*ass_var == op0)
487 91 : *a = fold_build1 (NEGATE_EXPR, TREE_TYPE (non_ass_var), non_ass_var);
488 : else
489 : {
490 1349 : *m = build_minus_one_cst (TREE_TYPE (non_ass_var));
491 1349 : *a = fold_build1 (NEGATE_EXPR, TREE_TYPE (non_ass_var), non_ass_var);
492 : }
493 :
494 1440 : *ass_var = dest;
495 1440 : return OK;
496 :
497 : default:
498 : return FAIL;
499 : }
500 : }
501 :
502 : /* Propagate VAR through phis on edge E. */
503 :
504 : static tree
505 519913 : propagate_through_phis (tree var, edge e)
506 : {
507 519913 : basic_block dest = e->dest;
508 519913 : gphi_iterator gsi;
509 :
510 984967 : for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
511 : {
512 535568 : gphi *phi = gsi.phi ();
513 535568 : if (PHI_ARG_DEF_FROM_EDGE (phi, e) == var)
514 70514 : return PHI_RESULT (phi);
515 : }
516 : return var;
517 : }
518 :
519 : /* Report an error for failing to tail convert must call CALL
520 : with error message ERR. Also clear the flag to prevent further
521 : errors. */
522 :
523 : static void
524 803996 : maybe_error_musttail (gcall *call, const char *err, bool diag_musttail)
525 : {
526 803996 : if (gimple_call_must_tail_p (call) && diag_musttail)
527 : {
528 44 : error_at (gimple_location (call), "cannot tail-call: %s", err);
529 : /* Avoid another error. ??? If there are multiple reasons why tail
530 : calls fail it might be useful to report them all to avoid
531 : whack-a-mole for the user. But currently there is too much
532 : redundancy in the reporting, so keep it simple. */
533 44 : gimple_call_set_must_tail (call, false); /* Avoid another error. */
534 44 : gimple_call_set_tail (call, false);
535 : }
536 803996 : if (dump_file && (dump_flags & TDF_DETAILS))
537 : {
538 13 : fprintf (dump_file, "Cannot tail-call: %s: ", err);
539 13 : print_gimple_stmt (dump_file, call, 0, TDF_SLIM);
540 : }
541 803996 : }
542 :
543 : /* Return true if there is no real work performed in the exception
544 : path starting at BB and it will in the end result in external exception.
545 : Search at most CNT basic blocks (so that we don't need to do trivial
546 : loop discovery). */
547 : static bool
548 165 : empty_eh_cleanup (basic_block bb, int *eh_has_tsan_func_exit, int cnt)
549 : {
550 270 : if (EDGE_COUNT (bb->succs) > 1)
551 : return false;
552 :
553 410 : for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);
554 140 : gsi_next (&gsi))
555 : {
556 332 : gimple *g = gsi_stmt (gsi);
557 332 : if (is_gimple_debug (g) || gimple_clobber_p (g))
558 6 : continue;
559 330 : if (eh_has_tsan_func_exit
560 320 : && !*eh_has_tsan_func_exit
561 316 : && sanitize_flags_p (SANITIZE_THREAD)
562 330 : && gimple_call_builtin_p (g, BUILT_IN_TSAN_FUNC_EXIT))
563 : {
564 4 : *eh_has_tsan_func_exit = 1;
565 4 : continue;
566 : }
567 452 : if (eh_has_tsan_func_exit
568 316 : && sanitize_flags_p (SANITIZE_ADDRESS)
569 534 : && asan_mark_p (g, ASAN_MARK_POISON))
570 130 : continue;
571 192 : if (is_gimple_resx (g))
572 : {
573 192 : if (stmt_can_throw_external (cfun, g))
574 165 : return true;
575 192 : if (single_succ_p (bb)
576 27 : && (single_succ_edge (bb)->flags & EDGE_EH)
577 54 : && cnt > 1)
578 27 : return empty_eh_cleanup (single_succ (bb), eh_has_tsan_func_exit,
579 27 : cnt - 1);
580 : }
581 : return false;
582 : }
583 78 : if (!single_succ_p (bb))
584 : return false;
585 78 : if (cnt == 1)
586 : return false;
587 78 : return empty_eh_cleanup (single_succ (bb), eh_has_tsan_func_exit, cnt - 1);
588 : }
589 :
590 : /* Argument for compute_live_vars/live_vars_at_stmt and what compute_live_vars
591 : returns. Computed lazily, but just once for the function. */
592 : static live_vars_map *live_vars;
593 : static vec<bitmap_head> live_vars_vec;
594 :
595 : /* Finds tailcalls falling into basic block BB when coming from edge ESUCC (or
596 : NULL). The list of found tailcalls is added to the start of RET.
597 : When ONLY_MUSTTAIL is set only handle musttail.
598 : Update OPT_TAILCALLS as output parameter. If DIAG_MUSTTAIL, diagnose
599 : failures for musttail calls. RETRY_TSAN_FUNC_EXIT is initially 0 and
600 : in that case the last call is attempted to be tail called, including
601 : __tsan_func_exit with -fsanitize=thread. It is set to -1 if we
602 : detect __tsan_func_exit call and in that case tree_optimize_tail_calls_1
603 : will retry with it set to 1 (regardless of whether turning the
604 : __tsan_func_exit was successfully detected as tail call or not) and that
605 : will allow turning musttail calls before that call into tail calls as well
606 : by adding __tsan_func_exit call before the call.
607 : IGNORED_EDGES and MUST_SEE_BBS are used in recursive calls when handling
608 : GIMPLE_CONDs for cleanups. */
609 :
610 : static void
611 7331083 : find_tail_calls (basic_block bb, edge esucc, struct tailcall **ret,
612 : bool only_musttail, bool &opt_tailcalls, bool diag_musttail,
613 : int &retry_tsan_func_exit, hash_set<edge> *ignored_edges,
614 : hash_set<basic_block> *must_see_bbs)
615 : {
616 7331083 : tree ass_var = NULL_TREE, ret_var, func, param;
617 7331083 : gimple *stmt;
618 7331083 : gcall *call = NULL;
619 7331083 : gimple_stmt_iterator gsi, agsi;
620 7331083 : bool tail_recursion;
621 7331083 : struct tailcall *nw;
622 7331083 : edge e;
623 7331083 : tree m, a;
624 7331083 : basic_block abb;
625 7331083 : size_t idx;
626 7331083 : tree var;
627 7331083 : bool only_tailr = false;
628 7331083 : bool has_tsan_func_exit = false;
629 7331083 : int eh_has_tsan_func_exit = -1;
630 7331083 : bool delete_ignored_edges = false;
631 :
632 7331083 : if (!single_succ_p (bb)
633 7331083 : && (EDGE_COUNT (bb->succs) || !cfun->has_musttail || !diag_musttail))
634 : {
635 1814123 : if (EDGE_COUNT (bb->succs) == 2
636 1776112 : && esucc
637 1776112 : && cfun->has_musttail
638 3415 : && diag_musttail
639 3314 : && (EDGE_SUCC (bb, 0)->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
640 3091 : && (EDGE_SUCC (bb, 1)->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
641 3091 : && (stmt = last_nondebug_stmt (bb))
642 1817214 : && gimple_code (stmt) == GIMPLE_COND)
643 : ;
644 1811032 : else if (esucc
645 1811032 : && cfun->has_musttail
646 358 : && diag_musttail
647 249 : && (stmt = last_nondebug_stmt (bb))
648 1811281 : && gimple_code (stmt) == GIMPLE_SWITCH)
649 : ;
650 : /* If there is an abnormal edge assume it's the only extra one.
651 : Tolerate that case so that we can give better error messages
652 : for musttail later. */
653 1811006 : else if (!has_abnormal_or_eh_outgoing_edge_p (bb))
654 : {
655 1787690 : if (dump_file)
656 109 : fprintf (dump_file, "Basic block %d has extra exit edges\n",
657 : bb->index);
658 6446748 : return;
659 : }
660 26433 : if (!cfun->has_musttail)
661 : return;
662 : }
663 :
664 5520306 : bool bad_stmt = false;
665 5520306 : gimple *last_stmt = nullptr;
666 24378900 : for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
667 : {
668 16201152 : stmt = gsi_stmt (gsi);
669 :
670 : /* Ignore labels, returns, nops, clobbers and debug stmts. */
671 16201152 : if (gimple_code (stmt) == GIMPLE_LABEL
672 : || gimple_code (stmt) == GIMPLE_RETURN
673 : || gimple_code (stmt) == GIMPLE_NOP
674 : || gimple_code (stmt) == GIMPLE_PREDICT
675 12254862 : || gimple_clobber_p (stmt)
676 11218241 : || is_gimple_debug (stmt))
677 12289937 : continue;
678 :
679 3911215 : if (cfun->has_musttail
680 5995 : && sanitize_flags_p (SANITIZE_THREAD)
681 44 : && gimple_call_builtin_p (stmt, BUILT_IN_TSAN_FUNC_EXIT)
682 3911231 : && diag_musttail)
683 : {
684 16 : if (retry_tsan_func_exit == 0)
685 8 : retry_tsan_func_exit = -1;
686 8 : else if (retry_tsan_func_exit == 1)
687 8 : continue;
688 : }
689 :
690 3911651 : if (cfun->has_musttail
691 5987 : && sanitize_flags_p (SANITIZE_ADDRESS)
692 4062 : && asan_mark_p (stmt, ASAN_MARK_POISON)
693 3911927 : && diag_musttail)
694 444 : continue;
695 :
696 : /* Especially at -O0 with -fsanitize=address we can end up with
697 : code like
698 : _26 = foo (x_24(D)); [must tail call]
699 : finally_tmp.3_27 = 0;
700 : goto <bb 5>; [INV]
701 :
702 : ...
703 :
704 : <bb 5> :
705 : # _6 = PHI <_26(3), _23(D)(4)>
706 : # finally_tmp.3_8 = PHI <finally_tmp.3_27(3), finally_tmp.3_22(4)>
707 : .ASAN_MARK (POISON, &c, 4);
708 : if (finally_tmp.3_8 == 1)
709 : goto <bb 7>; [INV]
710 : else
711 : goto <bb 6>; [INV]
712 : When walking backwards, ESUCC is the edge we are coming from,
713 : depending on its EDGE_TRUE_FLAG, comparison code
714 : and value compared against try to find out through which edge
715 : we need to go and which edge should be ignored. The code handles
716 : both INTEGER_CST PHI arguments and SSA_NAMEs set to constants
717 : (for -O0 where those aren't propagated). */
718 3910763 : if (cfun->has_musttail
719 : && diag_musttail
720 5543 : && esucc
721 4499 : && gimple_code (stmt) == GIMPLE_COND
722 3091 : && TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
723 3091 : && TREE_CODE (gimple_cond_rhs (stmt)) == INTEGER_CST
724 3091 : && INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))
725 3913854 : && tree_int_cst_sgn (gimple_cond_rhs (stmt)) >= 0)
726 : {
727 2995 : tree lhs = gimple_cond_lhs (stmt);
728 2995 : tree_code ccode = gimple_cond_code (stmt);
729 2995 : tree rhsv = gimple_cond_rhs (stmt);
730 2995 : if ((esucc->flags & EDGE_FALSE_VALUE) != 0)
731 2867 : ccode = invert_tree_comparison (ccode, false);
732 2995 : if (!ignored_edges)
733 : {
734 161 : ignored_edges = new hash_set<edge>;
735 161 : must_see_bbs = new hash_set<basic_block>;
736 161 : delete_ignored_edges = true;
737 : }
738 2995 : auto assign = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (lhs));
739 118 : if (assign && (gimple_assign_rhs_code (assign) == INTEGER_CST))
740 : {
741 0 : tree lhsv = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (lhs));
742 :
743 0 : if (const_binop (ccode, boolean_type_node, lhsv, rhsv)
744 0 : == boolean_true_node)
745 0 : continue;
746 : }
747 2995 : else if (gimple_code (SSA_NAME_DEF_STMT (lhs)) == GIMPLE_PHI)
748 : {
749 2556 : gimple *phi = SSA_NAME_DEF_STMT (lhs);
750 2556 : basic_block pbb = gimple_bb (phi);
751 2556 : must_see_bbs->add (pbb);
752 2556 : edge_iterator ei;
753 93238 : FOR_EACH_EDGE (e, ei, pbb->preds)
754 : {
755 90682 : tree lhsv = gimple_phi_arg_def_from_edge (phi, e);
756 90682 : gassign *assign;
757 90682 : if (TREE_CODE (lhsv) == SSA_NAME
758 : && (assign = dyn_cast<gassign *>
759 90682 : (SSA_NAME_DEF_STMT (lhsv)))
760 181364 : && (gimple_assign_rhs_code (assign) == INTEGER_CST))
761 90680 : lhsv = gimple_assign_rhs1 (assign);
762 90682 : if (TREE_CODE (lhsv) != INTEGER_CST
763 90682 : || const_binop (ccode, boolean_type_node,
764 90680 : lhsv, rhsv) != boolean_true_node)
765 16790 : ignored_edges->add (e);
766 : }
767 2556 : continue;
768 2556 : }
769 : }
770 3908207 : if (cfun->has_musttail
771 : && diag_musttail
772 2987 : && esucc
773 1943 : && gimple_code (stmt) == GIMPLE_SWITCH
774 3908233 : && (TREE_CODE (gimple_switch_index (as_a <gswitch *> (stmt)))
775 : == SSA_NAME))
776 : {
777 26 : gswitch *swtch = as_a <gswitch *> (stmt);
778 26 : tree idx = gimple_switch_index (swtch);
779 26 : if (!ignored_edges)
780 : {
781 24 : ignored_edges = new hash_set<edge>;
782 24 : must_see_bbs = new hash_set<basic_block>;
783 24 : delete_ignored_edges = true;
784 : }
785 26 : auto assign = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (idx));
786 2 : if (assign && (gimple_assign_rhs_code (assign) == INTEGER_CST))
787 : {
788 0 : tree val = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (idx));
789 0 : if (find_taken_edge_switch_expr (swtch, val) == esucc)
790 0 : continue;
791 : }
792 26 : else if (gimple_code (SSA_NAME_DEF_STMT (idx)) == GIMPLE_PHI)
793 : {
794 4 : gimple *phi = SSA_NAME_DEF_STMT (idx);
795 4 : basic_block pbb = gimple_bb (phi);
796 4 : must_see_bbs->add (pbb);
797 4 : edge_iterator ei;
798 148 : FOR_EACH_EDGE (e, ei, pbb->preds)
799 : {
800 144 : tree val = gimple_phi_arg_def_from_edge (phi, e);
801 144 : gassign *assign;
802 144 : if (TREE_CODE (val) == SSA_NAME
803 : && (assign = dyn_cast<gassign *>
804 144 : (SSA_NAME_DEF_STMT (val)))
805 288 : && (gimple_assign_rhs_code (assign) == INTEGER_CST))
806 144 : val = gimple_assign_rhs1 (assign);
807 144 : if (TREE_CODE (val) != INTEGER_CST
808 144 : || find_taken_edge_switch_expr (swtch, val) != esucc)
809 140 : ignored_edges->add (e);
810 : }
811 4 : continue;
812 4 : }
813 : }
814 :
815 3908203 : if (!last_stmt)
816 3224612 : last_stmt = stmt;
817 : /* Check for a call. */
818 3908203 : if (is_gimple_call (stmt))
819 : {
820 1639110 : call = as_a <gcall *> (stmt);
821 : /* Handle only musttail calls when not optimizing. */
822 1639110 : if (only_musttail && !gimple_call_must_tail_p (call))
823 : {
824 3881270 : maybe_delete_ignored_edges:
825 3881270 : if (delete_ignored_edges)
826 : {
827 356 : delete ignored_edges;
828 356 : delete must_see_bbs;
829 : }
830 : return;
831 : }
832 1639060 : if (bad_stmt)
833 : {
834 24 : maybe_error_musttail (call, _("memory reference or volatile "
835 : "after call"), diag_musttail);
836 24 : goto maybe_delete_ignored_edges;
837 : }
838 1639036 : ass_var = gimple_call_lhs (call);
839 1639036 : break;
840 : }
841 :
842 : /* Allow simple copies between local variables, even if they're
843 : aggregates. */
844 2269093 : if (is_gimple_assign (stmt)
845 2246026 : && auto_var_in_fn_p (gimple_assign_lhs (stmt), cfun->decl)
846 2325852 : && auto_var_in_fn_p (gimple_assign_rhs1 (stmt), cfun->decl))
847 23410 : continue;
848 :
849 : /* If the statement references memory or volatile operands, fail. */
850 2245683 : if (gimple_references_memory_p (stmt)
851 14359513 : || gimple_has_volatile_ops (stmt))
852 : {
853 1223884 : if (dump_file)
854 : {
855 26 : fprintf (dump_file, "Cannot handle ");
856 26 : print_gimple_stmt (dump_file, stmt, 0);
857 : }
858 1223884 : bad_stmt = true;
859 1223884 : if (!cfun->has_musttail)
860 : break;
861 : }
862 : }
863 :
864 5520232 : if (bad_stmt)
865 1223820 : goto maybe_delete_ignored_edges;
866 :
867 4296412 : if (gsi_end_p (gsi))
868 : {
869 2657376 : if (must_see_bbs)
870 3078 : must_see_bbs->remove (bb);
871 :
872 2657376 : edge_iterator ei;
873 : /* Recurse to the predecessors. */
874 6408733 : FOR_EACH_EDGE (e, ei, bb->preds)
875 : {
876 3751357 : if (ignored_edges && ignored_edges->contains (e))
877 2700 : continue;
878 3748657 : find_tail_calls (e->src, e, ret, only_musttail, opt_tailcalls,
879 : diag_musttail, retry_tsan_func_exit, ignored_edges,
880 : must_see_bbs);
881 : }
882 :
883 2657376 : goto maybe_delete_ignored_edges;
884 : }
885 :
886 1639036 : if (must_see_bbs && !must_see_bbs->is_empty ())
887 0 : goto maybe_delete_ignored_edges;
888 :
889 1639036 : if (delete_ignored_edges)
890 : {
891 14 : delete ignored_edges;
892 14 : delete must_see_bbs;
893 : }
894 :
895 1639036 : if (!suitable_for_tail_opt_p (call, diag_musttail))
896 : return;
897 :
898 1620484 : if (!suitable_for_tail_call_opt_p (call, diag_musttail))
899 16218 : opt_tailcalls = false;
900 :
901 : /* ??? It is OK if the argument of a function is taken in some cases,
902 : but not in all cases. See PR15387 and PR19616. Revisit for 4.1. */
903 1620484 : if (!diag_musttail || !gimple_call_must_tail_p (call))
904 1619732 : for (param = DECL_ARGUMENTS (current_function_decl);
905 4772761 : param; param = DECL_CHAIN (param))
906 3153029 : if (TREE_ADDRESSABLE (param))
907 : {
908 33132 : maybe_error_musttail (call, _("address of caller arguments taken"),
909 : diag_musttail);
910 : /* If current function has musttail calls, we can't disable tail
911 : calls altogether for the whole caller, because those might be
912 : actually fine. So just punt if this exact call is not
913 : a tail recursion. */
914 33132 : if (cfun->has_musttail)
915 : only_tailr = true;
916 : else
917 33060 : opt_tailcalls = false;
918 : }
919 :
920 : /* If the LHS of our call is not just a simple register or local
921 : variable, we can't transform this into a tail or sibling call.
922 : This situation happens, in (e.g.) "*p = foo()" where foo returns a
923 : struct. In this case we won't have a temporary here, but we need
924 : to carry out the side effect anyway, so tailcall is impossible.
925 :
926 : ??? In some situations (when the struct is returned in memory via
927 : invisible argument) we could deal with this, e.g. by passing 'p'
928 : itself as that argument to foo, but it's too early to do this here,
929 : and expand_call() will not handle it anyway. If it ever can, then
930 : we need to revisit this here, to allow that situation. */
931 1620484 : if (ass_var
932 455353 : && !is_gimple_reg (ass_var)
933 1671535 : && !auto_var_in_fn_p (ass_var, cfun->decl))
934 : {
935 9427 : maybe_error_musttail (call, _("return value in memory"), diag_musttail);
936 9427 : return;
937 : }
938 :
939 1611057 : if (cfun->calls_setjmp)
940 : {
941 177 : maybe_error_musttail (call, _("caller uses setjmp"), diag_musttail);
942 177 : return;
943 : }
944 :
945 : /* If the call might throw an exception that wouldn't propagate out of
946 : cfun, we can't transform to a tail or sibling call (82081). */
947 1610880 : if ((stmt_could_throw_p (cfun, stmt)
948 1610880 : && !stmt_can_throw_external (cfun, stmt)) || EDGE_COUNT (bb->succs) > 1)
949 : {
950 19506 : if (stmt != last_stmt)
951 : {
952 165 : maybe_error_musttail (call, _("code between call and return"),
953 : diag_musttail);
954 19506 : return;
955 : }
956 :
957 19341 : edge e;
958 19341 : edge_iterator ei;
959 38492 : FOR_EACH_EDGE (e, ei, bb->succs)
960 19359 : if (e->flags & EDGE_EH)
961 : break;
962 :
963 19341 : if (!e)
964 : {
965 19133 : maybe_error_musttail (call, _("call may throw exception that does not "
966 : "propagate"), diag_musttail);
967 19133 : return;
968 : }
969 :
970 208 : if (diag_musttail && gimple_call_must_tail_p (call))
971 159 : eh_has_tsan_func_exit = 0;
972 208 : if (!gimple_call_must_tail_p (call)
973 165 : || !empty_eh_cleanup (e->dest,
974 165 : eh_has_tsan_func_exit
975 : ? NULL : &eh_has_tsan_func_exit, 20)
976 373 : || EDGE_COUNT (bb->succs) > 2)
977 : {
978 43 : maybe_error_musttail (call, _("call may throw exception caught "
979 : "locally or perform cleanups"),
980 : diag_musttail);
981 43 : return;
982 : }
983 : }
984 :
985 : /* If the function returns a value, then at present, the tail call
986 : must return the same type of value. There is conceptually a copy
987 : between the object returned by the tail call candidate and the
988 : object returned by CFUN itself.
989 :
990 : This means that if we have:
991 :
992 : lhs = f (&<retval>); // f reads from <retval>
993 : // (lhs is usually also <retval>)
994 :
995 : there is a copy between the temporary object returned by f and lhs,
996 : meaning that any use of <retval> in f occurs before the assignment
997 : to lhs begins. Thus the <retval> that is live on entry to the call
998 : to f is really an independent local variable V that happens to be
999 : stored in the RESULT_DECL rather than a local VAR_DECL.
1000 :
1001 : Turning this into a tail call would remove the copy and make the
1002 : lifetimes of the return value and V overlap. The same applies to
1003 : tail recursion, since if f can read from <retval>, we have to assume
1004 : that CFUN might already have written to <retval> before the call.
1005 :
1006 : The problem doesn't apply when <retval> is passed by value, but that
1007 : isn't a case we handle anyway. */
1008 1591539 : tree result_decl = DECL_RESULT (cfun->decl);
1009 1591539 : if (result_decl
1010 1591539 : && may_be_aliased (result_decl)
1011 1597911 : && ref_maybe_used_by_stmt_p (call, result_decl, false))
1012 : {
1013 1510 : maybe_error_musttail (call, _("return value used after call"),
1014 : diag_musttail);
1015 1510 : return;
1016 : }
1017 :
1018 : /* We found the call, check whether it is suitable. */
1019 1590029 : tail_recursion = false;
1020 1590029 : func = gimple_call_fndecl (call);
1021 1590029 : if (func
1022 1510007 : && !fndecl_built_in_p (func)
1023 1157138 : && recursive_call_p (current_function_decl, func)
1024 1593100 : && !only_musttail)
1025 : {
1026 3048 : tree arg;
1027 :
1028 3048 : for (param = DECL_ARGUMENTS (current_function_decl), idx = 0;
1029 11470 : param && idx < gimple_call_num_args (call);
1030 8422 : param = DECL_CHAIN (param), idx ++)
1031 : {
1032 8579 : arg = gimple_call_arg (call, idx);
1033 8579 : if (param != arg)
1034 : {
1035 : /* Make sure there are no problems with copying. The parameter
1036 : have a copyable type and the two arguments must have reasonably
1037 : equivalent types. The latter requirement could be relaxed if
1038 : we emitted a suitable type conversion statement. */
1039 8356 : if (TREE_ADDRESSABLE (TREE_TYPE (param))
1040 16712 : || !useless_type_conversion_p (TREE_TYPE (param),
1041 8356 : TREE_TYPE (arg)))
1042 : break;
1043 :
1044 15907 : if (is_gimple_reg_type (TREE_TYPE (param))
1045 8320 : ? !is_gimple_reg (param)
1046 733 : : (!is_gimple_variable (param)
1047 733 : || TREE_THIS_VOLATILE (param)
1048 1466 : || may_be_aliased (param)))
1049 : break;
1050 : }
1051 : }
1052 3048 : if (idx == gimple_call_num_args (call) && !param)
1053 1590029 : tail_recursion = true;
1054 : }
1055 :
1056 1590029 : if (only_tailr && !tail_recursion)
1057 : return;
1058 :
1059 : /* Compute live vars if not computed yet. */
1060 1589957 : if (live_vars == NULL)
1061 : {
1062 1539832 : unsigned int cnt = 0;
1063 13681628 : FOR_EACH_LOCAL_DECL (cfun, idx, var)
1064 10802358 : if (VAR_P (var)
1065 10802358 : && auto_var_in_fn_p (var, cfun->decl)
1066 21476546 : && may_be_aliased (var))
1067 : {
1068 995242 : if (live_vars == NULL)
1069 236828 : live_vars = new live_vars_map;
1070 995242 : live_vars->put (DECL_UID (var), cnt++);
1071 : }
1072 1539832 : if (live_vars)
1073 236828 : live_vars_vec = compute_live_vars (cfun, live_vars);
1074 : }
1075 :
1076 : /* Determine a bitmap of variables which are still in scope after the
1077 : call. */
1078 1589957 : bitmap local_live_vars = NULL;
1079 1589957 : if (live_vars)
1080 286953 : local_live_vars = live_vars_at_stmt (live_vars_vec, live_vars, call);
1081 :
1082 : /* Make sure the tail invocation of this function does not indirectly
1083 : refer to local variables. (Passing variables directly by value
1084 : is OK.) */
1085 13832627 : FOR_EACH_LOCAL_DECL (cfun, idx, var)
1086 : {
1087 11073212 : if (TREE_CODE (var) != PARM_DECL
1088 11073212 : && auto_var_in_fn_p (var, cfun->decl)
1089 10994765 : && may_be_aliased (var)
1090 11896876 : && (ref_maybe_used_by_stmt_p (call, var, false)
1091 382282 : || call_may_clobber_ref_p (call, var, false)))
1092 : {
1093 484429 : if (!VAR_P (var))
1094 : {
1095 0 : if (diag_musttail && gimple_call_must_tail_p (call))
1096 : {
1097 0 : auto opt = OPT_Wmaybe_musttail_local_addr;
1098 0 : if (!warning_suppressed_p (call,
1099 : opt))
1100 : {
1101 0 : warning_at (gimple_location (call), opt,
1102 : "address of local variable can escape to "
1103 : "%<musttail%> call");
1104 0 : suppress_warning (call, opt);
1105 : }
1106 0 : continue;
1107 0 : }
1108 0 : if (local_live_vars)
1109 0 : BITMAP_FREE (local_live_vars);
1110 0 : maybe_error_musttail (call, _("call invocation refers to "
1111 : "locals"), diag_musttail);
1112 0 : return;
1113 : }
1114 : else
1115 : {
1116 484429 : unsigned int *v = live_vars->get (DECL_UID (var));
1117 484429 : if (bitmap_bit_p (local_live_vars, *v))
1118 : {
1119 220577 : if (diag_musttail && gimple_call_must_tail_p (call))
1120 : {
1121 472 : auto opt = OPT_Wmaybe_musttail_local_addr;
1122 472 : if (!warning_suppressed_p (call, opt))
1123 : {
1124 195 : if (!DECL_ARTIFICIAL (var) && DECL_NAME (var))
1125 194 : warning_at (gimple_location (call), opt,
1126 : "address of automatic variable %qD "
1127 : "can escape to %<musttail%> call",
1128 : var);
1129 : else
1130 1 : warning_at (gimple_location (call), opt,
1131 : "address of local variable can escape "
1132 : "to %<musttail%> call");
1133 195 : suppress_warning (call, opt);
1134 : }
1135 472 : continue;
1136 472 : }
1137 220105 : BITMAP_FREE (local_live_vars);
1138 220105 : maybe_error_musttail (call, _("call invocation refers to "
1139 : "locals"), diag_musttail);
1140 220105 : return;
1141 : }
1142 : }
1143 : }
1144 : }
1145 1369852 : if (diag_musttail
1146 318737 : && gimple_call_must_tail_p (call)
1147 1370595 : && !warning_suppressed_p (call, OPT_Wmaybe_musttail_local_addr))
1148 401 : for (tree param = DECL_ARGUMENTS (current_function_decl);
1149 801 : param; param = DECL_CHAIN (param))
1150 425 : if (may_be_aliased (param)
1151 425 : && (ref_maybe_used_by_stmt_p (call, param, false)
1152 0 : || call_may_clobber_ref_p (call, param, false)))
1153 : {
1154 25 : auto opt = OPT_Wmaybe_musttail_local_addr;
1155 25 : warning_at (gimple_location (call), opt,
1156 : "address of parameter %qD can escape to "
1157 : "%<musttail%> call", param);
1158 25 : suppress_warning (call, opt);
1159 25 : break;
1160 : }
1161 :
1162 1369852 : if (local_live_vars)
1163 66848 : BITMAP_FREE (local_live_vars);
1164 :
1165 : /* Now check the statements after the call. None of them has virtual
1166 : operands, so they may only depend on the call through its return
1167 : value. The return value should also be dependent on each of them,
1168 : since we are running after dce. */
1169 1369852 : m = NULL_TREE;
1170 1369852 : a = NULL_TREE;
1171 2254187 : auto_bitmap to_move_defs;
1172 2254187 : auto_vec<gimple *> to_move_stmts;
1173 1369852 : bool is_noreturn = gimple_call_noreturn_p (call);
1174 2254187 : auto_vec<edge> edges;
1175 :
1176 1369852 : abb = bb;
1177 1369852 : agsi = gsi;
1178 3345287 : while (!is_noreturn)
1179 : {
1180 3344612 : tree tmp_a = NULL_TREE;
1181 3344612 : tree tmp_m = NULL_TREE;
1182 3344612 : gsi_next (&agsi);
1183 :
1184 519913 : new_bb:
1185 3864525 : while (gsi_end_p (agsi))
1186 : {
1187 519753 : edge e = single_non_eh_succ_edge (abb);
1188 519753 : ass_var = propagate_through_phis (ass_var, e);
1189 519753 : if (!ass_var || ignored_edges)
1190 436444 : edges.safe_push (e);
1191 519753 : abb = e->dest;
1192 1039506 : agsi = gsi_start_bb (abb);
1193 : }
1194 :
1195 3344772 : stmt = gsi_stmt (agsi);
1196 3344772 : if (gimple_code (stmt) == GIMPLE_RETURN)
1197 : break;
1198 :
1199 4029582 : if (gimple_code (stmt) == GIMPLE_LABEL
1200 2044028 : || gimple_code (stmt) == GIMPLE_NOP
1201 2044028 : || gimple_code (stmt) == GIMPLE_PREDICT
1202 2024370 : || gimple_clobber_p (stmt)
1203 3907634 : || is_gimple_debug (stmt))
1204 1955338 : continue;
1205 :
1206 119616 : if (cfun->has_musttail
1207 743 : && sanitize_flags_p (SANITIZE_THREAD)
1208 10 : && retry_tsan_func_exit == 1
1209 10 : && gimple_call_builtin_p (stmt, BUILT_IN_TSAN_FUNC_EXIT)
1210 8 : && !has_tsan_func_exit
1211 119616 : && gimple_call_must_tail_p (call))
1212 : {
1213 8 : has_tsan_func_exit = true;
1214 8 : continue;
1215 : }
1216 :
1217 119896 : if (cfun->has_musttail
1218 735 : && sanitize_flags_p (SANITIZE_ADDRESS)
1219 483 : && asan_mark_p (stmt, ASAN_MARK_POISON)
1220 119896 : && diag_musttail)
1221 296 : continue;
1222 :
1223 : /* See earlier comment on GIMPLE_CONDs. Here we need to propagate
1224 : through on the forward walk, so based on the edges vector we've
1225 : walked through determine which edge to follow. */
1226 119304 : if (ignored_edges)
1227 : {
1228 186 : auto assign = dyn_cast<gassign *> (stmt);
1229 26 : if (assign
1230 26 : && gimple_assign_rhs_code (assign) == INTEGER_CST
1231 20 : && tree_int_cst_sgn (gimple_assign_rhs1 (assign)) >= 0)
1232 : {
1233 20 : use_operand_p use_p;
1234 20 : imm_use_iterator imm_iter;
1235 20 : bool bad_p = false;
1236 40 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter,
1237 : gimple_assign_lhs (assign))
1238 : {
1239 20 : gimple *use_stmt = USE_STMT (use_p);
1240 20 : if (is_gimple_debug (use_stmt)
1241 20 : || gimple_code (use_stmt) == GIMPLE_COND
1242 40 : || gimple_code (use_stmt) == GIMPLE_SWITCH)
1243 0 : continue;
1244 20 : if (gimple_code (use_stmt) == GIMPLE_PHI)
1245 : {
1246 20 : use_operand_p use_p2;
1247 20 : imm_use_iterator imm_iter2;
1248 186 : FOR_EACH_IMM_USE_FAST (use_p2, imm_iter2,
1249 : gimple_phi_result (use_stmt))
1250 : {
1251 166 : gimple *use_stmt2 = USE_STMT (use_p2);
1252 166 : if (is_gimple_debug (use_stmt2)
1253 166 : || gimple_code (use_stmt2) == GIMPLE_COND
1254 168 : || gimple_code (use_stmt2) == GIMPLE_SWITCH)
1255 166 : continue;
1256 : bad_p = true;
1257 : break;
1258 20 : }
1259 20 : if (bad_p)
1260 : break;
1261 : }
1262 : else
1263 : {
1264 : bad_p = true;
1265 : break;
1266 : }
1267 20 : }
1268 20 : if (!bad_p)
1269 20 : continue;
1270 : }
1271 166 : if (gimple_code (stmt) == GIMPLE_COND
1272 158 : && TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
1273 158 : && TREE_CODE (gimple_cond_rhs (stmt)) == INTEGER_CST
1274 158 : && INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))
1275 324 : && tree_int_cst_sgn (gimple_cond_rhs (stmt)) >= 0)
1276 : {
1277 158 : edge e = NULL, et, ef;
1278 158 : enum tree_code ccode = gimple_cond_code (stmt);
1279 158 : tree lhs = gimple_cond_lhs (stmt);
1280 158 : tree rhsv = gimple_cond_rhs (stmt);
1281 158 : extract_true_false_edges_from_block (gimple_bb (stmt), &et, &ef);
1282 158 : auto assign = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (lhs));
1283 0 : if (assign && (gimple_assign_rhs_code (assign) == INTEGER_CST))
1284 : {
1285 0 : tree lhsv = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (lhs));
1286 0 : tree r = const_binop (ccode, boolean_type_node, lhsv, rhsv);
1287 0 : if (r == boolean_true_node)
1288 0 : e = et;
1289 0 : else if (r == boolean_false_node)
1290 0 : e = ef;
1291 : }
1292 158 : else if (gimple_code (SSA_NAME_DEF_STMT (lhs)) == GIMPLE_PHI)
1293 : {
1294 158 : gimple *phi = SSA_NAME_DEF_STMT (lhs);
1295 158 : basic_block pbb = gimple_bb (phi);
1296 563 : for (edge e2 : edges)
1297 247 : if (e2->dest == pbb)
1298 : {
1299 158 : tree lhsv = gimple_phi_arg_def_from_edge (phi, e2);
1300 158 : if (TREE_CODE (lhsv) == SSA_NAME)
1301 158 : if (auto *g
1302 : = safe_dyn_cast<gassign *>
1303 158 : (SSA_NAME_DEF_STMT (lhsv)))
1304 316 : if (gimple_assign_rhs_code (g) == INTEGER_CST)
1305 158 : lhsv = gimple_assign_rhs1 (g);
1306 158 : tree r = const_binop (ccode, boolean_type_node,
1307 : lhsv, rhsv);
1308 158 : if (r == boolean_true_node)
1309 6 : e = et;
1310 152 : else if (r == boolean_false_node)
1311 152 : e = ef;
1312 : break;
1313 : }
1314 : }
1315 158 : if (e)
1316 : {
1317 158 : ass_var = propagate_through_phis (ass_var, e);
1318 158 : if (!ass_var || ignored_edges)
1319 158 : edges.safe_push (e);
1320 158 : abb = e->dest;
1321 158 : agsi = gsi_start_bb (abb);
1322 158 : goto new_bb;
1323 : }
1324 : }
1325 8 : if (gimple_code (stmt) == GIMPLE_SWITCH
1326 8 : && (TREE_CODE (gimple_switch_index (as_a <gswitch *> (stmt)))
1327 : == SSA_NAME))
1328 : {
1329 2 : edge e = NULL;
1330 2 : gswitch *swtch = as_a <gswitch *> (stmt);
1331 2 : tree idx = gimple_switch_index (swtch);
1332 2 : auto assign = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (idx));
1333 0 : if (assign
1334 0 : && (gimple_assign_rhs_code (assign) == INTEGER_CST))
1335 : {
1336 0 : tree val = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (idx));
1337 0 : e = find_taken_edge_switch_expr (swtch, val);
1338 : }
1339 2 : else if (gimple_code (SSA_NAME_DEF_STMT (idx)) == GIMPLE_PHI)
1340 : {
1341 2 : gimple *phi = SSA_NAME_DEF_STMT (idx);
1342 2 : basic_block pbb = gimple_bb (phi);
1343 7 : for (edge e2 : edges)
1344 3 : if (e2->dest == pbb)
1345 : {
1346 2 : tree val = gimple_phi_arg_def_from_edge (phi, e2);
1347 2 : if (TREE_CODE (val) == SSA_NAME)
1348 2 : if (auto *g
1349 : = safe_dyn_cast<gassign *>
1350 2 : (SSA_NAME_DEF_STMT (val)))
1351 4 : if (gimple_assign_rhs_code (g) == INTEGER_CST)
1352 2 : val = gimple_assign_rhs1 (g);
1353 2 : if (TREE_CODE (val) == INTEGER_CST)
1354 2 : e = find_taken_edge_switch_expr (swtch, val);
1355 : break;
1356 : }
1357 : }
1358 2 : if (e)
1359 : {
1360 2 : ass_var = propagate_through_phis (ass_var, e);
1361 2 : if (!ass_var || ignored_edges)
1362 2 : edges.safe_push (e);
1363 2 : abb = e->dest;
1364 2 : agsi = gsi_start_bb (abb);
1365 2 : goto new_bb;
1366 : }
1367 : }
1368 : }
1369 :
1370 119124 : if (gimple_code (stmt) != GIMPLE_ASSIGN)
1371 : {
1372 297 : maybe_error_musttail (call, _("unhandled code after call"),
1373 : diag_musttail);
1374 99297 : return;
1375 : }
1376 :
1377 : /* This is a gimple assign. */
1378 118827 : par ret = process_assignment (as_a <gassign *> (stmt), gsi,
1379 : &tmp_m, &tmp_a, &ass_var, to_move_defs);
1380 118827 : if (ret == FAIL || (ret == TRY_MOVE && !tail_recursion))
1381 : {
1382 98690 : maybe_error_musttail (call, _("return value changed after call"),
1383 : diag_musttail);
1384 98690 : return;
1385 : }
1386 20137 : else if (ret == TRY_MOVE)
1387 : {
1388 : /* Do not deal with checking dominance, the real fix is to
1389 : do path isolation for the transform phase anyway, removing
1390 : the need to compute the accumulators with new stmts. */
1391 40 : if (abb != bb)
1392 : return;
1393 81 : for (unsigned opno = 1; opno < gimple_num_ops (stmt); ++opno)
1394 : {
1395 54 : tree op = gimple_op (stmt, opno);
1396 54 : if (independent_of_stmt_p (op, stmt, gsi, to_move_defs) != op)
1397 : return;
1398 : }
1399 54 : bitmap_set_bit (to_move_defs,
1400 27 : SSA_NAME_VERSION (gimple_assign_lhs (stmt)));
1401 27 : to_move_stmts.safe_push (stmt);
1402 27 : continue;
1403 27 : }
1404 :
1405 20097 : if (tmp_a)
1406 : {
1407 8592 : tree type = TREE_TYPE (tmp_a);
1408 8592 : if (a)
1409 444 : a = fold_build2 (PLUS_EXPR, type, fold_convert (type, a), tmp_a);
1410 : else
1411 : a = tmp_a;
1412 : }
1413 20097 : if (tmp_m)
1414 : {
1415 2024 : tree type = TREE_TYPE (tmp_m);
1416 2024 : if (m)
1417 139 : m = fold_build2 (MULT_EXPR, type, fold_convert (type, m), tmp_m);
1418 : else
1419 : m = tmp_m;
1420 :
1421 2024 : if (a)
1422 1368 : a = fold_build2 (MULT_EXPR, type, fold_convert (type, a), tmp_m);
1423 : }
1424 : }
1425 :
1426 : /* See if this is a tail call we can handle. */
1427 1270852 : if (is_noreturn)
1428 : {
1429 675 : if (gimple_call_internal_p (call))
1430 : {
1431 659 : maybe_error_musttail (call, _("internal call"), diag_musttail);
1432 659 : return;
1433 : }
1434 16 : tree rettype = TREE_TYPE (TREE_TYPE (current_function_decl));
1435 16 : tree calltype = TREE_TYPE (gimple_call_fntype (call));
1436 16 : if (!VOID_TYPE_P (rettype)
1437 16 : && !useless_type_conversion_p (rettype, calltype))
1438 : {
1439 0 : maybe_error_musttail (call, _("call and return value are different"),
1440 : diag_musttail);
1441 0 : return;
1442 : }
1443 : ret_var = NULL_TREE;
1444 : }
1445 : else
1446 1270177 : ret_var = gimple_return_retval (as_a <greturn *> (stmt));
1447 :
1448 : /* We may proceed if there either is no return value, or the return value
1449 : is identical to the call's return or if the return decl is an empty type
1450 : variable and the call's return was not assigned. */
1451 1270177 : if (ret_var
1452 1270177 : && (ret_var != ass_var
1453 386748 : && !(is_empty_type (TREE_TYPE (ret_var)) && !ass_var)))
1454 : {
1455 385623 : bool ok = false;
1456 385623 : value_range val;
1457 385623 : if (ass_var == NULL_TREE && !tail_recursion)
1458 : {
1459 385511 : tree other_value = NULL_TREE;
1460 : /* If we have a function call that we know the return value is the same
1461 : as the argument, try the argument too. */
1462 385511 : int flags = gimple_call_return_flags (call);
1463 385511 : if ((flags & ERF_RETURNS_ARG) != 0
1464 385511 : && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (call))
1465 : {
1466 4475 : tree arg = gimple_call_arg (call, flags & ERF_RETURN_ARG_MASK);
1467 4475 : if (useless_type_conversion_p (TREE_TYPE (ret_var), TREE_TYPE (arg) ))
1468 : other_value = arg;
1469 : }
1470 : /* If IPA-VRP proves called function always returns a singleton range,
1471 : the return value is replaced by the only value in that range.
1472 : For tail call purposes, pretend such replacement didn't happen. */
1473 381036 : else if (tree type = gimple_range_type (call))
1474 83559 : if (tree callee = gimple_call_fndecl (call))
1475 : {
1476 83111 : tree valr;
1477 83111 : if ((INTEGRAL_TYPE_P (type)
1478 : || SCALAR_FLOAT_TYPE_P (type)
1479 83111 : || POINTER_TYPE_P (type))
1480 83111 : && useless_type_conversion_p (TREE_TYPE (TREE_TYPE (callee)),
1481 : type)
1482 83103 : && useless_type_conversion_p (TREE_TYPE (ret_var), type)
1483 22407 : && ipa_return_value_range (val, callee)
1484 93575 : && val.singleton_p (&valr))
1485 7244 : other_value = valr;
1486 : }
1487 :
1488 85788 : if (other_value)
1489 : {
1490 9921 : tree rv = ret_var;
1491 9921 : unsigned int i = edges.length ();
1492 : /* If ret_var is equal to other_value, we can tail optimize. */
1493 9921 : if (operand_equal_p (ret_var, other_value, 0))
1494 : ok = true;
1495 : else
1496 : /* Otherwise, if ret_var is a PHI result, try to find out
1497 : if other_value isn't propagated through PHIs on the path from
1498 : call's bb to SSA_NAME_DEF_STMT (ret_var)'s bb. */
1499 4130 : while (TREE_CODE (rv) == SSA_NAME
1500 4130 : && gimple_code (SSA_NAME_DEF_STMT (rv)) == GIMPLE_PHI)
1501 : {
1502 1622 : tree nrv = NULL_TREE;
1503 : gimple *g = SSA_NAME_DEF_STMT (rv);
1504 1622 : for (; i; --i)
1505 : {
1506 1586 : if (edges[i - 1]->dest == gimple_bb (g))
1507 : {
1508 1586 : nrv = gimple_phi_arg_def_from_edge (g,
1509 1586 : edges[i - 1]);
1510 1586 : --i;
1511 1586 : break;
1512 : }
1513 : }
1514 1622 : if (nrv == NULL_TREE)
1515 : break;
1516 1586 : if (operand_equal_p (nrv, other_value, 0))
1517 : {
1518 : ok = true;
1519 : break;
1520 : }
1521 : rv = nrv;
1522 : }
1523 : }
1524 : }
1525 2958 : if (!ok)
1526 : {
1527 378246 : maybe_error_musttail (call, _("call and return value are different"),
1528 : diag_musttail);
1529 378246 : return;
1530 : }
1531 385623 : }
1532 :
1533 : /* If this is not a tail recursive call, we cannot handle addends or
1534 : multiplicands. */
1535 891947 : if (!tail_recursion && (m || a))
1536 : {
1537 7612 : maybe_error_musttail (call, _("operations after non tail recursive "
1538 : "call"), diag_musttail);
1539 7612 : return;
1540 : }
1541 :
1542 : /* For pointers only allow additions. */
1543 1781 : if (m && POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
1544 : {
1545 0 : maybe_error_musttail (call, _("tail recursion with pointers can only "
1546 : "use additions"), diag_musttail);
1547 0 : return;
1548 : }
1549 :
1550 884335 : if (eh_has_tsan_func_exit != -1
1551 159 : && eh_has_tsan_func_exit != has_tsan_func_exit)
1552 : {
1553 0 : if (eh_has_tsan_func_exit)
1554 0 : maybe_error_musttail (call, _("call may throw exception caught "
1555 : "locally or perform cleanups"),
1556 : diag_musttail);
1557 : else
1558 0 : maybe_error_musttail (call, _("exception cleanups omit "
1559 : "__tsan_func_exit call"), diag_musttail);
1560 : return;
1561 : }
1562 :
1563 : /* Move queued defs. */
1564 884335 : if (tail_recursion)
1565 : {
1566 : unsigned i;
1567 1787 : FOR_EACH_VEC_ELT (to_move_stmts, i, stmt)
1568 : {
1569 3 : gimple_stmt_iterator mgsi = gsi_for_stmt (stmt);
1570 3 : gsi_move_before (&mgsi, &gsi);
1571 : }
1572 1781 : if (!tailr_arg_needs_copy)
1573 1321 : tailr_arg_needs_copy = BITMAP_ALLOC (NULL);
1574 1781 : for (param = DECL_ARGUMENTS (current_function_decl), idx = 0;
1575 6464 : param;
1576 4683 : param = DECL_CHAIN (param), idx++)
1577 : {
1578 4683 : tree ddef, arg = gimple_call_arg (call, idx);
1579 4683 : if (!is_gimple_reg (param)
1580 4683 : || ((ddef = ssa_default_def (cfun, param))
1581 4127 : && arg != ddef))
1582 2968 : bitmap_set_bit (tailr_arg_needs_copy, idx);
1583 : }
1584 : }
1585 :
1586 884335 : nw = XNEW (struct tailcall);
1587 :
1588 884335 : nw->call_gsi = gsi;
1589 :
1590 884335 : nw->tail_recursion = tail_recursion;
1591 884335 : nw->has_tsan_func_exit = has_tsan_func_exit;
1592 :
1593 884335 : nw->mult = m;
1594 884335 : nw->add = a;
1595 :
1596 884335 : nw->next = *ret;
1597 884335 : *ret = nw;
1598 : }
1599 :
1600 : /* Helper to insert PHI_ARGH to the phi of VAR in the destination of edge E. */
1601 :
1602 : static void
1603 239 : add_successor_phi_arg (edge e, tree var, tree phi_arg)
1604 : {
1605 239 : gphi_iterator gsi;
1606 :
1607 527 : for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
1608 527 : if (PHI_RESULT (gsi.phi ()) == var)
1609 : break;
1610 :
1611 239 : gcc_assert (!gsi_end_p (gsi));
1612 239 : add_phi_arg (gsi.phi (), phi_arg, e, UNKNOWN_LOCATION);
1613 239 : }
1614 :
1615 : /* Creates a GIMPLE statement which computes the operation specified by
1616 : CODE, ACC and OP1 to a new variable with name LABEL and inserts the
1617 : statement in the position specified by GSI. Returns the
1618 : tree node of the statement's result. */
1619 :
1620 : static tree
1621 196 : adjust_return_value_with_ops (enum tree_code code, const char *label,
1622 : tree acc, tree op1, gimple_stmt_iterator gsi)
1623 : {
1624 :
1625 196 : tree ret_type = TREE_TYPE (DECL_RESULT (current_function_decl));
1626 196 : tree result = make_temp_ssa_name (ret_type, NULL, label);
1627 196 : gassign *stmt;
1628 :
1629 196 : if (POINTER_TYPE_P (ret_type))
1630 : {
1631 6 : gcc_assert (code == PLUS_EXPR && TREE_TYPE (acc) == sizetype);
1632 : code = POINTER_PLUS_EXPR;
1633 : }
1634 196 : if (types_compatible_p (TREE_TYPE (acc), TREE_TYPE (op1))
1635 196 : && code != POINTER_PLUS_EXPR)
1636 185 : stmt = gimple_build_assign (result, code, acc, op1);
1637 : else
1638 : {
1639 11 : tree tem;
1640 11 : if (code == POINTER_PLUS_EXPR)
1641 6 : tem = fold_build2 (code, TREE_TYPE (op1), op1, acc);
1642 : else
1643 5 : tem = fold_build2 (code, TREE_TYPE (op1),
1644 : fold_convert (TREE_TYPE (op1), acc), op1);
1645 11 : tree rhs = fold_convert (ret_type, tem);
1646 11 : rhs = force_gimple_operand_gsi (&gsi, rhs,
1647 : false, NULL, true, GSI_SAME_STMT);
1648 11 : stmt = gimple_build_assign (result, rhs);
1649 : }
1650 :
1651 196 : gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
1652 196 : return result;
1653 : }
1654 :
1655 : /* Creates a new GIMPLE statement that adjusts the value of accumulator ACC by
1656 : the computation specified by CODE and OP1 and insert the statement
1657 : at the position specified by GSI as a new statement. Returns new SSA name
1658 : of updated accumulator. */
1659 :
1660 : static tree
1661 236 : update_accumulator_with_ops (enum tree_code code, tree acc, tree op1,
1662 : gimple_stmt_iterator gsi)
1663 : {
1664 236 : gassign *stmt;
1665 236 : tree var = copy_ssa_name (acc);
1666 236 : if (types_compatible_p (TREE_TYPE (acc), TREE_TYPE (op1)))
1667 222 : stmt = gimple_build_assign (var, code, acc, op1);
1668 : else
1669 : {
1670 14 : tree rhs = fold_convert (TREE_TYPE (acc),
1671 : fold_build2 (code,
1672 : TREE_TYPE (op1),
1673 : fold_convert (TREE_TYPE (op1), acc),
1674 : op1));
1675 14 : rhs = force_gimple_operand_gsi (&gsi, rhs,
1676 : false, NULL, false, GSI_CONTINUE_LINKING);
1677 14 : stmt = gimple_build_assign (var, rhs);
1678 : }
1679 236 : gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
1680 236 : return var;
1681 : }
1682 :
1683 : /* Adjust the accumulator values according to A and M after GSI, and update
1684 : the phi nodes on edge BACK. */
1685 :
1686 : static void
1687 1775 : adjust_accumulator_values (gimple_stmt_iterator gsi, tree m, tree a, edge back)
1688 : {
1689 1775 : tree var, a_acc_arg, m_acc_arg;
1690 :
1691 1775 : if (m)
1692 134 : m = force_gimple_operand_gsi (&gsi, m, true, NULL, true, GSI_SAME_STMT);
1693 1775 : if (a)
1694 102 : a = force_gimple_operand_gsi (&gsi, a, true, NULL, true, GSI_SAME_STMT);
1695 :
1696 1775 : a_acc_arg = a_acc;
1697 1775 : m_acc_arg = m_acc;
1698 1775 : if (a)
1699 : {
1700 102 : if (m_acc)
1701 : {
1702 18 : if (integer_onep (a))
1703 1 : var = m_acc;
1704 : else
1705 17 : var = adjust_return_value_with_ops (MULT_EXPR, "acc_tmp", m_acc,
1706 : a, gsi);
1707 : }
1708 : else
1709 : var = a;
1710 :
1711 102 : a_acc_arg = update_accumulator_with_ops (PLUS_EXPR, a_acc, var, gsi);
1712 : }
1713 :
1714 1775 : if (m)
1715 134 : m_acc_arg = update_accumulator_with_ops (MULT_EXPR, m_acc, m, gsi);
1716 :
1717 1775 : if (a_acc)
1718 105 : add_successor_phi_arg (back, a_acc, a_acc_arg);
1719 :
1720 1775 : if (m_acc)
1721 134 : add_successor_phi_arg (back, m_acc, m_acc_arg);
1722 1775 : }
1723 :
1724 : /* Adjust value of the return at the end of BB according to M and A
1725 : accumulators. */
1726 :
1727 : static void
1728 166 : adjust_return_value (basic_block bb, tree m, tree a)
1729 : {
1730 166 : tree retval;
1731 332 : greturn *ret_stmt = as_a <greturn *> (gimple_seq_last_stmt (bb_seq (bb)));
1732 166 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
1733 :
1734 166 : gcc_assert (gimple_code (ret_stmt) == GIMPLE_RETURN);
1735 :
1736 166 : retval = gimple_return_retval (ret_stmt);
1737 166 : if (!retval || retval == error_mark_node)
1738 0 : return;
1739 :
1740 166 : if (m)
1741 96 : retval = adjust_return_value_with_ops (MULT_EXPR, "mul_tmp", m_acc, retval,
1742 : gsi);
1743 166 : if (a)
1744 83 : retval = adjust_return_value_with_ops (PLUS_EXPR, "acc_tmp", a_acc, retval,
1745 : gsi);
1746 166 : gimple_return_set_retval (ret_stmt, retval);
1747 166 : update_stmt (ret_stmt);
1748 : }
1749 :
1750 : /* Subtract COUNT and FREQUENCY from the basic block and it's
1751 : outgoing edge. */
1752 : static void
1753 5058 : decrease_profile (basic_block bb, profile_count count)
1754 : {
1755 0 : bb->count = bb->count - count;
1756 0 : }
1757 :
1758 : /* Eliminates tail call described by T. TMP_VARS is a list of
1759 : temporary variables used to copy the function arguments.
1760 : Allocates *NEW_LOOP if not already done and initializes it. */
1761 :
1762 : static void
1763 1775 : eliminate_tail_call (struct tailcall *t, class loop *&new_loop)
1764 : {
1765 1775 : tree param, rslt;
1766 1775 : gimple *stmt, *call;
1767 1775 : tree arg;
1768 1775 : size_t idx;
1769 1775 : basic_block bb, first;
1770 1775 : edge e;
1771 1775 : gphi *phi;
1772 1775 : gphi_iterator gpi;
1773 1775 : gimple_stmt_iterator gsi;
1774 1775 : gimple *orig_stmt;
1775 :
1776 1775 : stmt = orig_stmt = gsi_stmt (t->call_gsi);
1777 1775 : bb = gsi_bb (t->call_gsi);
1778 :
1779 1775 : if (dump_file && (dump_flags & TDF_DETAILS))
1780 : {
1781 8 : fprintf (dump_file, "Eliminated tail recursion in bb %d : ",
1782 : bb->index);
1783 8 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1784 8 : fprintf (dump_file, "\n");
1785 : }
1786 :
1787 1775 : gcc_assert (is_gimple_call (stmt));
1788 :
1789 1775 : first = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1790 :
1791 : /* Remove the code after call_gsi that will become unreachable. The
1792 : possibly unreachable code in other blocks is removed later in
1793 : cfg cleanup. */
1794 1775 : gsi = t->call_gsi;
1795 1775 : gimple_stmt_iterator gsi2 = gsi_last_bb (gimple_bb (gsi_stmt (gsi)));
1796 3111 : while (gsi_stmt (gsi2) != gsi_stmt (gsi))
1797 : {
1798 1336 : gimple *t = gsi_stmt (gsi2);
1799 : /* Do not remove the return statement, so that redirect_edge_and_branch
1800 : sees how the block ends. */
1801 1336 : if (gimple_code (t) != GIMPLE_RETURN)
1802 : {
1803 1081 : gimple_stmt_iterator gsi3 = gsi2;
1804 1081 : gsi_prev (&gsi2);
1805 1081 : gsi_remove (&gsi3, true);
1806 1081 : release_defs (t);
1807 : }
1808 : else
1809 1336 : gsi_prev (&gsi2);
1810 : }
1811 :
1812 1775 : if (gimple_call_noreturn_p (as_a <gcall *> (stmt)))
1813 : {
1814 4 : e = make_edge (gsi_bb (t->call_gsi), first, EDGE_FALLTHRU);
1815 4 : e->probability = profile_probability::always ();
1816 : }
1817 : else
1818 : {
1819 : /* Number of executions of function has reduced by the tailcall. */
1820 1771 : e = single_non_eh_succ_edge (gsi_bb (t->call_gsi));
1821 :
1822 1771 : profile_count count = e->count ();
1823 :
1824 : /* When profile is inconsistent and the recursion edge is more frequent
1825 : than number of executions of functions, scale it down, so we do not
1826 : end up with 0 executions of entry block. */
1827 1771 : if (count >= ENTRY_BLOCK_PTR_FOR_FN (cfun)->count)
1828 39 : count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.apply_scale (7, 8);
1829 1771 : decrease_profile (EXIT_BLOCK_PTR_FOR_FN (cfun), count);
1830 1771 : decrease_profile (ENTRY_BLOCK_PTR_FOR_FN (cfun), count);
1831 1771 : if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1832 1516 : decrease_profile (e->dest, count);
1833 :
1834 : /* Replace the call by a jump to the start of function. */
1835 1771 : e = redirect_edge_and_branch (e, first);
1836 : }
1837 1775 : gcc_assert (e);
1838 1775 : PENDING_STMT (e) = NULL;
1839 :
1840 : /* Add the new loop. */
1841 1775 : if (!new_loop)
1842 : {
1843 1319 : new_loop = alloc_loop ();
1844 1319 : new_loop->header = first;
1845 1319 : new_loop->finite_p = true;
1846 : }
1847 : else
1848 456 : gcc_assert (new_loop->header == first);
1849 :
1850 : /* Add phi node entries for arguments. The ordering of the phi nodes should
1851 : be the same as the ordering of the arguments. */
1852 1775 : auto_vec<tree> copies;
1853 1775 : for (param = DECL_ARGUMENTS (current_function_decl),
1854 1775 : idx = 0, gpi = gsi_start_phis (first);
1855 6446 : param;
1856 4671 : param = DECL_CHAIN (param), idx++)
1857 : {
1858 4671 : if (!bitmap_bit_p (tailr_arg_needs_copy, idx))
1859 1700 : continue;
1860 :
1861 2971 : if (!is_gimple_reg_type (TREE_TYPE (param)))
1862 : {
1863 510 : if (param == gimple_call_arg (stmt, idx))
1864 148 : continue;
1865 : /* First check if param isn't used by any of the following
1866 : call arguments. If it is, we need to copy first to
1867 : a temporary and only after doing all the assignments copy it
1868 : to param. */
1869 362 : size_t idx2 = idx + 1;
1870 362 : tree param2 = DECL_CHAIN (param);
1871 1687 : for (; param2; param2 = DECL_CHAIN (param2), idx2++)
1872 1326 : if (!is_gimple_reg_type (TREE_TYPE (param)))
1873 : {
1874 1326 : tree base = get_base_address (gimple_call_arg (stmt, idx2));
1875 1326 : if (base == param)
1876 : break;
1877 : }
1878 362 : tree tmp = param;
1879 362 : if (param2)
1880 : {
1881 1 : tmp = create_tmp_var (TREE_TYPE (param));
1882 1 : copies.safe_push (param);
1883 1 : copies.safe_push (tmp);
1884 : }
1885 362 : gimple *g = gimple_build_assign (tmp, gimple_call_arg (stmt, idx));
1886 362 : gsi_insert_before (&t->call_gsi, g, GSI_SAME_STMT);
1887 362 : continue;
1888 362 : }
1889 :
1890 2461 : arg = gimple_call_arg (stmt, idx);
1891 2461 : phi = gpi.phi ();
1892 2461 : gcc_assert (param == SSA_NAME_VAR (PHI_RESULT (phi)));
1893 :
1894 2461 : add_phi_arg (phi, arg, e, gimple_location (stmt));
1895 2461 : gsi_next (&gpi);
1896 : }
1897 1776 : for (unsigned i = 0; i < copies.length (); i += 2)
1898 : {
1899 1 : gimple *g = gimple_build_assign (copies[i], copies[i + 1]);
1900 1 : gsi_insert_before (&t->call_gsi, g, GSI_SAME_STMT);
1901 : }
1902 :
1903 : /* Update the values of accumulators. */
1904 1775 : adjust_accumulator_values (t->call_gsi, t->mult, t->add, e);
1905 :
1906 1775 : call = gsi_stmt (t->call_gsi);
1907 1775 : rslt = gimple_call_lhs (call);
1908 1775 : if (rslt != NULL_TREE && TREE_CODE (rslt) == SSA_NAME)
1909 : {
1910 : /* Result of the call will no longer be defined. So adjust the
1911 : SSA_NAME_DEF_STMT accordingly. */
1912 1090 : SSA_NAME_DEF_STMT (rslt) = gimple_build_nop ();
1913 : }
1914 :
1915 1775 : gsi_remove (&t->call_gsi, true);
1916 1775 : release_defs (call);
1917 1775 : }
1918 :
1919 : /* Optimizes the tailcall described by T. If OPT_TAILCALLS is true, also
1920 : mark the tailcalls for the sibcall optimization. */
1921 :
1922 : static bool
1923 884329 : optimize_tail_call (struct tailcall *t, bool opt_tailcalls,
1924 : class loop *&new_loop)
1925 : {
1926 884329 : if (t->has_tsan_func_exit && (t->tail_recursion || opt_tailcalls))
1927 : {
1928 8 : tree builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_FUNC_EXIT);
1929 8 : gimple *g = gimple_build_call (builtin_decl, 0);
1930 8 : gimple_set_location (g, cfun->function_end_locus);
1931 8 : gsi_insert_before (&t->call_gsi, g, GSI_SAME_STMT);
1932 : }
1933 :
1934 884329 : if (t->tail_recursion)
1935 : {
1936 1775 : eliminate_tail_call (t, new_loop);
1937 1775 : return true;
1938 : }
1939 :
1940 882554 : if (opt_tailcalls)
1941 : {
1942 178878 : gcall *stmt = as_a <gcall *> (gsi_stmt (t->call_gsi));
1943 :
1944 178878 : gimple_call_set_tail (stmt, true);
1945 178878 : cfun->tail_call_marked = true;
1946 178878 : if (dump_file && (dump_flags & TDF_DETAILS))
1947 : {
1948 25 : fprintf (dump_file, "Found tail call ");
1949 25 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1950 25 : fprintf (dump_file, " in bb %i\n", (gsi_bb (t->call_gsi))->index);
1951 : }
1952 178878 : return t->has_tsan_func_exit;
1953 : }
1954 :
1955 : return false;
1956 : }
1957 :
1958 : /* Creates a tail-call accumulator of the same type as the return type of the
1959 : current function. LABEL is the name used to creating the temporary
1960 : variable for the accumulator. The accumulator will be inserted in the
1961 : phis of a basic block BB with single predecessor with an initial value
1962 : INIT converted to the current function return type. */
1963 :
1964 : static tree
1965 212 : create_tailcall_accumulator (const char *label, basic_block bb, tree init)
1966 : {
1967 212 : tree ret_type = TREE_TYPE (DECL_RESULT (current_function_decl));
1968 212 : if (POINTER_TYPE_P (ret_type))
1969 6 : ret_type = sizetype;
1970 :
1971 212 : tree tmp = make_temp_ssa_name (ret_type, NULL, label);
1972 212 : gphi *phi;
1973 :
1974 212 : phi = create_phi_node (tmp, bb);
1975 212 : add_phi_arg (phi, init, single_pred_edge (bb),
1976 : UNKNOWN_LOCATION);
1977 212 : return PHI_RESULT (phi);
1978 : }
1979 :
1980 : /* Optimizes tail calls in the function, turning the tail recursion
1981 : into iteration. When ONLY_MUSTTAIL is true only optimize musttail
1982 : marked calls. When DIAG_MUSTTAIL, diagnose if musttail calls can't
1983 : be tail call optimized. */
1984 :
1985 : static unsigned int
1986 3651940 : tree_optimize_tail_calls_1 (bool opt_tailcalls, bool only_musttail,
1987 : bool diag_musttail)
1988 : {
1989 3651940 : edge e;
1990 3651940 : bool phis_constructed = false;
1991 3651940 : struct tailcall *tailcalls = NULL, *act, *next;
1992 3651940 : bool changed = false;
1993 3651940 : basic_block first = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1994 3651940 : tree param;
1995 3651940 : edge_iterator ei;
1996 :
1997 7235192 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
1998 : {
1999 : /* Only traverse the normal exits, i.e. those that end with return
2000 : statement. */
2001 10748906 : if (safe_is_a <greturn *> (*gsi_last_bb (e->src)))
2002 : {
2003 3582402 : int retry_tsan_func_exit = 0;
2004 3582402 : find_tail_calls (e->src, e, &tailcalls, only_musttail, opt_tailcalls,
2005 : diag_musttail, retry_tsan_func_exit, NULL, NULL);
2006 3582402 : if (retry_tsan_func_exit == -1)
2007 : {
2008 8 : retry_tsan_func_exit = 1;
2009 8 : find_tail_calls (e->src, e, &tailcalls, only_musttail,
2010 : opt_tailcalls, diag_musttail,
2011 : retry_tsan_func_exit, NULL, NULL);
2012 : }
2013 : }
2014 : }
2015 3651940 : if (cfun->has_musttail && diag_musttail)
2016 : {
2017 508 : basic_block bb;
2018 508 : int retry_tsan_func_exit = 0;
2019 4833 : FOR_EACH_BB_FN (bb, cfun)
2020 4325 : if (EDGE_COUNT (bb->succs) == 0
2021 7275 : || (single_succ_p (bb)
2022 2872 : && (single_succ_edge (bb)->flags & EDGE_EH)))
2023 236 : if (gimple *c = last_nondebug_stmt (bb))
2024 236 : if (is_gimple_call (c)
2025 16 : && gimple_call_must_tail_p (as_a <gcall *> (c))
2026 252 : && gimple_call_noreturn_p (as_a <gcall *> (c)))
2027 16 : find_tail_calls (bb, NULL, &tailcalls, only_musttail,
2028 : opt_tailcalls, diag_musttail,
2029 : retry_tsan_func_exit, NULL, NULL);
2030 : }
2031 :
2032 3651940 : if (live_vars)
2033 : {
2034 236828 : destroy_live_vars (live_vars_vec);
2035 473656 : delete live_vars;
2036 236828 : live_vars = NULL;
2037 : }
2038 :
2039 3651940 : if (cfun->has_musttail)
2040 : {
2041 : /* We can't mix non-recursive must tail calls with tail recursive
2042 : calls which require accumulators, because in that case we have to
2043 : emit code in between the musttail calls and return, which prevent
2044 : calling them as tail calls. So, in that case give up on the
2045 : tail recursion. */
2046 1145 : for (act = tailcalls; act; act = act->next)
2047 732 : if (!act->tail_recursion)
2048 : {
2049 685 : gcall *call = as_a <gcall *> (gsi_stmt (act->call_gsi));
2050 685 : if (gimple_call_must_tail_p (call))
2051 : break;
2052 : }
2053 1086 : if (act)
2054 1792 : for (struct tailcall **p = &tailcalls; *p; )
2055 : {
2056 1119 : if ((*p)->tail_recursion && ((*p)->add || (*p)->mult))
2057 : {
2058 6 : struct tailcall *a = *p;
2059 6 : *p = (*p)->next;
2060 6 : gcall *call = as_a <gcall *> (gsi_stmt (a->call_gsi));
2061 6 : maybe_error_musttail (call, _("tail recursion with "
2062 : "accumulation mixed with "
2063 : "musttail non-recursive call"),
2064 : diag_musttail);
2065 6 : free (a);
2066 6 : }
2067 : else
2068 1113 : p = &(*p)->next;
2069 : }
2070 : }
2071 : /* Construct the phi nodes and accumulators if necessary. */
2072 3651940 : a_acc = m_acc = NULL_TREE;
2073 4536269 : for (act = tailcalls; act; act = act->next)
2074 : {
2075 884329 : if (!act->tail_recursion)
2076 882554 : continue;
2077 :
2078 1775 : if (!phis_constructed)
2079 : {
2080 : /* Ensure that there is only one predecessor of the block
2081 : or if there are existing degenerate PHI nodes. */
2082 1319 : if (!single_pred_p (first)
2083 1319 : || !gimple_seq_empty_p (phi_nodes (first)))
2084 0 : first
2085 0 : = split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
2086 :
2087 : /* Copy the args if needed. */
2088 1319 : unsigned idx;
2089 1319 : for (param = DECL_ARGUMENTS (current_function_decl), idx = 0;
2090 4735 : param;
2091 3416 : param = DECL_CHAIN (param), idx++)
2092 3416 : if (bitmap_bit_p (tailr_arg_needs_copy, idx))
2093 : {
2094 2170 : if (!is_gimple_reg_type (TREE_TYPE (param)))
2095 510 : continue;
2096 1660 : tree name = ssa_default_def (cfun, param);
2097 1660 : tree new_name = make_ssa_name (param, SSA_NAME_DEF_STMT (name));
2098 1660 : gphi *phi;
2099 :
2100 1660 : set_ssa_default_def (cfun, param, new_name);
2101 1660 : phi = create_phi_node (name, first);
2102 1660 : add_phi_arg (phi, new_name, single_pred_edge (first),
2103 1660 : EXPR_LOCATION (param));
2104 : }
2105 : phis_constructed = true;
2106 : }
2107 1775 : tree ret_type = TREE_TYPE (DECL_RESULT (current_function_decl));
2108 1775 : if (POINTER_TYPE_P (ret_type))
2109 187 : ret_type = sizetype;
2110 :
2111 1775 : if (act->add && !a_acc)
2112 96 : a_acc = create_tailcall_accumulator ("add_acc", first,
2113 : build_zero_cst (ret_type));
2114 :
2115 1775 : if (act->mult && !m_acc)
2116 116 : m_acc = create_tailcall_accumulator ("mult_acc", first,
2117 : build_one_cst (ret_type));
2118 : }
2119 :
2120 3651940 : if (a_acc || m_acc)
2121 : {
2122 : /* When the tail call elimination using accumulators is performed,
2123 : statements adding the accumulated value are inserted at all exits.
2124 : This turns all other tail calls to non-tail ones. */
2125 194 : opt_tailcalls = false;
2126 : }
2127 :
2128 3651940 : class loop *new_loop = NULL;
2129 4536269 : for (; tailcalls; tailcalls = next)
2130 : {
2131 884329 : next = tailcalls->next;
2132 884329 : changed |= optimize_tail_call (tailcalls, opt_tailcalls, new_loop);
2133 884329 : free (tailcalls);
2134 : }
2135 3651940 : if (new_loop)
2136 1319 : add_loop (new_loop, loops_for_fn (cfun)->tree_root);
2137 :
2138 3651940 : if (a_acc || m_acc)
2139 : {
2140 : /* Modify the remaining return statements. */
2141 360 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2142 : {
2143 498 : if (safe_is_a <greturn *> (*gsi_last_bb (e->src)))
2144 166 : adjust_return_value (e->src, m_acc, a_acc);
2145 : }
2146 : }
2147 :
2148 3651940 : if (changed)
2149 1325 : free_dominance_info (CDI_DOMINATORS);
2150 :
2151 : /* Add phi nodes for the virtual operands defined in the function to the
2152 : header of the loop created by tail recursion elimination. Do so
2153 : by triggering the SSA renamer. */
2154 3651940 : if (phis_constructed)
2155 1319 : mark_virtual_operands_for_renaming (cfun);
2156 :
2157 3651940 : if (tailr_arg_needs_copy)
2158 1321 : BITMAP_FREE (tailr_arg_needs_copy);
2159 :
2160 3651940 : if (diag_musttail)
2161 748214 : cfun->has_musttail = false;
2162 :
2163 3651940 : if (changed)
2164 1325 : return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
2165 : return 0;
2166 : }
2167 :
2168 : static bool
2169 4663834 : gate_tail_calls (void)
2170 : {
2171 4663834 : return flag_optimize_sibling_calls != 0 && dbg_cnt (tail_call);
2172 : }
2173 :
2174 : static unsigned int
2175 747974 : execute_tail_calls (void)
2176 : {
2177 0 : return tree_optimize_tail_calls_1 (true, false, true);
2178 : }
2179 :
2180 : namespace {
2181 :
2182 : const pass_data pass_data_tail_recursion =
2183 : {
2184 : GIMPLE_PASS, /* type */
2185 : "tailr", /* name */
2186 : OPTGROUP_NONE, /* optinfo_flags */
2187 : TV_NONE, /* tv_id */
2188 : ( PROP_cfg | PROP_ssa ), /* properties_required */
2189 : 0, /* properties_provided */
2190 : 0, /* properties_destroyed */
2191 : 0, /* todo_flags_start */
2192 : 0, /* todo_flags_finish */
2193 : };
2194 :
2195 : class pass_tail_recursion : public gimple_opt_pass
2196 : {
2197 : public:
2198 605970 : pass_tail_recursion (gcc::context *ctxt)
2199 1211940 : : gimple_opt_pass (pass_data_tail_recursion, ctxt)
2200 : {}
2201 :
2202 : /* opt_pass methods: */
2203 302985 : opt_pass * clone () final override
2204 : {
2205 302985 : return new pass_tail_recursion (m_ctxt);
2206 : }
2207 3595533 : bool gate (function *) final override { return gate_tail_calls (); }
2208 2903726 : unsigned int execute (function *) final override
2209 : {
2210 2903726 : return tree_optimize_tail_calls_1 (false, false, false);
2211 : }
2212 :
2213 : }; // class pass_tail_recursion
2214 :
2215 : } // anon namespace
2216 :
2217 : gimple_opt_pass *
2218 302985 : make_pass_tail_recursion (gcc::context *ctxt)
2219 : {
2220 302985 : return new pass_tail_recursion (ctxt);
2221 : }
2222 :
2223 : namespace {
2224 :
2225 : const pass_data pass_data_tail_calls =
2226 : {
2227 : GIMPLE_PASS, /* type */
2228 : "tailc", /* name */
2229 : OPTGROUP_NONE, /* optinfo_flags */
2230 : TV_NONE, /* tv_id */
2231 : ( PROP_cfg | PROP_ssa ), /* properties_required */
2232 : 0, /* properties_provided */
2233 : 0, /* properties_destroyed */
2234 : 0, /* todo_flags_start */
2235 : 0, /* todo_flags_finish */
2236 : };
2237 :
2238 : class pass_tail_calls : public gimple_opt_pass
2239 : {
2240 : public:
2241 302985 : pass_tail_calls (gcc::context *ctxt)
2242 605970 : : gimple_opt_pass (pass_data_tail_calls, ctxt)
2243 : {}
2244 :
2245 : /* opt_pass methods: */
2246 1068301 : bool gate (function *) final override { return gate_tail_calls (); }
2247 747974 : unsigned int execute (function *) final override
2248 : {
2249 747974 : return execute_tail_calls ();
2250 : }
2251 :
2252 : }; // class pass_tail_calls
2253 :
2254 : } // anon namespace
2255 :
2256 : gimple_opt_pass *
2257 302985 : make_pass_tail_calls (gcc::context *ctxt)
2258 : {
2259 302985 : return new pass_tail_calls (ctxt);
2260 : }
2261 :
2262 : namespace {
2263 :
2264 : const pass_data pass_data_musttail =
2265 : {
2266 : GIMPLE_PASS, /* type */
2267 : "musttail", /* name */
2268 : OPTGROUP_NONE, /* optinfo_flags */
2269 : TV_NONE, /* tv_id */
2270 : ( PROP_cfg | PROP_ssa ), /* properties_required */
2271 : 0, /* properties_provided */
2272 : 0, /* properties_destroyed */
2273 : 0, /* todo_flags_start */
2274 : 0, /* todo_flags_finish */
2275 : };
2276 :
2277 : class pass_musttail : public gimple_opt_pass
2278 : {
2279 : public:
2280 302985 : pass_musttail (gcc::context *ctxt)
2281 605970 : : gimple_opt_pass (pass_data_musttail, ctxt)
2282 : {}
2283 :
2284 : /* opt_pass methods: */
2285 : /* This pass is only used when the other tail call pass
2286 : doesn't run to make [[musttail]] still work. But only
2287 : run it when there is actually a musttail in the function. */
2288 1531899 : bool gate (function *f) final override
2289 : {
2290 1531899 : return f->has_musttail;
2291 : }
2292 240 : unsigned int execute (function *) final override
2293 : {
2294 240 : return tree_optimize_tail_calls_1 (true, true, true);
2295 : }
2296 :
2297 : }; // class pass_musttail
2298 :
2299 : } // anon namespace
2300 :
2301 : gimple_opt_pass *
2302 302985 : make_pass_musttail (gcc::context *ctxt)
2303 : {
2304 302985 : return new pass_musttail (ctxt);
2305 : }
|