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