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