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