OSDN Git Service

* init.c (build_new): Allow enumeration types for the array-bounds
[pf3gnuchains/gcc-fork.git] / gcc / sibcall.c
1 /* Generic sibling call optimization support
2    Copyright (C) 1999, 2000 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 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));
45
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.  */
50
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;
55 {
56   rtx insn, set, hard, soft;
57
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);
64
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;
77
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;
81     
82   /* Stack adjustment done after call may appear here.  */
83   insn = skip_stack_adjustment (insn);
84   if (! insn)
85     return 0;
86
87   /* Restore of GP register may appear here.  */
88   insn = skip_pic_restore (insn);
89   if (! insn)
90     return 0;
91
92   /* If there's nothing after, there's no soft return value.  */
93   insn = NEXT_INSN (insn);
94   if (! insn)
95     return 0;
96   
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;
101
102   soft = SET_DEST (set);
103   insn = NEXT_INSN (insn);
104
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)
111     {
112       soft = SET_DEST (set);
113       insn = NEXT_INSN (insn);
114     }
115
116   /* Don't fool with anything but pseudo registers.  */
117   if (GET_CODE (soft) != REG || REGNO (soft) < FIRST_PSEUDO_REGISTER)
118     return 0;
119
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;
123
124   *p_hard_return = hard;
125   *p_soft_return = soft;
126
127   return 1;
128 }
129
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.  */
133
134 static rtx
135 skip_copy_to_return_value (orig_insn, hardret, softret)
136      rtx orig_insn;
137      rtx hardret, softret;
138 {
139   rtx insn, set = NULL_RTX;
140
141   insn = next_nonnote_insn (orig_insn);
142   if (! insn)
143     return orig_insn;
144
145   set = single_set (insn);
146   if (! set)
147     return orig_insn;
148
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. 
152
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.  */
156
157 #ifndef OUTGOING_REGNO
158 #define OUTGOING_REGNO(N) (N)
159 #endif
160
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;
166
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;
170 }
171
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.  */
174
175 static rtx
176 skip_use_of_return_value (orig_insn, code)
177      rtx orig_insn;
178      enum rtx_code code;
179 {
180   rtx insn;
181
182   insn = next_nonnote_insn (orig_insn);
183
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;
190
191   return orig_insn;
192 }
193
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.  */
197
198 static rtx
199 skip_stack_adjustment (orig_insn)
200      rtx orig_insn;
201 {
202   rtx insn, set = NULL_RTX;
203
204   insn = next_nonnote_insn (orig_insn);
205
206   if (insn)
207     set = single_set (insn);
208
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;
216
217   return orig_insn;
218 }
219
220 /* If the first real insn after ORIG_INSN sets the pic register,
221    return it.  Otherwise return ORIG_INSN.  */
222
223 static rtx
224 skip_pic_restore (orig_insn)
225      rtx orig_insn;
226 {
227   rtx insn, set = NULL_RTX;
228
229   insn = next_nonnote_insn (orig_insn);
230
231   if (insn)
232     set = single_set (insn);
233
234   if (insn && set && SET_DEST (set) == pic_offset_table_rtx)
235     return insn;
236
237   return orig_insn;
238 }
239
240 /* If the first real insn after ORIG_INSN is a jump, return the JUMP_INSN.
241    Otherwise return ORIG_INSN.  */
242
243 static rtx
244 skip_jump_insn (orig_insn)
245      rtx orig_insn;
246 {
247   rtx insn;
248
249   insn = next_nonnote_insn (orig_insn);
250
251   if (insn
252       && GET_CODE (insn) == JUMP_INSN
253       && any_uncondjump_p (insn))
254     return insn;
255
256   return orig_insn;
257 }
258
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.  */
263
264 static int
265 uses_addressof (x)
266      rtx x;
267 {
268   RTX_CODE code;
269   int i, j;
270   const char *fmt;
271
272   if (x == NULL_RTX)
273     return 0;
274
275   code = GET_CODE (x);
276
277   if (code == ADDRESSOF || x == current_function_internal_arg_pointer)
278     return 1;
279
280   if (code == MEM)
281     return 0;
282
283   /* Scan all subexpressions. */
284   fmt = GET_RTX_FORMAT (code);
285   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
286     {
287       if (*fmt == 'e')
288         {
289           if (uses_addressof (XEXP (x, i)))
290             return 1;
291         }
292       else if (*fmt == 'E')
293         {
294           for (j = 0; j < XVECLEN (x, i); j++)
295             if (uses_addressof (XVECEXP (x, i, j)))
296               return 1;
297         }
298     }
299   return 0;
300 }
301
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.
307
308    This function handles CALL_PLACEHOLDERs which contain multiple sequences
309    of insns.  */
310
311 static int
312 sequence_uses_addressof (seq)
313      rtx seq;
314 {
315   rtx insn;
316
317   for (insn = seq; insn; insn = NEXT_INSN (insn))
318     if (INSN_P (insn))
319       {
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)
324           {
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;
334           }
335         else if (uses_addressof (PATTERN (insn))
336                  || (REG_NOTES (insn) && uses_addressof (REG_NOTES (insn))))
337           return 1;
338       }
339   return 0;
340 }
341
342 /* Remove all REG_EQUIV notes found in the insn chain.  */
343
344 static void
345 purge_reg_equiv_notes ()
346 {
347   rtx insn;
348
349   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
350     {
351       while (1)
352         {
353           rtx note = find_reg_note (insn, REG_EQUIV, 0);
354           if (note)
355             {
356               /* Remove the note and keep looking at the notes for
357                  this insn.  */
358               remove_note (insn, note);
359               continue;
360             }
361           break;
362         }
363     }
364 }
365
366 /* Clear RTX_UNCHANGING_P flag of incoming argument MEMs.  */
367
368 static void
369 purge_mem_unchanging_flag (x)
370      rtx x;
371 {
372   RTX_CODE code;
373   int i, j;
374   const char *fmt;
375
376   if (x == NULL_RTX)
377     return;
378
379   code = GET_CODE (x);
380
381   if (code == MEM)
382     {
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;
391     }
392
393   /* Scan all subexpressions. */
394   fmt = GET_RTX_FORMAT (code);
395   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
396     {
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));
402     }
403 }
404
405 /* Replace the CALL_PLACEHOLDER with one of its children.  INSN should be
406    the CALL_PLACEHOLDER insn; USE tells which child to use.  */
407
408 void
409 replace_call_placeholder (insn, use)
410      rtx insn;
411      sibcall_use_t use;
412 {
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();
421
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;
427   
428   /* "Delete" the placeholder insn. */
429   PUT_CODE (insn, NOTE);
430   NOTE_SOURCE_FILE (insn) = 0;
431   NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
432 }
433
434 /* Given a (possibly empty) set of potential sibling or tail recursion call
435    sites, determine if optimization is possible.
436
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.
440
441    Replace the CALL_PLACEHOLDER with an appropriate insn chain.  */
442
443 void
444 optimize_sibling_and_tail_recursive_calls ()
445 {
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;
452
453   insns = get_insns ();
454
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 ();
460
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);
465
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 (insns);
470
471   /* If there are no basic blocks, then there is nothing to do.  */
472   if (n_basic_blocks == 0)
473     return;
474
475   /* Find the exit block.
476
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)
483     {
484       rtx insn;
485
486       if (e->dest != EXIT_BLOCK_PTR || e->succ_next != NULL)
487         continue;
488
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))
494         {
495           /* This should only happen once, at the start of this block.  */
496           if (GET_CODE (insn) == CODE_LABEL)
497             continue;
498
499           if (GET_CODE (insn) == NOTE)
500             continue;
501
502           if (GET_CODE (insn) == INSN
503               && GET_CODE (PATTERN (insn)) == USE)
504             continue;
505
506           break;
507         }
508
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;
514     }
515
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);
519
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.
522
523      The different sequences represent different ways to implement the call,
524      ie, tail recursion, sibling call or normal call.
525
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))
530     {
531       if (GET_CODE (insn) == CALL_INSN
532           && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
533         {
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;
538
539           /* We must be careful with stack slots which are live at
540              potential optimization sites.
541
542              ?!? This test is overly conservative and will be replaced.  */
543           if (frame_offset)
544             goto failure;
545
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;
550
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;
558
559           call_block = BLOCK_FOR_INSN (insn);
560
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;
566
567           /* If the single successor is not the exit block, then we can not
568              perform sibcall or tail recursion optimizations. 
569
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;
576
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;
581
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))
585             {
586               temp = skip_copy_to_return_value (temp, hardret, softret);
587               if (temp == call_block->end)
588                 goto success;
589             }
590
591           /* Skip any stack adjustment.  */
592           temp = skip_stack_adjustment (temp);
593           if (temp == call_block->end)
594             goto success;
595
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;
600
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;
605
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;
612
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:
619
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.  */
623   
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);
633         }
634     }
635
636   if (successful_sibling_call)
637     {
638       rtx insn;
639
640       /* A sibling call sequence invalidates any REG_EQUIV notes made for
641          this function's incoming arguments. 
642
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. 
646
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 ();
651
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.
655          
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))
659         {
660           if (GET_CODE (insn) == NOTE)
661             {
662               if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
663                 break;
664             }
665           else if (INSN_P (insn))
666             purge_mem_unchanging_flag (PATTERN (insn));
667         }
668     }
669
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 ();
675
676   /* This information will be invalid after inline expansion.  Kill it now.  */
677   free_basic_block_vars (0);
678 }