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 1666452 : suitable_for_tail_opt_p (gcall *call, bool diag_musttail)
156 : {
157 1666452 : if (cfun->stdarg)
158 : {
159 18556 : maybe_error_musttail (call, _("caller uses stdargs"), diag_musttail);
160 18556 : 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 1647896 : 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 1647896 : if (cfun->calls_alloca)
177 : {
178 15869 : maybe_error_musttail (call, _("caller uses alloca"), diag_musttail);
179 15869 : 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 1632027 : if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ
186 1632027 : && 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 1632027 : 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 1631870 : 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 1631858 : if (diag_musttail
212 379920 : && gimple_call_must_tail_p (call)
213 1632615 : && warn_musttail_local_addr)
214 1742 : 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 530435 : single_non_eh_succ_edge (basic_block bb)
252 : {
253 530435 : edge e, ret = NULL;
254 530435 : edge_iterator ei;
255 1061037 : FOR_EACH_EDGE (e, ei, bb->succs)
256 530602 : if ((e->flags & EDGE_EH) == 0)
257 : {
258 530435 : gcc_assert (ret == NULL);
259 : ret = e;
260 : }
261 530435 : gcc_assert (ret);
262 530435 : 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 24967 : independent_of_stmt_p (tree expr, gimple *at, gimple_stmt_iterator gsi,
273 : bitmap to_move)
274 : {
275 24967 : basic_block bb, call_bb, at_bb;
276 24967 : edge e;
277 24967 : edge_iterator ei;
278 :
279 24967 : if (is_gimple_min_invariant (expr))
280 : return expr;
281 :
282 6563 : if (TREE_CODE (expr) != SSA_NAME)
283 : return NULL_TREE;
284 :
285 6563 : 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 6542 : at_bb = gimple_bb (at);
290 6542 : call_bb = gimple_bb (gsi_stmt (gsi));
291 7659 : for (bb = call_bb; bb != at_bb; bb = single_non_eh_succ_edge (bb)->dest)
292 1117 : bb->aux = &bb->aux;
293 6542 : bb->aux = &bb->aux;
294 :
295 6649 : while (1)
296 : {
297 6649 : at = SSA_NAME_DEF_STMT (expr);
298 6649 : bb = gimple_bb (at);
299 :
300 : /* The default definition or defined before the chain. */
301 6649 : if (!bb || !bb->aux)
302 : break;
303 :
304 3191 : if (bb == call_bb)
305 : {
306 19134 : for (; !gsi_end_p (gsi); gsi_next (&gsi))
307 16238 : if (gsi_stmt (gsi) == at)
308 : break;
309 :
310 3062 : if (!gsi_end_p (gsi))
311 166 : expr = NULL_TREE;
312 : break;
313 : }
314 :
315 129 : if (gimple_code (at) != GIMPLE_PHI)
316 : {
317 : expr = NULL_TREE;
318 : break;
319 : }
320 :
321 142 : FOR_EACH_EDGE (e, ei, bb->preds)
322 142 : if (e->src->aux)
323 : break;
324 129 : gcc_assert (e);
325 :
326 129 : expr = PHI_ARG_DEF_FROM_EDGE (at, e);
327 129 : if (TREE_CODE (expr) != SSA_NAME)
328 : {
329 : /* The value is a constant. */
330 : break;
331 : }
332 : }
333 :
334 : /* Unmark the blocks. */
335 7659 : for (bb = call_bb; bb != at_bb; bb = single_non_eh_succ_edge (bb)->dest)
336 1117 : bb->aux = NULL;
337 6542 : bb->aux = NULL;
338 :
339 6542 : 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 118133 : process_assignment (gassign *stmt,
350 : gimple_stmt_iterator call, tree *m,
351 : tree *a, tree *ass_var, bitmap to_move)
352 : {
353 118133 : tree op0, op1 = NULL_TREE, non_ass_var = NULL_TREE;
354 118133 : tree dest = gimple_assign_lhs (stmt);
355 118133 : enum tree_code code = gimple_assign_rhs_code (stmt);
356 118133 : enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
357 118133 : 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 57639 : if ((rhs_class == GIMPLE_SINGLE_RHS || gimple_assign_cast_p (stmt))
364 139723 : && src_var == *ass_var)
365 : {
366 : /* Reject a tailcall if the type conversion might need
367 : additional code. */
368 21615 : if (gimple_assign_cast_p (stmt))
369 : {
370 19380 : 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 17304 : if (BITINT_TYPE_P (TREE_TYPE (dest))
378 8652 : != BITINT_TYPE_P (TREE_TYPE (src_var)))
379 : return FAIL;
380 :
381 8601 : 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 17080 : else if (INTEGRAL_TYPE_P (TREE_TYPE (dest))
412 16577 : && !type_has_mode_precision_p (TREE_TYPE (dest)))
413 : return FAIL;
414 : }
415 :
416 10761 : *ass_var = dest;
417 10761 : return OK;
418 : }
419 :
420 96518 : switch (rhs_class)
421 : {
422 34096 : case GIMPLE_BINARY_RHS:
423 34096 : op1 = gimple_assign_rhs2 (stmt);
424 :
425 : /* Fall through. */
426 :
427 37880 : case GIMPLE_UNARY_RHS:
428 37880 : op0 = gimple_assign_rhs1 (stmt);
429 37880 : 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 37880 : if (!flag_associative_math)
439 37523 : 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 33608 : if (FIXED_POINT_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
444 : return FAIL;
445 :
446 33608 : if (rhs_class == GIMPLE_UNARY_RHS
447 3424 : && op0 == *ass_var)
448 : ;
449 32386 : else if (op0 == *ass_var
450 32386 : && (non_ass_var = independent_of_stmt_p (op1, stmt, call,
451 : to_move)))
452 : ;
453 13846 : else if (*ass_var
454 6660 : && op1 == *ass_var
455 20135 : && (non_ass_var = independent_of_stmt_p (op0, stmt, call,
456 : to_move)))
457 : ;
458 : else
459 : return TRY_MOVE;
460 :
461 25969 : switch (code)
462 : {
463 6854 : case PLUS_EXPR:
464 6854 : *a = non_ass_var;
465 6854 : *ass_var = dest;
466 6854 : return OK;
467 :
468 511 : case POINTER_PLUS_EXPR:
469 511 : if (op0 != *ass_var)
470 : return FAIL;
471 74 : *a = non_ass_var;
472 74 : *ass_var = dest;
473 74 : return OK;
474 :
475 431 : case MULT_EXPR:
476 431 : *m = non_ass_var;
477 431 : *ass_var = dest;
478 431 : 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 1422 : case MINUS_EXPR:
486 1422 : if (*ass_var == op0)
487 153 : *a = fold_build1 (NEGATE_EXPR, TREE_TYPE (non_ass_var), non_ass_var);
488 : else
489 : {
490 1269 : *m = build_minus_one_cst (TREE_TYPE (non_ass_var));
491 1269 : *a = fold_build1 (NEGATE_EXPR, TREE_TYPE (non_ass_var), non_ass_var);
492 : }
493 :
494 1422 : *ass_var = dest;
495 1422 : return OK;
496 :
497 : default:
498 : return FAIL;
499 : }
500 : }
501 :
502 : /* Propagate VAR through phis on edge E. */
503 :
504 : static tree
505 526582 : propagate_through_phis (tree var, edge e)
506 : {
507 526582 : basic_block dest = e->dest;
508 526582 : gphi_iterator gsi;
509 :
510 996345 : for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
511 : {
512 542313 : gphi *phi = gsi.phi ();
513 542313 : if (PHI_ARG_DEF_FROM_EDGE (phi, e) == var)
514 72550 : 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 807138 : maybe_error_musttail (gcall *call, const char *err, bool diag_musttail)
525 : {
526 807138 : 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 807138 : 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 807138 : }
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 7393342 : 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 7393342 : tree ass_var = NULL_TREE, ret_var, func, param;
617 7393342 : gimple *stmt;
618 7393342 : gcall *call = NULL;
619 7393342 : gimple_stmt_iterator gsi, agsi;
620 7393342 : bool tail_recursion;
621 7393342 : struct tailcall *nw;
622 7393342 : edge e;
623 7393342 : tree m, a;
624 7393342 : basic_block abb;
625 7393342 : size_t idx;
626 7393342 : tree var;
627 7393342 : bool only_tailr = false;
628 7393342 : bool has_tsan_func_exit = false;
629 7393342 : int eh_has_tsan_func_exit = -1;
630 7393342 : bool delete_ignored_edges = false;
631 :
632 7393342 : if (!single_succ_p (bb)
633 7393342 : && (EDGE_COUNT (bb->succs) || !cfun->has_musttail || !diag_musttail))
634 : {
635 1814157 : if (EDGE_COUNT (bb->succs) == 2
636 1776538 : && esucc
637 1776538 : && 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 1817248 : && gimple_code (stmt) == GIMPLE_COND)
643 : ;
644 1811066 : else if (esucc
645 1811066 : && cfun->has_musttail
646 358 : && diag_musttail
647 249 : && (stmt = last_nondebug_stmt (bb))
648 1811315 : && 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 1811040 : else if (!has_abnormal_or_eh_outgoing_edge_p (bb))
654 : {
655 1786552 : if (dump_file)
656 109 : fprintf (dump_file, "Basic block %d has extra exit edges\n",
657 : bb->index);
658 6483633 : return;
659 : }
660 27605 : if (!cfun->has_musttail)
661 : return;
662 : }
663 :
664 5582531 : bool bad_stmt = false;
665 5582531 : gimple *last_stmt = nullptr;
666 24686749 : for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
667 : {
668 16424988 : stmt = gsi_stmt (gsi);
669 :
670 : /* Ignore labels, returns, nops, clobbers and debug stmts. */
671 16424988 : 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 12432929 : || gimple_clobber_p (stmt)
676 11390379 : || is_gimple_debug (stmt))
677 12476667 : continue;
678 :
679 3948321 : if (cfun->has_musttail
680 6000 : && sanitize_flags_p (SANITIZE_THREAD)
681 44 : && gimple_call_builtin_p (stmt, BUILT_IN_TSAN_FUNC_EXIT)
682 3948337 : && 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 3948757 : if (cfun->has_musttail
691 5992 : && sanitize_flags_p (SANITIZE_ADDRESS)
692 4063 : && asan_mark_p (stmt, ASAN_MARK_POISON)
693 3949033 : && 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 3947869 : if (cfun->has_musttail
719 : && diag_musttail
720 5548 : && esucc
721 4504 : && 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 3950960 : && 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 : if (is_gimple_assign (SSA_NAME_DEF_STMT (lhs))
739 2995 : && (gimple_assign_rhs_code (SSA_NAME_DEF_STMT (lhs))
740 : == INTEGER_CST))
741 : {
742 0 : tree lhsv = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (lhs));
743 :
744 0 : if (const_binop (ccode, boolean_type_node, lhsv, rhsv)
745 0 : == boolean_true_node)
746 0 : continue;
747 : }
748 2995 : else if (gimple_code (SSA_NAME_DEF_STMT (lhs)) == GIMPLE_PHI)
749 : {
750 2556 : gimple *phi = SSA_NAME_DEF_STMT (lhs);
751 2556 : basic_block pbb = gimple_bb (phi);
752 2556 : must_see_bbs->add (pbb);
753 2556 : edge_iterator ei;
754 93238 : FOR_EACH_EDGE (e, ei, pbb->preds)
755 : {
756 90682 : tree lhsv = gimple_phi_arg_def_from_edge (phi, e);
757 90682 : if (TREE_CODE (lhsv) == SSA_NAME
758 90682 : && is_gimple_assign (SSA_NAME_DEF_STMT (lhsv))
759 181364 : && (gimple_assign_rhs_code (SSA_NAME_DEF_STMT (lhsv))
760 : == INTEGER_CST))
761 90680 : lhsv = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (lhsv));
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 3945313 : if (cfun->has_musttail
771 : && diag_musttail
772 2992 : && esucc
773 1948 : && gimple_code (stmt) == GIMPLE_SWITCH
774 3945339 : && (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 : if (is_gimple_assign (SSA_NAME_DEF_STMT (idx))
786 26 : && (gimple_assign_rhs_code (SSA_NAME_DEF_STMT (idx))
787 : == INTEGER_CST))
788 : {
789 0 : tree val = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (idx));
790 0 : if (find_taken_edge_switch_expr (swtch, val) == esucc)
791 0 : continue;
792 : }
793 26 : else if (gimple_code (SSA_NAME_DEF_STMT (idx)) == GIMPLE_PHI)
794 : {
795 4 : gimple *phi = SSA_NAME_DEF_STMT (idx);
796 4 : basic_block pbb = gimple_bb (phi);
797 4 : must_see_bbs->add (pbb);
798 4 : edge_iterator ei;
799 148 : FOR_EACH_EDGE (e, ei, pbb->preds)
800 : {
801 144 : tree val = gimple_phi_arg_def_from_edge (phi, e);
802 144 : if (TREE_CODE (val) == SSA_NAME
803 144 : && is_gimple_assign (SSA_NAME_DEF_STMT (val))
804 288 : && (gimple_assign_rhs_code (SSA_NAME_DEF_STMT (val))
805 : == INTEGER_CST))
806 144 : val = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val));
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 3945309 : if (!last_stmt)
816 3266793 : last_stmt = stmt;
817 : /* Check for a call. */
818 3945309 : if (is_gimple_call (stmt))
819 : {
820 1666526 : call = as_a <gcall *> (stmt);
821 : /* Handle only musttail calls when not optimizing. */
822 1666526 : if (only_musttail && !gimple_call_must_tail_p (call))
823 : {
824 3916079 : maybe_delete_ignored_edges:
825 3916079 : if (delete_ignored_edges)
826 : {
827 356 : delete ignored_edges;
828 356 : delete must_see_bbs;
829 : }
830 : return;
831 : }
832 1666476 : 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 1666452 : ass_var = gimple_call_lhs (call);
839 1666452 : break;
840 : }
841 :
842 : /* Allow simple copies between local variables, even if they're
843 : aggregates. */
844 2278783 : if (is_gimple_assign (stmt)
845 2255883 : && auto_var_in_fn_p (gimple_assign_lhs (stmt), cfun->decl)
846 2336678 : && auto_var_in_fn_p (gimple_assign_rhs1 (stmt), cfun->decl))
847 24396 : continue;
848 :
849 : /* If the statement references memory or volatile operands, fail. */
850 2254387 : if (gimple_references_memory_p (stmt)
851 14538595 : || gimple_has_volatile_ops (stmt))
852 : {
853 1236905 : if (dump_file)
854 : {
855 26 : fprintf (dump_file, "Cannot handle ");
856 26 : print_gimple_stmt (dump_file, stmt, 0);
857 : }
858 1236905 : bad_stmt = true;
859 1236905 : if (!cfun->has_musttail)
860 : break;
861 : }
862 : }
863 :
864 5582457 : if (bad_stmt)
865 1236841 : goto maybe_delete_ignored_edges;
866 :
867 4345616 : if (gsi_end_p (gsi))
868 : {
869 2679164 : if (must_see_bbs)
870 3078 : must_see_bbs->remove (bb);
871 :
872 2679164 : edge_iterator ei;
873 : /* Recurse to the predecessors. */
874 6447330 : FOR_EACH_EDGE (e, ei, bb->preds)
875 : {
876 3768166 : if (ignored_edges && ignored_edges->contains (e))
877 2700 : continue;
878 3765466 : 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 2679164 : goto maybe_delete_ignored_edges;
884 : }
885 :
886 1666452 : if (must_see_bbs && !must_see_bbs->is_empty ())
887 0 : goto maybe_delete_ignored_edges;
888 :
889 1666452 : if (delete_ignored_edges)
890 : {
891 14 : delete ignored_edges;
892 14 : delete must_see_bbs;
893 : }
894 :
895 1666452 : if (!suitable_for_tail_opt_p (call, diag_musttail))
896 : return;
897 :
898 1647896 : if (!suitable_for_tail_call_opt_p (call, diag_musttail))
899 16038 : 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 1647896 : if (!diag_musttail || !gimple_call_must_tail_p (call))
904 1647139 : for (param = DECL_ARGUMENTS (current_function_decl);
905 4853820 : param; param = DECL_CHAIN (param))
906 3206681 : if (TREE_ADDRESSABLE (param))
907 : {
908 34412 : 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 34412 : if (cfun->has_musttail)
915 : only_tailr = true;
916 : else
917 34340 : 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 1647896 : if (ass_var
932 463449 : && !is_gimple_reg (ass_var)
933 1700455 : && !auto_var_in_fn_p (ass_var, cfun->decl))
934 : {
935 9560 : maybe_error_musttail (call, _("return value in memory"), diag_musttail);
936 9560 : return;
937 : }
938 :
939 1638336 : 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 1638159 : if ((stmt_could_throw_p (cfun, stmt)
948 1638159 : && !stmt_can_throw_external (cfun, stmt)) || EDGE_COUNT (bb->succs) > 1)
949 : {
950 19742 : if (stmt != last_stmt)
951 : {
952 165 : maybe_error_musttail (call, _("code between call and return"),
953 : diag_musttail);
954 19742 : return;
955 : }
956 :
957 19577 : edge e;
958 19577 : edge_iterator ei;
959 38964 : FOR_EACH_EDGE (e, ei, bb->succs)
960 19595 : if (e->flags & EDGE_EH)
961 : break;
962 :
963 19577 : if (!e)
964 : {
965 19369 : maybe_error_musttail (call, _("call may throw exception that does not "
966 : "propagate"), diag_musttail);
967 19369 : 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 1618582 : tree result_decl = DECL_RESULT (cfun->decl);
1009 1618582 : if (result_decl
1010 1618582 : && may_be_aliased (result_decl)
1011 1625087 : && ref_maybe_used_by_stmt_p (call, result_decl, false))
1012 : {
1013 1508 : maybe_error_musttail (call, _("return value used after call"),
1014 : diag_musttail);
1015 1508 : return;
1016 : }
1017 :
1018 : /* We found the call, check whether it is suitable. */
1019 1617074 : tail_recursion = false;
1020 1617074 : func = gimple_call_fndecl (call);
1021 1617074 : if (func
1022 1537954 : && !fndecl_built_in_p (func)
1023 1187410 : && recursive_call_p (current_function_decl, func)
1024 1620129 : && !only_musttail)
1025 : {
1026 3032 : tree arg;
1027 :
1028 3032 : for (param = DECL_ARGUMENTS (current_function_decl), idx = 0;
1029 11414 : param && idx < gimple_call_num_args (call);
1030 8382 : param = DECL_CHAIN (param), idx ++)
1031 : {
1032 8537 : arg = gimple_call_arg (call, idx);
1033 8537 : 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 8309 : if (TREE_ADDRESSABLE (TREE_TYPE (param))
1040 16618 : || !useless_type_conversion_p (TREE_TYPE (param),
1041 8309 : TREE_TYPE (arg)))
1042 : break;
1043 :
1044 15803 : if (is_gimple_reg_type (TREE_TYPE (param))
1045 8273 : ? !is_gimple_reg (param)
1046 743 : : (!is_gimple_variable (param)
1047 743 : || TREE_THIS_VOLATILE (param)
1048 1486 : || may_be_aliased (param)))
1049 : break;
1050 : }
1051 : }
1052 3032 : if (idx == gimple_call_num_args (call) && !param)
1053 1617074 : tail_recursion = true;
1054 : }
1055 :
1056 1617074 : if (only_tailr && !tail_recursion)
1057 : return;
1058 :
1059 : /* Compute live vars if not computed yet. */
1060 1617002 : if (live_vars == NULL)
1061 : {
1062 1567455 : unsigned int cnt = 0;
1063 13731886 : FOR_EACH_LOCAL_DECL (cfun, idx, var)
1064 10803945 : if (VAR_P (var)
1065 10803945 : && auto_var_in_fn_p (var, cfun->decl)
1066 21482945 : && may_be_aliased (var))
1067 : {
1068 1002749 : if (live_vars == NULL)
1069 240657 : live_vars = new live_vars_map;
1070 1002749 : live_vars->put (DECL_UID (var), cnt++);
1071 : }
1072 1567455 : if (live_vars)
1073 240657 : 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 1617002 : bitmap local_live_vars = NULL;
1079 1617002 : if (live_vars)
1080 290204 : 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 13879171 : FOR_EACH_LOCAL_DECL (cfun, idx, var)
1086 : {
1087 11075216 : if (TREE_CODE (var) != PARM_DECL
1088 11075216 : && auto_var_in_fn_p (var, cfun->decl)
1089 10997721 : && may_be_aliased (var)
1090 11904586 : && (ref_maybe_used_by_stmt_p (call, var, false)
1091 382622 : || call_may_clobber_ref_p (call, var, false)))
1092 : {
1093 491492 : 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 491492 : unsigned int *v = live_vars->get (DECL_UID (var));
1117 491492 : if (bitmap_bit_p (local_live_vars, *v))
1118 : {
1119 223552 : 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 223080 : BITMAP_FREE (local_live_vars);
1138 223080 : maybe_error_musttail (call, _("call invocation refers to "
1139 : "locals"), diag_musttail);
1140 223080 : return;
1141 : }
1142 : }
1143 : }
1144 : }
1145 1393922 : if (diag_musttail
1146 321061 : && gimple_call_must_tail_p (call)
1147 1394670 : && !warning_suppressed_p (call, OPT_Wmaybe_musttail_local_addr))
1148 406 : for (tree param = DECL_ARGUMENTS (current_function_decl);
1149 806 : 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 1393922 : if (local_live_vars)
1163 67124 : 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 1393922 : m = NULL_TREE;
1170 1393922 : a = NULL_TREE;
1171 2303631 : auto_bitmap to_move_defs;
1172 2303631 : auto_vec<gimple *> to_move_stmts;
1173 1393922 : bool is_noreturn = gimple_call_noreturn_p (call);
1174 2303631 : auto_vec<edge> edges;
1175 :
1176 1393922 : abb = bb;
1177 1393922 : agsi = gsi;
1178 3427799 : while (!is_noreturn)
1179 : {
1180 3427127 : tree tmp_a = NULL_TREE;
1181 3427127 : tree tmp_m = NULL_TREE;
1182 3427127 : gsi_next (&agsi);
1183 :
1184 526582 : new_bb:
1185 3953709 : while (gsi_end_p (agsi))
1186 : {
1187 526422 : edge e = single_non_eh_succ_edge (abb);
1188 526422 : ass_var = propagate_through_phis (ass_var, e);
1189 526422 : if (!ass_var || ignored_edges)
1190 441077 : edges.safe_push (e);
1191 526422 : abb = e->dest;
1192 1052844 : agsi = gsi_start_bb (abb);
1193 : }
1194 :
1195 3427287 : stmt = gsi_stmt (agsi);
1196 3427287 : if (gimple_code (stmt) == GIMPLE_RETURN)
1197 : break;
1198 :
1199 4146604 : if (gimple_code (stmt) == GIMPLE_LABEL
1200 2102342 : || gimple_code (stmt) == GIMPLE_NOP
1201 2102342 : || gimple_code (stmt) == GIMPLE_PREDICT
1202 2082739 : || gimple_clobber_p (stmt)
1203 4022825 : || is_gimple_debug (stmt))
1204 2014196 : continue;
1205 :
1206 118922 : 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 118922 : && gimple_call_must_tail_p (call))
1212 : {
1213 8 : has_tsan_func_exit = true;
1214 8 : continue;
1215 : }
1216 :
1217 119202 : if (cfun->has_musttail
1218 735 : && sanitize_flags_p (SANITIZE_ADDRESS)
1219 483 : && asan_mark_p (stmt, ASAN_MARK_POISON)
1220 119202 : && 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 118610 : if (ignored_edges)
1227 : {
1228 186 : if (is_gimple_assign (stmt)
1229 26 : && gimple_assign_rhs_code (stmt) == INTEGER_CST
1230 206 : && tree_int_cst_sgn (gimple_assign_rhs1 (stmt)) >= 0)
1231 : {
1232 20 : use_operand_p use_p;
1233 20 : imm_use_iterator imm_iter;
1234 20 : bool bad_p = false;
1235 40 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter,
1236 : gimple_assign_lhs (stmt))
1237 : {
1238 20 : gimple *use_stmt = USE_STMT (use_p);
1239 20 : if (is_gimple_debug (use_stmt)
1240 20 : || gimple_code (use_stmt) == GIMPLE_COND
1241 40 : || gimple_code (use_stmt) == GIMPLE_SWITCH)
1242 0 : continue;
1243 20 : if (gimple_code (use_stmt) == GIMPLE_PHI)
1244 : {
1245 20 : use_operand_p use_p2;
1246 20 : imm_use_iterator imm_iter2;
1247 186 : FOR_EACH_IMM_USE_FAST (use_p2, imm_iter2,
1248 : gimple_phi_result (use_stmt))
1249 : {
1250 166 : gimple *use_stmt2 = USE_STMT (use_p2);
1251 166 : if (is_gimple_debug (use_stmt2)
1252 166 : || gimple_code (use_stmt2) == GIMPLE_COND
1253 168 : || gimple_code (use_stmt2) == GIMPLE_SWITCH)
1254 166 : continue;
1255 : bad_p = true;
1256 : break;
1257 20 : }
1258 20 : if (bad_p)
1259 : break;
1260 : }
1261 : else
1262 : {
1263 : bad_p = true;
1264 : break;
1265 : }
1266 20 : }
1267 20 : if (!bad_p)
1268 20 : continue;
1269 : }
1270 166 : if (gimple_code (stmt) == GIMPLE_COND
1271 158 : && TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
1272 158 : && TREE_CODE (gimple_cond_rhs (stmt)) == INTEGER_CST
1273 158 : && INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))
1274 324 : && tree_int_cst_sgn (gimple_cond_rhs (stmt)) >= 0)
1275 : {
1276 158 : edge e = NULL, et, ef;
1277 158 : enum tree_code ccode = gimple_cond_code (stmt);
1278 158 : tree lhs = gimple_cond_lhs (stmt);
1279 158 : tree rhsv = gimple_cond_rhs (stmt);
1280 158 : extract_true_false_edges_from_block (gimple_bb (stmt), &et, &ef);
1281 158 : if (is_gimple_assign (SSA_NAME_DEF_STMT (lhs))
1282 158 : && (gimple_assign_rhs_code (SSA_NAME_DEF_STMT (lhs))
1283 : == 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 (gimple *g = SSA_NAME_DEF_STMT (lhsv))
1302 158 : if (is_gimple_assign (g)
1303 158 : && gimple_assign_rhs_code (g) == INTEGER_CST)
1304 158 : lhsv = gimple_assign_rhs1 (g);
1305 158 : tree r = const_binop (ccode, boolean_type_node,
1306 : lhsv, rhsv);
1307 158 : if (r == boolean_true_node)
1308 6 : e = et;
1309 152 : else if (r == boolean_false_node)
1310 152 : e = ef;
1311 : break;
1312 : }
1313 : }
1314 158 : if (e)
1315 : {
1316 158 : ass_var = propagate_through_phis (ass_var, e);
1317 158 : if (!ass_var || ignored_edges)
1318 158 : edges.safe_push (e);
1319 158 : abb = e->dest;
1320 158 : agsi = gsi_start_bb (abb);
1321 158 : goto new_bb;
1322 : }
1323 : }
1324 8 : if (gimple_code (stmt) == GIMPLE_SWITCH
1325 8 : && (TREE_CODE (gimple_switch_index (as_a <gswitch *> (stmt)))
1326 : == SSA_NAME))
1327 : {
1328 2 : edge e = NULL;
1329 2 : gswitch *swtch = as_a <gswitch *> (stmt);
1330 2 : tree idx = gimple_switch_index (swtch);
1331 2 : if (is_gimple_assign (SSA_NAME_DEF_STMT (idx))
1332 2 : && (gimple_assign_rhs_code (SSA_NAME_DEF_STMT (idx))
1333 : == INTEGER_CST))
1334 : {
1335 0 : tree val = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (idx));
1336 0 : e = find_taken_edge_switch_expr (swtch, val);
1337 : }
1338 2 : else if (gimple_code (SSA_NAME_DEF_STMT (idx)) == GIMPLE_PHI)
1339 : {
1340 2 : gimple *phi = SSA_NAME_DEF_STMT (idx);
1341 2 : basic_block pbb = gimple_bb (phi);
1342 7 : for (edge e2 : edges)
1343 3 : if (e2->dest == pbb)
1344 : {
1345 2 : tree val = gimple_phi_arg_def_from_edge (phi, e2);
1346 2 : if (TREE_CODE (val) == SSA_NAME)
1347 2 : if (gimple *g = SSA_NAME_DEF_STMT (val))
1348 2 : if (is_gimple_assign (g)
1349 2 : && gimple_assign_rhs_code (g) == INTEGER_CST)
1350 2 : val = gimple_assign_rhs1 (g);
1351 2 : if (TREE_CODE (val) == INTEGER_CST)
1352 2 : e = find_taken_edge_switch_expr (swtch, val);
1353 : break;
1354 : }
1355 : }
1356 2 : if (e)
1357 : {
1358 2 : ass_var = propagate_through_phis (ass_var, e);
1359 2 : if (!ass_var || ignored_edges)
1360 2 : edges.safe_push (e);
1361 2 : abb = e->dest;
1362 2 : agsi = gsi_start_bb (abb);
1363 2 : goto new_bb;
1364 : }
1365 : }
1366 : }
1367 :
1368 118430 : if (gimple_code (stmt) != GIMPLE_ASSIGN)
1369 : {
1370 297 : maybe_error_musttail (call, _("unhandled code after call"),
1371 : diag_musttail);
1372 99019 : return;
1373 : }
1374 :
1375 : /* This is a gimple assign. */
1376 118133 : par ret = process_assignment (as_a <gassign *> (stmt), gsi,
1377 : &tmp_m, &tmp_a, &ass_var, to_move_defs);
1378 118133 : if (ret == FAIL || (ret == TRY_MOVE && !tail_recursion))
1379 : {
1380 98412 : maybe_error_musttail (call, _("return value changed after call"),
1381 : diag_musttail);
1382 98412 : return;
1383 : }
1384 19721 : else if (ret == TRY_MOVE)
1385 : {
1386 : /* Do not deal with checking dominance, the real fix is to
1387 : do path isolation for the transform phase anyway, removing
1388 : the need to compute the accumulators with new stmts. */
1389 40 : if (abb != bb)
1390 : return;
1391 81 : for (unsigned opno = 1; opno < gimple_num_ops (stmt); ++opno)
1392 : {
1393 54 : tree op = gimple_op (stmt, opno);
1394 54 : if (independent_of_stmt_p (op, stmt, gsi, to_move_defs) != op)
1395 : return;
1396 : }
1397 54 : bitmap_set_bit (to_move_defs,
1398 27 : SSA_NAME_VERSION (gimple_assign_lhs (stmt)));
1399 27 : to_move_stmts.safe_push (stmt);
1400 27 : continue;
1401 27 : }
1402 :
1403 19681 : if (tmp_a)
1404 : {
1405 8350 : tree type = TREE_TYPE (tmp_a);
1406 8350 : if (a)
1407 415 : a = fold_build2 (PLUS_EXPR, type, fold_convert (type, a), tmp_a);
1408 : else
1409 : a = tmp_a;
1410 : }
1411 19681 : if (tmp_m)
1412 : {
1413 1839 : tree type = TREE_TYPE (tmp_m);
1414 1839 : if (m)
1415 76 : m = fold_build2 (MULT_EXPR, type, fold_convert (type, m), tmp_m);
1416 : else
1417 : m = tmp_m;
1418 :
1419 1839 : if (a)
1420 1279 : a = fold_build2 (MULT_EXPR, type, fold_convert (type, a), tmp_m);
1421 : }
1422 : }
1423 :
1424 : /* See if this is a tail call we can handle. */
1425 1295200 : if (is_noreturn)
1426 : {
1427 672 : if (gimple_call_internal_p (call))
1428 : {
1429 656 : maybe_error_musttail (call, _("internal call"), diag_musttail);
1430 656 : return;
1431 : }
1432 16 : tree rettype = TREE_TYPE (TREE_TYPE (current_function_decl));
1433 16 : tree calltype = TREE_TYPE (gimple_call_fntype (call));
1434 16 : if (!VOID_TYPE_P (rettype)
1435 16 : && !useless_type_conversion_p (rettype, calltype))
1436 : {
1437 0 : maybe_error_musttail (call, _("call and return value are different"),
1438 : diag_musttail);
1439 0 : return;
1440 : }
1441 : ret_var = NULL_TREE;
1442 : }
1443 : else
1444 1294528 : ret_var = gimple_return_retval (as_a <greturn *> (stmt));
1445 :
1446 : /* We may proceed if there either is no return value, or the return value
1447 : is identical to the call's return or if the return decl is an empty type
1448 : variable and the call's return was not assigned. */
1449 1294528 : if (ret_var
1450 1294528 : && (ret_var != ass_var
1451 385944 : && !(is_empty_type (TREE_TYPE (ret_var)) && !ass_var)))
1452 : {
1453 384889 : bool ok = false;
1454 384889 : value_range val;
1455 384889 : if (ass_var == NULL_TREE && !tail_recursion)
1456 : {
1457 384777 : tree other_value = NULL_TREE;
1458 : /* If we have a function call that we know the return value is the same
1459 : as the argument, try the argument too. */
1460 384777 : int flags = gimple_call_return_flags (call);
1461 384777 : if ((flags & ERF_RETURNS_ARG) != 0
1462 384777 : && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (call))
1463 : {
1464 4506 : tree arg = gimple_call_arg (call, flags & ERF_RETURN_ARG_MASK);
1465 4506 : if (useless_type_conversion_p (TREE_TYPE (ret_var), TREE_TYPE (arg) ))
1466 : other_value = arg;
1467 : }
1468 : /* If IPA-VRP proves called function always returns a singleton range,
1469 : the return value is replaced by the only value in that range.
1470 : For tail call purposes, pretend such replacement didn't happen. */
1471 380271 : else if (tree type = gimple_range_type (call))
1472 84171 : if (tree callee = gimple_call_fndecl (call))
1473 : {
1474 83723 : tree valr;
1475 83723 : if ((INTEGRAL_TYPE_P (type)
1476 : || SCALAR_FLOAT_TYPE_P (type)
1477 83723 : || POINTER_TYPE_P (type))
1478 83723 : && useless_type_conversion_p (TREE_TYPE (TREE_TYPE (callee)),
1479 : type)
1480 83715 : && useless_type_conversion_p (TREE_TYPE (ret_var), type)
1481 22672 : && ipa_return_value_range (val, callee)
1482 94142 : && val.singleton_p (&valr))
1483 7199 : other_value = valr;
1484 : }
1485 :
1486 86429 : if (other_value)
1487 : {
1488 9905 : tree rv = ret_var;
1489 9905 : unsigned int i = edges.length ();
1490 : /* If ret_var is equal to other_value, we can tail optimize. */
1491 9905 : if (operand_equal_p (ret_var, other_value, 0))
1492 : ok = true;
1493 : else
1494 : /* Otherwise, if ret_var is a PHI result, try to find out
1495 : if other_value isn't propagated through PHIs on the path from
1496 : call's bb to SSA_NAME_DEF_STMT (ret_var)'s bb. */
1497 4069 : while (TREE_CODE (rv) == SSA_NAME
1498 4069 : && gimple_code (SSA_NAME_DEF_STMT (rv)) == GIMPLE_PHI)
1499 : {
1500 1596 : tree nrv = NULL_TREE;
1501 : gimple *g = SSA_NAME_DEF_STMT (rv);
1502 1596 : for (; i; --i)
1503 : {
1504 1560 : if (edges[i - 1]->dest == gimple_bb (g))
1505 : {
1506 1560 : nrv = gimple_phi_arg_def_from_edge (g,
1507 1560 : edges[i - 1]);
1508 1560 : --i;
1509 1560 : break;
1510 : }
1511 : }
1512 1596 : if (nrv == NULL_TREE)
1513 : break;
1514 1560 : if (operand_equal_p (nrv, other_value, 0))
1515 : {
1516 : ok = true;
1517 : break;
1518 : }
1519 : rv = nrv;
1520 : }
1521 : }
1522 : }
1523 2897 : if (!ok)
1524 : {
1525 377493 : maybe_error_musttail (call, _("call and return value are different"),
1526 : diag_musttail);
1527 377493 : return;
1528 : }
1529 384889 : }
1530 :
1531 : /* If this is not a tail recursive call, we cannot handle addends or
1532 : multiplicands. */
1533 917051 : if (!tail_recursion && (m || a))
1534 : {
1535 7342 : maybe_error_musttail (call, _("operations after non tail recursive "
1536 : "call"), diag_musttail);
1537 7342 : return;
1538 : }
1539 :
1540 : /* For pointers only allow additions. */
1541 1789 : if (m && POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
1542 : {
1543 0 : maybe_error_musttail (call, _("tail recursion with pointers can only "
1544 : "use additions"), diag_musttail);
1545 0 : return;
1546 : }
1547 :
1548 909709 : if (eh_has_tsan_func_exit != -1
1549 159 : && eh_has_tsan_func_exit != has_tsan_func_exit)
1550 : {
1551 0 : if (eh_has_tsan_func_exit)
1552 0 : maybe_error_musttail (call, _("call may throw exception caught "
1553 : "locally or perform cleanups"),
1554 : diag_musttail);
1555 : else
1556 0 : maybe_error_musttail (call, _("exception cleanups omit "
1557 : "__tsan_func_exit call"), diag_musttail);
1558 : return;
1559 : }
1560 :
1561 : /* Move queued defs. */
1562 909709 : if (tail_recursion)
1563 : {
1564 : unsigned i;
1565 1795 : FOR_EACH_VEC_ELT (to_move_stmts, i, stmt)
1566 : {
1567 3 : gimple_stmt_iterator mgsi = gsi_for_stmt (stmt);
1568 3 : gsi_move_before (&mgsi, &gsi);
1569 : }
1570 1789 : if (!tailr_arg_needs_copy)
1571 1329 : tailr_arg_needs_copy = BITMAP_ALLOC (NULL);
1572 1789 : for (param = DECL_ARGUMENTS (current_function_decl), idx = 0;
1573 6470 : param;
1574 4681 : param = DECL_CHAIN (param), idx++)
1575 : {
1576 4681 : tree ddef, arg = gimple_call_arg (call, idx);
1577 4681 : if (!is_gimple_reg (param)
1578 4681 : || ((ddef = ssa_default_def (cfun, param))
1579 4121 : && arg != ddef))
1580 2972 : bitmap_set_bit (tailr_arg_needs_copy, idx);
1581 : }
1582 : }
1583 :
1584 909709 : nw = XNEW (struct tailcall);
1585 :
1586 909709 : nw->call_gsi = gsi;
1587 :
1588 909709 : nw->tail_recursion = tail_recursion;
1589 909709 : nw->has_tsan_func_exit = has_tsan_func_exit;
1590 :
1591 909709 : nw->mult = m;
1592 909709 : nw->add = a;
1593 :
1594 909709 : nw->next = *ret;
1595 909709 : *ret = nw;
1596 : }
1597 :
1598 : /* Helper to insert PHI_ARGH to the phi of VAR in the destination of edge E. */
1599 :
1600 : static void
1601 240 : add_successor_phi_arg (edge e, tree var, tree phi_arg)
1602 : {
1603 240 : gphi_iterator gsi;
1604 :
1605 529 : for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
1606 529 : if (PHI_RESULT (gsi.phi ()) == var)
1607 : break;
1608 :
1609 240 : gcc_assert (!gsi_end_p (gsi));
1610 240 : add_phi_arg (gsi.phi (), phi_arg, e, UNKNOWN_LOCATION);
1611 240 : }
1612 :
1613 : /* Creates a GIMPLE statement which computes the operation specified by
1614 : CODE, ACC and OP1 to a new variable with name LABEL and inserts the
1615 : statement in the position specified by GSI. Returns the
1616 : tree node of the statement's result. */
1617 :
1618 : static tree
1619 197 : adjust_return_value_with_ops (enum tree_code code, const char *label,
1620 : tree acc, tree op1, gimple_stmt_iterator gsi)
1621 : {
1622 :
1623 197 : tree ret_type = TREE_TYPE (DECL_RESULT (current_function_decl));
1624 197 : tree result = make_temp_ssa_name (ret_type, NULL, label);
1625 197 : gassign *stmt;
1626 :
1627 197 : if (POINTER_TYPE_P (ret_type))
1628 : {
1629 6 : gcc_assert (code == PLUS_EXPR && TREE_TYPE (acc) == sizetype);
1630 : code = POINTER_PLUS_EXPR;
1631 : }
1632 197 : if (types_compatible_p (TREE_TYPE (acc), TREE_TYPE (op1))
1633 197 : && code != POINTER_PLUS_EXPR)
1634 186 : stmt = gimple_build_assign (result, code, acc, op1);
1635 : else
1636 : {
1637 11 : tree tem;
1638 11 : if (code == POINTER_PLUS_EXPR)
1639 6 : tem = fold_build2 (code, TREE_TYPE (op1), op1, acc);
1640 : else
1641 5 : tem = fold_build2 (code, TREE_TYPE (op1),
1642 : fold_convert (TREE_TYPE (op1), acc), op1);
1643 11 : tree rhs = fold_convert (ret_type, tem);
1644 11 : rhs = force_gimple_operand_gsi (&gsi, rhs,
1645 : false, NULL, true, GSI_SAME_STMT);
1646 11 : stmt = gimple_build_assign (result, rhs);
1647 : }
1648 :
1649 197 : gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
1650 197 : return result;
1651 : }
1652 :
1653 : /* Creates a new GIMPLE statement that adjusts the value of accumulator ACC by
1654 : the computation specified by CODE and OP1 and insert the statement
1655 : at the position specified by GSI as a new statement. Returns new SSA name
1656 : of updated accumulator. */
1657 :
1658 : static tree
1659 237 : update_accumulator_with_ops (enum tree_code code, tree acc, tree op1,
1660 : gimple_stmt_iterator gsi)
1661 : {
1662 237 : gassign *stmt;
1663 237 : tree var = copy_ssa_name (acc);
1664 237 : if (types_compatible_p (TREE_TYPE (acc), TREE_TYPE (op1)))
1665 223 : stmt = gimple_build_assign (var, code, acc, op1);
1666 : else
1667 : {
1668 14 : tree rhs = fold_convert (TREE_TYPE (acc),
1669 : fold_build2 (code,
1670 : TREE_TYPE (op1),
1671 : fold_convert (TREE_TYPE (op1), acc),
1672 : op1));
1673 14 : rhs = force_gimple_operand_gsi (&gsi, rhs,
1674 : false, NULL, false, GSI_CONTINUE_LINKING);
1675 14 : stmt = gimple_build_assign (var, rhs);
1676 : }
1677 237 : gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
1678 237 : return var;
1679 : }
1680 :
1681 : /* Adjust the accumulator values according to A and M after GSI, and update
1682 : the phi nodes on edge BACK. */
1683 :
1684 : static void
1685 1783 : adjust_accumulator_values (gimple_stmt_iterator gsi, tree m, tree a, edge back)
1686 : {
1687 1783 : tree var, a_acc_arg, m_acc_arg;
1688 :
1689 1783 : if (m)
1690 135 : m = force_gimple_operand_gsi (&gsi, m, true, NULL, true, GSI_SAME_STMT);
1691 1783 : if (a)
1692 102 : a = force_gimple_operand_gsi (&gsi, a, true, NULL, true, GSI_SAME_STMT);
1693 :
1694 1783 : a_acc_arg = a_acc;
1695 1783 : m_acc_arg = m_acc;
1696 1783 : if (a)
1697 : {
1698 102 : if (m_acc)
1699 : {
1700 18 : if (integer_onep (a))
1701 1 : var = m_acc;
1702 : else
1703 17 : var = adjust_return_value_with_ops (MULT_EXPR, "acc_tmp", m_acc,
1704 : a, gsi);
1705 : }
1706 : else
1707 : var = a;
1708 :
1709 102 : a_acc_arg = update_accumulator_with_ops (PLUS_EXPR, a_acc, var, gsi);
1710 : }
1711 :
1712 1783 : if (m)
1713 135 : m_acc_arg = update_accumulator_with_ops (MULT_EXPR, m_acc, m, gsi);
1714 :
1715 1783 : if (a_acc)
1716 105 : add_successor_phi_arg (back, a_acc, a_acc_arg);
1717 :
1718 1783 : if (m_acc)
1719 135 : add_successor_phi_arg (back, m_acc, m_acc_arg);
1720 1783 : }
1721 :
1722 : /* Adjust value of the return at the end of BB according to M and A
1723 : accumulators. */
1724 :
1725 : static void
1726 167 : adjust_return_value (basic_block bb, tree m, tree a)
1727 : {
1728 167 : tree retval;
1729 334 : greturn *ret_stmt = as_a <greturn *> (gimple_seq_last_stmt (bb_seq (bb)));
1730 167 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
1731 :
1732 167 : gcc_assert (gimple_code (ret_stmt) == GIMPLE_RETURN);
1733 :
1734 167 : retval = gimple_return_retval (ret_stmt);
1735 167 : if (!retval || retval == error_mark_node)
1736 0 : return;
1737 :
1738 167 : if (m)
1739 97 : retval = adjust_return_value_with_ops (MULT_EXPR, "mul_tmp", m_acc, retval,
1740 : gsi);
1741 167 : if (a)
1742 83 : retval = adjust_return_value_with_ops (PLUS_EXPR, "acc_tmp", a_acc, retval,
1743 : gsi);
1744 167 : gimple_return_set_retval (ret_stmt, retval);
1745 167 : update_stmt (ret_stmt);
1746 : }
1747 :
1748 : /* Subtract COUNT and FREQUENCY from the basic block and it's
1749 : outgoing edge. */
1750 : static void
1751 5082 : decrease_profile (basic_block bb, profile_count count)
1752 : {
1753 0 : bb->count = bb->count - count;
1754 0 : }
1755 :
1756 : /* Eliminates tail call described by T. TMP_VARS is a list of
1757 : temporary variables used to copy the function arguments.
1758 : Allocates *NEW_LOOP if not already done and initializes it. */
1759 :
1760 : static void
1761 1783 : eliminate_tail_call (struct tailcall *t, class loop *&new_loop)
1762 : {
1763 1783 : tree param, rslt;
1764 1783 : gimple *stmt, *call;
1765 1783 : tree arg;
1766 1783 : size_t idx;
1767 1783 : basic_block bb, first;
1768 1783 : edge e;
1769 1783 : gphi *phi;
1770 1783 : gphi_iterator gpi;
1771 1783 : gimple_stmt_iterator gsi;
1772 1783 : gimple *orig_stmt;
1773 :
1774 1783 : stmt = orig_stmt = gsi_stmt (t->call_gsi);
1775 1783 : bb = gsi_bb (t->call_gsi);
1776 :
1777 1783 : if (dump_file && (dump_flags & TDF_DETAILS))
1778 : {
1779 8 : fprintf (dump_file, "Eliminated tail recursion in bb %d : ",
1780 : bb->index);
1781 8 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1782 8 : fprintf (dump_file, "\n");
1783 : }
1784 :
1785 1783 : gcc_assert (is_gimple_call (stmt));
1786 :
1787 1783 : first = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1788 :
1789 : /* Remove the code after call_gsi that will become unreachable. The
1790 : possibly unreachable code in other blocks is removed later in
1791 : cfg cleanup. */
1792 1783 : gsi = t->call_gsi;
1793 1783 : gimple_stmt_iterator gsi2 = gsi_last_bb (gimple_bb (gsi_stmt (gsi)));
1794 3129 : while (gsi_stmt (gsi2) != gsi_stmt (gsi))
1795 : {
1796 1346 : gimple *t = gsi_stmt (gsi2);
1797 : /* Do not remove the return statement, so that redirect_edge_and_branch
1798 : sees how the block ends. */
1799 1346 : if (gimple_code (t) != GIMPLE_RETURN)
1800 : {
1801 1091 : gimple_stmt_iterator gsi3 = gsi2;
1802 1091 : gsi_prev (&gsi2);
1803 1091 : gsi_remove (&gsi3, true);
1804 1091 : release_defs (t);
1805 : }
1806 : else
1807 1346 : gsi_prev (&gsi2);
1808 : }
1809 :
1810 1783 : if (gimple_call_noreturn_p (as_a <gcall *> (stmt)))
1811 : {
1812 4 : e = make_edge (gsi_bb (t->call_gsi), first, EDGE_FALLTHRU);
1813 4 : e->probability = profile_probability::always ();
1814 : }
1815 : else
1816 : {
1817 : /* Number of executions of function has reduced by the tailcall. */
1818 1779 : e = single_non_eh_succ_edge (gsi_bb (t->call_gsi));
1819 :
1820 1779 : profile_count count = e->count ();
1821 :
1822 : /* When profile is inconsistent and the recursion edge is more frequent
1823 : than number of executions of functions, scale it down, so we do not
1824 : end up with 0 executions of entry block. */
1825 1779 : if (count >= ENTRY_BLOCK_PTR_FOR_FN (cfun)->count)
1826 39 : count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.apply_scale (7, 8);
1827 1779 : decrease_profile (EXIT_BLOCK_PTR_FOR_FN (cfun), count);
1828 1779 : decrease_profile (ENTRY_BLOCK_PTR_FOR_FN (cfun), count);
1829 1779 : if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1830 1524 : decrease_profile (e->dest, count);
1831 :
1832 : /* Replace the call by a jump to the start of function. */
1833 1779 : e = redirect_edge_and_branch (e, first);
1834 : }
1835 1783 : gcc_assert (e);
1836 1783 : PENDING_STMT (e) = NULL;
1837 :
1838 : /* Add the new loop. */
1839 1783 : if (!new_loop)
1840 : {
1841 1327 : new_loop = alloc_loop ();
1842 1327 : new_loop->header = first;
1843 1327 : new_loop->finite_p = true;
1844 : }
1845 : else
1846 456 : gcc_assert (new_loop->header == first);
1847 :
1848 : /* Add phi node entries for arguments. The ordering of the phi nodes should
1849 : be the same as the ordering of the arguments. */
1850 1783 : auto_vec<tree> copies;
1851 1783 : for (param = DECL_ARGUMENTS (current_function_decl),
1852 1783 : idx = 0, gpi = gsi_start_phis (first);
1853 6452 : param;
1854 4669 : param = DECL_CHAIN (param), idx++)
1855 : {
1856 4669 : if (!bitmap_bit_p (tailr_arg_needs_copy, idx))
1857 1694 : continue;
1858 :
1859 2975 : if (!is_gimple_reg_type (TREE_TYPE (param)))
1860 : {
1861 515 : if (param == gimple_call_arg (stmt, idx))
1862 149 : continue;
1863 : /* First check if param isn't used by any of the following
1864 : call arguments. If it is, we need to copy first to
1865 : a temporary and only after doing all the assignments copy it
1866 : to param. */
1867 366 : size_t idx2 = idx + 1;
1868 366 : tree param2 = DECL_CHAIN (param);
1869 1699 : for (; param2; param2 = DECL_CHAIN (param2), idx2++)
1870 1334 : if (!is_gimple_reg_type (TREE_TYPE (param)))
1871 : {
1872 1334 : tree base = get_base_address (gimple_call_arg (stmt, idx2));
1873 1334 : if (base == param)
1874 : break;
1875 : }
1876 366 : tree tmp = param;
1877 366 : if (param2)
1878 : {
1879 1 : tmp = create_tmp_var (TREE_TYPE (param));
1880 1 : copies.safe_push (param);
1881 1 : copies.safe_push (tmp);
1882 : }
1883 366 : gimple *g = gimple_build_assign (tmp, gimple_call_arg (stmt, idx));
1884 366 : gsi_insert_before (&t->call_gsi, g, GSI_SAME_STMT);
1885 366 : continue;
1886 366 : }
1887 :
1888 2460 : arg = gimple_call_arg (stmt, idx);
1889 2460 : phi = gpi.phi ();
1890 2460 : gcc_assert (param == SSA_NAME_VAR (PHI_RESULT (phi)));
1891 :
1892 2460 : add_phi_arg (phi, arg, e, gimple_location (stmt));
1893 2460 : gsi_next (&gpi);
1894 : }
1895 1784 : for (unsigned i = 0; i < copies.length (); i += 2)
1896 : {
1897 1 : gimple *g = gimple_build_assign (copies[i], copies[i + 1]);
1898 1 : gsi_insert_before (&t->call_gsi, g, GSI_SAME_STMT);
1899 : }
1900 :
1901 : /* Update the values of accumulators. */
1902 1783 : adjust_accumulator_values (t->call_gsi, t->mult, t->add, e);
1903 :
1904 1783 : call = gsi_stmt (t->call_gsi);
1905 1783 : rslt = gimple_call_lhs (call);
1906 1783 : if (rslt != NULL_TREE && TREE_CODE (rslt) == SSA_NAME)
1907 : {
1908 : /* Result of the call will no longer be defined. So adjust the
1909 : SSA_NAME_DEF_STMT accordingly. */
1910 1090 : SSA_NAME_DEF_STMT (rslt) = gimple_build_nop ();
1911 : }
1912 :
1913 1783 : gsi_remove (&t->call_gsi, true);
1914 1783 : release_defs (call);
1915 1783 : }
1916 :
1917 : /* Optimizes the tailcall described by T. If OPT_TAILCALLS is true, also
1918 : mark the tailcalls for the sibcall optimization. */
1919 :
1920 : static bool
1921 909703 : optimize_tail_call (struct tailcall *t, bool opt_tailcalls,
1922 : class loop *&new_loop)
1923 : {
1924 909703 : if (t->has_tsan_func_exit && (t->tail_recursion || opt_tailcalls))
1925 : {
1926 8 : tree builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_FUNC_EXIT);
1927 8 : gimple *g = gimple_build_call (builtin_decl, 0);
1928 8 : gimple_set_location (g, cfun->function_end_locus);
1929 8 : gsi_insert_before (&t->call_gsi, g, GSI_SAME_STMT);
1930 : }
1931 :
1932 909703 : if (t->tail_recursion)
1933 : {
1934 1783 : eliminate_tail_call (t, new_loop);
1935 1783 : return true;
1936 : }
1937 :
1938 907920 : if (opt_tailcalls)
1939 : {
1940 181902 : gcall *stmt = as_a <gcall *> (gsi_stmt (t->call_gsi));
1941 :
1942 181902 : gimple_call_set_tail (stmt, true);
1943 181902 : cfun->tail_call_marked = true;
1944 181902 : if (dump_file && (dump_flags & TDF_DETAILS))
1945 : {
1946 25 : fprintf (dump_file, "Found tail call ");
1947 25 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1948 25 : fprintf (dump_file, " in bb %i\n", (gsi_bb (t->call_gsi))->index);
1949 : }
1950 181902 : return t->has_tsan_func_exit;
1951 : }
1952 :
1953 : return false;
1954 : }
1955 :
1956 : /* Creates a tail-call accumulator of the same type as the return type of the
1957 : current function. LABEL is the name used to creating the temporary
1958 : variable for the accumulator. The accumulator will be inserted in the
1959 : phis of a basic block BB with single predecessor with an initial value
1960 : INIT converted to the current function return type. */
1961 :
1962 : static tree
1963 213 : create_tailcall_accumulator (const char *label, basic_block bb, tree init)
1964 : {
1965 213 : tree ret_type = TREE_TYPE (DECL_RESULT (current_function_decl));
1966 213 : if (POINTER_TYPE_P (ret_type))
1967 6 : ret_type = sizetype;
1968 :
1969 213 : tree tmp = make_temp_ssa_name (ret_type, NULL, label);
1970 213 : gphi *phi;
1971 :
1972 213 : phi = create_phi_node (tmp, bb);
1973 213 : add_phi_arg (phi, init, single_pred_edge (bb),
1974 : UNKNOWN_LOCATION);
1975 213 : return PHI_RESULT (phi);
1976 : }
1977 :
1978 : /* Optimizes tail calls in the function, turning the tail recursion
1979 : into iteration. When ONLY_MUSTTAIL is true only optimize musttail
1980 : marked calls. When DIAG_MUSTTAIL, diagnose if musttail calls can't
1981 : be tail call optimized. */
1982 :
1983 : static unsigned int
1984 3697453 : tree_optimize_tail_calls_1 (bool opt_tailcalls, bool only_musttail,
1985 : bool diag_musttail)
1986 : {
1987 3697453 : edge e;
1988 3697453 : bool phis_constructed = false;
1989 3697453 : struct tailcall *tailcalls = NULL, *act, *next;
1990 3697453 : bool changed = false;
1991 3697453 : basic_block first = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1992 3697453 : tree param;
1993 3697453 : edge_iterator ei;
1994 :
1995 7326155 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
1996 : {
1997 : /* Only traverse the normal exits, i.e. those that end with return
1998 : statement. */
1999 10885256 : if (safe_is_a <greturn *> (*gsi_last_bb (e->src)))
2000 : {
2001 3627852 : int retry_tsan_func_exit = 0;
2002 3627852 : find_tail_calls (e->src, e, &tailcalls, only_musttail, opt_tailcalls,
2003 : diag_musttail, retry_tsan_func_exit, NULL, NULL);
2004 3627852 : if (retry_tsan_func_exit == -1)
2005 : {
2006 8 : retry_tsan_func_exit = 1;
2007 8 : find_tail_calls (e->src, e, &tailcalls, only_musttail,
2008 : opt_tailcalls, diag_musttail,
2009 : retry_tsan_func_exit, NULL, NULL);
2010 : }
2011 : }
2012 : }
2013 3697453 : if (cfun->has_musttail && diag_musttail)
2014 : {
2015 513 : basic_block bb;
2016 513 : int retry_tsan_func_exit = 0;
2017 4848 : FOR_EACH_BB_FN (bb, cfun)
2018 4335 : if (EDGE_COUNT (bb->succs) == 0
2019 7295 : || (single_succ_p (bb)
2020 2882 : && (single_succ_edge (bb)->flags & EDGE_EH)))
2021 236 : if (gimple *c = last_nondebug_stmt (bb))
2022 236 : if (is_gimple_call (c)
2023 16 : && gimple_call_must_tail_p (as_a <gcall *> (c))
2024 252 : && gimple_call_noreturn_p (as_a <gcall *> (c)))
2025 16 : find_tail_calls (bb, NULL, &tailcalls, only_musttail,
2026 : opt_tailcalls, diag_musttail,
2027 : retry_tsan_func_exit, NULL, NULL);
2028 : }
2029 :
2030 3697453 : if (live_vars)
2031 : {
2032 240657 : destroy_live_vars (live_vars_vec);
2033 481314 : delete live_vars;
2034 240657 : live_vars = NULL;
2035 : }
2036 :
2037 3697453 : if (cfun->has_musttail)
2038 : {
2039 : /* We can't mix non-recursive must tail calls with tail recursive
2040 : calls which require accumulators, because in that case we have to
2041 : emit code in between the musttail calls and return, which prevent
2042 : calling them as tail calls. So, in that case give up on the
2043 : tail recursion. */
2044 1150 : for (act = tailcalls; act; act = act->next)
2045 737 : if (!act->tail_recursion)
2046 : {
2047 690 : gcall *call = as_a <gcall *> (gsi_stmt (act->call_gsi));
2048 690 : if (gimple_call_must_tail_p (call))
2049 : break;
2050 : }
2051 1091 : if (act)
2052 1802 : for (struct tailcall **p = &tailcalls; *p; )
2053 : {
2054 1124 : if ((*p)->tail_recursion && ((*p)->add || (*p)->mult))
2055 : {
2056 6 : struct tailcall *a = *p;
2057 6 : *p = (*p)->next;
2058 6 : gcall *call = as_a <gcall *> (gsi_stmt (a->call_gsi));
2059 6 : maybe_error_musttail (call, _("tail recursion with "
2060 : "accumulation mixed with "
2061 : "musttail non-recursive call"),
2062 : diag_musttail);
2063 6 : free (a);
2064 6 : }
2065 : else
2066 1118 : p = &(*p)->next;
2067 : }
2068 : }
2069 : /* Construct the phi nodes and accumulators if necessary. */
2070 3697453 : a_acc = m_acc = NULL_TREE;
2071 4607156 : for (act = tailcalls; act; act = act->next)
2072 : {
2073 909703 : if (!act->tail_recursion)
2074 907920 : continue;
2075 :
2076 1783 : if (!phis_constructed)
2077 : {
2078 : /* Ensure that there is only one predecessor of the block
2079 : or if there are existing degenerate PHI nodes. */
2080 1327 : if (!single_pred_p (first)
2081 1327 : || !gimple_seq_empty_p (phi_nodes (first)))
2082 0 : first
2083 0 : = split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
2084 :
2085 : /* Copy the args if needed. */
2086 1327 : unsigned idx;
2087 1327 : for (param = DECL_ARGUMENTS (current_function_decl), idx = 0;
2088 4741 : param;
2089 3414 : param = DECL_CHAIN (param), idx++)
2090 3414 : if (bitmap_bit_p (tailr_arg_needs_copy, idx))
2091 : {
2092 2174 : if (!is_gimple_reg_type (TREE_TYPE (param)))
2093 515 : continue;
2094 1659 : tree name = ssa_default_def (cfun, param);
2095 1659 : tree new_name = make_ssa_name (param, SSA_NAME_DEF_STMT (name));
2096 1659 : gphi *phi;
2097 :
2098 1659 : set_ssa_default_def (cfun, param, new_name);
2099 1659 : phi = create_phi_node (name, first);
2100 1659 : add_phi_arg (phi, new_name, single_pred_edge (first),
2101 1659 : EXPR_LOCATION (param));
2102 : }
2103 : phis_constructed = true;
2104 : }
2105 1783 : tree ret_type = TREE_TYPE (DECL_RESULT (current_function_decl));
2106 1783 : if (POINTER_TYPE_P (ret_type))
2107 186 : ret_type = sizetype;
2108 :
2109 1783 : if (act->add && !a_acc)
2110 96 : a_acc = create_tailcall_accumulator ("add_acc", first,
2111 : build_zero_cst (ret_type));
2112 :
2113 1783 : if (act->mult && !m_acc)
2114 117 : m_acc = create_tailcall_accumulator ("mult_acc", first,
2115 : build_one_cst (ret_type));
2116 : }
2117 :
2118 3697453 : if (a_acc || m_acc)
2119 : {
2120 : /* When the tail call elimination using accumulators is performed,
2121 : statements adding the accumulated value are inserted at all exits.
2122 : This turns all other tail calls to non-tail ones. */
2123 195 : opt_tailcalls = false;
2124 : }
2125 :
2126 3697453 : class loop *new_loop = NULL;
2127 4607156 : for (; tailcalls; tailcalls = next)
2128 : {
2129 909703 : next = tailcalls->next;
2130 909703 : changed |= optimize_tail_call (tailcalls, opt_tailcalls, new_loop);
2131 909703 : free (tailcalls);
2132 : }
2133 3697453 : if (new_loop)
2134 1327 : add_loop (new_loop, loops_for_fn (cfun)->tree_root);
2135 :
2136 3697453 : if (a_acc || m_acc)
2137 : {
2138 : /* Modify the remaining return statements. */
2139 362 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2140 : {
2141 501 : if (safe_is_a <greturn *> (*gsi_last_bb (e->src)))
2142 167 : adjust_return_value (e->src, m_acc, a_acc);
2143 : }
2144 : }
2145 :
2146 3697453 : if (changed)
2147 1333 : free_dominance_info (CDI_DOMINATORS);
2148 :
2149 : /* Add phi nodes for the virtual operands defined in the function to the
2150 : header of the loop created by tail recursion elimination. Do so
2151 : by triggering the SSA renamer. */
2152 3697453 : if (phis_constructed)
2153 1327 : mark_virtual_operands_for_renaming (cfun);
2154 :
2155 3697453 : if (tailr_arg_needs_copy)
2156 1329 : BITMAP_FREE (tailr_arg_needs_copy);
2157 :
2158 3697453 : if (diag_musttail)
2159 749188 : cfun->has_musttail = false;
2160 :
2161 3697453 : if (changed)
2162 1333 : return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
2163 : return 0;
2164 : }
2165 :
2166 : static bool
2167 4705739 : gate_tail_calls (void)
2168 : {
2169 4705739 : return flag_optimize_sibling_calls != 0 && dbg_cnt (tail_call);
2170 : }
2171 :
2172 : static unsigned int
2173 748943 : execute_tail_calls (void)
2174 : {
2175 0 : return tree_optimize_tail_calls_1 (true, false, true);
2176 : }
2177 :
2178 : namespace {
2179 :
2180 : const pass_data pass_data_tail_recursion =
2181 : {
2182 : GIMPLE_PASS, /* type */
2183 : "tailr", /* name */
2184 : OPTGROUP_NONE, /* optinfo_flags */
2185 : TV_NONE, /* tv_id */
2186 : ( PROP_cfg | PROP_ssa ), /* properties_required */
2187 : 0, /* properties_provided */
2188 : 0, /* properties_destroyed */
2189 : 0, /* todo_flags_start */
2190 : 0, /* todo_flags_finish */
2191 : };
2192 :
2193 : class pass_tail_recursion : public gimple_opt_pass
2194 : {
2195 : public:
2196 589072 : pass_tail_recursion (gcc::context *ctxt)
2197 1178144 : : gimple_opt_pass (pass_data_tail_recursion, ctxt)
2198 : {}
2199 :
2200 : /* opt_pass methods: */
2201 294536 : opt_pass * clone () final override
2202 : {
2203 294536 : return new pass_tail_recursion (m_ctxt);
2204 : }
2205 3637520 : bool gate (function *) final override { return gate_tail_calls (); }
2206 2948265 : unsigned int execute (function *) final override
2207 : {
2208 2948265 : return tree_optimize_tail_calls_1 (false, false, false);
2209 : }
2210 :
2211 : }; // class pass_tail_recursion
2212 :
2213 : } // anon namespace
2214 :
2215 : gimple_opt_pass *
2216 294536 : make_pass_tail_recursion (gcc::context *ctxt)
2217 : {
2218 294536 : return new pass_tail_recursion (ctxt);
2219 : }
2220 :
2221 : namespace {
2222 :
2223 : const pass_data pass_data_tail_calls =
2224 : {
2225 : GIMPLE_PASS, /* type */
2226 : "tailc", /* name */
2227 : OPTGROUP_NONE, /* optinfo_flags */
2228 : TV_NONE, /* tv_id */
2229 : ( PROP_cfg | PROP_ssa ), /* properties_required */
2230 : 0, /* properties_provided */
2231 : 0, /* properties_destroyed */
2232 : 0, /* todo_flags_start */
2233 : 0, /* todo_flags_finish */
2234 : };
2235 :
2236 : class pass_tail_calls : public gimple_opt_pass
2237 : {
2238 : public:
2239 294536 : pass_tail_calls (gcc::context *ctxt)
2240 589072 : : gimple_opt_pass (pass_data_tail_calls, ctxt)
2241 : {}
2242 :
2243 : /* opt_pass methods: */
2244 1068219 : bool gate (function *) final override { return gate_tail_calls (); }
2245 748943 : unsigned int execute (function *) final override
2246 : {
2247 748943 : return execute_tail_calls ();
2248 : }
2249 :
2250 : }; // class pass_tail_calls
2251 :
2252 : } // anon namespace
2253 :
2254 : gimple_opt_pass *
2255 294536 : make_pass_tail_calls (gcc::context *ctxt)
2256 : {
2257 294536 : return new pass_tail_calls (ctxt);
2258 : }
2259 :
2260 : namespace {
2261 :
2262 : const pass_data pass_data_musttail =
2263 : {
2264 : GIMPLE_PASS, /* type */
2265 : "musttail", /* name */
2266 : OPTGROUP_NONE, /* optinfo_flags */
2267 : TV_NONE, /* tv_id */
2268 : ( PROP_cfg | PROP_ssa ), /* properties_required */
2269 : 0, /* properties_provided */
2270 : 0, /* properties_destroyed */
2271 : 0, /* todo_flags_start */
2272 : 0, /* todo_flags_finish */
2273 : };
2274 :
2275 : class pass_musttail : public gimple_opt_pass
2276 : {
2277 : public:
2278 294536 : pass_musttail (gcc::context *ctxt)
2279 589072 : : gimple_opt_pass (pass_data_musttail, ctxt)
2280 : {}
2281 :
2282 : /* opt_pass methods: */
2283 : /* This pass is only used when the other tail call pass
2284 : doesn't run to make [[musttail]] still work. But only
2285 : run it when there is actually a musttail in the function. */
2286 1516711 : bool gate (function *f) final override
2287 : {
2288 1516711 : return f->has_musttail;
2289 : }
2290 245 : unsigned int execute (function *) final override
2291 : {
2292 245 : return tree_optimize_tail_calls_1 (true, true, true);
2293 : }
2294 :
2295 : }; // class pass_musttail
2296 :
2297 : } // anon namespace
2298 :
2299 : gimple_opt_pass *
2300 294536 : make_pass_musttail (gcc::context *ctxt)
2301 : {
2302 294536 : return new pass_musttail (ctxt);
2303 : }
|