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