]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-tailcall.c
2015-10-29 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / tree-tailcall.c
CommitLineData
5d94cf4d 1/* Tail call optimization on trees.
d353bf18 2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
4ee9c684 3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8c4c00c1 8the Free Software Foundation; either version 3, or (at your option)
4ee9c684 9any later version.
10
11GCC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
4ee9c684 19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
9ef16211 23#include "backend.h"
7c29e30e 24#include "target.h"
25#include "rtl.h"
4ee9c684 26#include "tree.h"
9ef16211 27#include "gimple.h"
7c29e30e 28#include "cfghooks.h"
29#include "tree-pass.h"
30#include "tm_p.h"
9ef16211 31#include "ssa.h"
7c29e30e 32#include "expmed.h"
33#include "insn-config.h"
34#include "emit-rtl.h"
35#include "cgraph.h"
36#include "gimple-pretty-print.h"
9ef16211 37#include "alias.h"
b20a8bb4 38#include "fold-const.h"
9ed99284 39#include "stor-layout.h"
bc61cadb 40#include "internal-fn.h"
dcf1a1ec 41#include "gimple-iterator.h"
e795d6e1 42#include "gimplify-me.h"
073c1fd5 43#include "tree-cfg.h"
073c1fd5 44#include "tree-into-ssa.h"
d53441c8 45#include "flags.h"
d53441c8 46#include "dojump.h"
47#include "explow.h"
48#include "calls.h"
d53441c8 49#include "varasm.h"
50#include "stmt.h"
9ed99284 51#include "expr.h"
073c1fd5 52#include "tree-dfa.h"
4ee9c684 53#include "except.h"
4ee9c684 54#include "langhooks.h"
3072d30e 55#include "dbgcnt.h"
8ff30f9a 56#include "cfgloop.h"
218e3e4e 57#include "common/common-target.h"
e9de52cc 58#include "ipa-utils.h"
4ee9c684 59
60/* The file implements the tail recursion elimination. It is also used to
365db11e 61 analyze the tail calls in general, passing the results to the rtl level
4ee9c684 62 where they are used for sibcall optimization.
63
64 In addition to the standard tail recursion elimination, we handle the most
65 trivial cases of making the call tail recursive by creating accumulators.
66 For example the following function
67
68 int sum (int n)
69 {
70 if (n > 0)
71 return n + sum (n - 1);
72 else
73 return 0;
74 }
75
76 is transformed into
77
78 int sum (int n)
79 {
80 int acc = 0;
81
82 while (n > 0)
83 acc += n--;
84
85 return acc;
86 }
87
48e1416a 88 To do this, we maintain two accumulators (a_acc and m_acc) that indicate
4ee9c684 89 when we reach the return x statement, we should return a_acc + x * m_acc
90 instead. They are initially initialized to 0 and 1, respectively,
91 so the semantics of the function is obviously preserved. If we are
92 guaranteed that the value of the accumulator never change, we
93 omit the accumulator.
94
95 There are three cases how the function may exit. The first one is
fbac255a 96 handled in adjust_return_value, the other two in adjust_accumulator_values
4ee9c684 97 (the second case is actually a special case of the third one and we
98 present it separately just for clarity):
99
100 1) Just return x, where x is not in any of the remaining special shapes.
101 We rewrite this to a gimple equivalent of return m_acc * x + a_acc.
48e1416a 102
4ee9c684 103 2) return f (...), where f is the current function, is rewritten in a
365db11e 104 classical tail-recursion elimination way, into assignment of arguments
4ee9c684 105 and jump to the start of the function. Values of the accumulators
106 are unchanged.
48e1416a 107
4ee9c684 108 3) return a + m * f(...), where a and m do not depend on call to f.
109 To preserve the semantics described before we want this to be rewritten
110 in such a way that we finally return
111
112 a_acc + (a + m * f(...)) * m_acc = (a_acc + a * m_acc) + (m * m_acc) * f(...).
113
114 I.e. we increase a_acc by a * m_acc, multiply m_acc by m and
115 eliminate the tail call to f. Special cases when the value is just
116 added or just multiplied are obtained by setting a = 0 or m = 1.
117
118 TODO -- it is possible to do similar tricks for other operations. */
119
120/* A structure that describes the tailcall. */
121
122struct tailcall
123{
4ee9c684 124 /* The iterator pointing to the call statement. */
75a70cf9 125 gimple_stmt_iterator call_gsi;
4ee9c684 126
127 /* True if it is a call to the current function. */
128 bool tail_recursion;
129
130 /* The return value of the caller is mult * f + add, where f is the return
131 value of the call. */
132 tree mult, add;
133
134 /* Next tailcall in the chain. */
135 struct tailcall *next;
136};
137
138/* The variables holding the value of multiplicative and additive
139 accumulator. */
140static tree m_acc, a_acc;
141
4ee9c684 142static bool optimize_tail_call (struct tailcall *, bool);
143static void eliminate_tail_call (struct tailcall *);
4ee9c684 144
145/* Returns false when the function is not suitable for tail call optimization
146 from some reason (e.g. if it takes variable number of arguments). */
147
148static bool
149suitable_for_tail_opt_p (void)
150{
18d50ae6 151 if (cfun->stdarg)
4ee9c684 152 return false;
153
4ee9c684 154 return true;
155}
156/* Returns false when the function is not suitable for tail call optimization
5c72dbe7 157 for some reason (e.g. if it takes variable number of arguments).
4ee9c684 158 This test must pass in addition to suitable_for_tail_opt_p in order to make
159 tail call discovery happen. */
160
161static bool
162suitable_for_tail_call_opt_p (void)
163{
6070c035 164 tree param;
165
4ee9c684 166 /* alloca (until we have stack slot life analysis) inhibits
167 sibling call optimizations, but not tail recursion. */
18d50ae6 168 if (cfun->calls_alloca)
4ee9c684 169 return false;
170
171 /* If we are using sjlj exceptions, we may need to add a call to
172 _Unwind_SjLj_Unregister at exit of the function. Which means
173 that we cannot do any sibcall transformations. */
218e3e4e 174 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ
cc7d6aed 175 && current_function_has_exception_handlers ())
4ee9c684 176 return false;
177
178 /* Any function that calls setjmp might have longjmp called from
179 any called function. ??? We really should represent this
180 properly in the CFG so that this needn't be special cased. */
18d50ae6 181 if (cfun->calls_setjmp)
4ee9c684 182 return false;
183
6070c035 184 /* ??? It is OK if the argument of a function is taken in some cases,
185 but not in all cases. See PR15387 and PR19616. Revisit for 4.1. */
186 for (param = DECL_ARGUMENTS (current_function_decl);
187 param;
1767a056 188 param = DECL_CHAIN (param))
6070c035 189 if (TREE_ADDRESSABLE (param))
190 return false;
191
4ee9c684 192 return true;
193}
194
195/* Checks whether the expression EXPR in stmt AT is independent of the
75a70cf9 196 statement pointed to by GSI (in a sense that we already know EXPR's value
197 at GSI). We use the fact that we are only called from the chain of
4ee9c684 198 basic blocks that have only single successor. Returns the expression
75a70cf9 199 containing the value of EXPR at GSI. */
4ee9c684 200
201static tree
42acab1c 202independent_of_stmt_p (tree expr, gimple *at, gimple_stmt_iterator gsi)
4ee9c684 203{
204 basic_block bb, call_bb, at_bb;
205 edge e;
cd665a06 206 edge_iterator ei;
4ee9c684 207
208 if (is_gimple_min_invariant (expr))
209 return expr;
210
211 if (TREE_CODE (expr) != SSA_NAME)
212 return NULL_TREE;
213
214 /* Mark the blocks in the chain leading to the end. */
75a70cf9 215 at_bb = gimple_bb (at);
216 call_bb = gimple_bb (gsi_stmt (gsi));
ea091dfd 217 for (bb = call_bb; bb != at_bb; bb = single_succ (bb))
4ee9c684 218 bb->aux = &bb->aux;
219 bb->aux = &bb->aux;
220
221 while (1)
48e1416a 222 {
4ee9c684 223 at = SSA_NAME_DEF_STMT (expr);
75a70cf9 224 bb = gimple_bb (at);
4ee9c684 225
2c763ed4 226 /* The default definition or defined before the chain. */
4ee9c684 227 if (!bb || !bb->aux)
228 break;
229
230 if (bb == call_bb)
231 {
75a70cf9 232 for (; !gsi_end_p (gsi); gsi_next (&gsi))
233 if (gsi_stmt (gsi) == at)
4ee9c684 234 break;
235
75a70cf9 236 if (!gsi_end_p (gsi))
4ee9c684 237 expr = NULL_TREE;
238 break;
239 }
240
75a70cf9 241 if (gimple_code (at) != GIMPLE_PHI)
4ee9c684 242 {
243 expr = NULL_TREE;
244 break;
245 }
246
cd665a06 247 FOR_EACH_EDGE (e, ei, bb->preds)
4ee9c684 248 if (e->src->aux)
249 break;
8c0963c4 250 gcc_assert (e);
4ee9c684 251
56004dc5 252 expr = PHI_ARG_DEF_FROM_EDGE (at, e);
634f6ce0 253 if (TREE_CODE (expr) != SSA_NAME)
254 {
255 /* The value is a constant. */
256 break;
257 }
4ee9c684 258 }
259
260 /* Unmark the blocks. */
ea091dfd 261 for (bb = call_bb; bb != at_bb; bb = single_succ (bb))
4ee9c684 262 bb->aux = NULL;
263 bb->aux = NULL;
264
265 return expr;
266}
267
75a70cf9 268/* Simulates the effect of an assignment STMT on the return value of the tail
269 recursive CALL passed in ASS_VAR. M and A are the multiplicative and the
270 additive factor for the real return value. */
4ee9c684 271
272static bool
1a91d914 273process_assignment (gassign *stmt, gimple_stmt_iterator call, tree *m,
4ee9c684 274 tree *a, tree *ass_var)
275{
dd8277d9 276 tree op0, op1 = NULL_TREE, non_ass_var = NULL_TREE;
75a70cf9 277 tree dest = gimple_assign_lhs (stmt);
278 enum tree_code code = gimple_assign_rhs_code (stmt);
279 enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
280 tree src_var = gimple_assign_rhs1 (stmt);
48e1416a 281
cefc957d 282 /* See if this is a simple copy operation of an SSA name to the function
283 result. In that case we may have a simple tail call. Ignore type
284 conversions that can never produce extra code between the function
285 call and the function return. */
75a70cf9 286 if ((rhs_class == GIMPLE_SINGLE_RHS || gimple_assign_cast_p (stmt))
287 && (TREE_CODE (src_var) == SSA_NAME))
4ee9c684 288 {
75a70cf9 289 /* Reject a tailcall if the type conversion might need
290 additional code. */
b1562bbb 291 if (gimple_assign_cast_p (stmt))
292 {
293 if (TYPE_MODE (TREE_TYPE (dest)) != TYPE_MODE (TREE_TYPE (src_var)))
294 return false;
295
296 /* Even if the type modes are the same, if the precision of the
297 type is smaller than mode's precision,
298 reduce_to_bit_field_precision would generate additional code. */
299 if (INTEGRAL_TYPE_P (TREE_TYPE (dest))
300 && (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (dest)))
301 > TYPE_PRECISION (TREE_TYPE (dest))))
302 return false;
303 }
75a70cf9 304
cefc957d 305 if (src_var != *ass_var)
4ee9c684 306 return false;
307
308 *ass_var = dest;
309 return true;
310 }
311
dd8277d9 312 switch (rhs_class)
313 {
314 case GIMPLE_BINARY_RHS:
315 op1 = gimple_assign_rhs2 (stmt);
316
317 /* Fall through. */
318
319 case GIMPLE_UNARY_RHS:
320 op0 = gimple_assign_rhs1 (stmt);
321 break;
322
323 default:
324 return false;
325 }
4ee9c684 326
9cff5cbd 327 /* Accumulator optimizations will reverse the order of operations.
328 We can only do that for floating-point types if we're assuming
329 that addition and multiplication are associative. */
49d060d7 330 if (!flag_associative_math)
9cff5cbd 331 if (FLOAT_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
332 return false;
333
dd8277d9 334 if (rhs_class == GIMPLE_UNARY_RHS)
335 ;
336 else if (op0 == *ass_var
a6f8b9b9 337 && (non_ass_var = independent_of_stmt_p (op1, stmt, call)))
4ee9c684 338 ;
339 else if (op1 == *ass_var
340 && (non_ass_var = independent_of_stmt_p (op0, stmt, call)))
341 ;
342 else
343 return false;
344
cefc957d 345 switch (code)
4ee9c684 346 {
347 case PLUS_EXPR:
4ee9c684 348 *a = non_ass_var;
349 *ass_var = dest;
350 return true;
351
a6f8b9b9 352 case POINTER_PLUS_EXPR:
353 if (op0 != *ass_var)
354 return false;
355 *a = non_ass_var;
356 *ass_var = dest;
357 return true;
358
4ee9c684 359 case MULT_EXPR:
4ee9c684 360 *m = non_ass_var;
361 *ass_var = dest;
362 return true;
363
dd8277d9 364 case NEGATE_EXPR:
e53c294c 365 *m = build_minus_one_cst (TREE_TYPE (op0));
dd8277d9 366 *ass_var = dest;
367 return true;
368
369 case MINUS_EXPR:
370 if (*ass_var == op0)
371 *a = fold_build1 (NEGATE_EXPR, TREE_TYPE (non_ass_var), non_ass_var);
372 else
373 {
e53c294c 374 *m = build_minus_one_cst (TREE_TYPE (non_ass_var));
dd8277d9 375 *a = fold_build1 (NEGATE_EXPR, TREE_TYPE (non_ass_var), non_ass_var);
376 }
377
378 *ass_var = dest;
379 return true;
380
381 /* TODO -- Handle POINTER_PLUS_EXPR. */
4ee9c684 382
383 default:
384 return false;
385 }
386}
387
388/* Propagate VAR through phis on edge E. */
389
390static tree
391propagate_through_phis (tree var, edge e)
392{
393 basic_block dest = e->dest;
1a91d914 394 gphi_iterator gsi;
48e1416a 395
75a70cf9 396 for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
397 {
1a91d914 398 gphi *phi = gsi.phi ();
75a70cf9 399 if (PHI_ARG_DEF_FROM_EDGE (phi, e) == var)
400 return PHI_RESULT (phi);
401 }
4ee9c684 402 return var;
403}
404
405/* Finds tailcalls falling into basic block BB. The list of found tailcalls is
406 added to the start of RET. */
407
408static void
409find_tail_calls (basic_block bb, struct tailcall **ret)
410{
75a70cf9 411 tree ass_var = NULL_TREE, ret_var, func, param;
42acab1c 412 gimple *stmt;
1a91d914 413 gcall *call = NULL;
75a70cf9 414 gimple_stmt_iterator gsi, agsi;
4ee9c684 415 bool tail_recursion;
416 struct tailcall *nw;
417 edge e;
418 tree m, a;
419 basic_block abb;
75a70cf9 420 size_t idx;
4debfcfc 421 tree var;
4ee9c684 422
ea091dfd 423 if (!single_succ_p (bb))
4ee9c684 424 return;
425
75a70cf9 426 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
4ee9c684 427 {
75a70cf9 428 stmt = gsi_stmt (gsi);
4ee9c684 429
97effc09 430 /* Ignore labels, returns, clobbers and debug stmts. */
2109076a 431 if (gimple_code (stmt) == GIMPLE_LABEL
432 || gimple_code (stmt) == GIMPLE_RETURN
97effc09 433 || gimple_clobber_p (stmt)
2109076a 434 || is_gimple_debug (stmt))
4ee9c684 435 continue;
436
4ee9c684 437 /* Check for a call. */
75a70cf9 438 if (is_gimple_call (stmt))
4ee9c684 439 {
1a91d914 440 call = as_a <gcall *> (stmt);
441 ass_var = gimple_call_lhs (call);
75a70cf9 442 break;
4ee9c684 443 }
444
dd277d48 445 /* If the statement references memory or volatile operands, fail. */
446 if (gimple_references_memory_p (stmt)
447 || gimple_has_volatile_ops (stmt))
4ee9c684 448 return;
449 }
450
75a70cf9 451 if (gsi_end_p (gsi))
4ee9c684 452 {
cd665a06 453 edge_iterator ei;
4ee9c684 454 /* Recurse to the predecessors. */
cd665a06 455 FOR_EACH_EDGE (e, ei, bb->preds)
4ee9c684 456 find_tail_calls (e->src, ret);
457
458 return;
459 }
460
48e1416a 461 /* If the LHS of our call is not just a simple register, we can't
7528fb13 462 transform this into a tail or sibling call. This situation happens,
463 in (e.g.) "*p = foo()" where foo returns a struct. In this case
464 we won't have a temporary here, but we need to carry out the side
465 effect anyway, so tailcall is impossible.
466
467 ??? In some situations (when the struct is returned in memory via
468 invisible argument) we could deal with this, e.g. by passing 'p'
469 itself as that argument to foo, but it's too early to do this here,
470 and expand_call() will not handle it anyway. If it ever can, then
471 we need to revisit this here, to allow that situation. */
472 if (ass_var && !is_gimple_reg (ass_var))
473 return;
474
4ee9c684 475 /* We found the call, check whether it is suitable. */
476 tail_recursion = false;
75a70cf9 477 func = gimple_call_fndecl (call);
89432048 478 if (func
479 && !DECL_BUILT_IN (func)
480 && recursive_call_p (current_function_decl, func))
4ee9c684 481 {
4debfcfc 482 tree arg;
cb245216 483
75a70cf9 484 for (param = DECL_ARGUMENTS (func), idx = 0;
485 param && idx < gimple_call_num_args (call);
1767a056 486 param = DECL_CHAIN (param), idx ++)
4ee9c684 487 {
75a70cf9 488 arg = gimple_call_arg (call, idx);
d4e3bde0 489 if (param != arg)
490 {
491 /* Make sure there are no problems with copying. The parameter
4ee9c684 492 have a copyable type and the two arguments must have reasonably
493 equivalent types. The latter requirement could be relaxed if
494 we emitted a suitable type conversion statement. */
d4e3bde0 495 if (!is_gimple_reg_type (TREE_TYPE (param))
c8ca3ee7 496 || !useless_type_conversion_p (TREE_TYPE (param),
75a70cf9 497 TREE_TYPE (arg)))
d4e3bde0 498 break;
499
500 /* The parameter should be a real operand, so that phi node
501 created for it at the start of the function has the meaning
502 of copying the value. This test implies is_gimple_reg_type
503 from the previous condition, however this one could be
504 relaxed by being more careful with copying the new value
75a70cf9 505 of the parameter (emitting appropriate GIMPLE_ASSIGN and
d4e3bde0 506 updating the virtual operands). */
507 if (!is_gimple_reg (param))
508 break;
509 }
4ee9c684 510 }
75a70cf9 511 if (idx == gimple_call_num_args (call) && !param)
4ee9c684 512 tail_recursion = true;
4debfcfc 513 }
cb245216 514
4debfcfc 515 /* Make sure the tail invocation of this function does not refer
516 to local variables. */
24ccd9c6 517 FOR_EACH_LOCAL_DECL (cfun, idx, var)
4debfcfc 518 {
8081b5e7 519 if (TREE_CODE (var) != PARM_DECL
520 && auto_var_in_fn_p (var, cfun->decl)
40a61243 521 && (ref_maybe_used_by_stmt_p (call, var)
522 || call_may_clobber_ref_p (call, var)))
4debfcfc 523 return;
4ee9c684 524 }
525
526 /* Now check the statements after the call. None of them has virtual
527 operands, so they may only depend on the call through its return
528 value. The return value should also be dependent on each of them,
529 since we are running after dce. */
530 m = NULL_TREE;
531 a = NULL_TREE;
532
533 abb = bb;
75a70cf9 534 agsi = gsi;
4ee9c684 535 while (1)
536 {
4e32b1c1 537 tree tmp_a = NULL_TREE;
538 tree tmp_m = NULL_TREE;
75a70cf9 539 gsi_next (&agsi);
4ee9c684 540
75a70cf9 541 while (gsi_end_p (agsi))
4ee9c684 542 {
ea091dfd 543 ass_var = propagate_through_phis (ass_var, single_succ_edge (abb));
544 abb = single_succ (abb);
75a70cf9 545 agsi = gsi_start_bb (abb);
4ee9c684 546 }
547
75a70cf9 548 stmt = gsi_stmt (agsi);
4ee9c684 549
75a70cf9 550 if (gimple_code (stmt) == GIMPLE_LABEL)
4ee9c684 551 continue;
552
75a70cf9 553 if (gimple_code (stmt) == GIMPLE_RETURN)
4ee9c684 554 break;
555
97effc09 556 if (gimple_clobber_p (stmt))
557 continue;
558
9845d120 559 if (is_gimple_debug (stmt))
560 continue;
561
75a70cf9 562 if (gimple_code (stmt) != GIMPLE_ASSIGN)
4ee9c684 563 return;
564
75a70cf9 565 /* This is a gimple assign. */
1a91d914 566 if (! process_assignment (as_a <gassign *> (stmt), gsi, &tmp_m,
567 &tmp_a, &ass_var))
4ee9c684 568 return;
4e32b1c1 569
570 if (tmp_a)
571 {
17297953 572 tree type = TREE_TYPE (tmp_a);
4e32b1c1 573 if (a)
17297953 574 a = fold_build2 (PLUS_EXPR, type, fold_convert (type, a), tmp_a);
4e32b1c1 575 else
576 a = tmp_a;
577 }
578 if (tmp_m)
579 {
17297953 580 tree type = TREE_TYPE (tmp_m);
4e32b1c1 581 if (m)
17297953 582 m = fold_build2 (MULT_EXPR, type, fold_convert (type, m), tmp_m);
4e32b1c1 583 else
584 m = tmp_m;
585
586 if (a)
17297953 587 a = fold_build2 (MULT_EXPR, type, fold_convert (type, a), tmp_m);
4e32b1c1 588 }
4ee9c684 589 }
590
cefc957d 591 /* See if this is a tail call we can handle. */
1a91d914 592 ret_var = gimple_return_retval (as_a <greturn *> (stmt));
4ee9c684 593
594 /* We may proceed if there either is no return value, or the return value
595 is identical to the call's return. */
596 if (ret_var
597 && (ret_var != ass_var))
598 return;
599
6108f5dd 600 /* If this is not a tail recursive call, we cannot handle addends or
601 multiplicands. */
602 if (!tail_recursion && (m || a))
603 return;
604
a6f8b9b9 605 /* For pointers only allow additions. */
606 if (m && POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
607 return;
608
945865c5 609 nw = XNEW (struct tailcall);
4ee9c684 610
75a70cf9 611 nw->call_gsi = gsi;
4ee9c684 612
613 nw->tail_recursion = tail_recursion;
614
615 nw->mult = m;
616 nw->add = a;
617
618 nw->next = *ret;
619 *ret = nw;
620}
621
75a70cf9 622/* Helper to insert PHI_ARGH to the phi of VAR in the destination of edge E. */
4ee9c684 623
624static void
75a70cf9 625add_successor_phi_arg (edge e, tree var, tree phi_arg)
626{
1a91d914 627 gphi_iterator gsi;
75a70cf9 628
629 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
1a91d914 630 if (PHI_RESULT (gsi.phi ()) == var)
75a70cf9 631 break;
632
633 gcc_assert (!gsi_end_p (gsi));
1a91d914 634 add_phi_arg (gsi.phi (), phi_arg, e, UNKNOWN_LOCATION);
75a70cf9 635}
636
637/* Creates a GIMPLE statement which computes the operation specified by
f5c4bbcd 638 CODE, ACC and OP1 to a new variable with name LABEL and inserts the
639 statement in the position specified by GSI. Returns the
75a70cf9 640 tree node of the statement's result. */
641
642static tree
48e1416a 643adjust_return_value_with_ops (enum tree_code code, const char *label,
bc56c183 644 tree acc, tree op1, gimple_stmt_iterator gsi)
4ee9c684 645{
75a70cf9 646
4ee9c684 647 tree ret_type = TREE_TYPE (DECL_RESULT (current_function_decl));
03d37e4e 648 tree result = make_temp_ssa_name (ret_type, NULL, label);
1a91d914 649 gassign *stmt;
75a70cf9 650
a6f8b9b9 651 if (POINTER_TYPE_P (ret_type))
652 {
653 gcc_assert (code == PLUS_EXPR && TREE_TYPE (acc) == sizetype);
654 code = POINTER_PLUS_EXPR;
655 }
656 if (types_compatible_p (TREE_TYPE (acc), TREE_TYPE (op1))
657 && code != POINTER_PLUS_EXPR)
e9cf809e 658 stmt = gimple_build_assign (result, code, acc, op1);
bc56c183 659 else
660 {
a6f8b9b9 661 tree tem;
662 if (code == POINTER_PLUS_EXPR)
663 tem = fold_build2 (code, TREE_TYPE (op1), op1, acc);
664 else
665 tem = fold_build2 (code, TREE_TYPE (op1),
666 fold_convert (TREE_TYPE (op1), acc), op1);
667 tree rhs = fold_convert (ret_type, tem);
bc56c183 668 rhs = force_gimple_operand_gsi (&gsi, rhs,
f5c4bbcd 669 false, NULL, true, GSI_SAME_STMT);
03d37e4e 670 stmt = gimple_build_assign (result, rhs);
bc56c183 671 }
672
bc56c183 673 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
75a70cf9 674 return result;
675}
676
48e1416a 677/* Creates a new GIMPLE statement that adjusts the value of accumulator ACC by
75a70cf9 678 the computation specified by CODE and OP1 and insert the statement
679 at the position specified by GSI as a new statement. Returns new SSA name
680 of updated accumulator. */
681
682static tree
683update_accumulator_with_ops (enum tree_code code, tree acc, tree op1,
684 gimple_stmt_iterator gsi)
685{
1a91d914 686 gassign *stmt;
f9e245b2 687 tree var = copy_ssa_name (acc);
bc56c183 688 if (types_compatible_p (TREE_TYPE (acc), TREE_TYPE (op1)))
e9cf809e 689 stmt = gimple_build_assign (var, code, acc, op1);
bc56c183 690 else
691 {
692 tree rhs = fold_convert (TREE_TYPE (acc),
693 fold_build2 (code,
694 TREE_TYPE (op1),
695 fold_convert (TREE_TYPE (op1), acc),
696 op1));
697 rhs = force_gimple_operand_gsi (&gsi, rhs,
698 false, NULL, false, GSI_CONTINUE_LINKING);
874117c8 699 stmt = gimple_build_assign (var, rhs);
bc56c183 700 }
75a70cf9 701 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
702 return var;
703}
704
705/* Adjust the accumulator values according to A and M after GSI, and update
706 the phi nodes on edge BACK. */
707
708static void
709adjust_accumulator_values (gimple_stmt_iterator gsi, tree m, tree a, edge back)
710{
4e32b1c1 711 tree var, a_acc_arg, m_acc_arg;
712
713 if (m)
714 m = force_gimple_operand_gsi (&gsi, m, true, NULL, true, GSI_SAME_STMT);
715 if (a)
716 a = force_gimple_operand_gsi (&gsi, a, true, NULL, true, GSI_SAME_STMT);
4ee9c684 717
4e32b1c1 718 a_acc_arg = a_acc;
719 m_acc_arg = m_acc;
4ee9c684 720 if (a)
721 {
722 if (m_acc)
723 {
724 if (integer_onep (a))
725 var = m_acc;
726 else
75a70cf9 727 var = adjust_return_value_with_ops (MULT_EXPR, "acc_tmp", m_acc,
bc56c183 728 a, gsi);
4ee9c684 729 }
730 else
731 var = a;
732
75a70cf9 733 a_acc_arg = update_accumulator_with_ops (PLUS_EXPR, a_acc, var, gsi);
4ee9c684 734 }
735
736 if (m)
75a70cf9 737 m_acc_arg = update_accumulator_with_ops (MULT_EXPR, m_acc, m, gsi);
4ee9c684 738
739 if (a_acc)
75a70cf9 740 add_successor_phi_arg (back, a_acc, a_acc_arg);
4ee9c684 741
742 if (m_acc)
75a70cf9 743 add_successor_phi_arg (back, m_acc, m_acc_arg);
4ee9c684 744}
745
365db11e 746/* Adjust value of the return at the end of BB according to M and A
4ee9c684 747 accumulators. */
748
749static void
750adjust_return_value (basic_block bb, tree m, tree a)
751{
75a70cf9 752 tree retval;
1a91d914 753 greturn *ret_stmt = as_a <greturn *> (gimple_seq_last_stmt (bb_seq (bb)));
75a70cf9 754 gimple_stmt_iterator gsi = gsi_last_bb (bb);
4ee9c684 755
75a70cf9 756 gcc_assert (gimple_code (ret_stmt) == GIMPLE_RETURN);
4ee9c684 757
75a70cf9 758 retval = gimple_return_retval (ret_stmt);
759 if (!retval || retval == error_mark_node)
4ee9c684 760 return;
761
4ee9c684 762 if (m)
75a70cf9 763 retval = adjust_return_value_with_ops (MULT_EXPR, "mul_tmp", m_acc, retval,
bc56c183 764 gsi);
4ee9c684 765 if (a)
75a70cf9 766 retval = adjust_return_value_with_ops (PLUS_EXPR, "acc_tmp", a_acc, retval,
bc56c183 767 gsi);
75a70cf9 768 gimple_return_set_retval (ret_stmt, retval);
22aa74c4 769 update_stmt (ret_stmt);
4ee9c684 770}
771
8dc7fca4 772/* Subtract COUNT and FREQUENCY from the basic block and it's
773 outgoing edge. */
774static void
775decrease_profile (basic_block bb, gcov_type count, int frequency)
776{
777 edge e;
778 bb->count -= count;
779 if (bb->count < 0)
780 bb->count = 0;
781 bb->frequency -= frequency;
782 if (bb->frequency < 0)
783 bb->frequency = 0;
784 if (!single_succ_p (bb))
785 {
786 gcc_assert (!EDGE_COUNT (bb->succs));
787 return;
788 }
789 e = single_succ_edge (bb);
790 e->count -= count;
791 if (e->count < 0)
792 e->count = 0;
793}
794
4304657c 795/* Returns true if argument PARAM of the tail recursive call needs to be copied
796 when the call is eliminated. */
797
798static bool
799arg_needs_copy_p (tree param)
800{
801 tree def;
802
4ae5778c 803 if (!is_gimple_reg (param))
4304657c 804 return false;
48e1416a 805
4304657c 806 /* Parameters that are only defined but never used need not be copied. */
c6dfe037 807 def = ssa_default_def (cfun, param);
4304657c 808 if (!def)
809 return false;
810
811 return true;
812}
813
4ee9c684 814/* Eliminates tail call described by T. TMP_VARS is a list of
815 temporary variables used to copy the function arguments. */
816
817static void
818eliminate_tail_call (struct tailcall *t)
819{
75a70cf9 820 tree param, rslt;
42acab1c 821 gimple *stmt, *call;
c2f47e15 822 tree arg;
75a70cf9 823 size_t idx;
4ee9c684 824 basic_block bb, first;
825 edge e;
1a91d914 826 gphi *phi;
827 gphi_iterator gpi;
75a70cf9 828 gimple_stmt_iterator gsi;
42acab1c 829 gimple *orig_stmt;
4ee9c684 830
75a70cf9 831 stmt = orig_stmt = gsi_stmt (t->call_gsi);
832 bb = gsi_bb (t->call_gsi);
4ee9c684 833
834 if (dump_file && (dump_flags & TDF_DETAILS))
835 {
836 fprintf (dump_file, "Eliminated tail recursion in bb %d : ",
837 bb->index);
75a70cf9 838 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
4ee9c684 839 fprintf (dump_file, "\n");
840 }
841
75a70cf9 842 gcc_assert (is_gimple_call (stmt));
4ee9c684 843
34154e27 844 first = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
4ee9c684 845
75a70cf9 846 /* Remove the code after call_gsi that will become unreachable. The
18a87dc5 847 possibly unreachable code in other blocks is removed later in
848 cfg cleanup. */
75a70cf9 849 gsi = t->call_gsi;
df8d8dce 850 gimple_stmt_iterator gsi2 = gsi_last_bb (gimple_bb (gsi_stmt (gsi)));
851 while (gsi_stmt (gsi2) != gsi_stmt (gsi))
18a87dc5 852 {
df8d8dce 853 gimple *t = gsi_stmt (gsi2);
18a87dc5 854 /* Do not remove the return statement, so that redirect_edge_and_branch
855 sees how the block ends. */
df8d8dce 856 if (gimple_code (t) != GIMPLE_RETURN)
857 {
858 gimple_stmt_iterator gsi3 = gsi2;
859 gsi_prev (&gsi2);
860 gsi_remove (&gsi3, true);
861 release_defs (t);
862 }
863 else
864 gsi_prev (&gsi2);
18a87dc5 865 }
866
8dc7fca4 867 /* Number of executions of function has reduced by the tailcall. */
75a70cf9 868 e = single_succ_edge (gsi_bb (t->call_gsi));
34154e27 869 decrease_profile (EXIT_BLOCK_PTR_FOR_FN (cfun), e->count, EDGE_FREQUENCY (e));
870 decrease_profile (ENTRY_BLOCK_PTR_FOR_FN (cfun), e->count,
871 EDGE_FREQUENCY (e));
872 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
8dc7fca4 873 decrease_profile (e->dest, e->count, EDGE_FREQUENCY (e));
874
4ee9c684 875 /* Replace the call by a jump to the start of function. */
75a70cf9 876 e = redirect_edge_and_branch (single_succ_edge (gsi_bb (t->call_gsi)),
877 first);
8c0963c4 878 gcc_assert (e);
75a70cf9 879 PENDING_STMT (e) = NULL;
4ee9c684 880
4304657c 881 /* Add phi node entries for arguments. The ordering of the phi nodes should
882 be the same as the ordering of the arguments. */
4ee9c684 883 for (param = DECL_ARGUMENTS (current_function_decl),
1a91d914 884 idx = 0, gpi = gsi_start_phis (first);
4ee9c684 885 param;
1767a056 886 param = DECL_CHAIN (param), idx++)
4ee9c684 887 {
4304657c 888 if (!arg_needs_copy_p (param))
4ee9c684 889 continue;
75a70cf9 890
891 arg = gimple_call_arg (stmt, idx);
1a91d914 892 phi = gpi.phi ();
4304657c 893 gcc_assert (param == SSA_NAME_VAR (PHI_RESULT (phi)));
4ee9c684 894
60d535d2 895 add_phi_arg (phi, arg, e, gimple_location (stmt));
1a91d914 896 gsi_next (&gpi);
4ee9c684 897 }
898
899 /* Update the values of accumulators. */
75a70cf9 900 adjust_accumulator_values (t->call_gsi, t->mult, t->add, e);
4ee9c684 901
75a70cf9 902 call = gsi_stmt (t->call_gsi);
903 rslt = gimple_call_lhs (call);
904 if (rslt != NULL_TREE)
4ee9c684 905 {
4ee9c684 906 /* Result of the call will no longer be defined. So adjust the
907 SSA_NAME_DEF_STMT accordingly. */
75a70cf9 908 SSA_NAME_DEF_STMT (rslt) = gimple_build_nop ();
4ee9c684 909 }
910
75a70cf9 911 gsi_remove (&t->call_gsi, true);
2d1c5976 912 release_defs (call);
4ee9c684 913}
914
915/* Optimizes the tailcall described by T. If OPT_TAILCALLS is true, also
916 mark the tailcalls for the sibcall optimization. */
917
918static bool
919optimize_tail_call (struct tailcall *t, bool opt_tailcalls)
920{
921 if (t->tail_recursion)
922 {
923 eliminate_tail_call (t);
924 return true;
925 }
926
927 if (opt_tailcalls)
928 {
1a91d914 929 gcall *stmt = as_a <gcall *> (gsi_stmt (t->call_gsi));
4ee9c684 930
75a70cf9 931 gimple_call_set_tail (stmt, true);
dc7cdd37 932 cfun->tail_call_marked = true;
4ee9c684 933 if (dump_file && (dump_flags & TDF_DETAILS))
934 {
935 fprintf (dump_file, "Found tail call ");
75a70cf9 936 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
937 fprintf (dump_file, " in bb %i\n", (gsi_bb (t->call_gsi))->index);
4ee9c684 938 }
939 }
940
941 return false;
942}
943
75a70cf9 944/* Creates a tail-call accumulator of the same type as the return type of the
945 current function. LABEL is the name used to creating the temporary
946 variable for the accumulator. The accumulator will be inserted in the
947 phis of a basic block BB with single predecessor with an initial value
948 INIT converted to the current function return type. */
949
950static tree
951create_tailcall_accumulator (const char *label, basic_block bb, tree init)
952{
953 tree ret_type = TREE_TYPE (DECL_RESULT (current_function_decl));
a6f8b9b9 954 if (POINTER_TYPE_P (ret_type))
955 ret_type = sizetype;
956
03d37e4e 957 tree tmp = make_temp_ssa_name (ret_type, NULL, label);
1a91d914 958 gphi *phi;
75a70cf9 959
75a70cf9 960 phi = create_phi_node (tmp, bb);
961 /* RET_TYPE can be a float when -ffast-maths is enabled. */
efbcb6de 962 add_phi_arg (phi, fold_convert (ret_type, init), single_pred_edge (bb),
60d535d2 963 UNKNOWN_LOCATION);
75a70cf9 964 return PHI_RESULT (phi);
965}
48e1416a 966
4ee9c684 967/* Optimizes tail calls in the function, turning the tail recursion
968 into iteration. */
969
6fa78c7b 970static unsigned int
4ee9c684 971tree_optimize_tail_calls_1 (bool opt_tailcalls)
972{
973 edge e;
974 bool phis_constructed = false;
975 struct tailcall *tailcalls = NULL, *act, *next;
976 bool changed = false;
34154e27 977 basic_block first = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
75a70cf9 978 tree param;
42acab1c 979 gimple *stmt;
cd665a06 980 edge_iterator ei;
4ee9c684 981
982 if (!suitable_for_tail_opt_p ())
6fa78c7b 983 return 0;
4ee9c684 984 if (opt_tailcalls)
985 opt_tailcalls = suitable_for_tail_call_opt_p ();
986
34154e27 987 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
4ee9c684 988 {
989 /* Only traverse the normal exits, i.e. those that end with return
990 statement. */
991 stmt = last_stmt (e->src);
992
993 if (stmt
75a70cf9 994 && gimple_code (stmt) == GIMPLE_RETURN)
4ee9c684 995 find_tail_calls (e->src, &tailcalls);
996 }
997
998 /* Construct the phi nodes and accumulators if necessary. */
999 a_acc = m_acc = NULL_TREE;
1000 for (act = tailcalls; act; act = act->next)
1001 {
1002 if (!act->tail_recursion)
1003 continue;
1004
1005 if (!phis_constructed)
1006 {
dd277d48 1007 /* Ensure that there is only one predecessor of the block
1008 or if there are existing degenerate PHI nodes. */
1009 if (!single_pred_p (first)
1010 || !gimple_seq_empty_p (phi_nodes (first)))
34154e27 1011 first =
1012 split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
4ee9c684 1013
1014 /* Copy the args if needed. */
1015 for (param = DECL_ARGUMENTS (current_function_decl);
1016 param;
1767a056 1017 param = DECL_CHAIN (param))
4304657c 1018 if (arg_needs_copy_p (param))
1019 {
c6dfe037 1020 tree name = ssa_default_def (cfun, param);
4304657c 1021 tree new_name = make_ssa_name (param, SSA_NAME_DEF_STMT (name));
1a91d914 1022 gphi *phi;
4304657c 1023
c6dfe037 1024 set_ssa_default_def (cfun, param, new_name);
4304657c 1025 phi = create_phi_node (name, first);
48e1416a 1026 add_phi_arg (phi, new_name, single_pred_edge (first),
60d535d2 1027 EXPR_LOCATION (param));
4304657c 1028 }
4ee9c684 1029 phis_constructed = true;
1030 }
1031
1032 if (act->add && !a_acc)
75a70cf9 1033 a_acc = create_tailcall_accumulator ("add_acc", first,
1034 integer_zero_node);
4ee9c684 1035
1036 if (act->mult && !m_acc)
75a70cf9 1037 m_acc = create_tailcall_accumulator ("mult_acc", first,
1038 integer_one_node);
4304657c 1039 }
1040
f09431f1 1041 if (a_acc || m_acc)
1042 {
1043 /* When the tail call elimination using accumulators is performed,
1044 statements adding the accumulated value are inserted at all exits.
1045 This turns all other tail calls to non-tail ones. */
1046 opt_tailcalls = false;
1047 }
1048
4ee9c684 1049 for (; tailcalls; tailcalls = next)
1050 {
1051 next = tailcalls->next;
1052 changed |= optimize_tail_call (tailcalls, opt_tailcalls);
1053 free (tailcalls);
1054 }
1055
1056 if (a_acc || m_acc)
1057 {
1058 /* Modify the remaining return statements. */
34154e27 1059 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
4ee9c684 1060 {
1061 stmt = last_stmt (e->src);
1062
1063 if (stmt
75a70cf9 1064 && gimple_code (stmt) == GIMPLE_RETURN)
4ee9c684 1065 adjust_return_value (e->src, m_acc, a_acc);
1066 }
1067 }
1068
1069 if (changed)
8ff30f9a 1070 {
1071 /* We may have created new loops. Make them magically appear. */
b3083327 1072 loops_state_set (LOOPS_NEED_FIXUP);
8ff30f9a 1073 free_dominance_info (CDI_DOMINATORS);
1074 }
4304657c 1075
24ccd9c6 1076 /* Add phi nodes for the virtual operands defined in the function to the
1077 header of the loop created by tail recursion elimination. Do so
1078 by triggering the SSA renamer. */
4304657c 1079 if (phis_constructed)
278611f2 1080 mark_virtual_operands_for_renaming (cfun);
24ccd9c6 1081
6fa78c7b 1082 if (changed)
1083 return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
1084 return 0;
4ee9c684 1085}
1086
4ee9c684 1087static bool
1088gate_tail_calls (void)
1089{
3072d30e 1090 return flag_optimize_sibling_calls != 0 && dbg_cnt (tail_call);
4ee9c684 1091}
1092
2a1990e9 1093static unsigned int
4ee9c684 1094execute_tail_calls (void)
1095{
6fa78c7b 1096 return tree_optimize_tail_calls_1 (true);
4ee9c684 1097}
1098
7620bc82 1099namespace {
1100
1101const pass_data pass_data_tail_recursion =
4ee9c684 1102{
cbe8bda8 1103 GIMPLE_PASS, /* type */
1104 "tailr", /* name */
1105 OPTGROUP_NONE, /* optinfo_flags */
cbe8bda8 1106 TV_NONE, /* tv_id */
1107 ( PROP_cfg | PROP_ssa ), /* properties_required */
1108 0, /* properties_provided */
1109 0, /* properties_destroyed */
1110 0, /* todo_flags_start */
8b88439e 1111 0, /* todo_flags_finish */
4ee9c684 1112};
1113
7620bc82 1114class pass_tail_recursion : public gimple_opt_pass
cbe8bda8 1115{
1116public:
9af5ce0c 1117 pass_tail_recursion (gcc::context *ctxt)
1118 : gimple_opt_pass (pass_data_tail_recursion, ctxt)
cbe8bda8 1119 {}
1120
1121 /* opt_pass methods: */
ae84f584 1122 opt_pass * clone () { return new pass_tail_recursion (m_ctxt); }
31315c24 1123 virtual bool gate (function *) { return gate_tail_calls (); }
65b0537f 1124 virtual unsigned int execute (function *)
1125 {
1126 return tree_optimize_tail_calls_1 (false);
1127 }
cbe8bda8 1128
1129}; // class pass_tail_recursion
1130
7620bc82 1131} // anon namespace
1132
cbe8bda8 1133gimple_opt_pass *
1134make_pass_tail_recursion (gcc::context *ctxt)
1135{
1136 return new pass_tail_recursion (ctxt);
1137}
1138
7620bc82 1139namespace {
1140
1141const pass_data pass_data_tail_calls =
4ee9c684 1142{
cbe8bda8 1143 GIMPLE_PASS, /* type */
1144 "tailc", /* name */
1145 OPTGROUP_NONE, /* optinfo_flags */
cbe8bda8 1146 TV_NONE, /* tv_id */
1147 ( PROP_cfg | PROP_ssa ), /* properties_required */
1148 0, /* properties_provided */
1149 0, /* properties_destroyed */
1150 0, /* todo_flags_start */
8b88439e 1151 0, /* todo_flags_finish */
4ee9c684 1152};
cbe8bda8 1153
7620bc82 1154class pass_tail_calls : public gimple_opt_pass
cbe8bda8 1155{
1156public:
9af5ce0c 1157 pass_tail_calls (gcc::context *ctxt)
1158 : gimple_opt_pass (pass_data_tail_calls, ctxt)
cbe8bda8 1159 {}
1160
1161 /* opt_pass methods: */
31315c24 1162 virtual bool gate (function *) { return gate_tail_calls (); }
65b0537f 1163 virtual unsigned int execute (function *) { return execute_tail_calls (); }
cbe8bda8 1164
1165}; // class pass_tail_calls
1166
7620bc82 1167} // anon namespace
1168
cbe8bda8 1169gimple_opt_pass *
1170make_pass_tail_calls (gcc::context *ctxt)
1171{
1172 return new pass_tail_calls (ctxt);
1173}