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
163 && SET_SRC (set
) == softret
)
168 /* The destination must be the same as the called function's return
169 value to ensure that any return value is put in the same place by the
170 current function and the function we're calling.
172 Further, the source must be the same as the pseudo into which the
173 called function's return value was copied. Otherwise we're returning
176 #ifndef OUTGOING_REGNO
177 #define OUTGOING_REGNO(N) (N)
180 if (SET_DEST (set
) == current_function_return_rtx
181 && REG_P (SET_DEST (set
))
182 && OUTGOING_REGNO (REGNO (SET_DEST (set
))) == REGNO (hardret
)
183 && SET_SRC (set
) == softret
)
186 /* Recognize the situation when the called function's return value
187 is copied in two steps: first into an intermediate pseudo, then
188 the into the calling functions return value register. */
190 if (REG_P (SET_DEST (set
))
191 && SET_SRC (set
) == softret
)
193 rtx x
= SET_DEST (set
);
195 insn
= next_nonnote_insn (insn
);
199 set
= single_set (insn
);
203 if (SET_DEST (set
) == current_function_return_rtx
204 && REG_P (SET_DEST (set
))
205 && OUTGOING_REGNO (REGNO (SET_DEST (set
))) == REGNO (hardret
)
206 && SET_SRC (set
) == x
)
210 /* It did not look like a copy of the return value, so return the
211 same insn we were passed. */
215 /* If the first real insn after ORIG_INSN is a CODE of this function's return
216 value, return insn. Otherwise return ORIG_INSN. */
219 skip_use_of_return_value (orig_insn
, code
)
225 insn
= next_nonnote_insn (orig_insn
);
228 && GET_CODE (insn
) == INSN
229 && GET_CODE (PATTERN (insn
)) == code
230 && (XEXP (PATTERN (insn
), 0) == current_function_return_rtx
231 || XEXP (PATTERN (insn
), 0) == const0_rtx
))
237 /* In case function does not return value, we get clobber of pseudo followed
238 by set to hard return value. */
240 skip_unreturned_value (orig_insn
)
243 rtx insn
= next_nonnote_insn (orig_insn
);
245 /* Skip possible clobber of pseudo return register. */
247 && GET_CODE (insn
) == INSN
248 && GET_CODE (PATTERN (insn
)) == CLOBBER
249 && REG_P (XEXP (PATTERN (insn
), 0))
250 && (REGNO (XEXP (PATTERN (insn
), 0)) >= FIRST_PSEUDO_REGISTER
))
252 rtx set_insn
= next_nonnote_insn (insn
);
256 set
= single_set (set_insn
);
258 || SET_SRC (set
) != XEXP (PATTERN (insn
), 0)
259 || SET_DEST (set
) != current_function_return_rtx
)
266 /* If the first real insn after ORIG_INSN adjusts the stack pointer
267 by a constant, return the insn with the stack pointer adjustment.
268 Otherwise return ORIG_INSN. */
271 skip_stack_adjustment (orig_insn
)
274 rtx insn
, set
= NULL_RTX
;
276 insn
= next_nonnote_insn (orig_insn
);
279 set
= single_set (insn
);
283 && GET_CODE (SET_SRC (set
)) == PLUS
284 && XEXP (SET_SRC (set
), 0) == stack_pointer_rtx
285 && GET_CODE (XEXP (SET_SRC (set
), 1)) == CONST_INT
286 && SET_DEST (set
) == stack_pointer_rtx
)
292 /* If the first real insn after ORIG_INSN sets the pic register,
293 return it. Otherwise return ORIG_INSN. */
296 skip_pic_restore (orig_insn
)
299 rtx insn
, set
= NULL_RTX
;
301 insn
= next_nonnote_insn (orig_insn
);
304 set
= single_set (insn
);
306 if (insn
&& set
&& SET_DEST (set
) == pic_offset_table_rtx
)
312 /* If the first real insn after ORIG_INSN is a jump, return the JUMP_INSN.
313 Otherwise return ORIG_INSN. */
316 skip_jump_insn (orig_insn
)
321 insn
= next_nonnote_insn (orig_insn
);
324 && GET_CODE (insn
) == JUMP_INSN
325 && any_uncondjump_p (insn
))
331 /* Using the above functions, see if INSN, skipping any of the above,
332 goes all the way to END, the end of a basic block. Return 1 if so. */
335 call_ends_block_p (insn
, end
)
340 /* END might be a note, so get the last nonnote insn of the block. */
341 end
= next_nonnote_insn (PREV_INSN (end
));
343 /* If the call was the end of the block, then we're OK. */
347 /* Skip over copying from the call's return value pseudo into
348 this function's hard return register and if that's the end
349 of the block, we're OK. */
350 new_insn
= skip_copy_to_return_value (insn
);
352 /* In case we return value in pseudo, we must set the pseudo to
353 return value of called function, otherwise we are returning
355 if (return_value_pseudo
&& insn
== new_insn
)
362 /* Skip any stack adjustment. */
363 insn
= skip_stack_adjustment (insn
);
367 /* Skip over a CLOBBER of the return value as a hard reg. */
368 insn
= skip_use_of_return_value (insn
, CLOBBER
);
372 /* Skip over a CLOBBER of the return value as a hard reg. */
373 insn
= skip_unreturned_value (insn
);
377 /* Skip over a USE of the return value (as a hard reg). */
378 insn
= skip_use_of_return_value (insn
, USE
);
382 /* Skip over a JUMP_INSN at the end of the block. If that doesn't end the
383 block, the original CALL_INSN didn't. */
384 insn
= skip_jump_insn (insn
);
388 /* Scan the rtx X for ADDRESSOF expressions or
389 current_function_internal_arg_pointer registers.
390 Return nonzero if an ADDRESSOF or current_function_internal_arg_pointer
391 is found outside of some MEM expression, else return zero. */
406 if (code
== ADDRESSOF
|| x
== current_function_internal_arg_pointer
)
412 /* Scan all subexpressions. */
413 fmt
= GET_RTX_FORMAT (code
);
414 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++, fmt
++)
418 if (uses_addressof (XEXP (x
, i
)))
421 else if (*fmt
== 'E')
423 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
424 if (uses_addressof (XVECEXP (x
, i
, j
)))
431 /* Scan the sequence of insns in SEQ to see if any have an ADDRESSOF
432 rtl expression or current_function_internal_arg_pointer occurrences
433 not enclosed within a MEM. If an ADDRESSOF expression or
434 current_function_internal_arg_pointer is found, return nonzero, otherwise
437 This function handles CALL_PLACEHOLDERs which contain multiple sequences
441 sequence_uses_addressof (seq
)
446 for (insn
= seq
; insn
; insn
= NEXT_INSN (insn
))
449 /* If this is a CALL_PLACEHOLDER, then recursively call ourselves
450 with each nonempty sequence attached to the CALL_PLACEHOLDER. */
451 if (GET_CODE (insn
) == CALL_INSN
452 && GET_CODE (PATTERN (insn
)) == CALL_PLACEHOLDER
)
454 if (XEXP (PATTERN (insn
), 0) != NULL_RTX
455 && sequence_uses_addressof (XEXP (PATTERN (insn
), 0)))
457 if (XEXP (PATTERN (insn
), 1) != NULL_RTX
458 && sequence_uses_addressof (XEXP (PATTERN (insn
), 1)))
460 if (XEXP (PATTERN (insn
), 2) != NULL_RTX
461 && sequence_uses_addressof (XEXP (PATTERN (insn
), 2)))
464 else if (uses_addressof (PATTERN (insn
))
465 || (REG_NOTES (insn
) && uses_addressof (REG_NOTES (insn
))))
471 /* Remove all REG_EQUIV notes found in the insn chain. */
474 purge_reg_equiv_notes ()
478 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
482 rtx note
= find_reg_note (insn
, REG_EQUIV
, 0);
485 /* Remove the note and keep looking at the notes for
487 remove_note (insn
, note
);
495 /* Clear RTX_UNCHANGING_P flag of incoming argument MEMs. */
498 purge_mem_unchanging_flag (x
)
512 if (RTX_UNCHANGING_P (x
)
513 && (XEXP (x
, 0) == current_function_internal_arg_pointer
514 || (GET_CODE (XEXP (x
, 0)) == PLUS
515 && XEXP (XEXP (x
, 0), 0) ==
516 current_function_internal_arg_pointer
517 && GET_CODE (XEXP (XEXP (x
, 0), 1)) == CONST_INT
)))
518 RTX_UNCHANGING_P (x
) = 0;
522 /* Scan all subexpressions. */
523 fmt
= GET_RTX_FORMAT (code
);
524 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++, fmt
++)
527 purge_mem_unchanging_flag (XEXP (x
, i
));
528 else if (*fmt
== 'E')
529 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
530 purge_mem_unchanging_flag (XVECEXP (x
, i
, j
));
534 /* Replace the CALL_PLACEHOLDER with one of its children. INSN should be
535 the CALL_PLACEHOLDER insn; USE tells which child to use. */
538 replace_call_placeholder (insn
, use
)
542 if (use
== sibcall_use_tail_recursion
)
543 emit_insns_before (XEXP (PATTERN (insn
), 2), insn
);
544 else if (use
== sibcall_use_sibcall
)
545 emit_insns_before (XEXP (PATTERN (insn
), 1), insn
);
546 else if (use
== sibcall_use_normal
)
547 emit_insns_before (XEXP (PATTERN (insn
), 0), insn
);
551 /* Turn off LABEL_PRESERVE_P for the tail recursion label if it
552 exists. We only had to set it long enough to keep the jump
553 pass above from deleting it as unused. */
554 if (XEXP (PATTERN (insn
), 3))
555 LABEL_PRESERVE_P (XEXP (PATTERN (insn
), 3)) = 0;
557 /* "Delete" the placeholder insn. */
561 /* Given a (possibly empty) set of potential sibling or tail recursion call
562 sites, determine if optimization is possible.
564 Potential sibling or tail recursion calls are marked with CALL_PLACEHOLDER
565 insns. The CALL_PLACEHOLDER insn holds chains of insns to implement a
566 normal call, sibling call or tail recursive call.
568 Replace the CALL_PLACEHOLDER with an appropriate insn chain. */
571 optimize_sibling_and_tail_recursive_calls ()
574 basic_block alternate_exit
= EXIT_BLOCK_PTR
;
575 int current_function_uses_addressof
;
576 int successful_sibling_call
= 0;
577 int replaced_call_placeholder
= 0;
580 insns
= get_insns ();
582 /* We do not perform these calls when flag_exceptions is true, so this
583 is probably a NOP at the current time. However, we may want to support
584 sibling and tail recursion optimizations in the future, so let's plan
585 ahead and find all the EH labels. */
586 find_exception_handler_labels ();
588 rebuild_jump_labels (insns
);
589 /* We need cfg information to determine which blocks are succeeded
590 only by the epilogue. */
591 find_basic_blocks (insns
, max_reg_num (), 0);
592 cleanup_cfg (CLEANUP_PRE_SIBCALL
| CLEANUP_PRE_LOOP
);
594 /* If there are no basic blocks, then there is nothing to do. */
595 if (n_basic_blocks
== 0)
598 return_value_pseudo
= NULL_RTX
;
600 /* Find the exit block.
602 It is possible that we have blocks which can reach the exit block
603 directly. However, most of the time a block will jump (or fall into)
604 N_BASIC_BLOCKS - 1, which in turn falls into the exit block. */
605 for (e
= EXIT_BLOCK_PTR
->pred
;
606 e
&& alternate_exit
== EXIT_BLOCK_PTR
;
611 if (e
->dest
!= EXIT_BLOCK_PTR
|| e
->succ_next
!= NULL
)
614 /* Walk forwards through the last normal block and see if it
615 does nothing except fall into the exit block. */
616 for (insn
= BLOCK_HEAD (n_basic_blocks
- 1);
618 insn
= NEXT_INSN (insn
))
621 /* This should only happen once, at the start of this block. */
622 if (GET_CODE (insn
) == CODE_LABEL
)
625 if (GET_CODE (insn
) == NOTE
)
628 if (GET_CODE (insn
) == INSN
629 && GET_CODE (PATTERN (insn
)) == USE
)
632 /* Exit block also may contain copy from pseudo containing
633 return value to hard register. */
634 if (GET_CODE (insn
) == INSN
635 && (set
= single_set (insn
))
636 && SET_DEST (set
) == current_function_return_rtx
637 && REG_P (SET_SRC (set
))
638 && !return_value_pseudo
)
640 return_value_pseudo
= SET_SRC (set
);
647 /* If INSN is zero, then the search walked all the way through the
648 block without hitting anything interesting. This block is a
649 valid alternate exit block. */
651 alternate_exit
= e
->src
;
653 return_value_pseudo
= NULL
;
656 /* If the function uses ADDRESSOF, we can't (easily) determine
657 at this point if the value will end up on the stack. */
658 current_function_uses_addressof
= sequence_uses_addressof (insns
);
660 /* Walk the insn chain and find any CALL_PLACEHOLDER insns. We need to
661 select one of the insn sequences attached to each CALL_PLACEHOLDER.
663 The different sequences represent different ways to implement the call,
664 ie, tail recursion, sibling call or normal call.
666 Since we do not create nested CALL_PLACEHOLDERs, the scan
667 continues with the insn that was after a replaced CALL_PLACEHOLDER;
668 we don't rescan the replacement insns. */
669 for (insn
= insns
; insn
; insn
= NEXT_INSN (insn
))
671 if (GET_CODE (insn
) == CALL_INSN
672 && GET_CODE (PATTERN (insn
)) == CALL_PLACEHOLDER
)
674 int sibcall
= (XEXP (PATTERN (insn
), 1) != NULL_RTX
);
675 int tailrecursion
= (XEXP (PATTERN (insn
), 2) != NULL_RTX
);
676 basic_block call_block
= BLOCK_FOR_INSN (insn
);
678 /* alloca (until we have stack slot life analysis) inhibits
679 sibling call optimizations, but not tail recursion.
680 Similarly if we use varargs or stdarg since they implicitly
681 may take the address of an argument. */
682 if (current_function_calls_alloca
683 || current_function_varargs
|| current_function_stdarg
)
686 /* See if there are any reasons we can't perform either sibling or
687 tail call optimizations. We must be careful with stack slots
688 which are live at potential optimization sites. ??? The first
689 test is overly conservative and should be replaced. */
691 /* Can't take address of local var if used by recursive call. */
692 || current_function_uses_addressof
693 /* Any function that calls setjmp might have longjmp called from
694 any called function. ??? We really should represent this
695 properly in the CFG so that this needn't be special cased. */
696 || current_function_calls_setjmp
697 /* Can't if more than one successor or single successor is not
698 exit block. These two tests prevent tail call optimization
699 in the presense of active exception handlers. */
700 || call_block
->succ
== NULL
701 || call_block
->succ
->succ_next
!= NULL
702 || (call_block
->succ
->dest
!= EXIT_BLOCK_PTR
703 && call_block
->succ
->dest
!= alternate_exit
)
704 /* If this call doesn't end the block, there are operations at
705 the end of the block which we must execute after returning. */
706 || ! call_ends_block_p (insn
, call_block
->end
))
707 sibcall
= 0, tailrecursion
= 0;
709 /* Select a set of insns to implement the call and emit them.
710 Tail recursion is the most efficient, so select it over
711 a tail/sibling call. */
713 successful_sibling_call
= 1;
715 replaced_call_placeholder
= 1;
716 replace_call_placeholder (insn
,
718 ? sibcall_use_tail_recursion
720 ? sibcall_use_sibcall
721 : sibcall_use_normal
);
725 if (successful_sibling_call
)
729 /* A sibling call sequence invalidates any REG_EQUIV notes made for
730 this function's incoming arguments.
732 At the start of RTL generation we know the only REG_EQUIV notes
733 in the rtl chain are those for incoming arguments, so we can safely
734 flush any REG_EQUIV note.
736 This is (slight) overkill. We could keep track of the highest
737 argument we clobber and be more selective in removing notes, but it
738 does not seem to be worth the effort. */
739 purge_reg_equiv_notes ();
741 /* A sibling call sequence also may invalidate RTX_UNCHANGING_P
742 flag of some incoming arguments MEM RTLs, because it can write into
743 those slots. We clear all those bits now.
745 This is (slight) overkill, we could keep track of which arguments
746 we actually write into. */
747 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
750 purge_mem_unchanging_flag (PATTERN (insn
));
754 /* There may have been NOTE_INSN_BLOCK_{BEGIN,END} notes in the
755 CALL_PLACEHOLDER alternatives that we didn't emit. Rebuild the
756 lexical block tree to correspond to the notes that still exist. */
757 if (replaced_call_placeholder
)
760 /* This information will be invalid after inline expansion. Kill it now. */
761 free_basic_block_vars (0);