1 /* Generic sibling call optimization support
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 #include "hard-reg-set.h"
29 #include "insn-config.h"
31 #include "basic-block.h"
35 /* In case alternate_exit_block contains copy from pseudo, to return value,
36 record the pseudo here. In such case the pseudo must be set to function
37 return in the sibcall sequence. */
38 static rtx return_value_pseudo
;
40 static int identify_call_return_value
PARAMS ((rtx
, rtx
*, rtx
*));
41 static rtx skip_copy_to_return_value
PARAMS ((rtx
));
42 static rtx skip_use_of_return_value
PARAMS ((rtx
, enum rtx_code
));
43 static rtx skip_stack_adjustment
PARAMS ((rtx
));
44 static rtx skip_pic_restore
PARAMS ((rtx
));
45 static rtx skip_jump_insn
PARAMS ((rtx
));
46 static int call_ends_block_p
PARAMS ((rtx
, rtx
));
47 static int uses_addressof
PARAMS ((rtx
));
48 static int sequence_uses_addressof
PARAMS ((rtx
));
49 static void purge_reg_equiv_notes
PARAMS ((void));
50 static void purge_mem_unchanging_flag
PARAMS ((rtx
));
51 static rtx skip_unreturned_value
PARAMS ((rtx
));
53 /* Examine a CALL_PLACEHOLDER pattern and determine where the call's
54 return value is located. P_HARD_RETURN receives the hard register
55 that the function used; P_SOFT_RETURN receives the pseudo register
56 that the sequence used. Return non-zero if the values were located. */
59 identify_call_return_value (cp
, p_hard_return
, p_soft_return
)
61 rtx
*p_hard_return
, *p_soft_return
;
63 rtx insn
, set
, hard
, soft
;
66 /* Search backward through the "normal" call sequence to the CALL insn. */
67 while (NEXT_INSN (insn
))
68 insn
= NEXT_INSN (insn
);
69 while (GET_CODE (insn
) != CALL_INSN
)
70 insn
= PREV_INSN (insn
);
72 /* Assume the pattern is (set (dest) (call ...)), or that the first
73 member of a parallel is. This is the hard return register used
75 if (GET_CODE (PATTERN (insn
)) == SET
76 && GET_CODE (SET_SRC (PATTERN (insn
))) == CALL
)
77 hard
= SET_DEST (PATTERN (insn
));
78 else if (GET_CODE (PATTERN (insn
)) == PARALLEL
79 && GET_CODE (XVECEXP (PATTERN (insn
), 0, 0)) == SET
80 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn
), 0, 0))) == CALL
)
81 hard
= SET_DEST (XVECEXP (PATTERN (insn
), 0, 0));
85 /* If we didn't get a single hard register (e.g. a parallel), give up. */
86 if (GET_CODE (hard
) != REG
)
89 /* Stack adjustment done after call may appear here. */
90 insn
= skip_stack_adjustment (insn
);
94 /* Restore of GP register may appear here. */
95 insn
= skip_pic_restore (insn
);
99 /* If there's nothing after, there's no soft return value. */
100 insn
= NEXT_INSN (insn
);
104 /* We're looking for a source of the hard return register. */
105 set
= single_set (insn
);
106 if (! set
|| SET_SRC (set
) != hard
)
109 soft
= SET_DEST (set
);
110 insn
= NEXT_INSN (insn
);
112 /* Allow this first destination to be copied to a second register,
113 as might happen if the first register wasn't the particular pseudo
114 we'd been expecting. */
116 && (set
= single_set (insn
)) != NULL_RTX
117 && SET_SRC (set
) == soft
)
119 soft
= SET_DEST (set
);
120 insn
= NEXT_INSN (insn
);
123 /* Don't fool with anything but pseudo registers. */
124 if (GET_CODE (soft
) != REG
|| REGNO (soft
) < FIRST_PSEUDO_REGISTER
)
127 /* This value must not be modified before the end of the sequence. */
128 if (reg_set_between_p (soft
, insn
, NULL_RTX
))
131 *p_hard_return
= hard
;
132 *p_soft_return
= soft
;
137 /* If the first real insn after ORIG_INSN copies to this function's
138 return value from RETVAL, then return the insn which performs the
139 copy. Otherwise return ORIG_INSN. */
142 skip_copy_to_return_value (orig_insn
)
145 rtx insn
, set
= NULL_RTX
;
146 rtx hardret
, softret
;
148 /* If there is no return value, we have nothing to do. */
149 if (! identify_call_return_value (PATTERN (orig_insn
), &hardret
, &softret
))
152 insn
= next_nonnote_insn (orig_insn
);
156 set
= single_set (insn
);
160 if (return_value_pseudo
)
162 if (SET_DEST (set
) == return_value_pseudo
)
167 /* The destination must be the same as the called function's return
168 value to ensure that any return value is put in the same place by the
169 current function and the function we're calling.
171 Further, the source must be the same as the pseudo into which the
172 called function's return value was copied. Otherwise we're returning
175 #ifndef OUTGOING_REGNO
176 #define OUTGOING_REGNO(N) (N)
179 if (SET_DEST (set
) == current_function_return_rtx
180 && REG_P (SET_DEST (set
))
181 && OUTGOING_REGNO (REGNO (SET_DEST (set
))) == REGNO (hardret
)
182 && SET_SRC (set
) == softret
)
185 /* Recognize the situation when the called function's return value
186 is copied in two steps: first into an intermediate pseudo, then
187 the into the calling functions return value register. */
189 if (REG_P (SET_DEST (set
))
190 && SET_SRC (set
) == softret
)
192 rtx x
= SET_DEST (set
);
194 insn
= next_nonnote_insn (insn
);
198 set
= single_set (insn
);
202 if (SET_DEST (set
) == current_function_return_rtx
203 && REG_P (SET_DEST (set
))
204 && OUTGOING_REGNO (REGNO (SET_DEST (set
))) == REGNO (hardret
)
205 && SET_SRC (set
) == x
)
209 /* It did not look like a copy of the return value, so return the
210 same insn we were passed. */
214 /* If the first real insn after ORIG_INSN is a CODE of this function's return
215 value, return insn. Otherwise return ORIG_INSN. */
218 skip_use_of_return_value (orig_insn
, code
)
224 insn
= next_nonnote_insn (orig_insn
);
227 && GET_CODE (insn
) == INSN
228 && GET_CODE (PATTERN (insn
)) == code
229 && (XEXP (PATTERN (insn
), 0) == current_function_return_rtx
230 || XEXP (PATTERN (insn
), 0) == const0_rtx
))
236 /* In case function does not return value, we get clobber of pseudo followed
237 by set to hard return value. */
239 skip_unreturned_value (orig_insn
)
242 rtx insn
= next_nonnote_insn (orig_insn
);
244 /* Skip possible clobber of pseudo return register. */
246 && GET_CODE (insn
) == INSN
247 && GET_CODE (PATTERN (insn
)) == CLOBBER
248 && REG_P (XEXP (PATTERN (insn
), 0))
249 && (REGNO (XEXP (PATTERN (insn
), 0)) >= FIRST_PSEUDO_REGISTER
))
251 rtx set_insn
= next_nonnote_insn (insn
);
255 set
= single_set (set_insn
);
257 || SET_SRC (set
) != XEXP (PATTERN (insn
), 0)
258 || SET_DEST (set
) != current_function_return_rtx
)
265 /* If the first real insn after ORIG_INSN adjusts the stack pointer
266 by a constant, return the insn with the stack pointer adjustment.
267 Otherwise return ORIG_INSN. */
270 skip_stack_adjustment (orig_insn
)
273 rtx insn
, set
= NULL_RTX
;
275 insn
= next_nonnote_insn (orig_insn
);
278 set
= single_set (insn
);
282 && GET_CODE (SET_SRC (set
)) == PLUS
283 && XEXP (SET_SRC (set
), 0) == stack_pointer_rtx
284 && GET_CODE (XEXP (SET_SRC (set
), 1)) == CONST_INT
285 && SET_DEST (set
) == stack_pointer_rtx
)
291 /* If the first real insn after ORIG_INSN sets the pic register,
292 return it. Otherwise return ORIG_INSN. */
295 skip_pic_restore (orig_insn
)
298 rtx insn
, set
= NULL_RTX
;
300 insn
= next_nonnote_insn (orig_insn
);
303 set
= single_set (insn
);
305 if (insn
&& set
&& SET_DEST (set
) == pic_offset_table_rtx
)
311 /* If the first real insn after ORIG_INSN is a jump, return the JUMP_INSN.
312 Otherwise return ORIG_INSN. */
315 skip_jump_insn (orig_insn
)
320 insn
= next_nonnote_insn (orig_insn
);
323 && GET_CODE (insn
) == JUMP_INSN
324 && any_uncondjump_p (insn
))
330 /* Using the above functions, see if INSN, skipping any of the above,
331 goes all the way to END, the end of a basic block. Return 1 if so. */
334 call_ends_block_p (insn
, end
)
339 /* END might be a note, so get the last nonnote insn of the block. */
340 end
= next_nonnote_insn (PREV_INSN (end
));
342 /* If the call was the end of the block, then we're OK. */
346 /* Skip over copying from the call's return value pseudo into
347 this function's hard return register and if that's the end
348 of the block, we're OK. */
349 new_insn
= skip_copy_to_return_value (insn
);
351 /* In case we return value in pseudo, we must set the pseudo to
352 return value of called function, otherwise we are returning
354 if (return_value_pseudo
&& insn
== new_insn
)
361 /* Skip any stack adjustment. */
362 insn
= skip_stack_adjustment (insn
);
366 /* Skip over a CLOBBER of the return value as a hard reg. */
367 insn
= skip_use_of_return_value (insn
, CLOBBER
);
371 /* Skip over a CLOBBER of the return value as a hard reg. */
372 insn
= skip_unreturned_value (insn
);
376 /* Skip over a USE of the return value (as a hard reg). */
377 insn
= skip_use_of_return_value (insn
, USE
);
381 /* Skip over a JUMP_INSN at the end of the block. If that doesn't end the
382 block, the original CALL_INSN didn't. */
383 insn
= skip_jump_insn (insn
);
387 /* Scan the rtx X for ADDRESSOF expressions or
388 current_function_internal_arg_pointer registers.
389 Return nonzero if an ADDRESSOF or current_function_internal_arg_pointer
390 is found outside of some MEM expression, else return zero. */
405 if (code
== ADDRESSOF
|| x
== current_function_internal_arg_pointer
)
411 /* Scan all subexpressions. */
412 fmt
= GET_RTX_FORMAT (code
);
413 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++, fmt
++)
417 if (uses_addressof (XEXP (x
, i
)))
420 else if (*fmt
== 'E')
422 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
423 if (uses_addressof (XVECEXP (x
, i
, j
)))
430 /* Scan the sequence of insns in SEQ to see if any have an ADDRESSOF
431 rtl expression or current_function_internal_arg_pointer occurences
432 not enclosed within a MEM. If an ADDRESSOF expression or
433 current_function_internal_arg_pointer is found, return nonzero, otherwise
436 This function handles CALL_PLACEHOLDERs which contain multiple sequences
440 sequence_uses_addressof (seq
)
445 for (insn
= seq
; insn
; insn
= NEXT_INSN (insn
))
448 /* If this is a CALL_PLACEHOLDER, then recursively call ourselves
449 with each nonempty sequence attached to the CALL_PLACEHOLDER. */
450 if (GET_CODE (insn
) == CALL_INSN
451 && GET_CODE (PATTERN (insn
)) == CALL_PLACEHOLDER
)
453 if (XEXP (PATTERN (insn
), 0) != NULL_RTX
454 && sequence_uses_addressof (XEXP (PATTERN (insn
), 0)))
456 if (XEXP (PATTERN (insn
), 1) != NULL_RTX
457 && sequence_uses_addressof (XEXP (PATTERN (insn
), 1)))
459 if (XEXP (PATTERN (insn
), 2) != NULL_RTX
460 && sequence_uses_addressof (XEXP (PATTERN (insn
), 2)))
463 else if (uses_addressof (PATTERN (insn
))
464 || (REG_NOTES (insn
) && uses_addressof (REG_NOTES (insn
))))
470 /* Remove all REG_EQUIV notes found in the insn chain. */
473 purge_reg_equiv_notes ()
477 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
481 rtx note
= find_reg_note (insn
, REG_EQUIV
, 0);
484 /* Remove the note and keep looking at the notes for
486 remove_note (insn
, note
);
494 /* Clear RTX_UNCHANGING_P flag of incoming argument MEMs. */
497 purge_mem_unchanging_flag (x
)
511 if (RTX_UNCHANGING_P (x
)
512 && (XEXP (x
, 0) == current_function_internal_arg_pointer
513 || (GET_CODE (XEXP (x
, 0)) == PLUS
514 && XEXP (XEXP (x
, 0), 0) ==
515 current_function_internal_arg_pointer
516 && GET_CODE (XEXP (XEXP (x
, 0), 1)) == CONST_INT
)))
517 RTX_UNCHANGING_P (x
) = 0;
521 /* Scan all subexpressions. */
522 fmt
= GET_RTX_FORMAT (code
);
523 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++, fmt
++)
526 purge_mem_unchanging_flag (XEXP (x
, i
));
527 else if (*fmt
== 'E')
528 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
529 purge_mem_unchanging_flag (XVECEXP (x
, i
, j
));
533 /* Replace the CALL_PLACEHOLDER with one of its children. INSN should be
534 the CALL_PLACEHOLDER insn; USE tells which child to use. */
537 replace_call_placeholder (insn
, use
)
541 if (use
== sibcall_use_tail_recursion
)
542 emit_insns_before (XEXP (PATTERN (insn
), 2), insn
);
543 else if (use
== sibcall_use_sibcall
)
544 emit_insns_before (XEXP (PATTERN (insn
), 1), insn
);
545 else if (use
== sibcall_use_normal
)
546 emit_insns_before (XEXP (PATTERN (insn
), 0), insn
);
550 /* Turn off LABEL_PRESERVE_P for the tail recursion label if it
551 exists. We only had to set it long enough to keep the jump
552 pass above from deleting it as unused. */
553 if (XEXP (PATTERN (insn
), 3))
554 LABEL_PRESERVE_P (XEXP (PATTERN (insn
), 3)) = 0;
556 /* "Delete" the placeholder insn. */
557 PUT_CODE (insn
, NOTE
);
558 NOTE_SOURCE_FILE (insn
) = 0;
559 NOTE_LINE_NUMBER (insn
) = NOTE_INSN_DELETED
;
562 /* Given a (possibly empty) set of potential sibling or tail recursion call
563 sites, determine if optimization is possible.
565 Potential sibling or tail recursion calls are marked with CALL_PLACEHOLDER
566 insns. The CALL_PLACEHOLDER insn holds chains of insns to implement a
567 normal call, sibling call or tail recursive call.
569 Replace the CALL_PLACEHOLDER with an appropriate insn chain. */
572 optimize_sibling_and_tail_recursive_calls ()
575 basic_block alternate_exit
= EXIT_BLOCK_PTR
;
576 int current_function_uses_addressof
;
577 int successful_sibling_call
= 0;
578 int replaced_call_placeholder
= 0;
581 insns
= get_insns ();
583 /* We do not perform these calls when flag_exceptions is true, so this
584 is probably a NOP at the current time. However, we may want to support
585 sibling and tail recursion optimizations in the future, so let's plan
586 ahead and find all the EH labels. */
587 find_exception_handler_labels ();
589 rebuild_jump_labels (insns
);
590 /* We need cfg information to determine which blocks are succeeded
591 only by the epilogue. */
592 find_basic_blocks (insns
, max_reg_num (), 0);
593 cleanup_cfg (CLEANUP_PRE_SIBCALL
| CLEANUP_PRE_LOOP
);
595 /* If there are no basic blocks, then there is nothing to do. */
596 if (n_basic_blocks
== 0)
599 return_value_pseudo
= NULL_RTX
;
601 /* Find the exit block.
603 It is possible that we have blocks which can reach the exit block
604 directly. However, most of the time a block will jump (or fall into)
605 N_BASIC_BLOCKS - 1, which in turn falls into the exit block. */
606 for (e
= EXIT_BLOCK_PTR
->pred
;
607 e
&& alternate_exit
== EXIT_BLOCK_PTR
;
612 if (e
->dest
!= EXIT_BLOCK_PTR
|| e
->succ_next
!= NULL
)
615 /* Walk forwards through the last normal block and see if it
616 does nothing except fall into the exit block. */
617 for (insn
= BLOCK_HEAD (n_basic_blocks
- 1);
619 insn
= NEXT_INSN (insn
))
622 /* This should only happen once, at the start of this block. */
623 if (GET_CODE (insn
) == CODE_LABEL
)
626 if (GET_CODE (insn
) == NOTE
)
629 if (GET_CODE (insn
) == INSN
630 && GET_CODE (PATTERN (insn
)) == USE
)
633 /* Exit block also may contain copy from pseudo containing
634 return value to hard register. */
635 if (GET_CODE (insn
) == INSN
636 && (set
= single_set (insn
))
637 && SET_DEST (set
) == current_function_return_rtx
638 && REG_P (SET_SRC (set
))
639 && !return_value_pseudo
)
641 return_value_pseudo
= SET_SRC (set
);
648 /* If INSN is zero, then the search walked all the way through the
649 block without hitting anything interesting. This block is a
650 valid alternate exit block. */
652 alternate_exit
= e
->src
;
654 return_value_pseudo
= NULL
;
657 /* If the function uses ADDRESSOF, we can't (easily) determine
658 at this point if the value will end up on the stack. */
659 current_function_uses_addressof
= sequence_uses_addressof (insns
);
661 /* Walk the insn chain and find any CALL_PLACEHOLDER insns. We need to
662 select one of the insn sequences attached to each CALL_PLACEHOLDER.
664 The different sequences represent different ways to implement the call,
665 ie, tail recursion, sibling call or normal call.
667 Since we do not create nested CALL_PLACEHOLDERs, the scan
668 continues with the insn that was after a replaced CALL_PLACEHOLDER;
669 we don't rescan the replacement insns. */
670 for (insn
= insns
; insn
; insn
= NEXT_INSN (insn
))
672 if (GET_CODE (insn
) == CALL_INSN
673 && GET_CODE (PATTERN (insn
)) == CALL_PLACEHOLDER
)
675 int sibcall
= (XEXP (PATTERN (insn
), 1) != NULL_RTX
);
676 int tailrecursion
= (XEXP (PATTERN (insn
), 2) != NULL_RTX
);
677 basic_block call_block
= BLOCK_FOR_INSN (insn
);
679 /* alloca (until we have stack slot life analysis) inhibits
680 sibling call optimizations, but not tail recursion.
681 Similarly if we use varargs or stdarg since they implicitly
682 may take the address of an argument. */
683 if (current_function_calls_alloca
684 || current_function_varargs
|| current_function_stdarg
)
687 /* See if there are any reasons we can't perform either sibling or
688 tail call optimizations. We must be careful with stack slots
689 which are live at potential optimization sites. ??? The first
690 test is overly conservative and should be replaced. */
692 /* Can't take address of local var if used by recursive call. */
693 || current_function_uses_addressof
694 /* Any function that calls setjmp might have longjmp called from
695 any called function. ??? We really should represent this
696 properly in the CFG so that this needn't be special cased. */
697 || current_function_calls_setjmp
698 /* Can't if more than one successor or single successor is not
699 exit block. These two tests prevent tail call optimization
700 in the presense of active exception handlers. */
701 || call_block
->succ
== NULL
702 || call_block
->succ
->succ_next
!= NULL
703 || (call_block
->succ
->dest
!= EXIT_BLOCK_PTR
704 && call_block
->succ
->dest
!= alternate_exit
)
705 /* If this call doesn't end the block, there are operations at
706 the end of the block which we must execute after returning. */
707 || ! call_ends_block_p (insn
, call_block
->end
))
708 sibcall
= 0, tailrecursion
= 0;
710 /* Select a set of insns to implement the call and emit them.
711 Tail recursion is the most efficient, so select it over
712 a tail/sibling call. */
714 successful_sibling_call
= 1;
716 replaced_call_placeholder
= 1;
717 replace_call_placeholder (insn
,
719 ? sibcall_use_tail_recursion
721 ? sibcall_use_sibcall
722 : sibcall_use_normal
);
726 if (successful_sibling_call
)
730 /* A sibling call sequence invalidates any REG_EQUIV notes made for
731 this function's incoming arguments.
733 At the start of RTL generation we know the only REG_EQUIV notes
734 in the rtl chain are those for incoming arguments, so we can safely
735 flush any REG_EQUIV note.
737 This is (slight) overkill. We could keep track of the highest
738 argument we clobber and be more selective in removing notes, but it
739 does not seem to be worth the effort. */
740 purge_reg_equiv_notes ();
742 /* A sibling call sequence also may invalidate RTX_UNCHANGING_P
743 flag of some incoming arguments MEM RTLs, because it can write into
744 those slots. We clear all those bits now.
746 This is (slight) overkill, we could keep track of which arguments
747 we actually write into. */
748 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
750 if (GET_CODE (insn
) == NOTE
)
752 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_FUNCTION_BEG
)
755 else if (INSN_P (insn
))
756 purge_mem_unchanging_flag (PATTERN (insn
));
760 /* There may have been NOTE_INSN_BLOCK_{BEGIN,END} notes in the
761 CALL_PLACEHOLDER alternatives that we didn't emit. Rebuild the
762 lexical block tree to correspond to the notes that still exist. */
763 if (replaced_call_placeholder
)
766 /* This information will be invalid after inline expansion. Kill it now. */
767 free_basic_block_vars (0);