* gcc_release: New script.
[official-gcc.git] / gcc / sibcall.c
blob98bc0c6e4d85177b1fe7fbe667238aeb81d4de4a
1 /* Generic sibling call optimization support
2 Copyright (C) 1999, 2000 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)
9 any later version.
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. */
21 #include "config.h"
22 #include "system.h"
24 #include "rtl.h"
25 #include "regs.h"
26 #include "function.h"
27 #include "hard-reg-set.h"
28 #include "flags.h"
29 #include "insn-config.h"
30 #include "recog.h"
31 #include "basic-block.h"
32 #include "output.h"
33 #include "except.h"
35 static int identify_call_return_value PARAMS ((rtx, rtx *, rtx *));
36 static rtx skip_copy_to_return_value PARAMS ((rtx, rtx, 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 uses_addressof PARAMS ((rtx));
42 static int sequence_uses_addressof PARAMS ((rtx));
43 static void purge_reg_equiv_notes PARAMS ((void));
44 static void purge_mem_unchanging_flag PARAMS ((rtx));
46 /* Examine a CALL_PLACEHOLDER pattern and determine where the call's
47 return value is located. P_HARD_RETURN receives the hard register
48 that the function used; P_SOFT_RETURN receives the pseudo register
49 that the sequence used. Return non-zero if the values were located. */
51 static int
52 identify_call_return_value (cp, p_hard_return, p_soft_return)
53 rtx cp;
54 rtx *p_hard_return, *p_soft_return;
56 rtx insn, set, hard, soft;
58 insn = XEXP (cp, 0);
59 /* Search backward through the "normal" call sequence to the CALL insn. */
60 while (NEXT_INSN (insn))
61 insn = NEXT_INSN (insn);
62 while (GET_CODE (insn) != CALL_INSN)
63 insn = PREV_INSN (insn);
65 /* Assume the pattern is (set (dest) (call ...)), or that the first
66 member of a parallel is. This is the hard return register used
67 by the function. */
68 if (GET_CODE (PATTERN (insn)) == SET
69 && GET_CODE (SET_SRC (PATTERN (insn))) == CALL)
70 hard = SET_DEST (PATTERN (insn));
71 else if (GET_CODE (PATTERN (insn)) == PARALLEL
72 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
73 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == CALL)
74 hard = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
75 else
76 return 0;
78 /* If we didn't get a single hard register (e.g. a parallel), give up. */
79 if (GET_CODE (hard) != REG)
80 return 0;
82 /* Stack adjustment done after call may appear here. */
83 insn = skip_stack_adjustment (insn);
84 if (! insn)
85 return 0;
87 /* Restore of GP register may appear here. */
88 insn = skip_pic_restore (insn);
89 if (! insn)
90 return 0;
92 /* If there's nothing after, there's no soft return value. */
93 insn = NEXT_INSN (insn);
94 if (! insn)
95 return 0;
97 /* We're looking for a source of the hard return register. */
98 set = single_set (insn);
99 if (! set || SET_SRC (set) != hard)
100 return 0;
102 soft = SET_DEST (set);
103 insn = NEXT_INSN (insn);
105 /* Allow this first destination to be copied to a second register,
106 as might happen if the first register wasn't the particular pseudo
107 we'd been expecting. */
108 if (insn
109 && (set = single_set (insn)) != NULL_RTX
110 && SET_SRC (set) == soft)
112 soft = SET_DEST (set);
113 insn = NEXT_INSN (insn);
116 /* Don't fool with anything but pseudo registers. */
117 if (GET_CODE (soft) != REG || REGNO (soft) < FIRST_PSEUDO_REGISTER)
118 return 0;
120 /* This value must not be modified before the end of the sequence. */
121 if (reg_set_between_p (soft, insn, NULL_RTX))
122 return 0;
124 *p_hard_return = hard;
125 *p_soft_return = soft;
127 return 1;
130 /* If the first real insn after ORIG_INSN copies to this function's
131 return value from RETVAL, then return the insn which performs the
132 copy. Otherwise return ORIG_INSN. */
134 static rtx
135 skip_copy_to_return_value (orig_insn, hardret, softret)
136 rtx orig_insn;
137 rtx hardret, softret;
139 rtx insn, set = NULL_RTX;
141 insn = next_nonnote_insn (orig_insn);
142 if (! insn)
143 return orig_insn;
145 set = single_set (insn);
146 if (! set)
147 return orig_insn;
149 /* The destination must be the same as the called function's return
150 value to ensure that any return value is put in the same place by the
151 current function and the function we're calling.
153 Further, the source must be the same as the pseudo into which the
154 called function's return value was copied. Otherwise we're returning
155 some other value. */
157 #ifndef OUTGOING_REGNO
158 #define OUTGOING_REGNO(N) (N)
159 #endif
161 if (SET_DEST (set) == current_function_return_rtx
162 && REG_P (SET_DEST (set))
163 && OUTGOING_REGNO (REGNO (SET_DEST (set))) == REGNO (hardret)
164 && SET_SRC (set) == softret)
165 return insn;
167 /* It did not look like a copy of the return value, so return the
168 same insn we were passed. */
169 return orig_insn;
172 /* If the first real insn after ORIG_INSN is a CODE of this function's return
173 value, return insn. Otherwise return ORIG_INSN. */
175 static rtx
176 skip_use_of_return_value (orig_insn, code)
177 rtx orig_insn;
178 enum rtx_code code;
180 rtx insn;
182 insn = next_nonnote_insn (orig_insn);
184 if (insn
185 && GET_CODE (insn) == INSN
186 && GET_CODE (PATTERN (insn)) == code
187 && (XEXP (PATTERN (insn), 0) == current_function_return_rtx
188 || XEXP (PATTERN (insn), 0) == const0_rtx))
189 return insn;
191 return orig_insn;
194 /* If the first real insn after ORIG_INSN adjusts the stack pointer
195 by a constant, return the insn with the stack pointer adjustment.
196 Otherwise return ORIG_INSN. */
198 static rtx
199 skip_stack_adjustment (orig_insn)
200 rtx orig_insn;
202 rtx insn, set = NULL_RTX;
204 insn = next_nonnote_insn (orig_insn);
206 if (insn)
207 set = single_set (insn);
209 if (insn
210 && set
211 && GET_CODE (SET_SRC (set)) == PLUS
212 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
213 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
214 && SET_DEST (set) == stack_pointer_rtx)
215 return insn;
217 return orig_insn;
220 /* If the first real insn after ORIG_INSN sets the pic register,
221 return it. Otherwise return ORIG_INSN. */
223 static rtx
224 skip_pic_restore (orig_insn)
225 rtx orig_insn;
227 rtx insn, set = NULL_RTX;
229 insn = next_nonnote_insn (orig_insn);
231 if (insn)
232 set = single_set (insn);
234 if (insn && set && SET_DEST (set) == pic_offset_table_rtx)
235 return insn;
237 return orig_insn;
240 /* If the first real insn after ORIG_INSN is a jump, return the JUMP_INSN.
241 Otherwise return ORIG_INSN. */
243 static rtx
244 skip_jump_insn (orig_insn)
245 rtx orig_insn;
247 rtx insn;
249 insn = next_nonnote_insn (orig_insn);
251 if (insn
252 && GET_CODE (insn) == JUMP_INSN
253 && any_uncondjump_p (insn))
254 return insn;
256 return orig_insn;
259 /* Scan the rtx X for ADDRESSOF expressions or
260 current_function_internal_arg_pointer registers.
261 Return nonzero if an ADDRESSOF or current_function_internal_arg_pointer
262 is found outside of some MEM expression, else return zero. */
264 static int
265 uses_addressof (x)
266 rtx x;
268 RTX_CODE code;
269 int i, j;
270 const char *fmt;
272 if (x == NULL_RTX)
273 return 0;
275 code = GET_CODE (x);
277 if (code == ADDRESSOF || x == current_function_internal_arg_pointer)
278 return 1;
280 if (code == MEM)
281 return 0;
283 /* Scan all subexpressions. */
284 fmt = GET_RTX_FORMAT (code);
285 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
287 if (*fmt == 'e')
289 if (uses_addressof (XEXP (x, i)))
290 return 1;
292 else if (*fmt == 'E')
294 for (j = 0; j < XVECLEN (x, i); j++)
295 if (uses_addressof (XVECEXP (x, i, j)))
296 return 1;
299 return 0;
302 /* Scan the sequence of insns in SEQ to see if any have an ADDRESSOF
303 rtl expression or current_function_internal_arg_pointer occurences
304 not enclosed within a MEM. If an ADDRESSOF expression or
305 current_function_internal_arg_pointer is found, return nonzero, otherwise
306 return zero.
308 This function handles CALL_PLACEHOLDERs which contain multiple sequences
309 of insns. */
311 static int
312 sequence_uses_addressof (seq)
313 rtx seq;
315 rtx insn;
317 for (insn = seq; insn; insn = NEXT_INSN (insn))
318 if (INSN_P (insn))
320 /* If this is a CALL_PLACEHOLDER, then recursively call ourselves
321 with each nonempty sequence attached to the CALL_PLACEHOLDER. */
322 if (GET_CODE (insn) == CALL_INSN
323 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
325 if (XEXP (PATTERN (insn), 0) != NULL_RTX
326 && sequence_uses_addressof (XEXP (PATTERN (insn), 0)))
327 return 1;
328 if (XEXP (PATTERN (insn), 1) != NULL_RTX
329 && sequence_uses_addressof (XEXP (PATTERN (insn), 1)))
330 return 1;
331 if (XEXP (PATTERN (insn), 2) != NULL_RTX
332 && sequence_uses_addressof (XEXP (PATTERN (insn), 2)))
333 return 1;
335 else if (uses_addressof (PATTERN (insn))
336 || (REG_NOTES (insn) && uses_addressof (REG_NOTES (insn))))
337 return 1;
339 return 0;
342 /* Remove all REG_EQUIV notes found in the insn chain. */
344 static void
345 purge_reg_equiv_notes ()
347 rtx insn;
349 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
351 while (1)
353 rtx note = find_reg_note (insn, REG_EQUIV, 0);
354 if (note)
356 /* Remove the note and keep looking at the notes for
357 this insn. */
358 remove_note (insn, note);
359 continue;
361 break;
366 /* Clear RTX_UNCHANGING_P flag of incoming argument MEMs. */
368 static void
369 purge_mem_unchanging_flag (x)
370 rtx x;
372 RTX_CODE code;
373 int i, j;
374 const char *fmt;
376 if (x == NULL_RTX)
377 return;
379 code = GET_CODE (x);
381 if (code == MEM)
383 if (RTX_UNCHANGING_P (x)
384 && (XEXP (x, 0) == current_function_internal_arg_pointer
385 || (GET_CODE (XEXP (x, 0)) == PLUS
386 && XEXP (XEXP (x, 0), 0) ==
387 current_function_internal_arg_pointer
388 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
389 RTX_UNCHANGING_P (x) = 0;
390 return;
393 /* Scan all subexpressions. */
394 fmt = GET_RTX_FORMAT (code);
395 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
397 if (*fmt == 'e')
398 purge_mem_unchanging_flag (XEXP (x, i));
399 else if (*fmt == 'E')
400 for (j = 0; j < XVECLEN (x, i); j++)
401 purge_mem_unchanging_flag (XVECEXP (x, i, j));
405 /* Replace the CALL_PLACEHOLDER with one of its children. INSN should be
406 the CALL_PLACEHOLDER insn; USE tells which child to use. */
408 void
409 replace_call_placeholder (insn, use)
410 rtx insn;
411 sibcall_use_t use;
413 if (use == sibcall_use_tail_recursion)
414 emit_insns_before (XEXP (PATTERN (insn), 2), insn);
415 else if (use == sibcall_use_sibcall)
416 emit_insns_before (XEXP (PATTERN (insn), 1), insn);
417 else if (use == sibcall_use_normal)
418 emit_insns_before (XEXP (PATTERN (insn), 0), insn);
419 else
420 abort();
422 /* Turn off LABEL_PRESERVE_P for the tail recursion label if it
423 exists. We only had to set it long enough to keep the jump
424 pass above from deleting it as unused. */
425 if (XEXP (PATTERN (insn), 3))
426 LABEL_PRESERVE_P (XEXP (PATTERN (insn), 3)) = 0;
428 /* "Delete" the placeholder insn. */
429 PUT_CODE (insn, NOTE);
430 NOTE_SOURCE_FILE (insn) = 0;
431 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
434 /* Given a (possibly empty) set of potential sibling or tail recursion call
435 sites, determine if optimization is possible.
437 Potential sibling or tail recursion calls are marked with CALL_PLACEHOLDER
438 insns. The CALL_PLACEHOLDER insn holds chains of insns to implement a
439 normal call, sibling call or tail recursive call.
441 Replace the CALL_PLACEHOLDER with an appropriate insn chain. */
443 void
444 optimize_sibling_and_tail_recursive_calls ()
446 rtx insn, insns;
447 basic_block alternate_exit = EXIT_BLOCK_PTR;
448 int current_function_uses_addressof;
449 int successful_sibling_call = 0;
450 int replaced_call_placeholder = 0;
451 edge e;
453 insns = get_insns ();
455 /* We do not perform these calls when flag_exceptions is true, so this
456 is probably a NOP at the current time. However, we may want to support
457 sibling and tail recursion optimizations in the future, so let's plan
458 ahead and find all the EH labels. */
459 find_exception_handler_labels ();
461 /* Run a jump optimization pass to clean up the CFG. We primarily want
462 this to thread jumps so that it is obvious which blocks jump to the
463 epilouge. */
464 jump_optimize_minimal (insns);
466 /* We need cfg information to determine which blocks are succeeded
467 only by the epilogue. */
468 find_basic_blocks (insns, max_reg_num (), 0);
469 cleanup_cfg ();
471 /* If there are no basic blocks, then there is nothing to do. */
472 if (n_basic_blocks == 0)
473 return;
475 /* Find the exit block.
477 It is possible that we have blocks which can reach the exit block
478 directly. However, most of the time a block will jump (or fall into)
479 N_BASIC_BLOCKS - 1, which in turn falls into the exit block. */
480 for (e = EXIT_BLOCK_PTR->pred;
481 e && alternate_exit == EXIT_BLOCK_PTR;
482 e = e->pred_next)
484 rtx insn;
486 if (e->dest != EXIT_BLOCK_PTR || e->succ_next != NULL)
487 continue;
489 /* Walk forwards through the last normal block and see if it
490 does nothing except fall into the exit block. */
491 for (insn = BLOCK_HEAD (n_basic_blocks - 1);
492 insn;
493 insn = NEXT_INSN (insn))
495 /* This should only happen once, at the start of this block. */
496 if (GET_CODE (insn) == CODE_LABEL)
497 continue;
499 if (GET_CODE (insn) == NOTE)
500 continue;
502 if (GET_CODE (insn) == INSN
503 && GET_CODE (PATTERN (insn)) == USE)
504 continue;
506 break;
509 /* If INSN is zero, then the search walked all the way through the
510 block without hitting anything interesting. This block is a
511 valid alternate exit block. */
512 if (insn == NULL)
513 alternate_exit = e->src;
516 /* If the function uses ADDRESSOF, we can't (easily) determine
517 at this point if the value will end up on the stack. */
518 current_function_uses_addressof = sequence_uses_addressof (insns);
520 /* Walk the insn chain and find any CALL_PLACEHOLDER insns. We need to
521 select one of the insn sequences attached to each CALL_PLACEHOLDER.
523 The different sequences represent different ways to implement the call,
524 ie, tail recursion, sibling call or normal call.
526 Since we do not create nested CALL_PLACEHOLDERs, the scan
527 continues with the insn that was after a replaced CALL_PLACEHOLDER;
528 we don't rescan the replacement insns. */
529 for (insn = insns; insn; insn = NEXT_INSN (insn))
531 if (GET_CODE (insn) == CALL_INSN
532 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
534 int sibcall = (XEXP (PATTERN (insn), 1) != NULL_RTX);
535 int tailrecursion = (XEXP (PATTERN (insn), 2) != NULL_RTX);
536 basic_block succ_block, call_block;
537 rtx temp, hardret, softret;
539 /* We must be careful with stack slots which are live at
540 potential optimization sites.
542 ?!? This test is overly conservative and will be replaced. */
543 if (frame_offset)
544 goto failure;
546 /* Taking the address of a local variable is fatal to tail
547 recursion if the address is used by the recursive call. */
548 if (current_function_uses_addressof)
549 goto failure;
551 /* alloca (until we have stack slot life analysis) inhibits
552 sibling call optimizations, but not tail recursion.
553 Similarly if we use varargs or stdarg since they implicitly
554 may take the address of an argument. */
555 if (current_function_calls_alloca
556 || current_function_varargs || current_function_stdarg)
557 sibcall = 0;
559 call_block = BLOCK_FOR_INSN (insn);
561 /* If the block has more than one successor, then we can not
562 perform sibcall or tail recursion optimizations. */
563 if (call_block->succ == NULL
564 || call_block->succ->succ_next != NULL)
565 goto failure;
567 /* If the single successor is not the exit block, then we can not
568 perform sibcall or tail recursion optimizations.
570 Note that this test combined with the previous is sufficient
571 to prevent tail call optimization in the presense of active
572 exception handlers. */
573 succ_block = call_block->succ->dest;
574 if (succ_block != EXIT_BLOCK_PTR && succ_block != alternate_exit)
575 goto failure;
577 /* If the call was the end of the block, then we're OK. */
578 temp = insn;
579 if (temp == call_block->end)
580 goto success;
582 /* Skip over copying from the call's return value pseudo into
583 this function's hard return register. */
584 if (identify_call_return_value (PATTERN (insn), &hardret, &softret))
586 temp = skip_copy_to_return_value (temp, hardret, softret);
587 if (temp == call_block->end)
588 goto success;
591 /* Skip any stack adjustment. */
592 temp = skip_stack_adjustment (temp);
593 if (temp == call_block->end)
594 goto success;
596 /* Skip over a CLOBBER of the return value (as a hard reg). */
597 temp = skip_use_of_return_value (temp, CLOBBER);
598 if (temp == call_block->end)
599 goto success;
601 /* Skip over a USE of the return value (as a hard reg). */
602 temp = skip_use_of_return_value (temp, USE);
603 if (temp == call_block->end)
604 goto success;
606 /* Skip over the JUMP_INSN at the end of the block. */
607 temp = skip_jump_insn (temp);
608 if (GET_CODE (temp) == NOTE)
609 temp = next_nonnote_insn (temp);
610 if (temp == call_block->end)
611 goto success;
613 /* There are operations at the end of the block which we must
614 execute after returning from the function call. So this call
615 can not be optimized. */
616 failure:
617 sibcall = 0, tailrecursion = 0;
618 success:
620 /* Select a set of insns to implement the call and emit them.
621 Tail recursion is the most efficient, so select it over
622 a tail/sibling call. */
624 if (sibcall)
625 successful_sibling_call = 1;
626 replaced_call_placeholder = 1;
627 replace_call_placeholder (insn,
628 tailrecursion != 0
629 ? sibcall_use_tail_recursion
630 : sibcall != 0
631 ? sibcall_use_sibcall
632 : sibcall_use_normal);
636 if (successful_sibling_call)
638 rtx insn;
640 /* A sibling call sequence invalidates any REG_EQUIV notes made for
641 this function's incoming arguments.
643 At the start of RTL generation we know the only REG_EQUIV notes
644 in the rtl chain are those for incoming arguments, so we can safely
645 flush any REG_EQUIV note.
647 This is (slight) overkill. We could keep track of the highest
648 argument we clobber and be more selective in removing notes, but it
649 does not seem to be worth the effort. */
650 purge_reg_equiv_notes ();
652 /* A sibling call sequence also may invalidate RTX_UNCHANGING_P
653 flag of some incoming arguments MEM RTLs, because it can write into
654 those slots. We clear all those bits now.
656 This is (slight) overkill, we could keep track of which arguments
657 we actually write into. */
658 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
660 if (GET_CODE (insn) == NOTE)
662 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
663 break;
665 else if (INSN_P (insn))
666 purge_mem_unchanging_flag (PATTERN (insn));
670 /* There may have been NOTE_INSN_BLOCK_{BEGIN,END} notes in the
671 CALL_PLACEHOLDER alternatives that we didn't emit. Rebuild the
672 lexical block tree to correspond to the notes that still exist. */
673 if (replaced_call_placeholder)
674 reorder_blocks ();
676 /* This information will be invalid after inline expansion. Kill it now. */
677 free_basic_block_vars (0);