OSDN Git Service

* reload1.c (move2add_last_cc0): New.
[pf3gnuchains/gcc-fork.git] / gcc / sibcall.c
1 /* Generic sibling call optimization support
2    Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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
9 version.
10
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
14 for more details.
15
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
19 02111-1307, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25
26 #include "rtl.h"
27 #include "regs.h"
28 #include "function.h"
29 #include "hard-reg-set.h"
30 #include "flags.h"
31 #include "insn-config.h"
32 #include "recog.h"
33 #include "basic-block.h"
34 #include "output.h"
35 #include "except.h"
36 #include "tree.h"
37
38 /* In case alternate_exit_block contains copy from pseudo, to return value,
39    record the pseudo here.  In such case the pseudo must be set to function
40    return in the sibcall sequence.  */
41 static rtx return_value_pseudo;
42
43 static int identify_call_return_value   PARAMS ((rtx, rtx *, rtx *));
44 static rtx skip_copy_to_return_value    PARAMS ((rtx));
45 static rtx skip_use_of_return_value     PARAMS ((rtx, enum rtx_code));
46 static rtx skip_stack_adjustment        PARAMS ((rtx));
47 static rtx skip_pic_restore             PARAMS ((rtx));
48 static rtx skip_jump_insn               PARAMS ((rtx));
49 static int call_ends_block_p            PARAMS ((rtx, rtx));
50 static int uses_addressof               PARAMS ((rtx));
51 static int sequence_uses_addressof      PARAMS ((rtx));
52 static void purge_reg_equiv_notes       PARAMS ((void));
53 static void purge_mem_unchanging_flag   PARAMS ((rtx));
54 static rtx skip_unreturned_value        PARAMS ((rtx));
55
56 /* Examine a CALL_PLACEHOLDER pattern and determine where the call's
57    return value is located.  P_HARD_RETURN receives the hard register
58    that the function used; P_SOFT_RETURN receives the pseudo register
59    that the sequence used.  Return nonzero if the values were located.  */
60
61 static int
62 identify_call_return_value (cp, p_hard_return, p_soft_return)
63      rtx cp;
64      rtx *p_hard_return, *p_soft_return;
65 {
66   rtx insn, set, hard, soft;
67
68   insn = XEXP (cp, 0);
69   /* Search backward through the "normal" call sequence to the CALL insn.  */
70   while (NEXT_INSN (insn))
71     insn = NEXT_INSN (insn);
72   while (GET_CODE (insn) != CALL_INSN)
73     insn = PREV_INSN (insn);
74
75   /* Assume the pattern is (set (dest) (call ...)), or that the first
76      member of a parallel is.  This is the hard return register used
77      by the function.  */
78   if (GET_CODE (PATTERN (insn)) == SET
79       && GET_CODE (SET_SRC (PATTERN (insn))) == CALL)
80     hard = SET_DEST (PATTERN (insn));
81   else if (GET_CODE (PATTERN (insn)) == PARALLEL
82            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
83            && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == CALL)
84     hard = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
85   else
86     return 0;
87
88   /* If we didn't get a single hard register (e.g. a parallel), give up.  */
89   if (GET_CODE (hard) != REG)
90     return 0;
91
92   /* Stack adjustment done after call may appear here.  */
93   insn = skip_stack_adjustment (insn);
94   if (! insn)
95     return 0;
96
97   /* Restore of GP register may appear here.  */
98   insn = skip_pic_restore (insn);
99   if (! insn)
100     return 0;
101
102   /* If there's nothing after, there's no soft return value.  */
103   insn = NEXT_INSN (insn);
104   if (! insn)
105     return 0;
106
107   /* We're looking for a source of the hard return register.  */
108   set = single_set (insn);
109   if (! set || SET_SRC (set) != hard)
110     return 0;
111
112   soft = SET_DEST (set);
113   insn = NEXT_INSN (insn);
114
115   /* Allow this first destination to be copied to a second register,
116      as might happen if the first register wasn't the particular pseudo
117      we'd been expecting.  */
118   if (insn
119       && (set = single_set (insn)) != NULL_RTX
120       && SET_SRC (set) == soft)
121     {
122       soft = SET_DEST (set);
123       insn = NEXT_INSN (insn);
124     }
125
126   /* Don't fool with anything but pseudo registers.  */
127   if (GET_CODE (soft) != REG || REGNO (soft) < FIRST_PSEUDO_REGISTER)
128     return 0;
129
130   /* This value must not be modified before the end of the sequence.  */
131   if (reg_set_between_p (soft, insn, NULL_RTX))
132     return 0;
133
134   *p_hard_return = hard;
135   *p_soft_return = soft;
136
137   return 1;
138 }
139
140 /* If the first real insn after ORIG_INSN copies to this function's
141    return value from RETVAL, then return the insn which performs the
142    copy.  Otherwise return ORIG_INSN.  */
143
144 static rtx
145 skip_copy_to_return_value (orig_insn)
146      rtx orig_insn;
147 {
148   rtx insn, set = NULL_RTX;
149   rtx hardret, softret;
150
151   /* If there is no return value, we have nothing to do.  */
152   if (! identify_call_return_value (PATTERN (orig_insn), &hardret, &softret))
153     return orig_insn;
154
155   insn = next_nonnote_insn (orig_insn);
156   if (! insn)
157     return orig_insn;
158
159   set = single_set (insn);
160   if (! set)
161     return orig_insn;
162
163   if (return_value_pseudo)
164     {
165       if (SET_DEST (set) == return_value_pseudo
166           && SET_SRC (set) == softret)
167         return insn;
168       return orig_insn;
169     }
170
171   /* The destination must be the same as the called function's return
172      value to ensure that any return value is put in the same place by the
173      current function and the function we're calling.
174
175      Further, the source must be the same as the pseudo into which the
176      called function's return value was copied.  Otherwise we're returning
177      some other value.  */
178
179 #ifndef OUTGOING_REGNO
180 #define OUTGOING_REGNO(N) (N)
181 #endif
182
183   if (SET_DEST (set) == current_function_return_rtx
184       && REG_P (SET_DEST (set))
185       && OUTGOING_REGNO (REGNO (SET_DEST (set))) == REGNO (hardret)
186       && SET_SRC (set) == softret)
187     return insn;
188
189   /* Recognize the situation when the called function's return value
190      is copied in two steps: first into an intermediate pseudo, then
191      the into the calling functions return value register.  */
192
193   if (REG_P (SET_DEST (set))
194       && SET_SRC (set) == softret)
195     {
196       rtx x = SET_DEST (set);
197
198       insn = next_nonnote_insn (insn);
199       if (! insn)
200         return orig_insn;
201
202       set = single_set (insn);
203       if (! set)
204         return orig_insn;
205
206       if (SET_DEST (set) == current_function_return_rtx
207           && REG_P (SET_DEST (set))
208           && OUTGOING_REGNO (REGNO (SET_DEST (set))) == REGNO (hardret)
209           && SET_SRC (set) == x)
210         return insn;
211     }
212
213   /* It did not look like a copy of the return value, so return the
214      same insn we were passed.  */
215   return orig_insn;
216 }
217
218 /* If the first real insn after ORIG_INSN is a CODE of this function's return
219    value, return insn.  Otherwise return ORIG_INSN.  */
220
221 static rtx
222 skip_use_of_return_value (orig_insn, code)
223      rtx orig_insn;
224      enum rtx_code code;
225 {
226   rtx insn;
227
228   insn = next_nonnote_insn (orig_insn);
229
230   if (insn
231       && GET_CODE (insn) == INSN
232       && GET_CODE (PATTERN (insn)) == code
233       && (XEXP (PATTERN (insn), 0) == current_function_return_rtx
234           || XEXP (PATTERN (insn), 0) == const0_rtx))
235     return insn;
236
237   return orig_insn;
238 }
239
240 /* In case function does not return value,  we get clobber of pseudo followed
241    by set to hard return value.  */
242 static rtx
243 skip_unreturned_value (orig_insn)
244      rtx orig_insn;
245 {
246   rtx insn = next_nonnote_insn (orig_insn);
247
248   /* Skip possible clobber of pseudo return register.  */
249   if (insn
250       && GET_CODE (insn) == INSN
251       && GET_CODE (PATTERN (insn)) == CLOBBER
252       && REG_P (XEXP (PATTERN (insn), 0))
253       && (REGNO (XEXP (PATTERN (insn), 0)) >= FIRST_PSEUDO_REGISTER))
254     {
255       rtx set_insn = next_nonnote_insn (insn);
256       rtx set;
257       if (!set_insn)
258         return insn;
259       set = single_set (set_insn);
260       if (!set
261           || SET_SRC (set) != XEXP (PATTERN (insn), 0)
262           || SET_DEST (set) != current_function_return_rtx)
263         return insn;
264       return set_insn;
265     }
266   return orig_insn;
267 }
268
269 /* If the first real insn after ORIG_INSN adjusts the stack pointer
270    by a constant, return the insn with the stack pointer adjustment.
271    Otherwise return ORIG_INSN.  */
272
273 static rtx
274 skip_stack_adjustment (orig_insn)
275      rtx orig_insn;
276 {
277   rtx insn, set = NULL_RTX;
278
279   insn = next_nonnote_insn (orig_insn);
280
281   if (insn)
282     set = single_set (insn);
283
284   if (insn
285       && set
286       && GET_CODE (SET_SRC (set)) == PLUS
287       && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
288       && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
289       && SET_DEST (set) == stack_pointer_rtx)
290     return insn;
291
292   return orig_insn;
293 }
294
295 /* If the first real insn after ORIG_INSN sets the pic register,
296    return it.  Otherwise return ORIG_INSN.  */
297
298 static rtx
299 skip_pic_restore (orig_insn)
300      rtx orig_insn;
301 {
302   rtx insn, set = NULL_RTX;
303
304   insn = next_nonnote_insn (orig_insn);
305
306   if (insn)
307     set = single_set (insn);
308
309   if (insn && set && SET_DEST (set) == pic_offset_table_rtx)
310     return insn;
311
312   return orig_insn;
313 }
314
315 /* If the first real insn after ORIG_INSN is a jump, return the JUMP_INSN.
316    Otherwise return ORIG_INSN.  */
317
318 static rtx
319 skip_jump_insn (orig_insn)
320      rtx orig_insn;
321 {
322   rtx insn;
323
324   insn = next_nonnote_insn (orig_insn);
325
326   if (insn
327       && GET_CODE (insn) == JUMP_INSN
328       && any_uncondjump_p (insn))
329     return insn;
330
331   return orig_insn;
332 }
333 \f
334 /* Using the above functions, see if INSN, skipping any of the above,
335    goes all the way to END, the end of a basic block.  Return 1 if so.  */
336
337 static int
338 call_ends_block_p (insn, end)
339      rtx insn;
340      rtx end;
341 {
342   rtx new_insn;
343   /* END might be a note, so get the last nonnote insn of the block.  */
344   end = next_nonnote_insn (PREV_INSN (end));
345
346   /* If the call was the end of the block, then we're OK.  */
347   if (insn == end)
348     return 1;
349
350   /* Skip over copying from the call's return value pseudo into
351      this function's hard return register and if that's the end
352      of the block, we're OK.  */
353   new_insn = skip_copy_to_return_value (insn);
354
355   /* In case we return value in pseudo, we must set the pseudo to
356      return value of called function, otherwise we are returning
357      something else.  */
358   if (return_value_pseudo && insn == new_insn)
359     return 0;
360   insn = new_insn;
361
362   if (insn == end)
363     return 1;
364
365   /* Skip any stack adjustment.  */
366   insn = skip_stack_adjustment (insn);
367   if (insn == end)
368     return 1;
369
370   /* Skip over a CLOBBER of the return value as a hard reg.  */
371   insn = skip_use_of_return_value (insn, CLOBBER);
372   if (insn == end)
373     return 1;
374
375   /* Skip over a CLOBBER of the return value as a hard reg.  */
376   insn = skip_unreturned_value (insn);
377   if (insn == end)
378     return 1;
379
380   /* Skip over a USE of the return value (as a hard reg).  */
381   insn = skip_use_of_return_value (insn, USE);
382   if (insn == end)
383     return 1;
384
385   /* Skip over a JUMP_INSN at the end of the block.  If that doesn't end the
386      block, the original CALL_INSN didn't.  */
387   insn = skip_jump_insn (insn);
388   return insn == end;
389 }
390
391 /* Scan the rtx X for ADDRESSOF expressions or
392    current_function_internal_arg_pointer registers.
393    Return nonzero if an ADDRESSOF or current_function_internal_arg_pointer
394    is found outside of some MEM expression, else return zero.  */
395
396 static int
397 uses_addressof (x)
398      rtx x;
399 {
400   RTX_CODE code;
401   int i, j;
402   const char *fmt;
403
404   if (x == NULL_RTX)
405     return 0;
406
407   code = GET_CODE (x);
408
409   if (code == ADDRESSOF || x == current_function_internal_arg_pointer)
410     return 1;
411
412   if (code == MEM)
413     return 0;
414
415   /* Scan all subexpressions.  */
416   fmt = GET_RTX_FORMAT (code);
417   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
418     {
419       if (*fmt == 'e')
420         {
421           if (uses_addressof (XEXP (x, i)))
422             return 1;
423         }
424       else if (*fmt == 'E')
425         {
426           for (j = 0; j < XVECLEN (x, i); j++)
427             if (uses_addressof (XVECEXP (x, i, j)))
428               return 1;
429         }
430     }
431   return 0;
432 }
433
434 /* Scan the sequence of insns in SEQ to see if any have an ADDRESSOF
435    rtl expression or current_function_internal_arg_pointer occurrences
436    not enclosed within a MEM.  If an ADDRESSOF expression or
437    current_function_internal_arg_pointer is found, return nonzero, otherwise
438    return zero.
439
440    This function handles CALL_PLACEHOLDERs which contain multiple sequences
441    of insns.  */
442
443 static int
444 sequence_uses_addressof (seq)
445      rtx seq;
446 {
447   rtx insn;
448
449   for (insn = seq; insn; insn = NEXT_INSN (insn))
450     if (INSN_P (insn))
451       {
452         /* If this is a CALL_PLACEHOLDER, then recursively call ourselves
453            with each nonempty sequence attached to the CALL_PLACEHOLDER.  */
454         if (GET_CODE (insn) == CALL_INSN
455             && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
456           {
457             if (XEXP (PATTERN (insn), 0) != NULL_RTX
458                 && sequence_uses_addressof (XEXP (PATTERN (insn), 0)))
459               return 1;
460             if (XEXP (PATTERN (insn), 1) != NULL_RTX
461                 && sequence_uses_addressof (XEXP (PATTERN (insn), 1)))
462               return 1;
463             if (XEXP (PATTERN (insn), 2) != NULL_RTX
464                 && sequence_uses_addressof (XEXP (PATTERN (insn), 2)))
465               return 1;
466           }
467         else if (uses_addressof (PATTERN (insn))
468                  || (REG_NOTES (insn) && uses_addressof (REG_NOTES (insn))))
469           return 1;
470       }
471   return 0;
472 }
473
474 /* Remove all REG_EQUIV notes found in the insn chain.  */
475
476 static void
477 purge_reg_equiv_notes ()
478 {
479   rtx insn;
480
481   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
482     {
483       while (1)
484         {
485           rtx note = find_reg_note (insn, REG_EQUIV, 0);
486           if (note)
487             {
488               /* Remove the note and keep looking at the notes for
489                  this insn.  */
490               remove_note (insn, note);
491               continue;
492             }
493           break;
494         }
495     }
496 }
497
498 /* Clear RTX_UNCHANGING_P flag of incoming argument MEMs.  */
499
500 static void
501 purge_mem_unchanging_flag (x)
502      rtx x;
503 {
504   RTX_CODE code;
505   int i, j;
506   const char *fmt;
507
508   if (x == NULL_RTX)
509     return;
510
511   code = GET_CODE (x);
512
513   if (code == MEM)
514     {
515       if (RTX_UNCHANGING_P (x)
516           && (XEXP (x, 0) == current_function_internal_arg_pointer
517               || (GET_CODE (XEXP (x, 0)) == PLUS
518                   && XEXP (XEXP (x, 0), 0) ==
519                      current_function_internal_arg_pointer
520                   && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
521         RTX_UNCHANGING_P (x) = 0;
522       return;
523     }
524
525   /* Scan all subexpressions.  */
526   fmt = GET_RTX_FORMAT (code);
527   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
528     {
529       if (*fmt == 'e')
530         purge_mem_unchanging_flag (XEXP (x, i));
531       else if (*fmt == 'E')
532         for (j = 0; j < XVECLEN (x, i); j++)
533           purge_mem_unchanging_flag (XVECEXP (x, i, j));
534     }
535 }
536
537 /* Replace the CALL_PLACEHOLDER with one of its children.  INSN should be
538    the CALL_PLACEHOLDER insn; USE tells which child to use.  */
539
540 void
541 replace_call_placeholder (insn, use)
542      rtx insn;
543      sibcall_use_t use;
544 {
545   if (use == sibcall_use_tail_recursion)
546     emit_insn_before (XEXP (PATTERN (insn), 2), insn);
547   else if (use == sibcall_use_sibcall)
548     emit_insn_before (XEXP (PATTERN (insn), 1), insn);
549   else if (use == sibcall_use_normal)
550     emit_insn_before (XEXP (PATTERN (insn), 0), insn);
551   else
552     abort ();
553
554   /* Turn off LABEL_PRESERVE_P for the tail recursion label if it
555      exists.  We only had to set it long enough to keep the jump
556      pass above from deleting it as unused.  */
557   if (XEXP (PATTERN (insn), 3))
558     LABEL_PRESERVE_P (XEXP (PATTERN (insn), 3)) = 0;
559
560   /* "Delete" the placeholder insn.  */
561   remove_insn (insn);
562 }
563
564 /* Given a (possibly empty) set of potential sibling or tail recursion call
565    sites, determine if optimization is possible.
566
567    Potential sibling or tail recursion calls are marked with CALL_PLACEHOLDER
568    insns.  The CALL_PLACEHOLDER insn holds chains of insns to implement a
569    normal call, sibling call or tail recursive call.
570
571    Replace the CALL_PLACEHOLDER with an appropriate insn chain.  */
572
573 void
574 optimize_sibling_and_tail_recursive_calls ()
575 {
576   rtx insn, insns;
577   basic_block alternate_exit = EXIT_BLOCK_PTR;
578   bool no_sibcalls_this_function = false;
579   bool successful_replacement = false;
580   bool replaced_call_placeholder = false;
581   edge e;
582
583   insns = get_insns ();
584
585   cleanup_cfg (CLEANUP_PRE_SIBCALL | CLEANUP_PRE_LOOP);
586
587   /* If there are no basic blocks, then there is nothing to do.  */
588   if (n_basic_blocks == 0)
589     return;
590
591   /* If we are using sjlj exceptions, we may need to add a call to
592      _Unwind_SjLj_Unregister at exit of the function.  Which means
593      that we cannot do any sibcall transformations.  */
594   if (USING_SJLJ_EXCEPTIONS && current_function_has_exception_handlers ())
595     no_sibcalls_this_function = true;
596
597   return_value_pseudo = NULL_RTX;
598
599   /* Find the exit block.
600
601      It is possible that we have blocks which can reach the exit block
602      directly.  However, most of the time a block will jump (or fall into)
603      N_BASIC_BLOCKS - 1, which in turn falls into the exit block.  */
604   for (e = EXIT_BLOCK_PTR->pred;
605        e && alternate_exit == EXIT_BLOCK_PTR;
606        e = e->pred_next)
607     {
608       rtx insn;
609
610       if (e->dest != EXIT_BLOCK_PTR || e->succ_next != NULL)
611         continue;
612
613       /* Walk forwards through the last normal block and see if it
614          does nothing except fall into the exit block.  */
615       for (insn = EXIT_BLOCK_PTR->prev_bb->head;
616            insn;
617            insn = NEXT_INSN (insn))
618         {
619           rtx set;
620           /* This should only happen once, at the start of this block.  */
621           if (GET_CODE (insn) == CODE_LABEL)
622             continue;
623
624           if (GET_CODE (insn) == NOTE)
625             continue;
626
627           if (GET_CODE (insn) == INSN
628               && GET_CODE (PATTERN (insn)) == USE)
629             continue;
630
631           /* Exit block also may contain copy from pseudo containing
632              return value to hard register.  */
633           if (GET_CODE (insn) == INSN
634               && (set = single_set (insn))
635               && SET_DEST (set) == current_function_return_rtx
636               && REG_P (SET_SRC (set))
637               && !return_value_pseudo)
638             {
639               return_value_pseudo = SET_SRC (set);
640               continue;
641             }
642
643           break;
644         }
645
646       /* If INSN is zero, then the search walked all the way through the
647          block without hitting anything interesting.  This block is a
648          valid alternate exit block.  */
649       if (insn == NULL)
650         alternate_exit = e->src;
651       else
652         return_value_pseudo = NULL;
653     }
654
655   /* If the function uses ADDRESSOF, we can't (easily) determine
656      at this point if the value will end up on the stack.  */
657   no_sibcalls_this_function |= sequence_uses_addressof (insns);
658
659   /* Walk the insn chain and find any CALL_PLACEHOLDER insns.  We need to
660      select one of the insn sequences attached to each CALL_PLACEHOLDER.
661
662      The different sequences represent different ways to implement the call,
663      ie, tail recursion, sibling call or normal call.
664
665      Since we do not create nested CALL_PLACEHOLDERs, the scan
666      continues with the insn that was after a replaced CALL_PLACEHOLDER;
667      we don't rescan the replacement insns.  */
668   for (insn = insns; insn; insn = NEXT_INSN (insn))
669     {
670       if (GET_CODE (insn) == CALL_INSN
671           && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
672         {
673           int sibcall = (XEXP (PATTERN (insn), 1) != NULL_RTX);
674           int tailrecursion = (XEXP (PATTERN (insn), 2) != NULL_RTX);
675           basic_block call_block = BLOCK_FOR_INSN (insn);
676
677           /* alloca (until we have stack slot life analysis) inhibits
678              sibling call optimizations, but not tail recursion.
679              Similarly if we use varargs or stdarg since they implicitly
680              may take the address of an argument.  */
681           if (current_function_calls_alloca || current_function_stdarg)
682             sibcall = 0;
683
684           /* See if there are any reasons we can't perform either sibling or
685              tail call optimizations.  We must be careful with stack slots
686              which are live at potential optimization sites.  */
687           if (no_sibcalls_this_function
688               /* ??? Overly conservative.  */
689               || frame_offset
690               /* Any function that calls setjmp might have longjmp called from
691                  any called function.  ??? We really should represent this
692                  properly in the CFG so that this needn't be special cased.  */
693               || current_function_calls_setjmp
694               /* Can't if more than one successor or single successor is not
695                  exit block.  These two tests prevent tail call optimization
696                  in the presence of active exception handlers.  */
697               || call_block->succ == NULL
698               || call_block->succ->succ_next != NULL
699               || (call_block->succ->dest != EXIT_BLOCK_PTR
700                   && call_block->succ->dest != alternate_exit)
701               /* If this call doesn't end the block, there are operations at
702                  the end of the block which we must execute after returning.  */
703               || ! call_ends_block_p (insn, call_block->end))
704             sibcall = 0, tailrecursion = 0;
705
706           /* Select a set of insns to implement the call and emit them.
707              Tail recursion is the most efficient, so select it over
708              a tail/sibling call.  */
709
710           if (sibcall || tailrecursion)
711             successful_replacement = true;
712           replaced_call_placeholder = true;
713
714           replace_call_placeholder (insn,
715                                     tailrecursion != 0
716                                       ? sibcall_use_tail_recursion
717                                       : sibcall != 0
718                                          ? sibcall_use_sibcall
719                                          : sibcall_use_normal);
720         }
721     }
722
723   if (successful_replacement)
724     {
725       rtx insn;
726       tree arg;
727
728       /* A sibling call sequence invalidates any REG_EQUIV notes made for
729          this function's incoming arguments.
730
731          At the start of RTL generation we know the only REG_EQUIV notes
732          in the rtl chain are those for incoming arguments, so we can safely
733          flush any REG_EQUIV note.
734
735          This is (slight) overkill.  We could keep track of the highest
736          argument we clobber and be more selective in removing notes, but it
737          does not seem to be worth the effort.  */
738       purge_reg_equiv_notes ();
739
740       /* A sibling call sequence also may invalidate RTX_UNCHANGING_P
741          flag of some incoming arguments MEM RTLs, because it can write into
742          those slots.  We clear all those bits now.
743
744          This is (slight) overkill, we could keep track of which arguments
745          we actually write into.  */
746       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
747         {
748           if (INSN_P (insn))
749             purge_mem_unchanging_flag (PATTERN (insn));
750         }
751
752       /* Similarly, invalidate RTX_UNCHANGING_P for any incoming
753          arguments passed in registers.  */
754       for (arg = DECL_ARGUMENTS (current_function_decl);
755            arg;
756            arg = TREE_CHAIN (arg))
757         {
758           if (REG_P (DECL_RTL (arg)))
759             RTX_UNCHANGING_P (DECL_RTL (arg)) = false;
760         }
761     }
762
763   /* There may have been NOTE_INSN_BLOCK_{BEGIN,END} notes in the
764      CALL_PLACEHOLDER alternatives that we didn't emit.  Rebuild the
765      lexical block tree to correspond to the notes that still exist.  */
766   if (replaced_call_placeholder)
767     reorder_blocks ();
768
769   /* This information will be invalid after inline expansion.  Kill it now.  */
770   free_basic_block_vars (0);
771   free_EXPR_LIST_list (&tail_recursion_label_list);
772 }