1 /* Generic sibling call optimization support
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC 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.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
27 #include "hard-reg-set.h"
29 #include "insn-config.h"
31 #include "basic-block.h"
35 static int identify_call_return_value PARAMS ((rtx, rtx *, rtx *));
36 static rtx skip_copy_to_return_value PARAMS ((rtx));
37 static rtx skip_use_of_return_value PARAMS ((rtx, enum rtx_code));
38 static rtx skip_stack_adjustment PARAMS ((rtx));
39 static rtx skip_pic_restore PARAMS ((rtx));
40 static rtx skip_jump_insn PARAMS ((rtx));
41 static int call_ends_block_p PARAMS ((rtx, rtx));
42 static int uses_addressof PARAMS ((rtx));
43 static int sequence_uses_addressof PARAMS ((rtx));
44 static void purge_reg_equiv_notes PARAMS ((void));
45 static void purge_mem_unchanging_flag PARAMS ((rtx));
47 /* Examine a CALL_PLACEHOLDER pattern and determine where the call's
48 return value is located. P_HARD_RETURN receives the hard register
49 that the function used; P_SOFT_RETURN receives the pseudo register
50 that the sequence used. Return non-zero if the values were located. */
53 identify_call_return_value (cp, p_hard_return, p_soft_return)
55 rtx *p_hard_return, *p_soft_return;
57 rtx insn, set, hard, soft;
60 /* Search backward through the "normal" call sequence to the CALL insn. */
61 while (NEXT_INSN (insn))
62 insn = NEXT_INSN (insn);
63 while (GET_CODE (insn) != CALL_INSN)
64 insn = PREV_INSN (insn);
66 /* Assume the pattern is (set (dest) (call ...)), or that the first
67 member of a parallel is. This is the hard return register used
69 if (GET_CODE (PATTERN (insn)) == SET
70 && GET_CODE (SET_SRC (PATTERN (insn))) == CALL)
71 hard = SET_DEST (PATTERN (insn));
72 else if (GET_CODE (PATTERN (insn)) == PARALLEL
73 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
74 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == CALL)
75 hard = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
79 /* If we didn't get a single hard register (e.g. a parallel), give up. */
80 if (GET_CODE (hard) != REG)
83 /* Stack adjustment done after call may appear here. */
84 insn = skip_stack_adjustment (insn);
88 /* Restore of GP register may appear here. */
89 insn = skip_pic_restore (insn);
93 /* If there's nothing after, there's no soft return value. */
94 insn = NEXT_INSN (insn);
98 /* We're looking for a source of the hard return register. */
99 set = single_set (insn);
100 if (! set || SET_SRC (set) != hard)
103 soft = SET_DEST (set);
104 insn = NEXT_INSN (insn);
106 /* Allow this first destination to be copied to a second register,
107 as might happen if the first register wasn't the particular pseudo
108 we'd been expecting. */
110 && (set = single_set (insn)) != NULL_RTX
111 && SET_SRC (set) == soft)
113 soft = SET_DEST (set);
114 insn = NEXT_INSN (insn);
117 /* Don't fool with anything but pseudo registers. */
118 if (GET_CODE (soft) != REG || REGNO (soft) < FIRST_PSEUDO_REGISTER)
121 /* This value must not be modified before the end of the sequence. */
122 if (reg_set_between_p (soft, insn, NULL_RTX))
125 *p_hard_return = hard;
126 *p_soft_return = soft;
131 /* If the first real insn after ORIG_INSN copies to this function's
132 return value from RETVAL, then return the insn which performs the
133 copy. Otherwise return ORIG_INSN. */
136 skip_copy_to_return_value (orig_insn)
139 rtx insn, set = NULL_RTX;
140 rtx hardret, softret;
142 /* If there is no return value, we have nothing to do. */
143 if (! identify_call_return_value (PATTERN (orig_insn), &hardret, &softret))
146 insn = next_nonnote_insn (orig_insn);
150 set = single_set (insn);
154 /* The destination must be the same as the called function's return
155 value to ensure that any return value is put in the same place by the
156 current function and the function we're calling.
158 Further, the source must be the same as the pseudo into which the
159 called function's return value was copied. Otherwise we're returning
162 #ifndef OUTGOING_REGNO
163 #define OUTGOING_REGNO(N) (N)
166 if (SET_DEST (set) == current_function_return_rtx
167 && REG_P (SET_DEST (set))
168 && OUTGOING_REGNO (REGNO (SET_DEST (set))) == REGNO (hardret)
169 && SET_SRC (set) == softret)
172 /* Recognize the situation when the called function's return value
173 is copied in two steps: first into an intermediate pseudo, then
174 the into the calling functions return value register. */
176 if (REG_P (SET_DEST (set))
177 && SET_SRC (set) == softret)
179 rtx x = SET_DEST (set);
181 insn = next_nonnote_insn (insn);
185 set = single_set (insn);
189 if (SET_DEST (set) == current_function_return_rtx
190 && REG_P (SET_DEST (set))
191 && OUTGOING_REGNO (REGNO (SET_DEST (set))) == REGNO (hardret)
192 && SET_SRC (set) == x)
196 /* It did not look like a copy of the return value, so return the
197 same insn we were passed. */
201 /* If the first real insn after ORIG_INSN is a CODE of this function's return
202 value, return insn. Otherwise return ORIG_INSN. */
205 skip_use_of_return_value (orig_insn, code)
211 insn = next_nonnote_insn (orig_insn);
214 && GET_CODE (insn) == INSN
215 && GET_CODE (PATTERN (insn)) == code
216 && (XEXP (PATTERN (insn), 0) == current_function_return_rtx
217 || XEXP (PATTERN (insn), 0) == const0_rtx))
223 /* If the first real insn after ORIG_INSN adjusts the stack pointer
224 by a constant, return the insn with the stack pointer adjustment.
225 Otherwise return ORIG_INSN. */
228 skip_stack_adjustment (orig_insn)
231 rtx insn, set = NULL_RTX;
233 insn = next_nonnote_insn (orig_insn);
236 set = single_set (insn);
240 && GET_CODE (SET_SRC (set)) == PLUS
241 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
242 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
243 && SET_DEST (set) == stack_pointer_rtx)
249 /* If the first real insn after ORIG_INSN sets the pic register,
250 return it. Otherwise return ORIG_INSN. */
253 skip_pic_restore (orig_insn)
256 rtx insn, set = NULL_RTX;
258 insn = next_nonnote_insn (orig_insn);
261 set = single_set (insn);
263 if (insn && set && SET_DEST (set) == pic_offset_table_rtx)
269 /* If the first real insn after ORIG_INSN is a jump, return the JUMP_INSN.
270 Otherwise return ORIG_INSN. */
273 skip_jump_insn (orig_insn)
278 insn = next_nonnote_insn (orig_insn);
281 && GET_CODE (insn) == JUMP_INSN
282 && any_uncondjump_p (insn))
288 /* Using the above functions, see if INSN, skipping any of the above,
289 goes all the way to END, the end of a basic block. Return 1 if so. */
292 call_ends_block_p (insn, end)
296 /* END might be a note, so get the last nonnote insn of the block. */
297 end = next_nonnote_insn (PREV_INSN (end));
299 /* If the call was the end of the block, then we're OK. */
303 /* Skip over copying from the call's return value pseudo into
304 this function's hard return register and if that's the end
305 of the block, we're OK. */
306 insn = skip_copy_to_return_value (insn);
310 /* Skip any stack adjustment. */
311 insn = skip_stack_adjustment (insn);
315 /* Skip over a CLOBBER of the return value as a hard reg. */
316 insn = skip_use_of_return_value (insn, CLOBBER);
320 /* Skip over a USE of the return value (as a hard reg). */
321 insn = skip_use_of_return_value (insn, USE);
325 /* Skip over a JUMP_INSN at the end of the block. If that doesn't end the
326 block, the original CALL_INSN didn't. */
327 insn = skip_jump_insn (insn);
331 /* Scan the rtx X for ADDRESSOF expressions or
332 current_function_internal_arg_pointer registers.
333 Return nonzero if an ADDRESSOF or current_function_internal_arg_pointer
334 is found outside of some MEM expression, else return zero. */
349 if (code == ADDRESSOF || x == current_function_internal_arg_pointer)
355 /* Scan all subexpressions. */
356 fmt = GET_RTX_FORMAT (code);
357 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
361 if (uses_addressof (XEXP (x, i)))
364 else if (*fmt == 'E')
366 for (j = 0; j < XVECLEN (x, i); j++)
367 if (uses_addressof (XVECEXP (x, i, j)))
374 /* Scan the sequence of insns in SEQ to see if any have an ADDRESSOF
375 rtl expression or current_function_internal_arg_pointer occurences
376 not enclosed within a MEM. If an ADDRESSOF expression or
377 current_function_internal_arg_pointer is found, return nonzero, otherwise
380 This function handles CALL_PLACEHOLDERs which contain multiple sequences
384 sequence_uses_addressof (seq)
389 for (insn = seq; insn; insn = NEXT_INSN (insn))
392 /* If this is a CALL_PLACEHOLDER, then recursively call ourselves
393 with each nonempty sequence attached to the CALL_PLACEHOLDER. */
394 if (GET_CODE (insn) == CALL_INSN
395 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
397 if (XEXP (PATTERN (insn), 0) != NULL_RTX
398 && sequence_uses_addressof (XEXP (PATTERN (insn), 0)))
400 if (XEXP (PATTERN (insn), 1) != NULL_RTX
401 && sequence_uses_addressof (XEXP (PATTERN (insn), 1)))
403 if (XEXP (PATTERN (insn), 2) != NULL_RTX
404 && sequence_uses_addressof (XEXP (PATTERN (insn), 2)))
407 else if (uses_addressof (PATTERN (insn))
408 || (REG_NOTES (insn) && uses_addressof (REG_NOTES (insn))))
414 /* Remove all REG_EQUIV notes found in the insn chain. */
417 purge_reg_equiv_notes ()
421 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
425 rtx note = find_reg_note (insn, REG_EQUIV, 0);
428 /* Remove the note and keep looking at the notes for
430 remove_note (insn, note);
438 /* Clear RTX_UNCHANGING_P flag of incoming argument MEMs. */
441 purge_mem_unchanging_flag (x)
455 if (RTX_UNCHANGING_P (x)
456 && (XEXP (x, 0) == current_function_internal_arg_pointer
457 || (GET_CODE (XEXP (x, 0)) == PLUS
458 && XEXP (XEXP (x, 0), 0) ==
459 current_function_internal_arg_pointer
460 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
461 RTX_UNCHANGING_P (x) = 0;
465 /* Scan all subexpressions. */
466 fmt = GET_RTX_FORMAT (code);
467 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
470 purge_mem_unchanging_flag (XEXP (x, i));
471 else if (*fmt == 'E')
472 for (j = 0; j < XVECLEN (x, i); j++)
473 purge_mem_unchanging_flag (XVECEXP (x, i, j));
477 /* Replace the CALL_PLACEHOLDER with one of its children. INSN should be
478 the CALL_PLACEHOLDER insn; USE tells which child to use. */
481 replace_call_placeholder (insn, use)
485 if (use == sibcall_use_tail_recursion)
486 emit_insns_before (XEXP (PATTERN (insn), 2), insn);
487 else if (use == sibcall_use_sibcall)
488 emit_insns_before (XEXP (PATTERN (insn), 1), insn);
489 else if (use == sibcall_use_normal)
490 emit_insns_before (XEXP (PATTERN (insn), 0), insn);
494 /* Turn off LABEL_PRESERVE_P for the tail recursion label if it
495 exists. We only had to set it long enough to keep the jump
496 pass above from deleting it as unused. */
497 if (XEXP (PATTERN (insn), 3))
498 LABEL_PRESERVE_P (XEXP (PATTERN (insn), 3)) = 0;
500 /* "Delete" the placeholder insn. */
501 PUT_CODE (insn, NOTE);
502 NOTE_SOURCE_FILE (insn) = 0;
503 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
506 /* Given a (possibly empty) set of potential sibling or tail recursion call
507 sites, determine if optimization is possible.
509 Potential sibling or tail recursion calls are marked with CALL_PLACEHOLDER
510 insns. The CALL_PLACEHOLDER insn holds chains of insns to implement a
511 normal call, sibling call or tail recursive call.
513 Replace the CALL_PLACEHOLDER with an appropriate insn chain. */
516 optimize_sibling_and_tail_recursive_calls ()
519 basic_block alternate_exit = EXIT_BLOCK_PTR;
520 int current_function_uses_addressof;
521 int successful_sibling_call = 0;
522 int replaced_call_placeholder = 0;
525 insns = get_insns ();
527 /* We do not perform these calls when flag_exceptions is true, so this
528 is probably a NOP at the current time. However, we may want to support
529 sibling and tail recursion optimizations in the future, so let's plan
530 ahead and find all the EH labels. */
531 find_exception_handler_labels ();
533 /* Run a jump optimization pass to clean up the CFG. We primarily want
534 this to thread jumps so that it is obvious which blocks jump to the
536 jump_optimize_minimal (insns);
538 /* We need cfg information to determine which blocks are succeeded
539 only by the epilogue. */
540 find_basic_blocks (insns, max_reg_num (), 0);
543 /* If there are no basic blocks, then there is nothing to do. */
544 if (n_basic_blocks == 0)
547 /* Find the exit block.
549 It is possible that we have blocks which can reach the exit block
550 directly. However, most of the time a block will jump (or fall into)
551 N_BASIC_BLOCKS - 1, which in turn falls into the exit block. */
552 for (e = EXIT_BLOCK_PTR->pred;
553 e && alternate_exit == EXIT_BLOCK_PTR;
558 if (e->dest != EXIT_BLOCK_PTR || e->succ_next != NULL)
561 /* Walk forwards through the last normal block and see if it
562 does nothing except fall into the exit block. */
563 for (insn = BLOCK_HEAD (n_basic_blocks - 1);
565 insn = NEXT_INSN (insn))
567 /* This should only happen once, at the start of this block. */
568 if (GET_CODE (insn) == CODE_LABEL)
571 if (GET_CODE (insn) == NOTE)
574 if (GET_CODE (insn) == INSN
575 && GET_CODE (PATTERN (insn)) == USE)
581 /* If INSN is zero, then the search walked all the way through the
582 block without hitting anything interesting. This block is a
583 valid alternate exit block. */
585 alternate_exit = e->src;
588 /* If the function uses ADDRESSOF, we can't (easily) determine
589 at this point if the value will end up on the stack. */
590 current_function_uses_addressof = sequence_uses_addressof (insns);
592 /* Walk the insn chain and find any CALL_PLACEHOLDER insns. We need to
593 select one of the insn sequences attached to each CALL_PLACEHOLDER.
595 The different sequences represent different ways to implement the call,
596 ie, tail recursion, sibling call or normal call.
598 Since we do not create nested CALL_PLACEHOLDERs, the scan
599 continues with the insn that was after a replaced CALL_PLACEHOLDER;
600 we don't rescan the replacement insns. */
601 for (insn = insns; insn; insn = NEXT_INSN (insn))
603 if (GET_CODE (insn) == CALL_INSN
604 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
606 int sibcall = (XEXP (PATTERN (insn), 1) != NULL_RTX);
607 int tailrecursion = (XEXP (PATTERN (insn), 2) != NULL_RTX);
608 basic_block call_block = BLOCK_FOR_INSN (insn);
610 /* alloca (until we have stack slot life analysis) inhibits
611 sibling call optimizations, but not tail recursion.
612 Similarly if we use varargs or stdarg since they implicitly
613 may take the address of an argument. */
614 if (current_function_calls_alloca
615 || current_function_varargs || current_function_stdarg)
618 /* See if there are any reasons we can't perform either sibling or
619 tail call optimizations. We must be careful with stack slots
620 which are live at potential optimization sites. ??? The first
621 test is overly conservative and should be replaced. */
623 /* Can't take address of local var if used by recursive call. */
624 || current_function_uses_addressof
625 /* Any function that calls setjmp might have longjmp called from
626 any called function. ??? We really should represent this
627 properly in the CFG so that this needn't be special cased. */
628 || current_function_calls_setjmp
629 /* Can't if more than one successor or single successor is not
630 exit block. These two tests prevent tail call optimization
631 in the presense of active exception handlers. */
632 || call_block->succ == NULL
633 || call_block->succ->succ_next != NULL
634 || (call_block->succ->dest != EXIT_BLOCK_PTR
635 && call_block->succ->dest != alternate_exit)
636 /* If this call doesn't end the block, there are operations at
637 the end of the block which we must execute after returning. */
638 || ! call_ends_block_p (insn, call_block->end))
639 sibcall = 0, tailrecursion = 0;
641 /* Select a set of insns to implement the call and emit them.
642 Tail recursion is the most efficient, so select it over
643 a tail/sibling call. */
645 successful_sibling_call = 1;
647 replaced_call_placeholder = 1;
648 replace_call_placeholder (insn,
650 ? sibcall_use_tail_recursion
652 ? sibcall_use_sibcall
653 : sibcall_use_normal);
657 if (successful_sibling_call)
661 /* A sibling call sequence invalidates any REG_EQUIV notes made for
662 this function's incoming arguments.
664 At the start of RTL generation we know the only REG_EQUIV notes
665 in the rtl chain are those for incoming arguments, so we can safely
666 flush any REG_EQUIV note.
668 This is (slight) overkill. We could keep track of the highest
669 argument we clobber and be more selective in removing notes, but it
670 does not seem to be worth the effort. */
671 purge_reg_equiv_notes ();
673 /* A sibling call sequence also may invalidate RTX_UNCHANGING_P
674 flag of some incoming arguments MEM RTLs, because it can write into
675 those slots. We clear all those bits now.
677 This is (slight) overkill, we could keep track of which arguments
678 we actually write into. */
679 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
681 if (GET_CODE (insn) == NOTE)
683 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
686 else if (INSN_P (insn))
687 purge_mem_unchanging_flag (PATTERN (insn));
691 /* There may have been NOTE_INSN_BLOCK_{BEGIN,END} notes in the
692 CALL_PLACEHOLDER alternatives that we didn't emit. Rebuild the
693 lexical block tree to correspond to the notes that still exist. */
694 if (replaced_call_placeholder)
697 /* This information will be invalid after inline expansion. Kill it now. */
698 free_basic_block_vars (0);