OSDN Git Service

Tweak comments
[pf3gnuchains/gcc-fork.git] / gcc / sibcall.c
1 /* Generic sibling call optimization support
2    Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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.
10
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.
15
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.  */
20
21 #include "config.h"
22 #include "system.h"
23
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"
34
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 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));
46
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.  */
51
52 static int
53 identify_call_return_value (cp, p_hard_return, p_soft_return)
54      rtx cp;
55      rtx *p_hard_return, *p_soft_return;
56 {
57   rtx insn, set, hard, soft;
58
59   insn = XEXP (cp, 0);
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);
65
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
68      by the function.  */
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));
76   else
77     return 0;
78
79   /* If we didn't get a single hard register (e.g. a parallel), give up.  */
80   if (GET_CODE (hard) != REG)
81     return 0;
82     
83   /* Stack adjustment done after call may appear here.  */
84   insn = skip_stack_adjustment (insn);
85   if (! insn)
86     return 0;
87
88   /* Restore of GP register may appear here.  */
89   insn = skip_pic_restore (insn);
90   if (! insn)
91     return 0;
92
93   /* If there's nothing after, there's no soft return value.  */
94   insn = NEXT_INSN (insn);
95   if (! insn)
96     return 0;
97   
98   /* We're looking for a source of the hard return register.  */
99   set = single_set (insn);
100   if (! set || SET_SRC (set) != hard)
101     return 0;
102
103   soft = SET_DEST (set);
104   insn = NEXT_INSN (insn);
105
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.  */
109   if (insn
110       && (set = single_set (insn)) != NULL_RTX
111       && SET_SRC (set) == soft)
112     {
113       soft = SET_DEST (set);
114       insn = NEXT_INSN (insn);
115     }
116
117   /* Don't fool with anything but pseudo registers.  */
118   if (GET_CODE (soft) != REG || REGNO (soft) < FIRST_PSEUDO_REGISTER)
119     return 0;
120
121   /* This value must not be modified before the end of the sequence.  */
122   if (reg_set_between_p (soft, insn, NULL_RTX))
123     return 0;
124
125   *p_hard_return = hard;
126   *p_soft_return = soft;
127
128   return 1;
129 }
130
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.  */
134
135 static rtx
136 skip_copy_to_return_value (orig_insn, hardret, softret)
137      rtx orig_insn;
138      rtx hardret, softret;
139 {
140   rtx insn, set = NULL_RTX;
141
142   insn = next_nonnote_insn (orig_insn);
143   if (! insn)
144     return orig_insn;
145
146   set = single_set (insn);
147   if (! set)
148     return orig_insn;
149
150   /* The destination must be the same as the called function's return
151      value to ensure that any return value is put in the same place by the
152      current function and the function we're calling. 
153
154      Further, the source must be the same as the pseudo into which the
155      called function's return value was copied.  Otherwise we're returning
156      some other value.  */
157
158 #ifndef OUTGOING_REGNO
159 #define OUTGOING_REGNO(N) (N)
160 #endif
161
162   if (SET_DEST (set) == current_function_return_rtx
163       && REG_P (SET_DEST (set))
164       && OUTGOING_REGNO (REGNO (SET_DEST (set))) == REGNO (hardret)
165       && SET_SRC (set) == softret)
166     return insn;
167
168   /* It did not look like a copy of the return value, so return the
169      same insn we were passed.  */
170   return orig_insn;
171 }
172
173 /* If the first real insn after ORIG_INSN is a CODE of this function's return
174    value, return insn.  Otherwise return ORIG_INSN.  */
175
176 static rtx
177 skip_use_of_return_value (orig_insn, code)
178      rtx orig_insn;
179      enum rtx_code code;
180 {
181   rtx insn;
182
183   insn = next_nonnote_insn (orig_insn);
184
185   if (insn
186       && GET_CODE (insn) == INSN
187       && GET_CODE (PATTERN (insn)) == code
188       && (XEXP (PATTERN (insn), 0) == current_function_return_rtx
189           || XEXP (PATTERN (insn), 0) == const0_rtx))
190     return insn;
191
192   return orig_insn;
193 }
194
195 /* If the first real insn after ORIG_INSN adjusts the stack pointer
196    by a constant, return the insn with the stack pointer adjustment.
197    Otherwise return ORIG_INSN.  */
198
199 static rtx
200 skip_stack_adjustment (orig_insn)
201      rtx orig_insn;
202 {
203   rtx insn, set = NULL_RTX;
204
205   insn = next_nonnote_insn (orig_insn);
206
207   if (insn)
208     set = single_set (insn);
209
210   if (insn
211       && set
212       && GET_CODE (SET_SRC (set)) == PLUS
213       && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
214       && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
215       && SET_DEST (set) == stack_pointer_rtx)
216     return insn;
217
218   return orig_insn;
219 }
220
221 /* If the first real insn after ORIG_INSN sets the pic register,
222    return it.  Otherwise return ORIG_INSN.  */
223
224 static rtx
225 skip_pic_restore (orig_insn)
226      rtx orig_insn;
227 {
228   rtx insn, set = NULL_RTX;
229
230   insn = next_nonnote_insn (orig_insn);
231
232   if (insn)
233     set = single_set (insn);
234
235   if (insn && set && SET_DEST (set) == pic_offset_table_rtx)
236     return insn;
237
238   return orig_insn;
239 }
240
241 /* If the first real insn after ORIG_INSN is a jump, return the JUMP_INSN.
242    Otherwise return ORIG_INSN.  */
243
244 static rtx
245 skip_jump_insn (orig_insn)
246      rtx orig_insn;
247 {
248   rtx insn;
249
250   insn = next_nonnote_insn (orig_insn);
251
252   if (insn
253       && GET_CODE (insn) == JUMP_INSN
254       && any_uncondjump_p (insn))
255     return insn;
256
257   return orig_insn;
258 }
259 \f
260 /* Using the above functions, see if INSN, skipping any of the above,
261    goes all the way to END, the end of a basic block.  Return 1 if so.  */
262
263 static int
264 call_ends_block_p (insn, end)
265      rtx insn;
266      rtx end;
267 {
268   rtx hardret, softret;
269
270   /* END might be a note, so get the last nonnote insn of the block.  */
271   end = next_nonnote_insn (PREV_INSN (end));
272
273   /* If the call was the end of the block, then we're OK.  */
274   if (insn == end)
275     return 1;
276
277   /* Skip over copying from the call's return value pseudo into
278      this function's hard return register and if that's the end
279      of the block, we're OK.  */
280   identify_call_return_value (PATTERN (insn), &hardret, &softret);
281   insn = skip_copy_to_return_value (insn, hardret, softret);
282   if (insn == end)
283     return 1;
284
285   /* Skip any stack adjustment.  */
286   insn = skip_stack_adjustment (insn);
287   if (insn == end)
288     return 1;
289
290   /* Skip over a CLOBBER of the return value as a hard reg.  */
291   insn = skip_use_of_return_value (insn, CLOBBER);
292   if (insn == end)
293     return 1;
294
295   /* Skip over a USE of the return value (as a hard reg).  */
296   insn = skip_use_of_return_value (insn, USE);
297   if (insn == end)
298     return 1;
299
300   /* Skip over a JUMP_INSN at the end of the block.  If that doesn't end the
301      block, the original CALL_INSN didn't.  */
302   insn = skip_jump_insn (insn);
303   return insn == end;
304 }
305
306 /* Scan the rtx X for ADDRESSOF expressions or
307    current_function_internal_arg_pointer registers.
308    Return nonzero if an ADDRESSOF or current_function_internal_arg_pointer
309    is found outside of some MEM expression, else return zero.  */
310
311 static int
312 uses_addressof (x)
313      rtx x;
314 {
315   RTX_CODE code;
316   int i, j;
317   const char *fmt;
318
319   if (x == NULL_RTX)
320     return 0;
321
322   code = GET_CODE (x);
323
324   if (code == ADDRESSOF || x == current_function_internal_arg_pointer)
325     return 1;
326
327   if (code == MEM)
328     return 0;
329
330   /* Scan all subexpressions. */
331   fmt = GET_RTX_FORMAT (code);
332   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
333     {
334       if (*fmt == 'e')
335         {
336           if (uses_addressof (XEXP (x, i)))
337             return 1;
338         }
339       else if (*fmt == 'E')
340         {
341           for (j = 0; j < XVECLEN (x, i); j++)
342             if (uses_addressof (XVECEXP (x, i, j)))
343               return 1;
344         }
345     }
346   return 0;
347 }
348
349 /* Scan the sequence of insns in SEQ to see if any have an ADDRESSOF
350    rtl expression or current_function_internal_arg_pointer occurences
351    not enclosed within a MEM.  If an ADDRESSOF expression or
352    current_function_internal_arg_pointer is found, return nonzero, otherwise
353    return zero.
354
355    This function handles CALL_PLACEHOLDERs which contain multiple sequences
356    of insns.  */
357
358 static int
359 sequence_uses_addressof (seq)
360      rtx seq;
361 {
362   rtx insn;
363
364   for (insn = seq; insn; insn = NEXT_INSN (insn))
365     if (INSN_P (insn))
366       {
367         /* If this is a CALL_PLACEHOLDER, then recursively call ourselves
368            with each nonempty sequence attached to the CALL_PLACEHOLDER.  */
369         if (GET_CODE (insn) == CALL_INSN
370             && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
371           {
372             if (XEXP (PATTERN (insn), 0) != NULL_RTX
373                 && sequence_uses_addressof (XEXP (PATTERN (insn), 0)))
374               return 1;
375             if (XEXP (PATTERN (insn), 1) != NULL_RTX
376                 && sequence_uses_addressof (XEXP (PATTERN (insn), 1)))
377               return 1;
378             if (XEXP (PATTERN (insn), 2) != NULL_RTX
379                 && sequence_uses_addressof (XEXP (PATTERN (insn), 2)))
380               return 1;
381           }
382         else if (uses_addressof (PATTERN (insn))
383                  || (REG_NOTES (insn) && uses_addressof (REG_NOTES (insn))))
384           return 1;
385       }
386   return 0;
387 }
388
389 /* Remove all REG_EQUIV notes found in the insn chain.  */
390
391 static void
392 purge_reg_equiv_notes ()
393 {
394   rtx insn;
395
396   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
397     {
398       while (1)
399         {
400           rtx note = find_reg_note (insn, REG_EQUIV, 0);
401           if (note)
402             {
403               /* Remove the note and keep looking at the notes for
404                  this insn.  */
405               remove_note (insn, note);
406               continue;
407             }
408           break;
409         }
410     }
411 }
412
413 /* Clear RTX_UNCHANGING_P flag of incoming argument MEMs.  */
414
415 static void
416 purge_mem_unchanging_flag (x)
417      rtx x;
418 {
419   RTX_CODE code;
420   int i, j;
421   const char *fmt;
422
423   if (x == NULL_RTX)
424     return;
425
426   code = GET_CODE (x);
427
428   if (code == MEM)
429     {
430       if (RTX_UNCHANGING_P (x)
431           && (XEXP (x, 0) == current_function_internal_arg_pointer
432               || (GET_CODE (XEXP (x, 0)) == PLUS
433                   && XEXP (XEXP (x, 0), 0) ==
434                      current_function_internal_arg_pointer
435                   && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
436         RTX_UNCHANGING_P (x) = 0;
437       return;
438     }
439
440   /* Scan all subexpressions. */
441   fmt = GET_RTX_FORMAT (code);
442   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
443     {
444       if (*fmt == 'e')
445         purge_mem_unchanging_flag (XEXP (x, i));
446       else if (*fmt == 'E')
447         for (j = 0; j < XVECLEN (x, i); j++)
448           purge_mem_unchanging_flag (XVECEXP (x, i, j));
449     }
450 }
451
452 /* Replace the CALL_PLACEHOLDER with one of its children.  INSN should be
453    the CALL_PLACEHOLDER insn; USE tells which child to use.  */
454
455 void
456 replace_call_placeholder (insn, use)
457      rtx insn;
458      sibcall_use_t use;
459 {
460   if (use == sibcall_use_tail_recursion)
461     emit_insns_before (XEXP (PATTERN (insn), 2), insn);
462   else if (use == sibcall_use_sibcall)
463     emit_insns_before (XEXP (PATTERN (insn), 1), insn);
464   else if (use == sibcall_use_normal)
465     emit_insns_before (XEXP (PATTERN (insn), 0), insn);
466   else
467     abort();
468
469   /* Turn off LABEL_PRESERVE_P for the tail recursion label if it
470      exists.  We only had to set it long enough to keep the jump
471      pass above from deleting it as unused.  */
472   if (XEXP (PATTERN (insn), 3))
473     LABEL_PRESERVE_P (XEXP (PATTERN (insn), 3)) = 0;
474   
475   /* "Delete" the placeholder insn. */
476   PUT_CODE (insn, NOTE);
477   NOTE_SOURCE_FILE (insn) = 0;
478   NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
479 }
480
481 /* Given a (possibly empty) set of potential sibling or tail recursion call
482    sites, determine if optimization is possible.
483
484    Potential sibling or tail recursion calls are marked with CALL_PLACEHOLDER
485    insns.  The CALL_PLACEHOLDER insn holds chains of insns to implement a
486    normal call, sibling call or tail recursive call.
487
488    Replace the CALL_PLACEHOLDER with an appropriate insn chain.  */
489
490 void
491 optimize_sibling_and_tail_recursive_calls ()
492 {
493   rtx insn, insns;
494   basic_block alternate_exit = EXIT_BLOCK_PTR;
495   int current_function_uses_addressof;
496   int successful_sibling_call = 0;
497   int replaced_call_placeholder = 0;
498   edge e;
499
500   insns = get_insns ();
501
502   /* We do not perform these calls when flag_exceptions is true, so this
503      is probably a NOP at the current time.  However, we may want to support
504      sibling and tail recursion optimizations in the future, so let's plan
505      ahead and find all the EH labels.  */
506   find_exception_handler_labels ();
507
508   /* Run a jump optimization pass to clean up the CFG.  We primarily want
509      this to thread jumps so that it is obvious which blocks jump to the
510      epilouge.  */
511   jump_optimize_minimal (insns);
512
513   /* We need cfg information to determine which blocks are succeeded
514      only by the epilogue.  */
515   find_basic_blocks (insns, max_reg_num (), 0);
516   cleanup_cfg (insns);
517
518   /* If there are no basic blocks, then there is nothing to do.  */
519   if (n_basic_blocks == 0)
520     return;
521
522   /* Find the exit block.
523
524      It is possible that we have blocks which can reach the exit block
525      directly.  However, most of the time a block will jump (or fall into)
526      N_BASIC_BLOCKS - 1, which in turn falls into the exit block.  */
527   for (e = EXIT_BLOCK_PTR->pred;
528        e && alternate_exit == EXIT_BLOCK_PTR;
529        e = e->pred_next)
530     {
531       rtx insn;
532
533       if (e->dest != EXIT_BLOCK_PTR || e->succ_next != NULL)
534         continue;
535
536       /* Walk forwards through the last normal block and see if it
537          does nothing except fall into the exit block.  */
538       for (insn = BLOCK_HEAD (n_basic_blocks - 1);
539            insn;
540            insn = NEXT_INSN (insn))
541         {
542           /* This should only happen once, at the start of this block.  */
543           if (GET_CODE (insn) == CODE_LABEL)
544             continue;
545
546           if (GET_CODE (insn) == NOTE)
547             continue;
548
549           if (GET_CODE (insn) == INSN
550               && GET_CODE (PATTERN (insn)) == USE)
551             continue;
552
553           break;
554         }
555
556       /* If INSN is zero, then the search walked all the way through the
557          block without hitting anything interesting.  This block is a
558          valid alternate exit block.  */
559       if (insn == NULL)
560         alternate_exit = e->src;
561     }
562
563   /* If the function uses ADDRESSOF, we can't (easily) determine
564      at this point if the value will end up on the stack.  */
565   current_function_uses_addressof = sequence_uses_addressof (insns);
566
567   /* Walk the insn chain and find any CALL_PLACEHOLDER insns.  We need to
568      select one of the insn sequences attached to each CALL_PLACEHOLDER.
569
570      The different sequences represent different ways to implement the call,
571      ie, tail recursion, sibling call or normal call.
572
573      Since we do not create nested CALL_PLACEHOLDERs, the scan
574      continues with the insn that was after a replaced CALL_PLACEHOLDER;
575      we don't rescan the replacement insns.  */
576   for (insn = insns; insn; insn = NEXT_INSN (insn))
577     {
578       if (GET_CODE (insn) == CALL_INSN
579           && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
580         {
581           int sibcall = (XEXP (PATTERN (insn), 1) != NULL_RTX);
582           int tailrecursion = (XEXP (PATTERN (insn), 2) != NULL_RTX);
583           basic_block call_block = BLOCK_FOR_INSN (insn);
584
585           /* alloca (until we have stack slot life analysis) inhibits
586              sibling call optimizations, but not tail recursion.
587              Similarly if we use varargs or stdarg since they implicitly
588              may take the address of an argument.  */
589           if (current_function_calls_alloca
590               || current_function_varargs || current_function_stdarg)
591             sibcall = 0;
592
593           /* See if there are any reasons we can't perform either sibling or
594              tail call optimizations.  We must be careful with stack slots
595              which are live at potential optimization sites.  ?!? The first
596              test is overly conservative and should be replaced.  */
597           if (frame_offset
598               /* Can't take address of local var if used by recursive call.  */
599               || current_function_uses_addressof
600               /* Can't if more than one successor or single successor is not
601                  exit block.  These two tests prevent tail call optimization
602                  in the presense of active exception handlers.  */
603               || call_block->succ == NULL
604               || call_block->succ->succ_next != NULL
605               || (call_block->succ->dest != EXIT_BLOCK_PTR
606                   && call_block->succ->dest != alternate_exit)
607               /* If this call doesn't end the block, there are operations at
608                  the end of the block which we must execute after returning. */
609               || ! call_ends_block_p (insn, call_block->end))
610             sibcall = 0, tailrecursion = 0;
611
612           /* Select a set of insns to implement the call and emit them.
613              Tail recursion is the most efficient, so select it over
614              a tail/sibling call.  */
615           if (sibcall)
616             successful_sibling_call = 1;
617
618           replaced_call_placeholder = 1;
619           replace_call_placeholder (insn, 
620                                     tailrecursion != 0 
621                                       ? sibcall_use_tail_recursion
622                                       : sibcall != 0
623                                          ? sibcall_use_sibcall
624                                          : sibcall_use_normal);
625         }
626     }
627
628   if (successful_sibling_call)
629     {
630       rtx insn;
631
632       /* A sibling call sequence invalidates any REG_EQUIV notes made for
633          this function's incoming arguments. 
634
635          At the start of RTL generation we know the only REG_EQUIV notes
636          in the rtl chain are those for incoming arguments, so we can safely
637          flush any REG_EQUIV note. 
638
639          This is (slight) overkill.  We could keep track of the highest
640          argument we clobber and be more selective in removing notes, but it
641          does not seem to be worth the effort.  */
642       purge_reg_equiv_notes ();
643
644       /* A sibling call sequence also may invalidate RTX_UNCHANGING_P
645          flag of some incoming arguments MEM RTLs, because it can write into
646          those slots.  We clear all those bits now.
647          
648          This is (slight) overkill, we could keep track of which arguments
649          we actually write into.  */
650       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
651         {
652           if (GET_CODE (insn) == NOTE)
653             {
654               if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
655                 break;
656             }
657           else if (INSN_P (insn))
658             purge_mem_unchanging_flag (PATTERN (insn));
659         }
660     }
661
662   /* There may have been NOTE_INSN_BLOCK_{BEGIN,END} notes in the 
663      CALL_PLACEHOLDER alternatives that we didn't emit.  Rebuild the
664      lexical block tree to correspond to the notes that still exist.  */
665   if (replaced_call_placeholder)
666     reorder_blocks ();
667
668   /* This information will be invalid after inline expansion.  Kill it now.  */
669   free_basic_block_vars (0);
670 }