OSDN Git Service

* gcc.dg/ipa/ipa-sra-2.c: Add dg-require-effective-target
[pf3gnuchains/gcc-fork.git] / gcc / function.c
1 /* Expands front end tree to back end RTL for GCC.
2    Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3    1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4    2010, 2011  Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* This file handles the generation of rtl code from tree structure
23    at the level of the function as a whole.
24    It creates the rtl expressions for parameters and auto variables
25    and has full responsibility for allocating stack slots.
26
27    `expand_function_start' is called at the beginning of a function,
28    before the function body is parsed, and `expand_function_end' is
29    called after parsing the body.
30
31    Call `assign_stack_local' to allocate a stack slot for a local variable.
32    This is usually done during the RTL generation for the function body,
33    but it can also be done in the reload pass when a pseudo-register does
34    not get a hard register.  */
35
36 #include "config.h"
37 #include "system.h"
38 #include "coretypes.h"
39 #include "tm.h"
40 #include "rtl-error.h"
41 #include "tree.h"
42 #include "flags.h"
43 #include "except.h"
44 #include "function.h"
45 #include "expr.h"
46 #include "optabs.h"
47 #include "libfuncs.h"
48 #include "regs.h"
49 #include "hard-reg-set.h"
50 #include "insn-config.h"
51 #include "recog.h"
52 #include "output.h"
53 #include "basic-block.h"
54 #include "hashtab.h"
55 #include "ggc.h"
56 #include "tm_p.h"
57 #include "integrate.h"
58 #include "langhooks.h"
59 #include "target.h"
60 #include "common/common-target.h"
61 #include "cfglayout.h"
62 #include "gimple.h"
63 #include "tree-pass.h"
64 #include "predict.h"
65 #include "df.h"
66 #include "timevar.h"
67 #include "vecprim.h"
68
69 /* So we can assign to cfun in this file.  */
70 #undef cfun
71
72 #ifndef STACK_ALIGNMENT_NEEDED
73 #define STACK_ALIGNMENT_NEEDED 1
74 #endif
75
76 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
77
78 /* Some systems use __main in a way incompatible with its use in gcc, in these
79    cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
80    give the same symbol without quotes for an alternative entry point.  You
81    must define both, or neither.  */
82 #ifndef NAME__MAIN
83 #define NAME__MAIN "__main"
84 #endif
85
86 /* Round a value to the lowest integer less than it that is a multiple of
87    the required alignment.  Avoid using division in case the value is
88    negative.  Assume the alignment is a power of two.  */
89 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
90
91 /* Similar, but round to the next highest integer that meets the
92    alignment.  */
93 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
94
95 /* Nonzero if function being compiled doesn't contain any calls
96    (ignoring the prologue and epilogue).  This is set prior to
97    local register allocation and is valid for the remaining
98    compiler passes.  */
99 int current_function_is_leaf;
100
101 /* Nonzero if function being compiled doesn't modify the stack pointer
102    (ignoring the prologue and epilogue).  This is only valid after
103    pass_stack_ptr_mod has run.  */
104 int current_function_sp_is_unchanging;
105
106 /* Nonzero if the function being compiled is a leaf function which only
107    uses leaf registers.  This is valid after reload (specifically after
108    sched2) and is useful only if the port defines LEAF_REGISTERS.  */
109 int current_function_uses_only_leaf_regs;
110
111 /* Nonzero once virtual register instantiation has been done.
112    assign_stack_local uses frame_pointer_rtx when this is nonzero.
113    calls.c:emit_library_call_value_1 uses it to set up
114    post-instantiation libcalls.  */
115 int virtuals_instantiated;
116
117 /* Assign unique numbers to labels generated for profiling, debugging, etc.  */
118 static GTY(()) int funcdef_no;
119
120 /* These variables hold pointers to functions to create and destroy
121    target specific, per-function data structures.  */
122 struct machine_function * (*init_machine_status) (void);
123
124 /* The currently compiled function.  */
125 struct function *cfun = 0;
126
127 /* These hashes record the prologue and epilogue insns.  */
128 static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
129   htab_t prologue_insn_hash;
130 static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
131   htab_t epilogue_insn_hash;
132 \f
133
134 htab_t types_used_by_vars_hash = NULL;
135 VEC(tree,gc) *types_used_by_cur_var_decl;
136
137 /* Forward declarations.  */
138
139 static struct temp_slot *find_temp_slot_from_address (rtx);
140 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
141 static void pad_below (struct args_size *, enum machine_mode, tree);
142 static void reorder_blocks_1 (rtx, tree, VEC(tree,heap) **);
143 static int all_blocks (tree, tree *);
144 static tree *get_block_vector (tree, int *);
145 extern tree debug_find_var_in_block_tree (tree, tree);
146 /* We always define `record_insns' even if it's not used so that we
147    can always export `prologue_epilogue_contains'.  */
148 static void record_insns (rtx, rtx, htab_t *) ATTRIBUTE_UNUSED;
149 static bool contains (const_rtx, htab_t);
150 static void prepare_function_start (void);
151 static void do_clobber_return_reg (rtx, void *);
152 static void do_use_return_reg (rtx, void *);
153 static void set_insn_locators (rtx, int) ATTRIBUTE_UNUSED;
154 \f
155 /* Stack of nested functions.  */
156 /* Keep track of the cfun stack.  */
157
158 typedef struct function *function_p;
159
160 DEF_VEC_P(function_p);
161 DEF_VEC_ALLOC_P(function_p,heap);
162 static VEC(function_p,heap) *function_context_stack;
163
164 /* Save the current context for compilation of a nested function.
165    This is called from language-specific code.  */
166
167 void
168 push_function_context (void)
169 {
170   if (cfun == 0)
171     allocate_struct_function (NULL, false);
172
173   VEC_safe_push (function_p, heap, function_context_stack, cfun);
174   set_cfun (NULL);
175 }
176
177 /* Restore the last saved context, at the end of a nested function.
178    This function is called from language-specific code.  */
179
180 void
181 pop_function_context (void)
182 {
183   struct function *p = VEC_pop (function_p, function_context_stack);
184   set_cfun (p);
185   current_function_decl = p->decl;
186
187   /* Reset variables that have known state during rtx generation.  */
188   virtuals_instantiated = 0;
189   generating_concat_p = 1;
190 }
191
192 /* Clear out all parts of the state in F that can safely be discarded
193    after the function has been parsed, but not compiled, to let
194    garbage collection reclaim the memory.  */
195
196 void
197 free_after_parsing (struct function *f)
198 {
199   f->language = 0;
200 }
201
202 /* Clear out all parts of the state in F that can safely be discarded
203    after the function has been compiled, to let garbage collection
204    reclaim the memory.  */
205
206 void
207 free_after_compilation (struct function *f)
208 {
209   prologue_insn_hash = NULL;
210   epilogue_insn_hash = NULL;
211
212   free (crtl->emit.regno_pointer_align);
213
214   memset (crtl, 0, sizeof (struct rtl_data));
215   f->eh = NULL;
216   f->machine = NULL;
217   f->cfg = NULL;
218
219   regno_reg_rtx = NULL;
220   insn_locators_free ();
221 }
222 \f
223 /* Return size needed for stack frame based on slots so far allocated.
224    This size counts from zero.  It is not rounded to PREFERRED_STACK_BOUNDARY;
225    the caller may have to do that.  */
226
227 HOST_WIDE_INT
228 get_frame_size (void)
229 {
230   if (FRAME_GROWS_DOWNWARD)
231     return -frame_offset;
232   else
233     return frame_offset;
234 }
235
236 /* Issue an error message and return TRUE if frame OFFSET overflows in
237    the signed target pointer arithmetics for function FUNC.  Otherwise
238    return FALSE.  */
239
240 bool
241 frame_offset_overflow (HOST_WIDE_INT offset, tree func)
242 {
243   unsigned HOST_WIDE_INT size = FRAME_GROWS_DOWNWARD ? -offset : offset;
244
245   if (size > ((unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (Pmode) - 1))
246                /* Leave room for the fixed part of the frame.  */
247                - 64 * UNITS_PER_WORD)
248     {
249       error_at (DECL_SOURCE_LOCATION (func),
250                 "total size of local objects too large");
251       return TRUE;
252     }
253
254   return FALSE;
255 }
256
257 /* Return stack slot alignment in bits for TYPE and MODE.  */
258
259 static unsigned int
260 get_stack_local_alignment (tree type, enum machine_mode mode)
261 {
262   unsigned int alignment;
263
264   if (mode == BLKmode)
265     alignment = BIGGEST_ALIGNMENT;
266   else
267     alignment = GET_MODE_ALIGNMENT (mode);
268
269   /* Allow the frond-end to (possibly) increase the alignment of this
270      stack slot.  */
271   if (! type)
272     type = lang_hooks.types.type_for_mode (mode, 0);
273
274   return STACK_SLOT_ALIGNMENT (type, mode, alignment);
275 }
276
277 /* Determine whether it is possible to fit a stack slot of size SIZE and
278    alignment ALIGNMENT into an area in the stack frame that starts at
279    frame offset START and has a length of LENGTH.  If so, store the frame
280    offset to be used for the stack slot in *POFFSET and return true;
281    return false otherwise.  This function will extend the frame size when
282    given a start/length pair that lies at the end of the frame.  */
283
284 static bool
285 try_fit_stack_local (HOST_WIDE_INT start, HOST_WIDE_INT length,
286                      HOST_WIDE_INT size, unsigned int alignment,
287                      HOST_WIDE_INT *poffset)
288 {
289   HOST_WIDE_INT this_frame_offset;
290   int frame_off, frame_alignment, frame_phase;
291
292   /* Calculate how many bytes the start of local variables is off from
293      stack alignment.  */
294   frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
295   frame_off = STARTING_FRAME_OFFSET % frame_alignment;
296   frame_phase = frame_off ? frame_alignment - frame_off : 0;
297
298   /* Round the frame offset to the specified alignment.  */
299
300   /*  We must be careful here, since FRAME_OFFSET might be negative and
301       division with a negative dividend isn't as well defined as we might
302       like.  So we instead assume that ALIGNMENT is a power of two and
303       use logical operations which are unambiguous.  */
304   if (FRAME_GROWS_DOWNWARD)
305     this_frame_offset
306       = (FLOOR_ROUND (start + length - size - frame_phase,
307                       (unsigned HOST_WIDE_INT) alignment)
308          + frame_phase);
309   else
310     this_frame_offset
311       = (CEIL_ROUND (start - frame_phase,
312                      (unsigned HOST_WIDE_INT) alignment)
313          + frame_phase);
314
315   /* See if it fits.  If this space is at the edge of the frame,
316      consider extending the frame to make it fit.  Our caller relies on
317      this when allocating a new slot.  */
318   if (frame_offset == start && this_frame_offset < frame_offset)
319     frame_offset = this_frame_offset;
320   else if (this_frame_offset < start)
321     return false;
322   else if (start + length == frame_offset
323            && this_frame_offset + size > start + length)
324     frame_offset = this_frame_offset + size;
325   else if (this_frame_offset + size > start + length)
326     return false;
327
328   *poffset = this_frame_offset;
329   return true;
330 }
331
332 /* Create a new frame_space structure describing free space in the stack
333    frame beginning at START and ending at END, and chain it into the
334    function's frame_space_list.  */
335
336 static void
337 add_frame_space (HOST_WIDE_INT start, HOST_WIDE_INT end)
338 {
339   struct frame_space *space = ggc_alloc_frame_space ();
340   space->next = crtl->frame_space_list;
341   crtl->frame_space_list = space;
342   space->start = start;
343   space->length = end - start;
344 }
345
346 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
347    with machine mode MODE.
348
349    ALIGN controls the amount of alignment for the address of the slot:
350    0 means according to MODE,
351    -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
352    -2 means use BITS_PER_UNIT,
353    positive specifies alignment boundary in bits.
354
355    KIND has ASLK_REDUCE_ALIGN bit set if it is OK to reduce
356    alignment and ASLK_RECORD_PAD bit set if we should remember
357    extra space we allocated for alignment purposes.  When we are
358    called from assign_stack_temp_for_type, it is not set so we don't
359    track the same stack slot in two independent lists.
360
361    We do not round to stack_boundary here.  */
362
363 rtx
364 assign_stack_local_1 (enum machine_mode mode, HOST_WIDE_INT size,
365                       int align, int kind)
366 {
367   rtx x, addr;
368   int bigend_correction = 0;
369   HOST_WIDE_INT slot_offset = 0, old_frame_offset;
370   unsigned int alignment, alignment_in_bits;
371
372   if (align == 0)
373     {
374       alignment = get_stack_local_alignment (NULL, mode);
375       alignment /= BITS_PER_UNIT;
376     }
377   else if (align == -1)
378     {
379       alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
380       size = CEIL_ROUND (size, alignment);
381     }
382   else if (align == -2)
383     alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
384   else
385     alignment = align / BITS_PER_UNIT;
386
387   alignment_in_bits = alignment * BITS_PER_UNIT;
388
389   /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT.  */
390   if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
391     {
392       alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
393       alignment = alignment_in_bits / BITS_PER_UNIT;
394     }
395
396   if (SUPPORTS_STACK_ALIGNMENT)
397     {
398       if (crtl->stack_alignment_estimated < alignment_in_bits)
399         {
400           if (!crtl->stack_realign_processed)
401             crtl->stack_alignment_estimated = alignment_in_bits;
402           else
403             {
404               /* If stack is realigned and stack alignment value
405                  hasn't been finalized, it is OK not to increase
406                  stack_alignment_estimated.  The bigger alignment
407                  requirement is recorded in stack_alignment_needed
408                  below.  */
409               gcc_assert (!crtl->stack_realign_finalized);
410               if (!crtl->stack_realign_needed)
411                 {
412                   /* It is OK to reduce the alignment as long as the
413                      requested size is 0 or the estimated stack
414                      alignment >= mode alignment.  */
415                   gcc_assert ((kind & ASLK_REDUCE_ALIGN)
416                               || size == 0
417                               || (crtl->stack_alignment_estimated
418                                   >= GET_MODE_ALIGNMENT (mode)));
419                   alignment_in_bits = crtl->stack_alignment_estimated;
420                   alignment = alignment_in_bits / BITS_PER_UNIT;
421                 }
422             }
423         }
424     }
425
426   if (crtl->stack_alignment_needed < alignment_in_bits)
427     crtl->stack_alignment_needed = alignment_in_bits;
428   if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
429     crtl->max_used_stack_slot_alignment = alignment_in_bits;
430
431   if (mode != BLKmode || size != 0)
432     {
433       if (kind & ASLK_RECORD_PAD)
434         {
435           struct frame_space **psp;
436
437           for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
438             {
439               struct frame_space *space = *psp;
440               if (!try_fit_stack_local (space->start, space->length, size,
441                                         alignment, &slot_offset))
442                 continue;
443               *psp = space->next;
444               if (slot_offset > space->start)
445                 add_frame_space (space->start, slot_offset);
446               if (slot_offset + size < space->start + space->length)
447                 add_frame_space (slot_offset + size,
448                                  space->start + space->length);
449               goto found_space;
450             }
451         }
452     }
453   else if (!STACK_ALIGNMENT_NEEDED)
454     {
455       slot_offset = frame_offset;
456       goto found_space;
457     }
458
459   old_frame_offset = frame_offset;
460
461   if (FRAME_GROWS_DOWNWARD)
462     {
463       frame_offset -= size;
464       try_fit_stack_local (frame_offset, size, size, alignment, &slot_offset);
465
466       if (kind & ASLK_RECORD_PAD)
467         {
468           if (slot_offset > frame_offset)
469             add_frame_space (frame_offset, slot_offset);
470           if (slot_offset + size < old_frame_offset)
471             add_frame_space (slot_offset + size, old_frame_offset);
472         }
473     }
474   else
475     {
476       frame_offset += size;
477       try_fit_stack_local (old_frame_offset, size, size, alignment, &slot_offset);
478
479       if (kind & ASLK_RECORD_PAD)
480         {
481           if (slot_offset > old_frame_offset)
482             add_frame_space (old_frame_offset, slot_offset);
483           if (slot_offset + size < frame_offset)
484             add_frame_space (slot_offset + size, frame_offset);
485         }
486     }
487
488  found_space:
489   /* On a big-endian machine, if we are allocating more space than we will use,
490      use the least significant bytes of those that are allocated.  */
491   if (BYTES_BIG_ENDIAN && mode != BLKmode && GET_MODE_SIZE (mode) < size)
492     bigend_correction = size - GET_MODE_SIZE (mode);
493
494   /* If we have already instantiated virtual registers, return the actual
495      address relative to the frame pointer.  */
496   if (virtuals_instantiated)
497     addr = plus_constant (frame_pointer_rtx,
498                           trunc_int_for_mode
499                           (slot_offset + bigend_correction
500                            + STARTING_FRAME_OFFSET, Pmode));
501   else
502     addr = plus_constant (virtual_stack_vars_rtx,
503                           trunc_int_for_mode
504                           (slot_offset + bigend_correction,
505                            Pmode));
506
507   x = gen_rtx_MEM (mode, addr);
508   set_mem_align (x, alignment_in_bits);
509   MEM_NOTRAP_P (x) = 1;
510
511   stack_slot_list
512     = gen_rtx_EXPR_LIST (VOIDmode, x, stack_slot_list);
513
514   if (frame_offset_overflow (frame_offset, current_function_decl))
515     frame_offset = 0;
516
517   return x;
518 }
519
520 /* Wrap up assign_stack_local_1 with last parameter as false.  */
521
522 rtx
523 assign_stack_local (enum machine_mode mode, HOST_WIDE_INT size, int align)
524 {
525   return assign_stack_local_1 (mode, size, align, ASLK_RECORD_PAD);
526 }
527 \f
528 \f
529 /* In order to evaluate some expressions, such as function calls returning
530    structures in memory, we need to temporarily allocate stack locations.
531    We record each allocated temporary in the following structure.
532
533    Associated with each temporary slot is a nesting level.  When we pop up
534    one level, all temporaries associated with the previous level are freed.
535    Normally, all temporaries are freed after the execution of the statement
536    in which they were created.  However, if we are inside a ({...}) grouping,
537    the result may be in a temporary and hence must be preserved.  If the
538    result could be in a temporary, we preserve it if we can determine which
539    one it is in.  If we cannot determine which temporary may contain the
540    result, all temporaries are preserved.  A temporary is preserved by
541    pretending it was allocated at the previous nesting level.
542
543    Automatic variables are also assigned temporary slots, at the nesting
544    level where they are defined.  They are marked a "kept" so that
545    free_temp_slots will not free them.  */
546
547 struct GTY(()) temp_slot {
548   /* Points to next temporary slot.  */
549   struct temp_slot *next;
550   /* Points to previous temporary slot.  */
551   struct temp_slot *prev;
552   /* The rtx to used to reference the slot.  */
553   rtx slot;
554   /* The size, in units, of the slot.  */
555   HOST_WIDE_INT size;
556   /* The type of the object in the slot, or zero if it doesn't correspond
557      to a type.  We use this to determine whether a slot can be reused.
558      It can be reused if objects of the type of the new slot will always
559      conflict with objects of the type of the old slot.  */
560   tree type;
561   /* The alignment (in bits) of the slot.  */
562   unsigned int align;
563   /* Nonzero if this temporary is currently in use.  */
564   char in_use;
565   /* Nonzero if this temporary has its address taken.  */
566   char addr_taken;
567   /* Nesting level at which this slot is being used.  */
568   int level;
569   /* Nonzero if this should survive a call to free_temp_slots.  */
570   int keep;
571   /* The offset of the slot from the frame_pointer, including extra space
572      for alignment.  This info is for combine_temp_slots.  */
573   HOST_WIDE_INT base_offset;
574   /* The size of the slot, including extra space for alignment.  This
575      info is for combine_temp_slots.  */
576   HOST_WIDE_INT full_size;
577 };
578
579 /* A table of addresses that represent a stack slot.  The table is a mapping
580    from address RTXen to a temp slot.  */
581 static GTY((param_is(struct temp_slot_address_entry))) htab_t temp_slot_address_table;
582
583 /* Entry for the above hash table.  */
584 struct GTY(()) temp_slot_address_entry {
585   hashval_t hash;
586   rtx address;
587   struct temp_slot *temp_slot;
588 };
589
590 /* Removes temporary slot TEMP from LIST.  */
591
592 static void
593 cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
594 {
595   if (temp->next)
596     temp->next->prev = temp->prev;
597   if (temp->prev)
598     temp->prev->next = temp->next;
599   else
600     *list = temp->next;
601
602   temp->prev = temp->next = NULL;
603 }
604
605 /* Inserts temporary slot TEMP to LIST.  */
606
607 static void
608 insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
609 {
610   temp->next = *list;
611   if (*list)
612     (*list)->prev = temp;
613   temp->prev = NULL;
614   *list = temp;
615 }
616
617 /* Returns the list of used temp slots at LEVEL.  */
618
619 static struct temp_slot **
620 temp_slots_at_level (int level)
621 {
622   if (level >= (int) VEC_length (temp_slot_p, used_temp_slots))
623     VEC_safe_grow_cleared (temp_slot_p, gc, used_temp_slots, level + 1);
624
625   return &(VEC_address (temp_slot_p, used_temp_slots)[level]);
626 }
627
628 /* Returns the maximal temporary slot level.  */
629
630 static int
631 max_slot_level (void)
632 {
633   if (!used_temp_slots)
634     return -1;
635
636   return VEC_length (temp_slot_p, used_temp_slots) - 1;
637 }
638
639 /* Moves temporary slot TEMP to LEVEL.  */
640
641 static void
642 move_slot_to_level (struct temp_slot *temp, int level)
643 {
644   cut_slot_from_list (temp, temp_slots_at_level (temp->level));
645   insert_slot_to_list (temp, temp_slots_at_level (level));
646   temp->level = level;
647 }
648
649 /* Make temporary slot TEMP available.  */
650
651 static void
652 make_slot_available (struct temp_slot *temp)
653 {
654   cut_slot_from_list (temp, temp_slots_at_level (temp->level));
655   insert_slot_to_list (temp, &avail_temp_slots);
656   temp->in_use = 0;
657   temp->level = -1;
658 }
659
660 /* Compute the hash value for an address -> temp slot mapping.
661    The value is cached on the mapping entry.  */
662 static hashval_t
663 temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
664 {
665   int do_not_record = 0;
666   return hash_rtx (t->address, GET_MODE (t->address),
667                    &do_not_record, NULL, false);
668 }
669
670 /* Return the hash value for an address -> temp slot mapping.  */
671 static hashval_t
672 temp_slot_address_hash (const void *p)
673 {
674   const struct temp_slot_address_entry *t;
675   t = (const struct temp_slot_address_entry *) p;
676   return t->hash;
677 }
678
679 /* Compare two address -> temp slot mapping entries.  */
680 static int
681 temp_slot_address_eq (const void *p1, const void *p2)
682 {
683   const struct temp_slot_address_entry *t1, *t2;
684   t1 = (const struct temp_slot_address_entry *) p1;
685   t2 = (const struct temp_slot_address_entry *) p2;
686   return exp_equiv_p (t1->address, t2->address, 0, true);
687 }
688
689 /* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping.  */
690 static void
691 insert_temp_slot_address (rtx address, struct temp_slot *temp_slot)
692 {
693   void **slot;
694   struct temp_slot_address_entry *t = ggc_alloc_temp_slot_address_entry ();
695   t->address = address;
696   t->temp_slot = temp_slot;
697   t->hash = temp_slot_address_compute_hash (t);
698   slot = htab_find_slot_with_hash (temp_slot_address_table, t, t->hash, INSERT);
699   *slot = t;
700 }
701
702 /* Remove an address -> temp slot mapping entry if the temp slot is
703    not in use anymore.  Callback for remove_unused_temp_slot_addresses.  */
704 static int
705 remove_unused_temp_slot_addresses_1 (void **slot, void *data ATTRIBUTE_UNUSED)
706 {
707   const struct temp_slot_address_entry *t;
708   t = (const struct temp_slot_address_entry *) *slot;
709   if (! t->temp_slot->in_use)
710     *slot = NULL;
711   return 1;
712 }
713
714 /* Remove all mappings of addresses to unused temp slots.  */
715 static void
716 remove_unused_temp_slot_addresses (void)
717 {
718   htab_traverse (temp_slot_address_table,
719                  remove_unused_temp_slot_addresses_1,
720                  NULL);
721 }
722
723 /* Find the temp slot corresponding to the object at address X.  */
724
725 static struct temp_slot *
726 find_temp_slot_from_address (rtx x)
727 {
728   struct temp_slot *p;
729   struct temp_slot_address_entry tmp, *t;
730
731   /* First try the easy way:
732      See if X exists in the address -> temp slot mapping.  */
733   tmp.address = x;
734   tmp.temp_slot = NULL;
735   tmp.hash = temp_slot_address_compute_hash (&tmp);
736   t = (struct temp_slot_address_entry *)
737     htab_find_with_hash (temp_slot_address_table, &tmp, tmp.hash);
738   if (t)
739     return t->temp_slot;
740
741   /* If we have a sum involving a register, see if it points to a temp
742      slot.  */
743   if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
744       && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
745     return p;
746   else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
747            && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
748     return p;
749
750   /* Last resort: Address is a virtual stack var address.  */
751   if (GET_CODE (x) == PLUS
752       && XEXP (x, 0) == virtual_stack_vars_rtx
753       && CONST_INT_P (XEXP (x, 1)))
754     {
755       int i;
756       for (i = max_slot_level (); i >= 0; i--)
757         for (p = *temp_slots_at_level (i); p; p = p->next)
758           {
759             if (INTVAL (XEXP (x, 1)) >= p->base_offset
760                 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size)
761               return p;
762           }
763     }
764
765   return NULL;
766 }
767 \f
768 /* Allocate a temporary stack slot and record it for possible later
769    reuse.
770
771    MODE is the machine mode to be given to the returned rtx.
772
773    SIZE is the size in units of the space required.  We do no rounding here
774    since assign_stack_local will do any required rounding.
775
776    KEEP is 1 if this slot is to be retained after a call to
777    free_temp_slots.  Automatic variables for a block are allocated
778    with this flag.  KEEP values of 2 or 3 were needed respectively
779    for variables whose lifetime is controlled by CLEANUP_POINT_EXPRs
780    or for SAVE_EXPRs, but they are now unused.
781
782    TYPE is the type that will be used for the stack slot.  */
783
784 rtx
785 assign_stack_temp_for_type (enum machine_mode mode, HOST_WIDE_INT size,
786                             int keep, tree type)
787 {
788   unsigned int align;
789   struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
790   rtx slot;
791
792   /* If SIZE is -1 it means that somebody tried to allocate a temporary
793      of a variable size.  */
794   gcc_assert (size != -1);
795
796   /* These are now unused.  */
797   gcc_assert (keep <= 1);
798
799   align = get_stack_local_alignment (type, mode);
800
801   /* Try to find an available, already-allocated temporary of the proper
802      mode which meets the size and alignment requirements.  Choose the
803      smallest one with the closest alignment.
804
805      If assign_stack_temp is called outside of the tree->rtl expansion,
806      we cannot reuse the stack slots (that may still refer to
807      VIRTUAL_STACK_VARS_REGNUM).  */
808   if (!virtuals_instantiated)
809     {
810       for (p = avail_temp_slots; p; p = p->next)
811         {
812           if (p->align >= align && p->size >= size
813               && GET_MODE (p->slot) == mode
814               && objects_must_conflict_p (p->type, type)
815               && (best_p == 0 || best_p->size > p->size
816                   || (best_p->size == p->size && best_p->align > p->align)))
817             {
818               if (p->align == align && p->size == size)
819                 {
820                   selected = p;
821                   cut_slot_from_list (selected, &avail_temp_slots);
822                   best_p = 0;
823                   break;
824                 }
825               best_p = p;
826             }
827         }
828     }
829
830   /* Make our best, if any, the one to use.  */
831   if (best_p)
832     {
833       selected = best_p;
834       cut_slot_from_list (selected, &avail_temp_slots);
835
836       /* If there are enough aligned bytes left over, make them into a new
837          temp_slot so that the extra bytes don't get wasted.  Do this only
838          for BLKmode slots, so that we can be sure of the alignment.  */
839       if (GET_MODE (best_p->slot) == BLKmode)
840         {
841           int alignment = best_p->align / BITS_PER_UNIT;
842           HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
843
844           if (best_p->size - rounded_size >= alignment)
845             {
846               p = ggc_alloc_temp_slot ();
847               p->in_use = p->addr_taken = 0;
848               p->size = best_p->size - rounded_size;
849               p->base_offset = best_p->base_offset + rounded_size;
850               p->full_size = best_p->full_size - rounded_size;
851               p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
852               p->align = best_p->align;
853               p->type = best_p->type;
854               insert_slot_to_list (p, &avail_temp_slots);
855
856               stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
857                                                    stack_slot_list);
858
859               best_p->size = rounded_size;
860               best_p->full_size = rounded_size;
861             }
862         }
863     }
864
865   /* If we still didn't find one, make a new temporary.  */
866   if (selected == 0)
867     {
868       HOST_WIDE_INT frame_offset_old = frame_offset;
869
870       p = ggc_alloc_temp_slot ();
871
872       /* We are passing an explicit alignment request to assign_stack_local.
873          One side effect of that is assign_stack_local will not round SIZE
874          to ensure the frame offset remains suitably aligned.
875
876          So for requests which depended on the rounding of SIZE, we go ahead
877          and round it now.  We also make sure ALIGNMENT is at least
878          BIGGEST_ALIGNMENT.  */
879       gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
880       p->slot = assign_stack_local_1 (mode,
881                                       (mode == BLKmode
882                                        ? CEIL_ROUND (size,
883                                                      (int) align
884                                                      / BITS_PER_UNIT)
885                                        : size),
886                                       align, 0);
887
888       p->align = align;
889
890       /* The following slot size computation is necessary because we don't
891          know the actual size of the temporary slot until assign_stack_local
892          has performed all the frame alignment and size rounding for the
893          requested temporary.  Note that extra space added for alignment
894          can be either above or below this stack slot depending on which
895          way the frame grows.  We include the extra space if and only if it
896          is above this slot.  */
897       if (FRAME_GROWS_DOWNWARD)
898         p->size = frame_offset_old - frame_offset;
899       else
900         p->size = size;
901
902       /* Now define the fields used by combine_temp_slots.  */
903       if (FRAME_GROWS_DOWNWARD)
904         {
905           p->base_offset = frame_offset;
906           p->full_size = frame_offset_old - frame_offset;
907         }
908       else
909         {
910           p->base_offset = frame_offset_old;
911           p->full_size = frame_offset - frame_offset_old;
912         }
913
914       selected = p;
915     }
916
917   p = selected;
918   p->in_use = 1;
919   p->addr_taken = 0;
920   p->type = type;
921   p->level = temp_slot_level;
922   p->keep = keep;
923
924   pp = temp_slots_at_level (p->level);
925   insert_slot_to_list (p, pp);
926   insert_temp_slot_address (XEXP (p->slot, 0), p);
927
928   /* Create a new MEM rtx to avoid clobbering MEM flags of old slots.  */
929   slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
930   stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
931
932   /* If we know the alias set for the memory that will be used, use
933      it.  If there's no TYPE, then we don't know anything about the
934      alias set for the memory.  */
935   set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
936   set_mem_align (slot, align);
937
938   /* If a type is specified, set the relevant flags.  */
939   if (type != 0)
940     {
941       MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
942       gcc_checking_assert (!MEM_SCALAR_P (slot) && !MEM_IN_STRUCT_P (slot));
943       if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
944         MEM_IN_STRUCT_P (slot) = 1;
945       else
946         MEM_SCALAR_P (slot) = 1;
947     }
948   MEM_NOTRAP_P (slot) = 1;
949
950   return slot;
951 }
952
953 /* Allocate a temporary stack slot and record it for possible later
954    reuse.  First three arguments are same as in preceding function.  */
955
956 rtx
957 assign_stack_temp (enum machine_mode mode, HOST_WIDE_INT size, int keep)
958 {
959   return assign_stack_temp_for_type (mode, size, keep, NULL_TREE);
960 }
961 \f
962 /* Assign a temporary.
963    If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
964    and so that should be used in error messages.  In either case, we
965    allocate of the given type.
966    KEEP is as for assign_stack_temp.
967    MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
968    it is 0 if a register is OK.
969    DONT_PROMOTE is 1 if we should not promote values in register
970    to wider modes.  */
971
972 rtx
973 assign_temp (tree type_or_decl, int keep, int memory_required,
974              int dont_promote ATTRIBUTE_UNUSED)
975 {
976   tree type, decl;
977   enum machine_mode mode;
978 #ifdef PROMOTE_MODE
979   int unsignedp;
980 #endif
981
982   if (DECL_P (type_or_decl))
983     decl = type_or_decl, type = TREE_TYPE (decl);
984   else
985     decl = NULL, type = type_or_decl;
986
987   mode = TYPE_MODE (type);
988 #ifdef PROMOTE_MODE
989   unsignedp = TYPE_UNSIGNED (type);
990 #endif
991
992   if (mode == BLKmode || memory_required)
993     {
994       HOST_WIDE_INT size = int_size_in_bytes (type);
995       rtx tmp;
996
997       /* Zero sized arrays are GNU C extension.  Set size to 1 to avoid
998          problems with allocating the stack space.  */
999       if (size == 0)
1000         size = 1;
1001
1002       /* Unfortunately, we don't yet know how to allocate variable-sized
1003          temporaries.  However, sometimes we can find a fixed upper limit on
1004          the size, so try that instead.  */
1005       else if (size == -1)
1006         size = max_int_size_in_bytes (type);
1007
1008       /* The size of the temporary may be too large to fit into an integer.  */
1009       /* ??? Not sure this should happen except for user silliness, so limit
1010          this to things that aren't compiler-generated temporaries.  The
1011          rest of the time we'll die in assign_stack_temp_for_type.  */
1012       if (decl && size == -1
1013           && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
1014         {
1015           error ("size of variable %q+D is too large", decl);
1016           size = 1;
1017         }
1018
1019       tmp = assign_stack_temp_for_type (mode, size, keep, type);
1020       return tmp;
1021     }
1022
1023 #ifdef PROMOTE_MODE
1024   if (! dont_promote)
1025     mode = promote_mode (type, mode, &unsignedp);
1026 #endif
1027
1028   return gen_reg_rtx (mode);
1029 }
1030 \f
1031 /* Combine temporary stack slots which are adjacent on the stack.
1032
1033    This allows for better use of already allocated stack space.  This is only
1034    done for BLKmode slots because we can be sure that we won't have alignment
1035    problems in this case.  */
1036
1037 static void
1038 combine_temp_slots (void)
1039 {
1040   struct temp_slot *p, *q, *next, *next_q;
1041   int num_slots;
1042
1043   /* We can't combine slots, because the information about which slot
1044      is in which alias set will be lost.  */
1045   if (flag_strict_aliasing)
1046     return;
1047
1048   /* If there are a lot of temp slots, don't do anything unless
1049      high levels of optimization.  */
1050   if (! flag_expensive_optimizations)
1051     for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
1052       if (num_slots > 100 || (num_slots > 10 && optimize == 0))
1053         return;
1054
1055   for (p = avail_temp_slots; p; p = next)
1056     {
1057       int delete_p = 0;
1058
1059       next = p->next;
1060
1061       if (GET_MODE (p->slot) != BLKmode)
1062         continue;
1063
1064       for (q = p->next; q; q = next_q)
1065         {
1066           int delete_q = 0;
1067
1068           next_q = q->next;
1069
1070           if (GET_MODE (q->slot) != BLKmode)
1071             continue;
1072
1073           if (p->base_offset + p->full_size == q->base_offset)
1074             {
1075               /* Q comes after P; combine Q into P.  */
1076               p->size += q->size;
1077               p->full_size += q->full_size;
1078               delete_q = 1;
1079             }
1080           else if (q->base_offset + q->full_size == p->base_offset)
1081             {
1082               /* P comes after Q; combine P into Q.  */
1083               q->size += p->size;
1084               q->full_size += p->full_size;
1085               delete_p = 1;
1086               break;
1087             }
1088           if (delete_q)
1089             cut_slot_from_list (q, &avail_temp_slots);
1090         }
1091
1092       /* Either delete P or advance past it.  */
1093       if (delete_p)
1094         cut_slot_from_list (p, &avail_temp_slots);
1095     }
1096 }
1097 \f
1098 /* Indicate that NEW_RTX is an alternate way of referring to the temp
1099    slot that previously was known by OLD_RTX.  */
1100
1101 void
1102 update_temp_slot_address (rtx old_rtx, rtx new_rtx)
1103 {
1104   struct temp_slot *p;
1105
1106   if (rtx_equal_p (old_rtx, new_rtx))
1107     return;
1108
1109   p = find_temp_slot_from_address (old_rtx);
1110
1111   /* If we didn't find one, see if both OLD_RTX is a PLUS.  If so, and
1112      NEW_RTX is a register, see if one operand of the PLUS is a
1113      temporary location.  If so, NEW_RTX points into it.  Otherwise,
1114      if both OLD_RTX and NEW_RTX are a PLUS and if there is a register
1115      in common between them.  If so, try a recursive call on those
1116      values.  */
1117   if (p == 0)
1118     {
1119       if (GET_CODE (old_rtx) != PLUS)
1120         return;
1121
1122       if (REG_P (new_rtx))
1123         {
1124           update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1125           update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
1126           return;
1127         }
1128       else if (GET_CODE (new_rtx) != PLUS)
1129         return;
1130
1131       if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 0)))
1132         update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 1));
1133       else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 0)))
1134         update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 1));
1135       else if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 1)))
1136         update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 0));
1137       else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 1)))
1138         update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 0));
1139
1140       return;
1141     }
1142
1143   /* Otherwise add an alias for the temp's address.  */
1144   insert_temp_slot_address (new_rtx, p);
1145 }
1146
1147 /* If X could be a reference to a temporary slot, mark the fact that its
1148    address was taken.  */
1149
1150 void
1151 mark_temp_addr_taken (rtx x)
1152 {
1153   struct temp_slot *p;
1154
1155   if (x == 0)
1156     return;
1157
1158   /* If X is not in memory or is at a constant address, it cannot be in
1159      a temporary slot.  */
1160   if (!MEM_P (x) || CONSTANT_P (XEXP (x, 0)))
1161     return;
1162
1163   p = find_temp_slot_from_address (XEXP (x, 0));
1164   if (p != 0)
1165     p->addr_taken = 1;
1166 }
1167
1168 /* If X could be a reference to a temporary slot, mark that slot as
1169    belonging to the to one level higher than the current level.  If X
1170    matched one of our slots, just mark that one.  Otherwise, we can't
1171    easily predict which it is, so upgrade all of them.  Kept slots
1172    need not be touched.
1173
1174    This is called when an ({...}) construct occurs and a statement
1175    returns a value in memory.  */
1176
1177 void
1178 preserve_temp_slots (rtx x)
1179 {
1180   struct temp_slot *p = 0, *next;
1181
1182   /* If there is no result, we still might have some objects whose address
1183      were taken, so we need to make sure they stay around.  */
1184   if (x == 0)
1185     {
1186       for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1187         {
1188           next = p->next;
1189
1190           if (p->addr_taken)
1191             move_slot_to_level (p, temp_slot_level - 1);
1192         }
1193
1194       return;
1195     }
1196
1197   /* If X is a register that is being used as a pointer, see if we have
1198      a temporary slot we know it points to.  To be consistent with
1199      the code below, we really should preserve all non-kept slots
1200      if we can't find a match, but that seems to be much too costly.  */
1201   if (REG_P (x) && REG_POINTER (x))
1202     p = find_temp_slot_from_address (x);
1203
1204   /* If X is not in memory or is at a constant address, it cannot be in
1205      a temporary slot, but it can contain something whose address was
1206      taken.  */
1207   if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1208     {
1209       for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1210         {
1211           next = p->next;
1212
1213           if (p->addr_taken)
1214             move_slot_to_level (p, temp_slot_level - 1);
1215         }
1216
1217       return;
1218     }
1219
1220   /* First see if we can find a match.  */
1221   if (p == 0)
1222     p = find_temp_slot_from_address (XEXP (x, 0));
1223
1224   if (p != 0)
1225     {
1226       /* Move everything at our level whose address was taken to our new
1227          level in case we used its address.  */
1228       struct temp_slot *q;
1229
1230       if (p->level == temp_slot_level)
1231         {
1232           for (q = *temp_slots_at_level (temp_slot_level); q; q = next)
1233             {
1234               next = q->next;
1235
1236               if (p != q && q->addr_taken)
1237                 move_slot_to_level (q, temp_slot_level - 1);
1238             }
1239
1240           move_slot_to_level (p, temp_slot_level - 1);
1241           p->addr_taken = 0;
1242         }
1243       return;
1244     }
1245
1246   /* Otherwise, preserve all non-kept slots at this level.  */
1247   for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1248     {
1249       next = p->next;
1250
1251       if (!p->keep)
1252         move_slot_to_level (p, temp_slot_level - 1);
1253     }
1254 }
1255
1256 /* Free all temporaries used so far.  This is normally called at the
1257    end of generating code for a statement.  */
1258
1259 void
1260 free_temp_slots (void)
1261 {
1262   struct temp_slot *p, *next;
1263   bool some_available = false;
1264
1265   for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1266     {
1267       next = p->next;
1268
1269       if (!p->keep)
1270         {
1271           make_slot_available (p);
1272           some_available = true;
1273         }
1274     }
1275
1276   if (some_available)
1277     {
1278       remove_unused_temp_slot_addresses ();
1279       combine_temp_slots ();
1280     }
1281 }
1282
1283 /* Push deeper into the nesting level for stack temporaries.  */
1284
1285 void
1286 push_temp_slots (void)
1287 {
1288   temp_slot_level++;
1289 }
1290
1291 /* Pop a temporary nesting level.  All slots in use in the current level
1292    are freed.  */
1293
1294 void
1295 pop_temp_slots (void)
1296 {
1297   struct temp_slot *p, *next;
1298   bool some_available = false;
1299
1300   for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1301     {
1302       next = p->next;
1303       make_slot_available (p);
1304       some_available = true;
1305     }
1306
1307   if (some_available)
1308     {
1309       remove_unused_temp_slot_addresses ();
1310       combine_temp_slots ();
1311     }
1312
1313   temp_slot_level--;
1314 }
1315
1316 /* Initialize temporary slots.  */
1317
1318 void
1319 init_temp_slots (void)
1320 {
1321   /* We have not allocated any temporaries yet.  */
1322   avail_temp_slots = 0;
1323   used_temp_slots = 0;
1324   temp_slot_level = 0;
1325
1326   /* Set up the table to map addresses to temp slots.  */
1327   if (! temp_slot_address_table)
1328     temp_slot_address_table = htab_create_ggc (32,
1329                                                temp_slot_address_hash,
1330                                                temp_slot_address_eq,
1331                                                NULL);
1332   else
1333     htab_empty (temp_slot_address_table);
1334 }
1335 \f
1336 /* These routines are responsible for converting virtual register references
1337    to the actual hard register references once RTL generation is complete.
1338
1339    The following four variables are used for communication between the
1340    routines.  They contain the offsets of the virtual registers from their
1341    respective hard registers.  */
1342
1343 static int in_arg_offset;
1344 static int var_offset;
1345 static int dynamic_offset;
1346 static int out_arg_offset;
1347 static int cfa_offset;
1348
1349 /* In most machines, the stack pointer register is equivalent to the bottom
1350    of the stack.  */
1351
1352 #ifndef STACK_POINTER_OFFSET
1353 #define STACK_POINTER_OFFSET    0
1354 #endif
1355
1356 /* If not defined, pick an appropriate default for the offset of dynamically
1357    allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1358    REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE.  */
1359
1360 #ifndef STACK_DYNAMIC_OFFSET
1361
1362 /* The bottom of the stack points to the actual arguments.  If
1363    REG_PARM_STACK_SPACE is defined, this includes the space for the register
1364    parameters.  However, if OUTGOING_REG_PARM_STACK space is not defined,
1365    stack space for register parameters is not pushed by the caller, but
1366    rather part of the fixed stack areas and hence not included in
1367    `crtl->outgoing_args_size'.  Nevertheless, we must allow
1368    for it when allocating stack dynamic objects.  */
1369
1370 #if defined(REG_PARM_STACK_SPACE)
1371 #define STACK_DYNAMIC_OFFSET(FNDECL)    \
1372 ((ACCUMULATE_OUTGOING_ARGS                                                    \
1373   ? (crtl->outgoing_args_size                                 \
1374      + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1375                                                : REG_PARM_STACK_SPACE (FNDECL))) \
1376   : 0) + (STACK_POINTER_OFFSET))
1377 #else
1378 #define STACK_DYNAMIC_OFFSET(FNDECL)    \
1379 ((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0)            \
1380  + (STACK_POINTER_OFFSET))
1381 #endif
1382 #endif
1383
1384 \f
1385 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1386    is a virtual register, return the equivalent hard register and set the
1387    offset indirectly through the pointer.  Otherwise, return 0.  */
1388
1389 static rtx
1390 instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
1391 {
1392   rtx new_rtx;
1393   HOST_WIDE_INT offset;
1394
1395   if (x == virtual_incoming_args_rtx)
1396     {
1397       if (stack_realign_drap)
1398         {
1399           /* Replace virtual_incoming_args_rtx with internal arg
1400              pointer if DRAP is used to realign stack.  */
1401           new_rtx = crtl->args.internal_arg_pointer;
1402           offset = 0;
1403         }
1404       else
1405         new_rtx = arg_pointer_rtx, offset = in_arg_offset;
1406     }
1407   else if (x == virtual_stack_vars_rtx)
1408     new_rtx = frame_pointer_rtx, offset = var_offset;
1409   else if (x == virtual_stack_dynamic_rtx)
1410     new_rtx = stack_pointer_rtx, offset = dynamic_offset;
1411   else if (x == virtual_outgoing_args_rtx)
1412     new_rtx = stack_pointer_rtx, offset = out_arg_offset;
1413   else if (x == virtual_cfa_rtx)
1414     {
1415 #ifdef FRAME_POINTER_CFA_OFFSET
1416       new_rtx = frame_pointer_rtx;
1417 #else
1418       new_rtx = arg_pointer_rtx;
1419 #endif
1420       offset = cfa_offset;
1421     }
1422   else if (x == virtual_preferred_stack_boundary_rtx)
1423     {
1424       new_rtx = GEN_INT (crtl->preferred_stack_boundary / BITS_PER_UNIT);
1425       offset = 0;
1426     }
1427   else
1428     return NULL_RTX;
1429
1430   *poffset = offset;
1431   return new_rtx;
1432 }
1433
1434 /* A subroutine of instantiate_virtual_regs, called via for_each_rtx.
1435    Instantiate any virtual registers present inside of *LOC.  The expression
1436    is simplified, as much as possible, but is not to be considered "valid"
1437    in any sense implied by the target.  If any change is made, set CHANGED
1438    to true.  */
1439
1440 static int
1441 instantiate_virtual_regs_in_rtx (rtx *loc, void *data)
1442 {
1443   HOST_WIDE_INT offset;
1444   bool *changed = (bool *) data;
1445   rtx x, new_rtx;
1446
1447   x = *loc;
1448   if (x == 0)
1449     return 0;
1450
1451   switch (GET_CODE (x))
1452     {
1453     case REG:
1454       new_rtx = instantiate_new_reg (x, &offset);
1455       if (new_rtx)
1456         {
1457           *loc = plus_constant (new_rtx, offset);
1458           if (changed)
1459             *changed = true;
1460         }
1461       return -1;
1462
1463     case PLUS:
1464       new_rtx = instantiate_new_reg (XEXP (x, 0), &offset);
1465       if (new_rtx)
1466         {
1467           new_rtx = plus_constant (new_rtx, offset);
1468           *loc = simplify_gen_binary (PLUS, GET_MODE (x), new_rtx, XEXP (x, 1));
1469           if (changed)
1470             *changed = true;
1471           return -1;
1472         }
1473
1474       /* FIXME -- from old code */
1475           /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1476              we can commute the PLUS and SUBREG because pointers into the
1477              frame are well-behaved.  */
1478       break;
1479
1480     default:
1481       break;
1482     }
1483
1484   return 0;
1485 }
1486
1487 /* A subroutine of instantiate_virtual_regs_in_insn.  Return true if X
1488    matches the predicate for insn CODE operand OPERAND.  */
1489
1490 static int
1491 safe_insn_predicate (int code, int operand, rtx x)
1492 {
1493   return code < 0 || insn_operand_matches ((enum insn_code) code, operand, x);
1494 }
1495
1496 /* A subroutine of instantiate_virtual_regs.  Instantiate any virtual
1497    registers present inside of insn.  The result will be a valid insn.  */
1498
1499 static void
1500 instantiate_virtual_regs_in_insn (rtx insn)
1501 {
1502   HOST_WIDE_INT offset;
1503   int insn_code, i;
1504   bool any_change = false;
1505   rtx set, new_rtx, x, seq;
1506
1507   /* There are some special cases to be handled first.  */
1508   set = single_set (insn);
1509   if (set)
1510     {
1511       /* We're allowed to assign to a virtual register.  This is interpreted
1512          to mean that the underlying register gets assigned the inverse
1513          transformation.  This is used, for example, in the handling of
1514          non-local gotos.  */
1515       new_rtx = instantiate_new_reg (SET_DEST (set), &offset);
1516       if (new_rtx)
1517         {
1518           start_sequence ();
1519
1520           for_each_rtx (&SET_SRC (set), instantiate_virtual_regs_in_rtx, NULL);
1521           x = simplify_gen_binary (PLUS, GET_MODE (new_rtx), SET_SRC (set),
1522                                    GEN_INT (-offset));
1523           x = force_operand (x, new_rtx);
1524           if (x != new_rtx)
1525             emit_move_insn (new_rtx, x);
1526
1527           seq = get_insns ();
1528           end_sequence ();
1529
1530           emit_insn_before (seq, insn);
1531           delete_insn (insn);
1532           return;
1533         }
1534
1535       /* Handle a straight copy from a virtual register by generating a
1536          new add insn.  The difference between this and falling through
1537          to the generic case is avoiding a new pseudo and eliminating a
1538          move insn in the initial rtl stream.  */
1539       new_rtx = instantiate_new_reg (SET_SRC (set), &offset);
1540       if (new_rtx && offset != 0
1541           && REG_P (SET_DEST (set))
1542           && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1543         {
1544           start_sequence ();
1545
1546           x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS,
1547                                    new_rtx, GEN_INT (offset), SET_DEST (set),
1548                                    1, OPTAB_LIB_WIDEN);
1549           if (x != SET_DEST (set))
1550             emit_move_insn (SET_DEST (set), x);
1551
1552           seq = get_insns ();
1553           end_sequence ();
1554
1555           emit_insn_before (seq, insn);
1556           delete_insn (insn);
1557           return;
1558         }
1559
1560       extract_insn (insn);
1561       insn_code = INSN_CODE (insn);
1562
1563       /* Handle a plus involving a virtual register by determining if the
1564          operands remain valid if they're modified in place.  */
1565       if (GET_CODE (SET_SRC (set)) == PLUS
1566           && recog_data.n_operands >= 3
1567           && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1568           && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1569           && CONST_INT_P (recog_data.operand[2])
1570           && (new_rtx = instantiate_new_reg (recog_data.operand[1], &offset)))
1571         {
1572           offset += INTVAL (recog_data.operand[2]);
1573
1574           /* If the sum is zero, then replace with a plain move.  */
1575           if (offset == 0
1576               && REG_P (SET_DEST (set))
1577               && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1578             {
1579               start_sequence ();
1580               emit_move_insn (SET_DEST (set), new_rtx);
1581               seq = get_insns ();
1582               end_sequence ();
1583
1584               emit_insn_before (seq, insn);
1585               delete_insn (insn);
1586               return;
1587             }
1588
1589           x = gen_int_mode (offset, recog_data.operand_mode[2]);
1590
1591           /* Using validate_change and apply_change_group here leaves
1592              recog_data in an invalid state.  Since we know exactly what
1593              we want to check, do those two by hand.  */
1594           if (safe_insn_predicate (insn_code, 1, new_rtx)
1595               && safe_insn_predicate (insn_code, 2, x))
1596             {
1597               *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
1598               *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1599               any_change = true;
1600
1601               /* Fall through into the regular operand fixup loop in
1602                  order to take care of operands other than 1 and 2.  */
1603             }
1604         }
1605     }
1606   else
1607     {
1608       extract_insn (insn);
1609       insn_code = INSN_CODE (insn);
1610     }
1611
1612   /* In the general case, we expect virtual registers to appear only in
1613      operands, and then only as either bare registers or inside memories.  */
1614   for (i = 0; i < recog_data.n_operands; ++i)
1615     {
1616       x = recog_data.operand[i];
1617       switch (GET_CODE (x))
1618         {
1619         case MEM:
1620           {
1621             rtx addr = XEXP (x, 0);
1622             bool changed = false;
1623
1624             for_each_rtx (&addr, instantiate_virtual_regs_in_rtx, &changed);
1625             if (!changed)
1626               continue;
1627
1628             start_sequence ();
1629             x = replace_equiv_address (x, addr);
1630             /* It may happen that the address with the virtual reg
1631                was valid (e.g. based on the virtual stack reg, which might
1632                be acceptable to the predicates with all offsets), whereas
1633                the address now isn't anymore, for instance when the address
1634                is still offsetted, but the base reg isn't virtual-stack-reg
1635                anymore.  Below we would do a force_reg on the whole operand,
1636                but this insn might actually only accept memory.  Hence,
1637                before doing that last resort, try to reload the address into
1638                a register, so this operand stays a MEM.  */
1639             if (!safe_insn_predicate (insn_code, i, x))
1640               {
1641                 addr = force_reg (GET_MODE (addr), addr);
1642                 x = replace_equiv_address (x, addr);
1643               }
1644             seq = get_insns ();
1645             end_sequence ();
1646             if (seq)
1647               emit_insn_before (seq, insn);
1648           }
1649           break;
1650
1651         case REG:
1652           new_rtx = instantiate_new_reg (x, &offset);
1653           if (new_rtx == NULL)
1654             continue;
1655           if (offset == 0)
1656             x = new_rtx;
1657           else
1658             {
1659               start_sequence ();
1660
1661               /* Careful, special mode predicates may have stuff in
1662                  insn_data[insn_code].operand[i].mode that isn't useful
1663                  to us for computing a new value.  */
1664               /* ??? Recognize address_operand and/or "p" constraints
1665                  to see if (plus new offset) is a valid before we put
1666                  this through expand_simple_binop.  */
1667               x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
1668                                        GEN_INT (offset), NULL_RTX,
1669                                        1, OPTAB_LIB_WIDEN);
1670               seq = get_insns ();
1671               end_sequence ();
1672               emit_insn_before (seq, insn);
1673             }
1674           break;
1675
1676         case SUBREG:
1677           new_rtx = instantiate_new_reg (SUBREG_REG (x), &offset);
1678           if (new_rtx == NULL)
1679             continue;
1680           if (offset != 0)
1681             {
1682               start_sequence ();
1683               new_rtx = expand_simple_binop (GET_MODE (new_rtx), PLUS, new_rtx,
1684                                          GEN_INT (offset), NULL_RTX,
1685                                          1, OPTAB_LIB_WIDEN);
1686               seq = get_insns ();
1687               end_sequence ();
1688               emit_insn_before (seq, insn);
1689             }
1690           x = simplify_gen_subreg (recog_data.operand_mode[i], new_rtx,
1691                                    GET_MODE (new_rtx), SUBREG_BYTE (x));
1692           gcc_assert (x);
1693           break;
1694
1695         default:
1696           continue;
1697         }
1698
1699       /* At this point, X contains the new value for the operand.
1700          Validate the new value vs the insn predicate.  Note that
1701          asm insns will have insn_code -1 here.  */
1702       if (!safe_insn_predicate (insn_code, i, x))
1703         {
1704           start_sequence ();
1705           if (REG_P (x))
1706             {
1707               gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1708               x = copy_to_reg (x);
1709             }
1710           else
1711             x = force_reg (insn_data[insn_code].operand[i].mode, x);
1712           seq = get_insns ();
1713           end_sequence ();
1714           if (seq)
1715             emit_insn_before (seq, insn);
1716         }
1717
1718       *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1719       any_change = true;
1720     }
1721
1722   if (any_change)
1723     {
1724       /* Propagate operand changes into the duplicates.  */
1725       for (i = 0; i < recog_data.n_dups; ++i)
1726         *recog_data.dup_loc[i]
1727           = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1728
1729       /* Force re-recognition of the instruction for validation.  */
1730       INSN_CODE (insn) = -1;
1731     }
1732
1733   if (asm_noperands (PATTERN (insn)) >= 0)
1734     {
1735       if (!check_asm_operands (PATTERN (insn)))
1736         {
1737           error_for_asm (insn, "impossible constraint in %<asm%>");
1738           delete_insn (insn);
1739         }
1740     }
1741   else
1742     {
1743       if (recog_memoized (insn) < 0)
1744         fatal_insn_not_found (insn);
1745     }
1746 }
1747
1748 /* Subroutine of instantiate_decls.  Given RTL representing a decl,
1749    do any instantiation required.  */
1750
1751 void
1752 instantiate_decl_rtl (rtx x)
1753 {
1754   rtx addr;
1755
1756   if (x == 0)
1757     return;
1758
1759   /* If this is a CONCAT, recurse for the pieces.  */
1760   if (GET_CODE (x) == CONCAT)
1761     {
1762       instantiate_decl_rtl (XEXP (x, 0));
1763       instantiate_decl_rtl (XEXP (x, 1));
1764       return;
1765     }
1766
1767   /* If this is not a MEM, no need to do anything.  Similarly if the
1768      address is a constant or a register that is not a virtual register.  */
1769   if (!MEM_P (x))
1770     return;
1771
1772   addr = XEXP (x, 0);
1773   if (CONSTANT_P (addr)
1774       || (REG_P (addr)
1775           && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
1776               || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
1777     return;
1778
1779   for_each_rtx (&XEXP (x, 0), instantiate_virtual_regs_in_rtx, NULL);
1780 }
1781
1782 /* Helper for instantiate_decls called via walk_tree: Process all decls
1783    in the given DECL_VALUE_EXPR.  */
1784
1785 static tree
1786 instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1787 {
1788   tree t = *tp;
1789   if (! EXPR_P (t))
1790     {
1791       *walk_subtrees = 0;
1792       if (DECL_P (t))
1793         {
1794           if (DECL_RTL_SET_P (t))
1795             instantiate_decl_rtl (DECL_RTL (t));
1796           if (TREE_CODE (t) == PARM_DECL && DECL_NAMELESS (t)
1797               && DECL_INCOMING_RTL (t))
1798             instantiate_decl_rtl (DECL_INCOMING_RTL (t));
1799           if ((TREE_CODE (t) == VAR_DECL
1800                || TREE_CODE (t) == RESULT_DECL)
1801               && DECL_HAS_VALUE_EXPR_P (t))
1802             {
1803               tree v = DECL_VALUE_EXPR (t);
1804               walk_tree (&v, instantiate_expr, NULL, NULL);
1805             }
1806         }
1807     }
1808   return NULL;
1809 }
1810
1811 /* Subroutine of instantiate_decls: Process all decls in the given
1812    BLOCK node and all its subblocks.  */
1813
1814 static void
1815 instantiate_decls_1 (tree let)
1816 {
1817   tree t;
1818
1819   for (t = BLOCK_VARS (let); t; t = DECL_CHAIN (t))
1820     {
1821       if (DECL_RTL_SET_P (t))
1822         instantiate_decl_rtl (DECL_RTL (t));
1823       if (TREE_CODE (t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (t))
1824         {
1825           tree v = DECL_VALUE_EXPR (t);
1826           walk_tree (&v, instantiate_expr, NULL, NULL);
1827         }
1828     }
1829
1830   /* Process all subblocks.  */
1831   for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
1832     instantiate_decls_1 (t);
1833 }
1834
1835 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
1836    all virtual registers in their DECL_RTL's.  */
1837
1838 static void
1839 instantiate_decls (tree fndecl)
1840 {
1841   tree decl;
1842   unsigned ix;
1843
1844   /* Process all parameters of the function.  */
1845   for (decl = DECL_ARGUMENTS (fndecl); decl; decl = DECL_CHAIN (decl))
1846     {
1847       instantiate_decl_rtl (DECL_RTL (decl));
1848       instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
1849       if (DECL_HAS_VALUE_EXPR_P (decl))
1850         {
1851           tree v = DECL_VALUE_EXPR (decl);
1852           walk_tree (&v, instantiate_expr, NULL, NULL);
1853         }
1854     }
1855
1856   if ((decl = DECL_RESULT (fndecl))
1857       && TREE_CODE (decl) == RESULT_DECL)
1858     {
1859       if (DECL_RTL_SET_P (decl))
1860         instantiate_decl_rtl (DECL_RTL (decl));
1861       if (DECL_HAS_VALUE_EXPR_P (decl))
1862         {
1863           tree v = DECL_VALUE_EXPR (decl);
1864           walk_tree (&v, instantiate_expr, NULL, NULL);
1865         }
1866     }
1867
1868   /* Now process all variables defined in the function or its subblocks.  */
1869   instantiate_decls_1 (DECL_INITIAL (fndecl));
1870
1871   FOR_EACH_LOCAL_DECL (cfun, ix, decl)
1872     if (DECL_RTL_SET_P (decl))
1873       instantiate_decl_rtl (DECL_RTL (decl));
1874   VEC_free (tree, gc, cfun->local_decls);
1875 }
1876
1877 /* Pass through the INSNS of function FNDECL and convert virtual register
1878    references to hard register references.  */
1879
1880 static unsigned int
1881 instantiate_virtual_regs (void)
1882 {
1883   rtx insn;
1884
1885   /* Compute the offsets to use for this function.  */
1886   in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1887   var_offset = STARTING_FRAME_OFFSET;
1888   dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
1889   out_arg_offset = STACK_POINTER_OFFSET;
1890 #ifdef FRAME_POINTER_CFA_OFFSET
1891   cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1892 #else
1893   cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1894 #endif
1895
1896   /* Initialize recognition, indicating that volatile is OK.  */
1897   init_recog ();
1898
1899   /* Scan through all the insns, instantiating every virtual register still
1900      present.  */
1901   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1902     if (INSN_P (insn))
1903       {
1904         /* These patterns in the instruction stream can never be recognized.
1905            Fortunately, they shouldn't contain virtual registers either.  */
1906         if (GET_CODE (PATTERN (insn)) == USE
1907             || GET_CODE (PATTERN (insn)) == CLOBBER
1908             || GET_CODE (PATTERN (insn)) == ADDR_VEC
1909             || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
1910             || GET_CODE (PATTERN (insn)) == ASM_INPUT)
1911           continue;
1912         else if (DEBUG_INSN_P (insn))
1913           for_each_rtx (&INSN_VAR_LOCATION (insn),
1914                         instantiate_virtual_regs_in_rtx, NULL);
1915         else
1916           instantiate_virtual_regs_in_insn (insn);
1917
1918         if (INSN_DELETED_P (insn))
1919           continue;
1920
1921         for_each_rtx (&REG_NOTES (insn), instantiate_virtual_regs_in_rtx, NULL);
1922
1923         /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE.  */
1924         if (CALL_P (insn))
1925           for_each_rtx (&CALL_INSN_FUNCTION_USAGE (insn),
1926                         instantiate_virtual_regs_in_rtx, NULL);
1927       }
1928
1929   /* Instantiate the virtual registers in the DECLs for debugging purposes.  */
1930   instantiate_decls (current_function_decl);
1931
1932   targetm.instantiate_decls ();
1933
1934   /* Indicate that, from now on, assign_stack_local should use
1935      frame_pointer_rtx.  */
1936   virtuals_instantiated = 1;
1937
1938   return 0;
1939 }
1940
1941 struct rtl_opt_pass pass_instantiate_virtual_regs =
1942 {
1943  {
1944   RTL_PASS,
1945   "vregs",                              /* name */
1946   NULL,                                 /* gate */
1947   instantiate_virtual_regs,             /* execute */
1948   NULL,                                 /* sub */
1949   NULL,                                 /* next */
1950   0,                                    /* static_pass_number */
1951   TV_NONE,                              /* tv_id */
1952   0,                                    /* properties_required */
1953   0,                                    /* properties_provided */
1954   0,                                    /* properties_destroyed */
1955   0,                                    /* todo_flags_start */
1956   0                                     /* todo_flags_finish */
1957  }
1958 };
1959
1960 \f
1961 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
1962    This means a type for which function calls must pass an address to the
1963    function or get an address back from the function.
1964    EXP may be a type node or an expression (whose type is tested).  */
1965
1966 int
1967 aggregate_value_p (const_tree exp, const_tree fntype)
1968 {
1969   const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
1970   int i, regno, nregs;
1971   rtx reg;
1972
1973   if (fntype)
1974     switch (TREE_CODE (fntype))
1975       {
1976       case CALL_EXPR:
1977         {
1978           tree fndecl = get_callee_fndecl (fntype);
1979           fntype = (fndecl
1980                     ? TREE_TYPE (fndecl)
1981                     : TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype))));
1982         }
1983         break;
1984       case FUNCTION_DECL:
1985         fntype = TREE_TYPE (fntype);
1986         break;
1987       case FUNCTION_TYPE:
1988       case METHOD_TYPE:
1989         break;
1990       case IDENTIFIER_NODE:
1991         fntype = NULL_TREE;
1992         break;
1993       default:
1994         /* We don't expect other tree types here.  */
1995         gcc_unreachable ();
1996       }
1997
1998   if (VOID_TYPE_P (type))
1999     return 0;
2000
2001   /* If a record should be passed the same as its first (and only) member
2002      don't pass it as an aggregate.  */
2003   if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2004     return aggregate_value_p (first_field (type), fntype);
2005
2006   /* If the front end has decided that this needs to be passed by
2007      reference, do so.  */
2008   if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
2009       && DECL_BY_REFERENCE (exp))
2010     return 1;
2011
2012   /* Function types that are TREE_ADDRESSABLE force return in memory.  */
2013   if (fntype && TREE_ADDRESSABLE (fntype))
2014     return 1;
2015
2016   /* Types that are TREE_ADDRESSABLE must be constructed in memory,
2017      and thus can't be returned in registers.  */
2018   if (TREE_ADDRESSABLE (type))
2019     return 1;
2020
2021   if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
2022     return 1;
2023
2024   if (targetm.calls.return_in_memory (type, fntype))
2025     return 1;
2026
2027   /* Make sure we have suitable call-clobbered regs to return
2028      the value in; if not, we must return it in memory.  */
2029   reg = hard_function_value (type, 0, fntype, 0);
2030
2031   /* If we have something other than a REG (e.g. a PARALLEL), then assume
2032      it is OK.  */
2033   if (!REG_P (reg))
2034     return 0;
2035
2036   regno = REGNO (reg);
2037   nregs = hard_regno_nregs[regno][TYPE_MODE (type)];
2038   for (i = 0; i < nregs; i++)
2039     if (! call_used_regs[regno + i])
2040       return 1;
2041
2042   return 0;
2043 }
2044 \f
2045 /* Return true if we should assign DECL a pseudo register; false if it
2046    should live on the local stack.  */
2047
2048 bool
2049 use_register_for_decl (const_tree decl)
2050 {
2051   if (!targetm.calls.allocate_stack_slots_for_args())
2052     return true;
2053
2054   /* Honor volatile.  */
2055   if (TREE_SIDE_EFFECTS (decl))
2056     return false;
2057
2058   /* Honor addressability.  */
2059   if (TREE_ADDRESSABLE (decl))
2060     return false;
2061
2062   /* Only register-like things go in registers.  */
2063   if (DECL_MODE (decl) == BLKmode)
2064     return false;
2065
2066   /* If -ffloat-store specified, don't put explicit float variables
2067      into registers.  */
2068   /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2069      propagates values across these stores, and it probably shouldn't.  */
2070   if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2071     return false;
2072
2073   /* If we're not interested in tracking debugging information for
2074      this decl, then we can certainly put it in a register.  */
2075   if (DECL_IGNORED_P (decl))
2076     return true;
2077
2078   if (optimize)
2079     return true;
2080
2081   if (!DECL_REGISTER (decl))
2082     return false;
2083
2084   switch (TREE_CODE (TREE_TYPE (decl)))
2085     {
2086     case RECORD_TYPE:
2087     case UNION_TYPE:
2088     case QUAL_UNION_TYPE:
2089       /* When not optimizing, disregard register keyword for variables with
2090          types containing methods, otherwise the methods won't be callable
2091          from the debugger.  */
2092       if (TYPE_METHODS (TREE_TYPE (decl)))
2093         return false;
2094       break;
2095     default:
2096       break;
2097     }
2098
2099   return true;
2100 }
2101
2102 /* Return true if TYPE should be passed by invisible reference.  */
2103
2104 bool
2105 pass_by_reference (CUMULATIVE_ARGS *ca, enum machine_mode mode,
2106                    tree type, bool named_arg)
2107 {
2108   if (type)
2109     {
2110       /* If this type contains non-trivial constructors, then it is
2111          forbidden for the middle-end to create any new copies.  */
2112       if (TREE_ADDRESSABLE (type))
2113         return true;
2114
2115       /* GCC post 3.4 passes *all* variable sized types by reference.  */
2116       if (!TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2117         return true;
2118
2119       /* If a record type should be passed the same as its first (and only)
2120          member, use the type and mode of that member.  */
2121       if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2122         {
2123           type = TREE_TYPE (first_field (type));
2124           mode = TYPE_MODE (type);
2125         }
2126     }
2127
2128   return targetm.calls.pass_by_reference (pack_cumulative_args (ca), mode,
2129                                           type, named_arg);
2130 }
2131
2132 /* Return true if TYPE, which is passed by reference, should be callee
2133    copied instead of caller copied.  */
2134
2135 bool
2136 reference_callee_copied (CUMULATIVE_ARGS *ca, enum machine_mode mode,
2137                          tree type, bool named_arg)
2138 {
2139   if (type && TREE_ADDRESSABLE (type))
2140     return false;
2141   return targetm.calls.callee_copies (pack_cumulative_args (ca), mode, type,
2142                                       named_arg);
2143 }
2144
2145 /* Structures to communicate between the subroutines of assign_parms.
2146    The first holds data persistent across all parameters, the second
2147    is cleared out for each parameter.  */
2148
2149 struct assign_parm_data_all
2150 {
2151   /* When INIT_CUMULATIVE_ARGS gets revamped, allocating CUMULATIVE_ARGS
2152      should become a job of the target or otherwise encapsulated.  */
2153   CUMULATIVE_ARGS args_so_far_v;
2154   cumulative_args_t args_so_far;
2155   struct args_size stack_args_size;
2156   tree function_result_decl;
2157   tree orig_fnargs;
2158   rtx first_conversion_insn;
2159   rtx last_conversion_insn;
2160   HOST_WIDE_INT pretend_args_size;
2161   HOST_WIDE_INT extra_pretend_bytes;
2162   int reg_parm_stack_space;
2163 };
2164
2165 struct assign_parm_data_one
2166 {
2167   tree nominal_type;
2168   tree passed_type;
2169   rtx entry_parm;
2170   rtx stack_parm;
2171   enum machine_mode nominal_mode;
2172   enum machine_mode passed_mode;
2173   enum machine_mode promoted_mode;
2174   struct locate_and_pad_arg_data locate;
2175   int partial;
2176   BOOL_BITFIELD named_arg : 1;
2177   BOOL_BITFIELD passed_pointer : 1;
2178   BOOL_BITFIELD on_stack : 1;
2179   BOOL_BITFIELD loaded_in_reg : 1;
2180 };
2181
2182 /* A subroutine of assign_parms.  Initialize ALL.  */
2183
2184 static void
2185 assign_parms_initialize_all (struct assign_parm_data_all *all)
2186 {
2187   tree fntype ATTRIBUTE_UNUSED;
2188
2189   memset (all, 0, sizeof (*all));
2190
2191   fntype = TREE_TYPE (current_function_decl);
2192
2193 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
2194   INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far_v, fntype, NULL_RTX);
2195 #else
2196   INIT_CUMULATIVE_ARGS (all->args_so_far_v, fntype, NULL_RTX,
2197                         current_function_decl, -1);
2198 #endif
2199   all->args_so_far = pack_cumulative_args (&all->args_so_far_v);
2200
2201 #ifdef REG_PARM_STACK_SPACE
2202   all->reg_parm_stack_space = REG_PARM_STACK_SPACE (current_function_decl);
2203 #endif
2204 }
2205
2206 /* If ARGS contains entries with complex types, split the entry into two
2207    entries of the component type.  Return a new list of substitutions are
2208    needed, else the old list.  */
2209
2210 static void
2211 split_complex_args (VEC(tree, heap) **args)
2212 {
2213   unsigned i;
2214   tree p;
2215
2216   FOR_EACH_VEC_ELT (tree, *args, i, p)
2217     {
2218       tree type = TREE_TYPE (p);
2219       if (TREE_CODE (type) == COMPLEX_TYPE
2220           && targetm.calls.split_complex_arg (type))
2221         {
2222           tree decl;
2223           tree subtype = TREE_TYPE (type);
2224           bool addressable = TREE_ADDRESSABLE (p);
2225
2226           /* Rewrite the PARM_DECL's type with its component.  */
2227           p = copy_node (p);
2228           TREE_TYPE (p) = subtype;
2229           DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2230           DECL_MODE (p) = VOIDmode;
2231           DECL_SIZE (p) = NULL;
2232           DECL_SIZE_UNIT (p) = NULL;
2233           /* If this arg must go in memory, put it in a pseudo here.
2234              We can't allow it to go in memory as per normal parms,
2235              because the usual place might not have the imag part
2236              adjacent to the real part.  */
2237           DECL_ARTIFICIAL (p) = addressable;
2238           DECL_IGNORED_P (p) = addressable;
2239           TREE_ADDRESSABLE (p) = 0;
2240           layout_decl (p, 0);
2241           VEC_replace (tree, *args, i, p);
2242
2243           /* Build a second synthetic decl.  */
2244           decl = build_decl (EXPR_LOCATION (p),
2245                              PARM_DECL, NULL_TREE, subtype);
2246           DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2247           DECL_ARTIFICIAL (decl) = addressable;
2248           DECL_IGNORED_P (decl) = addressable;
2249           layout_decl (decl, 0);
2250           VEC_safe_insert (tree, heap, *args, ++i, decl);
2251         }
2252     }
2253 }
2254
2255 /* A subroutine of assign_parms.  Adjust the parameter list to incorporate
2256    the hidden struct return argument, and (abi willing) complex args.
2257    Return the new parameter list.  */
2258
2259 static VEC(tree, heap) *
2260 assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2261 {
2262   tree fndecl = current_function_decl;
2263   tree fntype = TREE_TYPE (fndecl);
2264   VEC(tree, heap) *fnargs = NULL;
2265   tree arg;
2266
2267   for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
2268     VEC_safe_push (tree, heap, fnargs, arg);
2269
2270   all->orig_fnargs = DECL_ARGUMENTS (fndecl);
2271
2272   /* If struct value address is treated as the first argument, make it so.  */
2273   if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
2274       && ! cfun->returns_pcc_struct
2275       && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2276     {
2277       tree type = build_pointer_type (TREE_TYPE (fntype));
2278       tree decl;
2279
2280       decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2281                          PARM_DECL, get_identifier (".result_ptr"), type);
2282       DECL_ARG_TYPE (decl) = type;
2283       DECL_ARTIFICIAL (decl) = 1;
2284       DECL_NAMELESS (decl) = 1;
2285       TREE_CONSTANT (decl) = 1;
2286
2287       DECL_CHAIN (decl) = all->orig_fnargs;
2288       all->orig_fnargs = decl;
2289       VEC_safe_insert (tree, heap, fnargs, 0, decl);
2290
2291       all->function_result_decl = decl;
2292     }
2293
2294   /* If the target wants to split complex arguments into scalars, do so.  */
2295   if (targetm.calls.split_complex_arg)
2296     split_complex_args (&fnargs);
2297
2298   return fnargs;
2299 }
2300
2301 /* A subroutine of assign_parms.  Examine PARM and pull out type and mode
2302    data for the parameter.  Incorporate ABI specifics such as pass-by-
2303    reference and type promotion.  */
2304
2305 static void
2306 assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2307                              struct assign_parm_data_one *data)
2308 {
2309   tree nominal_type, passed_type;
2310   enum machine_mode nominal_mode, passed_mode, promoted_mode;
2311   int unsignedp;
2312
2313   memset (data, 0, sizeof (*data));
2314
2315   /* NAMED_ARG is a misnomer.  We really mean 'non-variadic'. */
2316   if (!cfun->stdarg)
2317     data->named_arg = 1;  /* No variadic parms.  */
2318   else if (DECL_CHAIN (parm))
2319     data->named_arg = 1;  /* Not the last non-variadic parm. */
2320   else if (targetm.calls.strict_argument_naming (all->args_so_far))
2321     data->named_arg = 1;  /* Only variadic ones are unnamed.  */
2322   else
2323     data->named_arg = 0;  /* Treat as variadic.  */
2324
2325   nominal_type = TREE_TYPE (parm);
2326   passed_type = DECL_ARG_TYPE (parm);
2327
2328   /* Look out for errors propagating this far.  Also, if the parameter's
2329      type is void then its value doesn't matter.  */
2330   if (TREE_TYPE (parm) == error_mark_node
2331       /* This can happen after weird syntax errors
2332          or if an enum type is defined among the parms.  */
2333       || TREE_CODE (parm) != PARM_DECL
2334       || passed_type == NULL
2335       || VOID_TYPE_P (nominal_type))
2336     {
2337       nominal_type = passed_type = void_type_node;
2338       nominal_mode = passed_mode = promoted_mode = VOIDmode;
2339       goto egress;
2340     }
2341
2342   /* Find mode of arg as it is passed, and mode of arg as it should be
2343      during execution of this function.  */
2344   passed_mode = TYPE_MODE (passed_type);
2345   nominal_mode = TYPE_MODE (nominal_type);
2346
2347   /* If the parm is to be passed as a transparent union or record, use the
2348      type of the first field for the tests below.  We have already verified
2349      that the modes are the same.  */
2350   if ((TREE_CODE (passed_type) == UNION_TYPE
2351        || TREE_CODE (passed_type) == RECORD_TYPE)
2352       && TYPE_TRANSPARENT_AGGR (passed_type))
2353     passed_type = TREE_TYPE (first_field (passed_type));
2354
2355   /* See if this arg was passed by invisible reference.  */
2356   if (pass_by_reference (&all->args_so_far_v, passed_mode,
2357                          passed_type, data->named_arg))
2358     {
2359       passed_type = nominal_type = build_pointer_type (passed_type);
2360       data->passed_pointer = true;
2361       passed_mode = nominal_mode = Pmode;
2362     }
2363
2364   /* Find mode as it is passed by the ABI.  */
2365   unsignedp = TYPE_UNSIGNED (passed_type);
2366   promoted_mode = promote_function_mode (passed_type, passed_mode, &unsignedp,
2367                                          TREE_TYPE (current_function_decl), 0);
2368
2369  egress:
2370   data->nominal_type = nominal_type;
2371   data->passed_type = passed_type;
2372   data->nominal_mode = nominal_mode;
2373   data->passed_mode = passed_mode;
2374   data->promoted_mode = promoted_mode;
2375 }
2376
2377 /* A subroutine of assign_parms.  Invoke setup_incoming_varargs.  */
2378
2379 static void
2380 assign_parms_setup_varargs (struct assign_parm_data_all *all,
2381                             struct assign_parm_data_one *data, bool no_rtl)
2382 {
2383   int varargs_pretend_bytes = 0;
2384
2385   targetm.calls.setup_incoming_varargs (all->args_so_far,
2386                                         data->promoted_mode,
2387                                         data->passed_type,
2388                                         &varargs_pretend_bytes, no_rtl);
2389
2390   /* If the back-end has requested extra stack space, record how much is
2391      needed.  Do not change pretend_args_size otherwise since it may be
2392      nonzero from an earlier partial argument.  */
2393   if (varargs_pretend_bytes > 0)
2394     all->pretend_args_size = varargs_pretend_bytes;
2395 }
2396
2397 /* A subroutine of assign_parms.  Set DATA->ENTRY_PARM corresponding to
2398    the incoming location of the current parameter.  */
2399
2400 static void
2401 assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2402                             struct assign_parm_data_one *data)
2403 {
2404   HOST_WIDE_INT pretend_bytes = 0;
2405   rtx entry_parm;
2406   bool in_regs;
2407
2408   if (data->promoted_mode == VOIDmode)
2409     {
2410       data->entry_parm = data->stack_parm = const0_rtx;
2411       return;
2412     }
2413
2414   entry_parm = targetm.calls.function_incoming_arg (all->args_so_far,
2415                                                     data->promoted_mode,
2416                                                     data->passed_type,
2417                                                     data->named_arg);
2418
2419   if (entry_parm == 0)
2420     data->promoted_mode = data->passed_mode;
2421
2422   /* Determine parm's home in the stack, in case it arrives in the stack
2423      or we should pretend it did.  Compute the stack position and rtx where
2424      the argument arrives and its size.
2425
2426      There is one complexity here:  If this was a parameter that would
2427      have been passed in registers, but wasn't only because it is
2428      __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2429      it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2430      In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2431      as it was the previous time.  */
2432   in_regs = entry_parm != 0;
2433 #ifdef STACK_PARMS_IN_REG_PARM_AREA
2434   in_regs = true;
2435 #endif
2436   if (!in_regs && !data->named_arg)
2437     {
2438       if (targetm.calls.pretend_outgoing_varargs_named (all->args_so_far))
2439         {
2440           rtx tem;
2441           tem = targetm.calls.function_incoming_arg (all->args_so_far,
2442                                                      data->promoted_mode,
2443                                                      data->passed_type, true);
2444           in_regs = tem != NULL;
2445         }
2446     }
2447
2448   /* If this parameter was passed both in registers and in the stack, use
2449      the copy on the stack.  */
2450   if (targetm.calls.must_pass_in_stack (data->promoted_mode,
2451                                         data->passed_type))
2452     entry_parm = 0;
2453
2454   if (entry_parm)
2455     {
2456       int partial;
2457
2458       partial = targetm.calls.arg_partial_bytes (all->args_so_far,
2459                                                  data->promoted_mode,
2460                                                  data->passed_type,
2461                                                  data->named_arg);
2462       data->partial = partial;
2463
2464       /* The caller might already have allocated stack space for the
2465          register parameters.  */
2466       if (partial != 0 && all->reg_parm_stack_space == 0)
2467         {
2468           /* Part of this argument is passed in registers and part
2469              is passed on the stack.  Ask the prologue code to extend
2470              the stack part so that we can recreate the full value.
2471
2472              PRETEND_BYTES is the size of the registers we need to store.
2473              CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2474              stack space that the prologue should allocate.
2475
2476              Internally, gcc assumes that the argument pointer is aligned
2477              to STACK_BOUNDARY bits.  This is used both for alignment
2478              optimizations (see init_emit) and to locate arguments that are
2479              aligned to more than PARM_BOUNDARY bits.  We must preserve this
2480              invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2481              a stack boundary.  */
2482
2483           /* We assume at most one partial arg, and it must be the first
2484              argument on the stack.  */
2485           gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2486
2487           pretend_bytes = partial;
2488           all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2489
2490           /* We want to align relative to the actual stack pointer, so
2491              don't include this in the stack size until later.  */
2492           all->extra_pretend_bytes = all->pretend_args_size;
2493         }
2494     }
2495
2496   locate_and_pad_parm (data->promoted_mode, data->passed_type, in_regs,
2497                        entry_parm ? data->partial : 0, current_function_decl,
2498                        &all->stack_args_size, &data->locate);
2499
2500   /* Update parm_stack_boundary if this parameter is passed in the
2501      stack.  */
2502   if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2503     crtl->parm_stack_boundary = data->locate.boundary;
2504
2505   /* Adjust offsets to include the pretend args.  */
2506   pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2507   data->locate.slot_offset.constant += pretend_bytes;
2508   data->locate.offset.constant += pretend_bytes;
2509
2510   data->entry_parm = entry_parm;
2511 }
2512
2513 /* A subroutine of assign_parms.  If there is actually space on the stack
2514    for this parm, count it in stack_args_size and return true.  */
2515
2516 static bool
2517 assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2518                            struct assign_parm_data_one *data)
2519 {
2520   /* Trivially true if we've no incoming register.  */
2521   if (data->entry_parm == NULL)
2522     ;
2523   /* Also true if we're partially in registers and partially not,
2524      since we've arranged to drop the entire argument on the stack.  */
2525   else if (data->partial != 0)
2526     ;
2527   /* Also true if the target says that it's passed in both registers
2528      and on the stack.  */
2529   else if (GET_CODE (data->entry_parm) == PARALLEL
2530            && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2531     ;
2532   /* Also true if the target says that there's stack allocated for
2533      all register parameters.  */
2534   else if (all->reg_parm_stack_space > 0)
2535     ;
2536   /* Otherwise, no, this parameter has no ABI defined stack slot.  */
2537   else
2538     return false;
2539
2540   all->stack_args_size.constant += data->locate.size.constant;
2541   if (data->locate.size.var)
2542     ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2543
2544   return true;
2545 }
2546
2547 /* A subroutine of assign_parms.  Given that this parameter is allocated
2548    stack space by the ABI, find it.  */
2549
2550 static void
2551 assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2552 {
2553   rtx offset_rtx, stack_parm;
2554   unsigned int align, boundary;
2555
2556   /* If we're passing this arg using a reg, make its stack home the
2557      aligned stack slot.  */
2558   if (data->entry_parm)
2559     offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2560   else
2561     offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2562
2563   stack_parm = crtl->args.internal_arg_pointer;
2564   if (offset_rtx != const0_rtx)
2565     stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2566   stack_parm = gen_rtx_MEM (data->promoted_mode, stack_parm);
2567
2568   if (!data->passed_pointer)
2569     {
2570       set_mem_attributes (stack_parm, parm, 1);
2571       /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2572          while promoted mode's size is needed.  */
2573       if (data->promoted_mode != BLKmode
2574           && data->promoted_mode != DECL_MODE (parm))
2575         {
2576           set_mem_size (stack_parm, GET_MODE_SIZE (data->promoted_mode));
2577           if (MEM_EXPR (stack_parm) && MEM_OFFSET_KNOWN_P (stack_parm))
2578             {
2579               int offset = subreg_lowpart_offset (DECL_MODE (parm),
2580                                                   data->promoted_mode);
2581               if (offset)
2582                 set_mem_offset (stack_parm, MEM_OFFSET (stack_parm) - offset);
2583             }
2584         }
2585     }
2586
2587   boundary = data->locate.boundary;
2588   align = BITS_PER_UNIT;
2589
2590   /* If we're padding upward, we know that the alignment of the slot
2591      is TARGET_FUNCTION_ARG_BOUNDARY.  If we're using slot_offset, we're
2592      intentionally forcing upward padding.  Otherwise we have to come
2593      up with a guess at the alignment based on OFFSET_RTX.  */
2594   if (data->locate.where_pad != downward || data->entry_parm)
2595     align = boundary;
2596   else if (CONST_INT_P (offset_rtx))
2597     {
2598       align = INTVAL (offset_rtx) * BITS_PER_UNIT | boundary;
2599       align = align & -align;
2600     }
2601   set_mem_align (stack_parm, align);
2602
2603   if (data->entry_parm)
2604     set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2605
2606   data->stack_parm = stack_parm;
2607 }
2608
2609 /* A subroutine of assign_parms.  Adjust DATA->ENTRY_RTL such that it's
2610    always valid and contiguous.  */
2611
2612 static void
2613 assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2614 {
2615   rtx entry_parm = data->entry_parm;
2616   rtx stack_parm = data->stack_parm;
2617
2618   /* If this parm was passed part in regs and part in memory, pretend it
2619      arrived entirely in memory by pushing the register-part onto the stack.
2620      In the special case of a DImode or DFmode that is split, we could put
2621      it together in a pseudoreg directly, but for now that's not worth
2622      bothering with.  */
2623   if (data->partial != 0)
2624     {
2625       /* Handle calls that pass values in multiple non-contiguous
2626          locations.  The Irix 6 ABI has examples of this.  */
2627       if (GET_CODE (entry_parm) == PARALLEL)
2628         emit_group_store (validize_mem (stack_parm), entry_parm,
2629                           data->passed_type,
2630                           int_size_in_bytes (data->passed_type));
2631       else
2632         {
2633           gcc_assert (data->partial % UNITS_PER_WORD == 0);
2634           move_block_from_reg (REGNO (entry_parm), validize_mem (stack_parm),
2635                                data->partial / UNITS_PER_WORD);
2636         }
2637
2638       entry_parm = stack_parm;
2639     }
2640
2641   /* If we didn't decide this parm came in a register, by default it came
2642      on the stack.  */
2643   else if (entry_parm == NULL)
2644     entry_parm = stack_parm;
2645
2646   /* When an argument is passed in multiple locations, we can't make use
2647      of this information, but we can save some copying if the whole argument
2648      is passed in a single register.  */
2649   else if (GET_CODE (entry_parm) == PARALLEL
2650            && data->nominal_mode != BLKmode
2651            && data->passed_mode != BLKmode)
2652     {
2653       size_t i, len = XVECLEN (entry_parm, 0);
2654
2655       for (i = 0; i < len; i++)
2656         if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2657             && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2658             && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2659                 == data->passed_mode)
2660             && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2661           {
2662             entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2663             break;
2664           }
2665     }
2666
2667   data->entry_parm = entry_parm;
2668 }
2669
2670 /* A subroutine of assign_parms.  Reconstitute any values which were
2671    passed in multiple registers and would fit in a single register.  */
2672
2673 static void
2674 assign_parm_remove_parallels (struct assign_parm_data_one *data)
2675 {
2676   rtx entry_parm = data->entry_parm;
2677
2678   /* Convert the PARALLEL to a REG of the same mode as the parallel.
2679      This can be done with register operations rather than on the
2680      stack, even if we will store the reconstituted parameter on the
2681      stack later.  */
2682   if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
2683     {
2684       rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
2685       emit_group_store (parmreg, entry_parm, data->passed_type,
2686                         GET_MODE_SIZE (GET_MODE (entry_parm)));
2687       entry_parm = parmreg;
2688     }
2689
2690   data->entry_parm = entry_parm;
2691 }
2692
2693 /* A subroutine of assign_parms.  Adjust DATA->STACK_RTL such that it's
2694    always valid and properly aligned.  */
2695
2696 static void
2697 assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2698 {
2699   rtx stack_parm = data->stack_parm;
2700
2701   /* If we can't trust the parm stack slot to be aligned enough for its
2702      ultimate type, don't use that slot after entry.  We'll make another
2703      stack slot, if we need one.  */
2704   if (stack_parm
2705       && ((STRICT_ALIGNMENT
2706            && GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm))
2707           || (data->nominal_type
2708               && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2709               && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2710     stack_parm = NULL;
2711
2712   /* If parm was passed in memory, and we need to convert it on entry,
2713      don't store it back in that same slot.  */
2714   else if (data->entry_parm == stack_parm
2715            && data->nominal_mode != BLKmode
2716            && data->nominal_mode != data->passed_mode)
2717     stack_parm = NULL;
2718
2719   /* If stack protection is in effect for this function, don't leave any
2720      pointers in their passed stack slots.  */
2721   else if (crtl->stack_protect_guard
2722            && (flag_stack_protect == 2
2723                || data->passed_pointer
2724                || POINTER_TYPE_P (data->nominal_type)))
2725     stack_parm = NULL;
2726
2727   data->stack_parm = stack_parm;
2728 }
2729
2730 /* A subroutine of assign_parms.  Return true if the current parameter
2731    should be stored as a BLKmode in the current frame.  */
2732
2733 static bool
2734 assign_parm_setup_block_p (struct assign_parm_data_one *data)
2735 {
2736   if (data->nominal_mode == BLKmode)
2737     return true;
2738   if (GET_MODE (data->entry_parm) == BLKmode)
2739     return true;
2740
2741 #ifdef BLOCK_REG_PADDING
2742   /* Only assign_parm_setup_block knows how to deal with register arguments
2743      that are padded at the least significant end.  */
2744   if (REG_P (data->entry_parm)
2745       && GET_MODE_SIZE (data->promoted_mode) < UNITS_PER_WORD
2746       && (BLOCK_REG_PADDING (data->passed_mode, data->passed_type, 1)
2747           == (BYTES_BIG_ENDIAN ? upward : downward)))
2748     return true;
2749 #endif
2750
2751   return false;
2752 }
2753
2754 /* A subroutine of assign_parms.  Arrange for the parameter to be
2755    present and valid in DATA->STACK_RTL.  */
2756
2757 static void
2758 assign_parm_setup_block (struct assign_parm_data_all *all,
2759                          tree parm, struct assign_parm_data_one *data)
2760 {
2761   rtx entry_parm = data->entry_parm;
2762   rtx stack_parm = data->stack_parm;
2763   HOST_WIDE_INT size;
2764   HOST_WIDE_INT size_stored;
2765
2766   if (GET_CODE (entry_parm) == PARALLEL)
2767     entry_parm = emit_group_move_into_temps (entry_parm);
2768
2769   size = int_size_in_bytes (data->passed_type);
2770   size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2771   if (stack_parm == 0)
2772     {
2773       DECL_ALIGN (parm) = MAX (DECL_ALIGN (parm), BITS_PER_WORD);
2774       stack_parm = assign_stack_local (BLKmode, size_stored,
2775                                        DECL_ALIGN (parm));
2776       if (GET_MODE_SIZE (GET_MODE (entry_parm)) == size)
2777         PUT_MODE (stack_parm, GET_MODE (entry_parm));
2778       set_mem_attributes (stack_parm, parm, 1);
2779     }
2780
2781   /* If a BLKmode arrives in registers, copy it to a stack slot.  Handle
2782      calls that pass values in multiple non-contiguous locations.  */
2783   if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2784     {
2785       rtx mem;
2786
2787       /* Note that we will be storing an integral number of words.
2788          So we have to be careful to ensure that we allocate an
2789          integral number of words.  We do this above when we call
2790          assign_stack_local if space was not allocated in the argument
2791          list.  If it was, this will not work if PARM_BOUNDARY is not
2792          a multiple of BITS_PER_WORD.  It isn't clear how to fix this
2793          if it becomes a problem.  Exception is when BLKmode arrives
2794          with arguments not conforming to word_mode.  */
2795
2796       if (data->stack_parm == 0)
2797         ;
2798       else if (GET_CODE (entry_parm) == PARALLEL)
2799         ;
2800       else
2801         gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2802
2803       mem = validize_mem (stack_parm);
2804
2805       /* Handle values in multiple non-contiguous locations.  */
2806       if (GET_CODE (entry_parm) == PARALLEL)
2807         {
2808           push_to_sequence2 (all->first_conversion_insn,
2809                              all->last_conversion_insn);
2810           emit_group_store (mem, entry_parm, data->passed_type, size);
2811           all->first_conversion_insn = get_insns ();
2812           all->last_conversion_insn = get_last_insn ();
2813           end_sequence ();
2814         }
2815
2816       else if (size == 0)
2817         ;
2818
2819       /* If SIZE is that of a mode no bigger than a word, just use
2820          that mode's store operation.  */
2821       else if (size <= UNITS_PER_WORD)
2822         {
2823           enum machine_mode mode
2824             = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2825
2826           if (mode != BLKmode
2827 #ifdef BLOCK_REG_PADDING
2828               && (size == UNITS_PER_WORD
2829                   || (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2830                       != (BYTES_BIG_ENDIAN ? upward : downward)))
2831 #endif
2832               )
2833             {
2834               rtx reg;
2835
2836               /* We are really truncating a word_mode value containing
2837                  SIZE bytes into a value of mode MODE.  If such an
2838                  operation requires no actual instructions, we can refer
2839                  to the value directly in mode MODE, otherwise we must
2840                  start with the register in word_mode and explicitly
2841                  convert it.  */
2842               if (TRULY_NOOP_TRUNCATION (size * BITS_PER_UNIT, BITS_PER_WORD))
2843                 reg = gen_rtx_REG (mode, REGNO (entry_parm));
2844               else
2845                 {
2846                   reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2847                   reg = convert_to_mode (mode, copy_to_reg (reg), 1);
2848                 }
2849               emit_move_insn (change_address (mem, mode, 0), reg);
2850             }
2851
2852           /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
2853              machine must be aligned to the left before storing
2854              to memory.  Note that the previous test doesn't
2855              handle all cases (e.g. SIZE == 3).  */
2856           else if (size != UNITS_PER_WORD
2857 #ifdef BLOCK_REG_PADDING
2858                    && (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2859                        == downward)
2860 #else
2861                    && BYTES_BIG_ENDIAN
2862 #endif
2863                    )
2864             {
2865               rtx tem, x;
2866               int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2867               rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2868
2869               x = expand_shift (LSHIFT_EXPR, word_mode, reg, by, NULL_RTX, 1);
2870               tem = change_address (mem, word_mode, 0);
2871               emit_move_insn (tem, x);
2872             }
2873           else
2874             move_block_from_reg (REGNO (entry_parm), mem,
2875                                  size_stored / UNITS_PER_WORD);
2876         }
2877       else
2878         move_block_from_reg (REGNO (entry_parm), mem,
2879                              size_stored / UNITS_PER_WORD);
2880     }
2881   else if (data->stack_parm == 0)
2882     {
2883       push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
2884       emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
2885                        BLOCK_OP_NORMAL);
2886       all->first_conversion_insn = get_insns ();
2887       all->last_conversion_insn = get_last_insn ();
2888       end_sequence ();
2889     }
2890
2891   data->stack_parm = stack_parm;
2892   SET_DECL_RTL (parm, stack_parm);
2893 }
2894
2895 /* A subroutine of assign_parm_setup_reg, called through note_stores.
2896    This collects sets and clobbers of hard registers in a HARD_REG_SET,
2897    which is pointed to by DATA.  */
2898 static void
2899 record_hard_reg_sets (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
2900 {
2901   HARD_REG_SET *pset = (HARD_REG_SET *)data;
2902   if (REG_P (x) && HARD_REGISTER_P (x))
2903     add_to_hard_reg_set (pset, GET_MODE (x), REGNO (x));
2904 }
2905
2906 /* A subroutine of assign_parms.  Allocate a pseudo to hold the current
2907    parameter.  Get it there.  Perform all ABI specified conversions.  */
2908
2909 static void
2910 assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
2911                        struct assign_parm_data_one *data)
2912 {
2913   rtx parmreg, validated_mem;
2914   rtx equiv_stack_parm;
2915   enum machine_mode promoted_nominal_mode;
2916   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
2917   bool did_conversion = false;
2918   bool need_conversion, moved;
2919
2920   /* Store the parm in a pseudoregister during the function, but we may
2921      need to do it in a wider mode.  Using 2 here makes the result
2922      consistent with promote_decl_mode and thus expand_expr_real_1.  */
2923   promoted_nominal_mode
2924     = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
2925                              TREE_TYPE (current_function_decl), 2);
2926
2927   parmreg = gen_reg_rtx (promoted_nominal_mode);
2928
2929   if (!DECL_ARTIFICIAL (parm))
2930     mark_user_reg (parmreg);
2931
2932   /* If this was an item that we received a pointer to,
2933      set DECL_RTL appropriately.  */
2934   if (data->passed_pointer)
2935     {
2936       rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->passed_type)), parmreg);
2937       set_mem_attributes (x, parm, 1);
2938       SET_DECL_RTL (parm, x);
2939     }
2940   else
2941     SET_DECL_RTL (parm, parmreg);
2942
2943   assign_parm_remove_parallels (data);
2944
2945   /* Copy the value into the register, thus bridging between
2946      assign_parm_find_data_types and expand_expr_real_1.  */
2947
2948   equiv_stack_parm = data->stack_parm;
2949   validated_mem = validize_mem (data->entry_parm);
2950
2951   need_conversion = (data->nominal_mode != data->passed_mode
2952                      || promoted_nominal_mode != data->promoted_mode);
2953   moved = false;
2954
2955   if (need_conversion
2956       && GET_MODE_CLASS (data->nominal_mode) == MODE_INT
2957       && data->nominal_mode == data->passed_mode
2958       && data->nominal_mode == GET_MODE (data->entry_parm))
2959     {
2960       /* ENTRY_PARM has been converted to PROMOTED_MODE, its
2961          mode, by the caller.  We now have to convert it to
2962          NOMINAL_MODE, if different.  However, PARMREG may be in
2963          a different mode than NOMINAL_MODE if it is being stored
2964          promoted.
2965
2966          If ENTRY_PARM is a hard register, it might be in a register
2967          not valid for operating in its mode (e.g., an odd-numbered
2968          register for a DFmode).  In that case, moves are the only
2969          thing valid, so we can't do a convert from there.  This
2970          occurs when the calling sequence allow such misaligned
2971          usages.
2972
2973          In addition, the conversion may involve a call, which could
2974          clobber parameters which haven't been copied to pseudo
2975          registers yet.
2976
2977          First, we try to emit an insn which performs the necessary
2978          conversion.  We verify that this insn does not clobber any
2979          hard registers.  */
2980
2981       enum insn_code icode;
2982       rtx op0, op1;
2983
2984       icode = can_extend_p (promoted_nominal_mode, data->passed_mode,
2985                             unsignedp);
2986
2987       op0 = parmreg;
2988       op1 = validated_mem;
2989       if (icode != CODE_FOR_nothing
2990           && insn_operand_matches (icode, 0, op0)
2991           && insn_operand_matches (icode, 1, op1))
2992         {
2993           enum rtx_code code = unsignedp ? ZERO_EXTEND : SIGN_EXTEND;
2994           rtx insn, insns;
2995           HARD_REG_SET hardregs;
2996
2997           start_sequence ();
2998           insn = gen_extend_insn (op0, op1, promoted_nominal_mode,
2999                                   data->passed_mode, unsignedp);
3000           emit_insn (insn);
3001           insns = get_insns ();
3002
3003           moved = true;
3004           CLEAR_HARD_REG_SET (hardregs);
3005           for (insn = insns; insn && moved; insn = NEXT_INSN (insn))
3006             {
3007               if (INSN_P (insn))
3008                 note_stores (PATTERN (insn), record_hard_reg_sets,
3009                              &hardregs);
3010               if (!hard_reg_set_empty_p (hardregs))
3011                 moved = false;
3012             }
3013
3014           end_sequence ();
3015
3016           if (moved)
3017             {
3018               emit_insn (insns);
3019               if (equiv_stack_parm != NULL_RTX)
3020                 equiv_stack_parm = gen_rtx_fmt_e (code, GET_MODE (parmreg),
3021                                                   equiv_stack_parm);
3022             }
3023         }
3024     }
3025
3026   if (moved)
3027     /* Nothing to do.  */
3028     ;
3029   else if (need_conversion)
3030     {
3031       /* We did not have an insn to convert directly, or the sequence
3032          generated appeared unsafe.  We must first copy the parm to a
3033          pseudo reg, and save the conversion until after all
3034          parameters have been moved.  */
3035
3036       int save_tree_used;
3037       rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3038
3039       emit_move_insn (tempreg, validated_mem);
3040
3041       push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3042       tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
3043
3044       if (GET_CODE (tempreg) == SUBREG
3045           && GET_MODE (tempreg) == data->nominal_mode
3046           && REG_P (SUBREG_REG (tempreg))
3047           && data->nominal_mode == data->passed_mode
3048           && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm)
3049           && GET_MODE_SIZE (GET_MODE (tempreg))
3050              < GET_MODE_SIZE (GET_MODE (data->entry_parm)))
3051         {
3052           /* The argument is already sign/zero extended, so note it
3053              into the subreg.  */
3054           SUBREG_PROMOTED_VAR_P (tempreg) = 1;
3055           SUBREG_PROMOTED_UNSIGNED_SET (tempreg, unsignedp);
3056         }
3057
3058       /* TREE_USED gets set erroneously during expand_assignment.  */
3059       save_tree_used = TREE_USED (parm);
3060       expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
3061       TREE_USED (parm) = save_tree_used;
3062       all->first_conversion_insn = get_insns ();
3063       all->last_conversion_insn = get_last_insn ();
3064       end_sequence ();
3065
3066       did_conversion = true;
3067     }
3068   else
3069     emit_move_insn (parmreg, validated_mem);
3070
3071   /* If we were passed a pointer but the actual value can safely live
3072      in a register, put it in one.  */
3073   if (data->passed_pointer
3074       && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
3075       /* If by-reference argument was promoted, demote it.  */
3076       && (TYPE_MODE (TREE_TYPE (parm)) != GET_MODE (DECL_RTL (parm))
3077           || use_register_for_decl (parm)))
3078     {
3079       /* We can't use nominal_mode, because it will have been set to
3080          Pmode above.  We must use the actual mode of the parm.  */
3081       parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3082       mark_user_reg (parmreg);
3083
3084       if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
3085         {
3086           rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
3087           int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
3088
3089           push_to_sequence2 (all->first_conversion_insn,
3090                              all->last_conversion_insn);
3091           emit_move_insn (tempreg, DECL_RTL (parm));
3092           tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
3093           emit_move_insn (parmreg, tempreg);
3094           all->first_conversion_insn = get_insns ();
3095           all->last_conversion_insn = get_last_insn ();
3096           end_sequence ();
3097
3098           did_conversion = true;
3099         }
3100       else
3101         emit_move_insn (parmreg, DECL_RTL (parm));
3102
3103       SET_DECL_RTL (parm, parmreg);
3104
3105       /* STACK_PARM is the pointer, not the parm, and PARMREG is
3106          now the parm.  */
3107       data->stack_parm = NULL;
3108     }
3109
3110   /* Mark the register as eliminable if we did no conversion and it was
3111      copied from memory at a fixed offset, and the arg pointer was not
3112      copied to a pseudo-reg.  If the arg pointer is a pseudo reg or the
3113      offset formed an invalid address, such memory-equivalences as we
3114      make here would screw up life analysis for it.  */
3115   if (data->nominal_mode == data->passed_mode
3116       && !did_conversion
3117       && data->stack_parm != 0
3118       && MEM_P (data->stack_parm)
3119       && data->locate.offset.var == 0
3120       && reg_mentioned_p (virtual_incoming_args_rtx,
3121                           XEXP (data->stack_parm, 0)))
3122     {
3123       rtx linsn = get_last_insn ();
3124       rtx sinsn, set;
3125
3126       /* Mark complex types separately.  */
3127       if (GET_CODE (parmreg) == CONCAT)
3128         {
3129           enum machine_mode submode
3130             = GET_MODE_INNER (GET_MODE (parmreg));
3131           int regnor = REGNO (XEXP (parmreg, 0));
3132           int regnoi = REGNO (XEXP (parmreg, 1));
3133           rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3134           rtx stacki = adjust_address_nv (data->stack_parm, submode,
3135                                           GET_MODE_SIZE (submode));
3136
3137           /* Scan backwards for the set of the real and
3138              imaginary parts.  */
3139           for (sinsn = linsn; sinsn != 0;
3140                sinsn = prev_nonnote_insn (sinsn))
3141             {
3142               set = single_set (sinsn);
3143               if (set == 0)
3144                 continue;
3145
3146               if (SET_DEST (set) == regno_reg_rtx [regnoi])
3147                 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
3148               else if (SET_DEST (set) == regno_reg_rtx [regnor])
3149                 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
3150             }
3151         }
3152       else if ((set = single_set (linsn)) != 0
3153                && SET_DEST (set) == parmreg)
3154         set_unique_reg_note (linsn, REG_EQUIV, equiv_stack_parm);
3155     }
3156
3157   /* For pointer data type, suggest pointer register.  */
3158   if (POINTER_TYPE_P (TREE_TYPE (parm)))
3159     mark_reg_pointer (parmreg,
3160                       TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3161 }
3162
3163 /* A subroutine of assign_parms.  Allocate stack space to hold the current
3164    parameter.  Get it there.  Perform all ABI specified conversions.  */
3165
3166 static void
3167 assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3168                          struct assign_parm_data_one *data)
3169 {
3170   /* Value must be stored in the stack slot STACK_PARM during function
3171      execution.  */
3172   bool to_conversion = false;
3173
3174   assign_parm_remove_parallels (data);
3175
3176   if (data->promoted_mode != data->nominal_mode)
3177     {
3178       /* Conversion is required.  */
3179       rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3180
3181       emit_move_insn (tempreg, validize_mem (data->entry_parm));
3182
3183       push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3184       to_conversion = true;
3185
3186       data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3187                                           TYPE_UNSIGNED (TREE_TYPE (parm)));
3188
3189       if (data->stack_parm)
3190         {
3191           int offset = subreg_lowpart_offset (data->nominal_mode,
3192                                               GET_MODE (data->stack_parm));
3193           /* ??? This may need a big-endian conversion on sparc64.  */
3194           data->stack_parm
3195             = adjust_address (data->stack_parm, data->nominal_mode, 0);
3196           if (offset && MEM_OFFSET_KNOWN_P (data->stack_parm))
3197             set_mem_offset (data->stack_parm,
3198                             MEM_OFFSET (data->stack_parm) + offset);
3199         }
3200     }
3201
3202   if (data->entry_parm != data->stack_parm)
3203     {
3204       rtx src, dest;
3205
3206       if (data->stack_parm == 0)
3207         {
3208           int align = STACK_SLOT_ALIGNMENT (data->passed_type,
3209                                             GET_MODE (data->entry_parm),
3210                                             TYPE_ALIGN (data->passed_type));
3211           data->stack_parm
3212             = assign_stack_local (GET_MODE (data->entry_parm),
3213                                   GET_MODE_SIZE (GET_MODE (data->entry_parm)),
3214                                   align);
3215           set_mem_attributes (data->stack_parm, parm, 1);
3216         }
3217
3218       dest = validize_mem (data->stack_parm);
3219       src = validize_mem (data->entry_parm);
3220
3221       if (MEM_P (src))
3222         {
3223           /* Use a block move to handle potentially misaligned entry_parm.  */
3224           if (!to_conversion)
3225             push_to_sequence2 (all->first_conversion_insn,
3226                                all->last_conversion_insn);
3227           to_conversion = true;
3228
3229           emit_block_move (dest, src,
3230                            GEN_INT (int_size_in_bytes (data->passed_type)),
3231                            BLOCK_OP_NORMAL);
3232         }
3233       else
3234         emit_move_insn (dest, src);
3235     }
3236
3237   if (to_conversion)
3238     {
3239       all->first_conversion_insn = get_insns ();
3240       all->last_conversion_insn = get_last_insn ();
3241       end_sequence ();
3242     }
3243
3244   SET_DECL_RTL (parm, data->stack_parm);
3245 }
3246
3247 /* A subroutine of assign_parms.  If the ABI splits complex arguments, then
3248    undo the frobbing that we did in assign_parms_augmented_arg_list.  */
3249
3250 static void
3251 assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3252                               VEC(tree, heap) *fnargs)
3253 {
3254   tree parm;
3255   tree orig_fnargs = all->orig_fnargs;
3256   unsigned i = 0;
3257
3258   for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
3259     {
3260       if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3261           && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3262         {
3263           rtx tmp, real, imag;
3264           enum machine_mode inner = GET_MODE_INNER (DECL_MODE (parm));
3265
3266           real = DECL_RTL (VEC_index (tree, fnargs, i));
3267           imag = DECL_RTL (VEC_index (tree, fnargs, i + 1));
3268           if (inner != GET_MODE (real))
3269             {
3270               real = gen_lowpart_SUBREG (inner, real);
3271               imag = gen_lowpart_SUBREG (inner, imag);
3272             }
3273
3274           if (TREE_ADDRESSABLE (parm))
3275             {
3276               rtx rmem, imem;
3277               HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
3278               int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3279                                                 DECL_MODE (parm),
3280                                                 TYPE_ALIGN (TREE_TYPE (parm)));
3281
3282               /* split_complex_arg put the real and imag parts in
3283                  pseudos.  Move them to memory.  */
3284               tmp = assign_stack_local (DECL_MODE (parm), size, align);
3285               set_mem_attributes (tmp, parm, 1);
3286               rmem = adjust_address_nv (tmp, inner, 0);
3287               imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
3288               push_to_sequence2 (all->first_conversion_insn,
3289                                  all->last_conversion_insn);
3290               emit_move_insn (rmem, real);
3291               emit_move_insn (imem, imag);
3292               all->first_conversion_insn = get_insns ();
3293               all->last_conversion_insn = get_last_insn ();
3294               end_sequence ();
3295             }
3296           else
3297             tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3298           SET_DECL_RTL (parm, tmp);
3299
3300           real = DECL_INCOMING_RTL (VEC_index (tree, fnargs, i));
3301           imag = DECL_INCOMING_RTL (VEC_index (tree, fnargs, i + 1));
3302           if (inner != GET_MODE (real))
3303             {
3304               real = gen_lowpart_SUBREG (inner, real);
3305               imag = gen_lowpart_SUBREG (inner, imag);
3306             }
3307           tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3308           set_decl_incoming_rtl (parm, tmp, false);
3309           i++;
3310         }
3311     }
3312 }
3313
3314 /* Assign RTL expressions to the function's parameters.  This may involve
3315    copying them into registers and using those registers as the DECL_RTL.  */
3316
3317 static void
3318 assign_parms (tree fndecl)
3319 {
3320   struct assign_parm_data_all all;
3321   tree parm;
3322   VEC(tree, heap) *fnargs;
3323   unsigned i;
3324
3325   crtl->args.internal_arg_pointer
3326     = targetm.calls.internal_arg_pointer ();
3327
3328   assign_parms_initialize_all (&all);
3329   fnargs = assign_parms_augmented_arg_list (&all);
3330
3331   FOR_EACH_VEC_ELT (tree, fnargs, i, parm)
3332     {
3333       struct assign_parm_data_one data;
3334
3335       /* Extract the type of PARM; adjust it according to ABI.  */
3336       assign_parm_find_data_types (&all, parm, &data);
3337
3338       /* Early out for errors and void parameters.  */
3339       if (data.passed_mode == VOIDmode)
3340         {
3341           SET_DECL_RTL (parm, const0_rtx);
3342           DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3343           continue;
3344         }
3345
3346       /* Estimate stack alignment from parameter alignment.  */
3347       if (SUPPORTS_STACK_ALIGNMENT)
3348         {
3349           unsigned int align
3350             = targetm.calls.function_arg_boundary (data.promoted_mode,
3351                                                    data.passed_type);
3352           align = MINIMUM_ALIGNMENT (data.passed_type, data.promoted_mode,
3353                                      align);
3354           if (TYPE_ALIGN (data.nominal_type) > align)
3355             align = MINIMUM_ALIGNMENT (data.nominal_type,
3356                                        TYPE_MODE (data.nominal_type),
3357                                        TYPE_ALIGN (data.nominal_type));
3358           if (crtl->stack_alignment_estimated < align)
3359             {
3360               gcc_assert (!crtl->stack_realign_processed);
3361               crtl->stack_alignment_estimated = align;
3362             }
3363         }
3364
3365       if (cfun->stdarg && !DECL_CHAIN (parm))
3366         assign_parms_setup_varargs (&all, &data, false);
3367
3368       /* Find out where the parameter arrives in this function.  */
3369       assign_parm_find_entry_rtl (&all, &data);
3370
3371       /* Find out where stack space for this parameter might be.  */
3372       if (assign_parm_is_stack_parm (&all, &data))
3373         {
3374           assign_parm_find_stack_rtl (parm, &data);
3375           assign_parm_adjust_entry_rtl (&data);
3376         }
3377
3378       /* Record permanently how this parm was passed.  */
3379       if (data.passed_pointer)
3380         {
3381           rtx incoming_rtl
3382             = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data.passed_type)),
3383                            data.entry_parm);
3384           set_decl_incoming_rtl (parm, incoming_rtl, true);
3385         }
3386       else
3387         set_decl_incoming_rtl (parm, data.entry_parm, false);
3388
3389       /* Update info on where next arg arrives in registers.  */
3390       targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3391                                           data.passed_type, data.named_arg);
3392
3393       assign_parm_adjust_stack_rtl (&data);
3394
3395       if (assign_parm_setup_block_p (&data))
3396         assign_parm_setup_block (&all, parm, &data);
3397       else if (data.passed_pointer || use_register_for_decl (parm))
3398         assign_parm_setup_reg (&all, parm, &data);
3399       else
3400         assign_parm_setup_stack (&all, parm, &data);
3401     }
3402
3403   if (targetm.calls.split_complex_arg)
3404     assign_parms_unsplit_complex (&all, fnargs);
3405
3406   VEC_free (tree, heap, fnargs);
3407
3408   /* Output all parameter conversion instructions (possibly including calls)
3409      now that all parameters have been copied out of hard registers.  */
3410   emit_insn (all.first_conversion_insn);
3411
3412   /* Estimate reload stack alignment from scalar return mode.  */
3413   if (SUPPORTS_STACK_ALIGNMENT)
3414     {
3415       if (DECL_RESULT (fndecl))
3416         {
3417           tree type = TREE_TYPE (DECL_RESULT (fndecl));
3418           enum machine_mode mode = TYPE_MODE (type);
3419
3420           if (mode != BLKmode
3421               && mode != VOIDmode
3422               && !AGGREGATE_TYPE_P (type))
3423             {
3424               unsigned int align = GET_MODE_ALIGNMENT (mode);
3425               if (crtl->stack_alignment_estimated < align)
3426                 {
3427                   gcc_assert (!crtl->stack_realign_processed);
3428                   crtl->stack_alignment_estimated = align;
3429                 }
3430             }
3431         }
3432     }
3433
3434   /* If we are receiving a struct value address as the first argument, set up
3435      the RTL for the function result. As this might require code to convert
3436      the transmitted address to Pmode, we do this here to ensure that possible
3437      preliminary conversions of the address have been emitted already.  */
3438   if (all.function_result_decl)
3439     {
3440       tree result = DECL_RESULT (current_function_decl);
3441       rtx addr = DECL_RTL (all.function_result_decl);
3442       rtx x;
3443
3444       if (DECL_BY_REFERENCE (result))
3445         {
3446           SET_DECL_VALUE_EXPR (result, all.function_result_decl);
3447           x = addr;
3448         }
3449       else
3450         {
3451           SET_DECL_VALUE_EXPR (result,
3452                                build1 (INDIRECT_REF, TREE_TYPE (result),
3453                                        all.function_result_decl));
3454           addr = convert_memory_address (Pmode, addr);
3455           x = gen_rtx_MEM (DECL_MODE (result), addr);
3456           set_mem_attributes (x, result, 1);
3457         }
3458
3459       DECL_HAS_VALUE_EXPR_P (result) = 1;
3460
3461       SET_DECL_RTL (result, x);
3462     }
3463
3464   /* We have aligned all the args, so add space for the pretend args.  */
3465   crtl->args.pretend_args_size = all.pretend_args_size;
3466   all.stack_args_size.constant += all.extra_pretend_bytes;
3467   crtl->args.size = all.stack_args_size.constant;
3468
3469   /* Adjust function incoming argument size for alignment and
3470      minimum length.  */
3471
3472 #ifdef REG_PARM_STACK_SPACE
3473   crtl->args.size = MAX (crtl->args.size,
3474                                     REG_PARM_STACK_SPACE (fndecl));
3475 #endif
3476
3477   crtl->args.size = CEIL_ROUND (crtl->args.size,
3478                                            PARM_BOUNDARY / BITS_PER_UNIT);
3479
3480 #ifdef ARGS_GROW_DOWNWARD
3481   crtl->args.arg_offset_rtx
3482     = (all.stack_args_size.var == 0 ? GEN_INT (-all.stack_args_size.constant)
3483        : expand_expr (size_diffop (all.stack_args_size.var,
3484                                    size_int (-all.stack_args_size.constant)),
3485                       NULL_RTX, VOIDmode, EXPAND_NORMAL));
3486 #else
3487   crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3488 #endif
3489
3490   /* See how many bytes, if any, of its args a function should try to pop
3491      on return.  */
3492
3493   crtl->args.pops_args = targetm.calls.return_pops_args (fndecl,
3494                                                          TREE_TYPE (fndecl),
3495                                                          crtl->args.size);
3496
3497   /* For stdarg.h function, save info about
3498      regs and stack space used by the named args.  */
3499
3500   crtl->args.info = all.args_so_far_v;
3501
3502   /* Set the rtx used for the function return value.  Put this in its
3503      own variable so any optimizers that need this information don't have
3504      to include tree.h.  Do this here so it gets done when an inlined
3505      function gets output.  */
3506
3507   crtl->return_rtx
3508     = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3509        ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3510
3511   /* If scalar return value was computed in a pseudo-reg, or was a named
3512      return value that got dumped to the stack, copy that to the hard
3513      return register.  */
3514   if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3515     {
3516       tree decl_result = DECL_RESULT (fndecl);
3517       rtx decl_rtl = DECL_RTL (decl_result);
3518
3519       if (REG_P (decl_rtl)
3520           ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3521           : DECL_REGISTER (decl_result))
3522         {
3523           rtx real_decl_rtl;
3524
3525           real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3526                                                         fndecl, true);
3527           REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3528           /* The delay slot scheduler assumes that crtl->return_rtx
3529              holds the hard register containing the return value, not a
3530              temporary pseudo.  */
3531           crtl->return_rtx = real_decl_rtl;
3532         }
3533     }
3534 }
3535
3536 /* A subroutine of gimplify_parameters, invoked via walk_tree.
3537    For all seen types, gimplify their sizes.  */
3538
3539 static tree
3540 gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3541 {
3542   tree t = *tp;
3543
3544   *walk_subtrees = 0;
3545   if (TYPE_P (t))
3546     {
3547       if (POINTER_TYPE_P (t))
3548         *walk_subtrees = 1;
3549       else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3550                && !TYPE_SIZES_GIMPLIFIED (t))
3551         {
3552           gimplify_type_sizes (t, (gimple_seq *) data);
3553           *walk_subtrees = 1;
3554         }
3555     }
3556
3557   return NULL;
3558 }
3559
3560 /* Gimplify the parameter list for current_function_decl.  This involves
3561    evaluating SAVE_EXPRs of variable sized parameters and generating code
3562    to implement callee-copies reference parameters.  Returns a sequence of
3563    statements to add to the beginning of the function.  */
3564
3565 gimple_seq
3566 gimplify_parameters (void)
3567 {
3568   struct assign_parm_data_all all;
3569   tree parm;
3570   gimple_seq stmts = NULL;
3571   VEC(tree, heap) *fnargs;
3572   unsigned i;
3573
3574   assign_parms_initialize_all (&all);
3575   fnargs = assign_parms_augmented_arg_list (&all);
3576
3577   FOR_EACH_VEC_ELT (tree, fnargs, i, parm)
3578     {
3579       struct assign_parm_data_one data;
3580
3581       /* Extract the type of PARM; adjust it according to ABI.  */
3582       assign_parm_find_data_types (&all, parm, &data);
3583
3584       /* Early out for errors and void parameters.  */
3585       if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3586         continue;
3587
3588       /* Update info on where next arg arrives in registers.  */
3589       targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3590                                           data.passed_type, data.named_arg);
3591
3592       /* ??? Once upon a time variable_size stuffed parameter list
3593          SAVE_EXPRs (amongst others) onto a pending sizes list.  This
3594          turned out to be less than manageable in the gimple world.
3595          Now we have to hunt them down ourselves.  */
3596       walk_tree_without_duplicates (&data.passed_type,
3597                                     gimplify_parm_type, &stmts);
3598
3599       if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
3600         {
3601           gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3602           gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3603         }
3604
3605       if (data.passed_pointer)
3606         {
3607           tree type = TREE_TYPE (data.passed_type);
3608           if (reference_callee_copied (&all.args_so_far_v, TYPE_MODE (type),
3609                                        type, data.named_arg))
3610             {
3611               tree local, t;
3612
3613               /* For constant-sized objects, this is trivial; for
3614                  variable-sized objects, we have to play games.  */
3615               if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3616                   && !(flag_stack_check == GENERIC_STACK_CHECK
3617                        && compare_tree_int (DECL_SIZE_UNIT (parm),
3618                                             STACK_CHECK_MAX_VAR_SIZE) > 0))
3619                 {
3620                   local = create_tmp_var (type, get_name (parm));
3621                   DECL_IGNORED_P (local) = 0;
3622                   /* If PARM was addressable, move that flag over
3623                      to the local copy, as its address will be taken,
3624                      not the PARMs.  Keep the parms address taken
3625                      as we'll query that flag during gimplification.  */
3626                   if (TREE_ADDRESSABLE (parm))
3627                     TREE_ADDRESSABLE (local) = 1;
3628                   else if (TREE_CODE (type) == COMPLEX_TYPE
3629                            || TREE_CODE (type) == VECTOR_TYPE)
3630                     DECL_GIMPLE_REG_P (local) = 1;
3631                 }
3632               else
3633                 {
3634                   tree ptr_type, addr;
3635
3636                   ptr_type = build_pointer_type (type);
3637                   addr = create_tmp_reg (ptr_type, get_name (parm));
3638                   DECL_IGNORED_P (addr) = 0;
3639                   local = build_fold_indirect_ref (addr);
3640
3641                   t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
3642                   t = build_call_expr (t, 2, DECL_SIZE_UNIT (parm),
3643                                        size_int (DECL_ALIGN (parm)));
3644
3645                   /* The call has been built for a variable-sized object.  */
3646                   CALL_ALLOCA_FOR_VAR_P (t) = 1;
3647                   t = fold_convert (ptr_type, t);
3648                   t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
3649                   gimplify_and_add (t, &stmts);
3650                 }
3651
3652               gimplify_assign (local, parm, &stmts);
3653
3654               SET_DECL_VALUE_EXPR (parm, local);
3655               DECL_HAS_VALUE_EXPR_P (parm) = 1;
3656             }
3657         }
3658     }
3659
3660   VEC_free (tree, heap, fnargs);
3661
3662   return stmts;
3663 }
3664 \f
3665 /* Compute the size and offset from the start of the stacked arguments for a
3666    parm passed in mode PASSED_MODE and with type TYPE.
3667
3668    INITIAL_OFFSET_PTR points to the current offset into the stacked
3669    arguments.
3670
3671    The starting offset and size for this parm are returned in
3672    LOCATE->OFFSET and LOCATE->SIZE, respectively.  When IN_REGS is
3673    nonzero, the offset is that of stack slot, which is returned in
3674    LOCATE->SLOT_OFFSET.  LOCATE->ALIGNMENT_PAD is the amount of
3675    padding required from the initial offset ptr to the stack slot.
3676
3677    IN_REGS is nonzero if the argument will be passed in registers.  It will
3678    never be set if REG_PARM_STACK_SPACE is not defined.
3679
3680    FNDECL is the function in which the argument was defined.
3681
3682    There are two types of rounding that are done.  The first, controlled by
3683    TARGET_FUNCTION_ARG_BOUNDARY, forces the offset from the start of the
3684    argument list to be aligned to the specific boundary (in bits).  This
3685    rounding affects the initial and starting offsets, but not the argument
3686    size.
3687
3688    The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
3689    optionally rounds the size of the parm to PARM_BOUNDARY.  The
3690    initial offset is not affected by this rounding, while the size always
3691    is and the starting offset may be.  */
3692
3693 /*  LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
3694     INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
3695     callers pass in the total size of args so far as
3696     INITIAL_OFFSET_PTR.  LOCATE->SIZE is always positive.  */
3697
3698 void
3699 locate_and_pad_parm (enum machine_mode passed_mode, tree type, int in_regs,
3700                      int partial, tree fndecl ATTRIBUTE_UNUSED,
3701                      struct args_size *initial_offset_ptr,
3702                      struct locate_and_pad_arg_data *locate)
3703 {
3704   tree sizetree;
3705   enum direction where_pad;
3706   unsigned int boundary, round_boundary;
3707   int reg_parm_stack_space = 0;
3708   int part_size_in_regs;
3709
3710 #ifdef REG_PARM_STACK_SPACE
3711   reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
3712
3713   /* If we have found a stack parm before we reach the end of the
3714      area reserved for registers, skip that area.  */
3715   if (! in_regs)
3716     {
3717       if (reg_parm_stack_space > 0)
3718         {
3719           if (initial_offset_ptr->var)
3720             {
3721               initial_offset_ptr->var
3722                 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
3723                               ssize_int (reg_parm_stack_space));
3724               initial_offset_ptr->constant = 0;
3725             }
3726           else if (initial_offset_ptr->constant < reg_parm_stack_space)
3727             initial_offset_ptr->constant = reg_parm_stack_space;
3728         }
3729     }
3730 #endif /* REG_PARM_STACK_SPACE */
3731
3732   part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
3733
3734   sizetree
3735     = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
3736   where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
3737   boundary = targetm.calls.function_arg_boundary (passed_mode, type);
3738   round_boundary = targetm.calls.function_arg_round_boundary (passed_mode,
3739                                                               type);
3740   locate->where_pad = where_pad;
3741
3742   /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT.  */
3743   if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
3744     boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
3745
3746   locate->boundary = boundary;
3747
3748   if (SUPPORTS_STACK_ALIGNMENT)
3749     {
3750       /* stack_alignment_estimated can't change after stack has been
3751          realigned.  */
3752       if (crtl->stack_alignment_estimated < boundary)
3753         {
3754           if (!crtl->stack_realign_processed)
3755             crtl->stack_alignment_estimated = boundary;
3756           else
3757             {
3758               /* If stack is realigned and stack alignment value
3759                  hasn't been finalized, it is OK not to increase
3760                  stack_alignment_estimated.  The bigger alignment
3761                  requirement is recorded in stack_alignment_needed
3762                  below.  */
3763               gcc_assert (!crtl->stack_realign_finalized
3764                           && crtl->stack_realign_needed);
3765             }
3766         }
3767     }
3768
3769   /* Remember if the outgoing parameter requires extra alignment on the
3770      calling function side.  */
3771   if (crtl->stack_alignment_needed < boundary)
3772     crtl->stack_alignment_needed = boundary;
3773   if (crtl->preferred_stack_boundary < boundary)
3774     crtl->preferred_stack_boundary = boundary;
3775
3776 #ifdef ARGS_GROW_DOWNWARD
3777   locate->slot_offset.constant = -initial_offset_ptr->constant;
3778   if (initial_offset_ptr->var)
3779     locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
3780                                           initial_offset_ptr->var);
3781
3782   {
3783     tree s2 = sizetree;
3784     if (where_pad != none
3785         && (!host_integerp (sizetree, 1)
3786             || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % round_boundary))
3787       s2 = round_up (s2, round_boundary / BITS_PER_UNIT);
3788     SUB_PARM_SIZE (locate->slot_offset, s2);
3789   }
3790
3791   locate->slot_offset.constant += part_size_in_regs;
3792
3793   if (!in_regs
3794 #ifdef REG_PARM_STACK_SPACE
3795       || REG_PARM_STACK_SPACE (fndecl) > 0
3796 #endif
3797      )
3798     pad_to_arg_alignment (&locate->slot_offset, boundary,
3799                           &locate->alignment_pad);
3800
3801   locate->size.constant = (-initial_offset_ptr->constant
3802                            - locate->slot_offset.constant);
3803   if (initial_offset_ptr->var)
3804     locate->size.var = size_binop (MINUS_EXPR,
3805                                    size_binop (MINUS_EXPR,
3806                                                ssize_int (0),
3807                                                initial_offset_ptr->var),
3808                                    locate->slot_offset.var);
3809
3810   /* Pad_below needs the pre-rounded size to know how much to pad
3811      below.  */
3812   locate->offset = locate->slot_offset;
3813   if (where_pad == downward)
3814     pad_below (&locate->offset, passed_mode, sizetree);
3815
3816 #else /* !ARGS_GROW_DOWNWARD */
3817   if (!in_regs
3818 #ifdef REG_PARM_STACK_SPACE
3819       || REG_PARM_STACK_SPACE (fndecl) > 0
3820 #endif
3821       )
3822     pad_to_arg_alignment (initial_offset_ptr, boundary,
3823                           &locate->alignment_pad);
3824   locate->slot_offset = *initial_offset_ptr;
3825
3826 #ifdef PUSH_ROUNDING
3827   if (passed_mode != BLKmode)
3828     sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
3829 #endif
3830
3831   /* Pad_below needs the pre-rounded size to know how much to pad below
3832      so this must be done before rounding up.  */
3833   locate->offset = locate->slot_offset;
3834   if (where_pad == downward)
3835     pad_below (&locate->offset, passed_mode, sizetree);
3836
3837   if (where_pad != none
3838       && (!host_integerp (sizetree, 1)
3839           || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % round_boundary))
3840     sizetree = round_up (sizetree, round_boundary / BITS_PER_UNIT);
3841
3842   ADD_PARM_SIZE (locate->size, sizetree);
3843
3844   locate->size.constant -= part_size_in_regs;
3845 #endif /* ARGS_GROW_DOWNWARD */
3846
3847 #ifdef FUNCTION_ARG_OFFSET
3848   locate->offset.constant += FUNCTION_ARG_OFFSET (passed_mode, type);
3849 #endif
3850 }
3851
3852 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
3853    BOUNDARY is measured in bits, but must be a multiple of a storage unit.  */
3854
3855 static void
3856 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
3857                       struct args_size *alignment_pad)
3858 {
3859   tree save_var = NULL_TREE;
3860   HOST_WIDE_INT save_constant = 0;
3861   int boundary_in_bytes = boundary / BITS_PER_UNIT;
3862   HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
3863
3864 #ifdef SPARC_STACK_BOUNDARY_HACK
3865   /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
3866      the real alignment of %sp.  However, when it does this, the
3867      alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY.  */
3868   if (SPARC_STACK_BOUNDARY_HACK)
3869     sp_offset = 0;
3870 #endif
3871
3872   if (boundary > PARM_BOUNDARY)
3873     {
3874       save_var = offset_ptr->var;
3875       save_constant = offset_ptr->constant;
3876     }
3877
3878   alignment_pad->var = NULL_TREE;
3879   alignment_pad->constant = 0;
3880
3881   if (boundary > BITS_PER_UNIT)
3882     {
3883       if (offset_ptr->var)
3884         {
3885           tree sp_offset_tree = ssize_int (sp_offset);
3886           tree offset = size_binop (PLUS_EXPR,
3887                                     ARGS_SIZE_TREE (*offset_ptr),
3888                                     sp_offset_tree);
3889 #ifdef ARGS_GROW_DOWNWARD
3890           tree rounded = round_down (offset, boundary / BITS_PER_UNIT);
3891 #else
3892           tree rounded = round_up   (offset, boundary / BITS_PER_UNIT);
3893 #endif
3894
3895           offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
3896           /* ARGS_SIZE_TREE includes constant term.  */
3897           offset_ptr->constant = 0;
3898           if (boundary > PARM_BOUNDARY)
3899             alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
3900                                              save_var);
3901         }
3902       else
3903         {
3904           offset_ptr->constant = -sp_offset +
3905 #ifdef ARGS_GROW_DOWNWARD
3906             FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3907 #else
3908             CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3909 #endif
3910             if (boundary > PARM_BOUNDARY)
3911               alignment_pad->constant = offset_ptr->constant - save_constant;
3912         }
3913     }
3914 }
3915
3916 static void
3917 pad_below (struct args_size *offset_ptr, enum machine_mode passed_mode, tree sizetree)
3918 {
3919   if (passed_mode != BLKmode)
3920     {
3921       if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
3922         offset_ptr->constant
3923           += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
3924                / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
3925               - GET_MODE_SIZE (passed_mode));
3926     }
3927   else
3928     {
3929       if (TREE_CODE (sizetree) != INTEGER_CST
3930           || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
3931         {
3932           /* Round the size up to multiple of PARM_BOUNDARY bits.  */
3933           tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
3934           /* Add it in.  */
3935           ADD_PARM_SIZE (*offset_ptr, s2);
3936           SUB_PARM_SIZE (*offset_ptr, sizetree);
3937         }
3938     }
3939 }
3940 \f
3941
3942 /* True if register REGNO was alive at a place where `setjmp' was
3943    called and was set more than once or is an argument.  Such regs may
3944    be clobbered by `longjmp'.  */
3945
3946 static bool
3947 regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
3948 {
3949   /* There appear to be cases where some local vars never reach the
3950      backend but have bogus regnos.  */
3951   if (regno >= max_reg_num ())
3952     return false;
3953
3954   return ((REG_N_SETS (regno) > 1
3955            || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR), regno))
3956           && REGNO_REG_SET_P (setjmp_crosses, regno));
3957 }
3958
3959 /* Walk the tree of blocks describing the binding levels within a
3960    function and warn about variables the might be killed by setjmp or
3961    vfork.  This is done after calling flow_analysis before register
3962    allocation since that will clobber the pseudo-regs to hard
3963    regs.  */
3964
3965 static void
3966 setjmp_vars_warning (bitmap setjmp_crosses, tree block)
3967 {
3968   tree decl, sub;
3969
3970   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
3971     {
3972       if (TREE_CODE (decl) == VAR_DECL
3973           && DECL_RTL_SET_P (decl)
3974           && REG_P (DECL_RTL (decl))
3975           && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
3976         warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
3977                  " %<longjmp%> or %<vfork%>", decl);
3978     }
3979
3980   for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
3981     setjmp_vars_warning (setjmp_crosses, sub);
3982 }
3983
3984 /* Do the appropriate part of setjmp_vars_warning
3985    but for arguments instead of local variables.  */
3986
3987 static void
3988 setjmp_args_warning (bitmap setjmp_crosses)
3989 {
3990   tree decl;
3991   for (decl = DECL_ARGUMENTS (current_function_decl);
3992        decl; decl = DECL_CHAIN (decl))
3993     if (DECL_RTL (decl) != 0
3994         && REG_P (DECL_RTL (decl))
3995         && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
3996       warning (OPT_Wclobbered,
3997                "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
3998                decl);
3999 }
4000
4001 /* Generate warning messages for variables live across setjmp.  */
4002
4003 void
4004 generate_setjmp_warnings (void)
4005 {
4006   bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
4007
4008   if (n_basic_blocks == NUM_FIXED_BLOCKS
4009       || bitmap_empty_p (setjmp_crosses))
4010     return;
4011
4012   setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
4013   setjmp_args_warning (setjmp_crosses);
4014 }
4015
4016 \f
4017 /* Reverse the order of elements in the fragment chain T of blocks,
4018    and return the new head of the chain (old last element).  */
4019
4020 static tree
4021 block_fragments_nreverse (tree t)
4022 {
4023   tree prev = 0, block, next;
4024   for (block = t; block; block = next)
4025     {
4026       next = BLOCK_FRAGMENT_CHAIN (block);
4027       BLOCK_FRAGMENT_CHAIN (block) = prev;
4028       prev = block;
4029     }
4030   return prev;
4031 }
4032
4033 /* Reverse the order of elements in the chain T of blocks,
4034    and return the new head of the chain (old last element).
4035    Also do the same on subblocks and reverse the order of elements
4036    in BLOCK_FRAGMENT_CHAIN as well.  */
4037
4038 static tree
4039 blocks_nreverse_all (tree t)
4040 {
4041   tree prev = 0, block, next;
4042   for (block = t; block; block = next)
4043     {
4044       next = BLOCK_CHAIN (block);
4045       BLOCK_CHAIN (block) = prev;
4046       BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4047       if (BLOCK_FRAGMENT_CHAIN (block)
4048           && BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE)
4049         BLOCK_FRAGMENT_CHAIN (block)
4050           = block_fragments_nreverse (BLOCK_FRAGMENT_CHAIN (block));
4051       prev = block;
4052     }
4053   return prev;
4054 }
4055
4056
4057 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
4058    and create duplicate blocks.  */
4059 /* ??? Need an option to either create block fragments or to create
4060    abstract origin duplicates of a source block.  It really depends
4061    on what optimization has been performed.  */
4062
4063 void
4064 reorder_blocks (void)
4065 {
4066   tree block = DECL_INITIAL (current_function_decl);
4067   VEC(tree,heap) *block_stack;
4068
4069   if (block == NULL_TREE)
4070     return;
4071
4072   block_stack = VEC_alloc (tree, heap, 10);
4073
4074   /* Reset the TREE_ASM_WRITTEN bit for all blocks.  */
4075   clear_block_marks (block);
4076
4077   /* Prune the old trees away, so that they don't get in the way.  */
4078   BLOCK_SUBBLOCKS (block) = NULL_TREE;
4079   BLOCK_CHAIN (block) = NULL_TREE;
4080
4081   /* Recreate the block tree from the note nesting.  */
4082   reorder_blocks_1 (get_insns (), block, &block_stack);
4083   BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4084
4085   VEC_free (tree, heap, block_stack);
4086 }
4087
4088 /* Helper function for reorder_blocks.  Reset TREE_ASM_WRITTEN.  */
4089
4090 void
4091 clear_block_marks (tree block)
4092 {
4093   while (block)
4094     {
4095       TREE_ASM_WRITTEN (block) = 0;
4096       clear_block_marks (BLOCK_SUBBLOCKS (block));
4097       block = BLOCK_CHAIN (block);
4098     }
4099 }
4100
4101 static void
4102 reorder_blocks_1 (rtx insns, tree current_block, VEC(tree,heap) **p_block_stack)
4103 {
4104   rtx insn;
4105
4106   for (insn = insns; insn; insn = NEXT_INSN (insn))
4107     {
4108       if (NOTE_P (insn))
4109         {
4110           if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
4111             {
4112               tree block = NOTE_BLOCK (insn);
4113               tree origin;
4114
4115               gcc_assert (BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE);
4116               origin = block;
4117
4118               /* If we have seen this block before, that means it now
4119                  spans multiple address regions.  Create a new fragment.  */
4120               if (TREE_ASM_WRITTEN (block))
4121                 {
4122                   tree new_block = copy_node (block);
4123
4124                   BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
4125                   BLOCK_FRAGMENT_CHAIN (new_block)
4126                     = BLOCK_FRAGMENT_CHAIN (origin);
4127                   BLOCK_FRAGMENT_CHAIN (origin) = new_block;
4128
4129                   NOTE_BLOCK (insn) = new_block;
4130                   block = new_block;
4131                 }
4132
4133               BLOCK_SUBBLOCKS (block) = 0;
4134               TREE_ASM_WRITTEN (block) = 1;
4135               /* When there's only one block for the entire function,
4136                  current_block == block and we mustn't do this, it
4137                  will cause infinite recursion.  */
4138               if (block != current_block)
4139                 {
4140                   if (block != origin)
4141                     gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block);
4142
4143                   BLOCK_SUPERCONTEXT (block) = current_block;
4144                   BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4145                   BLOCK_SUBBLOCKS (current_block) = block;
4146                   current_block = origin;
4147                 }
4148               VEC_safe_push (tree, heap, *p_block_stack, block);
4149             }
4150           else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
4151             {
4152               NOTE_BLOCK (insn) = VEC_pop (tree, *p_block_stack);
4153               current_block = BLOCK_SUPERCONTEXT (current_block);
4154             }
4155         }
4156     }
4157 }
4158
4159 /* Reverse the order of elements in the chain T of blocks,
4160    and return the new head of the chain (old last element).  */
4161
4162 tree
4163 blocks_nreverse (tree t)
4164 {
4165   tree prev = 0, block, next;
4166   for (block = t; block; block = next)
4167     {
4168       next = BLOCK_CHAIN (block);
4169       BLOCK_CHAIN (block) = prev;
4170       prev = block;
4171     }
4172   return prev;
4173 }
4174
4175 /* Concatenate two chains of blocks (chained through BLOCK_CHAIN)
4176    by modifying the last node in chain 1 to point to chain 2.  */
4177
4178 tree
4179 block_chainon (tree op1, tree op2)
4180 {
4181   tree t1;
4182
4183   if (!op1)
4184     return op2;
4185   if (!op2)
4186     return op1;
4187
4188   for (t1 = op1; BLOCK_CHAIN (t1); t1 = BLOCK_CHAIN (t1))
4189     continue;
4190   BLOCK_CHAIN (t1) = op2;
4191
4192 #ifdef ENABLE_TREE_CHECKING
4193   {
4194     tree t2;
4195     for (t2 = op2; t2; t2 = BLOCK_CHAIN (t2))
4196       gcc_assert (t2 != t1);
4197   }
4198 #endif
4199
4200   return op1;
4201 }
4202
4203 /* Count the subblocks of the list starting with BLOCK.  If VECTOR is
4204    non-NULL, list them all into VECTOR, in a depth-first preorder
4205    traversal of the block tree.  Also clear TREE_ASM_WRITTEN in all
4206    blocks.  */
4207
4208 static int
4209 all_blocks (tree block, tree *vector)
4210 {
4211   int n_blocks = 0;
4212
4213   while (block)
4214     {
4215       TREE_ASM_WRITTEN (block) = 0;
4216
4217       /* Record this block.  */
4218       if (vector)
4219         vector[n_blocks] = block;
4220
4221       ++n_blocks;
4222
4223       /* Record the subblocks, and their subblocks...  */
4224       n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4225                               vector ? vector + n_blocks : 0);
4226       block = BLOCK_CHAIN (block);
4227     }
4228
4229   return n_blocks;
4230 }
4231
4232 /* Return a vector containing all the blocks rooted at BLOCK.  The
4233    number of elements in the vector is stored in N_BLOCKS_P.  The
4234    vector is dynamically allocated; it is the caller's responsibility
4235    to call `free' on the pointer returned.  */
4236
4237 static tree *
4238 get_block_vector (tree block, int *n_blocks_p)
4239 {
4240   tree *block_vector;
4241
4242   *n_blocks_p = all_blocks (block, NULL);
4243   block_vector = XNEWVEC (tree, *n_blocks_p);
4244   all_blocks (block, block_vector);
4245
4246   return block_vector;
4247 }
4248
4249 static GTY(()) int next_block_index = 2;
4250
4251 /* Set BLOCK_NUMBER for all the blocks in FN.  */
4252
4253 void
4254 number_blocks (tree fn)
4255 {
4256   int i;
4257   int n_blocks;
4258   tree *block_vector;
4259
4260   /* For SDB and XCOFF debugging output, we start numbering the blocks
4261      from 1 within each function, rather than keeping a running
4262      count.  */
4263 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
4264   if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
4265     next_block_index = 1;
4266 #endif
4267
4268   block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4269
4270   /* The top-level BLOCK isn't numbered at all.  */
4271   for (i = 1; i < n_blocks; ++i)
4272     /* We number the blocks from two.  */
4273     BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4274
4275   free (block_vector);
4276
4277   return;
4278 }
4279
4280 /* If VAR is present in a subblock of BLOCK, return the subblock.  */
4281
4282 DEBUG_FUNCTION tree
4283 debug_find_var_in_block_tree (tree var, tree block)
4284 {
4285   tree t;
4286
4287   for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4288     if (t == var)
4289       return block;
4290
4291   for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4292     {
4293       tree ret = debug_find_var_in_block_tree (var, t);
4294       if (ret)
4295         return ret;
4296     }
4297
4298   return NULL_TREE;
4299 }
4300 \f
4301 /* Keep track of whether we're in a dummy function context.  If we are,
4302    we don't want to invoke the set_current_function hook, because we'll
4303    get into trouble if the hook calls target_reinit () recursively or
4304    when the initial initialization is not yet complete.  */
4305
4306 static bool in_dummy_function;
4307
4308 /* Invoke the target hook when setting cfun.  Update the optimization options
4309    if the function uses different options than the default.  */
4310
4311 static void
4312 invoke_set_current_function_hook (tree fndecl)
4313 {
4314   if (!in_dummy_function)
4315     {
4316       tree opts = ((fndecl)
4317                    ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4318                    : optimization_default_node);
4319
4320       if (!opts)
4321         opts = optimization_default_node;
4322
4323       /* Change optimization options if needed.  */
4324       if (optimization_current_node != opts)
4325         {
4326           optimization_current_node = opts;
4327           cl_optimization_restore (&global_options, TREE_OPTIMIZATION (opts));
4328         }
4329
4330       targetm.set_current_function (fndecl);
4331     }
4332 }
4333
4334 /* cfun should never be set directly; use this function.  */
4335
4336 void
4337 set_cfun (struct function *new_cfun)
4338 {
4339   if (cfun != new_cfun)
4340     {
4341       cfun = new_cfun;
4342       invoke_set_current_function_hook (new_cfun ? new_cfun->decl : NULL_TREE);
4343     }
4344 }
4345
4346 /* Initialized with NOGC, making this poisonous to the garbage collector.  */
4347
4348 static VEC(function_p,heap) *cfun_stack;
4349
4350 /* Push the current cfun onto the stack, and set cfun to new_cfun.  */
4351
4352 void
4353 push_cfun (struct function *new_cfun)
4354 {
4355   VEC_safe_push (function_p, heap, cfun_stack, cfun);
4356   set_cfun (new_cfun);
4357 }
4358
4359 /* Pop cfun from the stack.  */
4360
4361 void
4362 pop_cfun (void)
4363 {
4364   struct function *new_cfun = VEC_pop (function_p, cfun_stack);
4365   set_cfun (new_cfun);
4366 }
4367
4368 /* Return value of funcdef and increase it.  */
4369 int
4370 get_next_funcdef_no (void)
4371 {
4372   return funcdef_no++;
4373 }
4374
4375 /* Return value of funcdef.  */
4376 int
4377 get_last_funcdef_no (void)
4378 {
4379   return funcdef_no;
4380 }
4381
4382 /* Allocate a function structure for FNDECL and set its contents
4383    to the defaults.  Set cfun to the newly-allocated object.
4384    Some of the helper functions invoked during initialization assume
4385    that cfun has already been set.  Therefore, assign the new object
4386    directly into cfun and invoke the back end hook explicitly at the
4387    very end, rather than initializing a temporary and calling set_cfun
4388    on it.
4389
4390    ABSTRACT_P is true if this is a function that will never be seen by
4391    the middle-end.  Such functions are front-end concepts (like C++
4392    function templates) that do not correspond directly to functions
4393    placed in object files.  */
4394
4395 void
4396 allocate_struct_function (tree fndecl, bool abstract_p)
4397 {
4398   tree result;
4399   tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4400
4401   cfun = ggc_alloc_cleared_function ();
4402
4403   init_eh_for_function ();
4404
4405   if (init_machine_status)
4406     cfun->machine = (*init_machine_status) ();
4407
4408 #ifdef OVERRIDE_ABI_FORMAT
4409   OVERRIDE_ABI_FORMAT (fndecl);
4410 #endif
4411
4412   invoke_set_current_function_hook (fndecl);
4413
4414   if (fndecl != NULL_TREE)
4415     {
4416       DECL_STRUCT_FUNCTION (fndecl) = cfun;
4417       cfun->decl = fndecl;
4418       current_function_funcdef_no = get_next_funcdef_no ();
4419
4420       result = DECL_RESULT (fndecl);
4421       if (!abstract_p && aggregate_value_p (result, fndecl))
4422         {
4423 #ifdef PCC_STATIC_STRUCT_RETURN
4424           cfun->returns_pcc_struct = 1;
4425 #endif
4426           cfun->returns_struct = 1;
4427         }
4428
4429       cfun->stdarg = stdarg_p (fntype);
4430
4431       /* Assume all registers in stdarg functions need to be saved.  */
4432       cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4433       cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4434
4435       /* ??? This could be set on a per-function basis by the front-end
4436          but is this worth the hassle?  */
4437       cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4438     }
4439 }
4440
4441 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4442    instead of just setting it.  */
4443
4444 void
4445 push_struct_function (tree fndecl)
4446 {
4447   VEC_safe_push (function_p, heap, cfun_stack, cfun);
4448   allocate_struct_function (fndecl, false);
4449 }
4450
4451 /* Reset crtl and other non-struct-function variables to defaults as
4452    appropriate for emitting rtl at the start of a function.  */
4453
4454 static void
4455 prepare_function_start (void)
4456 {
4457   gcc_assert (!crtl->emit.x_last_insn);
4458   init_temp_slots ();
4459   init_emit ();
4460   init_varasm_status ();
4461   init_expr ();
4462   default_rtl_profile ();
4463
4464   if (flag_stack_usage_info)
4465     {
4466       cfun->su = ggc_alloc_cleared_stack_usage ();
4467       cfun->su->static_stack_size = -1;
4468     }
4469
4470   cse_not_expected = ! optimize;
4471
4472   /* Caller save not needed yet.  */
4473   caller_save_needed = 0;
4474
4475   /* We haven't done register allocation yet.  */
4476   reg_renumber = 0;
4477
4478   /* Indicate that we have not instantiated virtual registers yet.  */
4479   virtuals_instantiated = 0;
4480
4481   /* Indicate that we want CONCATs now.  */
4482   generating_concat_p = 1;
4483
4484   /* Indicate we have no need of a frame pointer yet.  */
4485   frame_pointer_needed = 0;
4486 }
4487
4488 /* Initialize the rtl expansion mechanism so that we can do simple things
4489    like generate sequences.  This is used to provide a context during global
4490    initialization of some passes.  You must call expand_dummy_function_end
4491    to exit this context.  */
4492
4493 void
4494 init_dummy_function_start (void)
4495 {
4496   gcc_assert (!in_dummy_function);
4497   in_dummy_function = true;
4498   push_struct_function (NULL_TREE);
4499   prepare_function_start ();
4500 }
4501
4502 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4503    and initialize static variables for generating RTL for the statements
4504    of the function.  */
4505
4506 void
4507 init_function_start (tree subr)
4508 {
4509   if (subr && DECL_STRUCT_FUNCTION (subr))
4510     set_cfun (DECL_STRUCT_FUNCTION (subr));
4511   else
4512     allocate_struct_function (subr, false);
4513   prepare_function_start ();
4514   decide_function_section (subr);
4515
4516   /* Warn if this value is an aggregate type,
4517      regardless of which calling convention we are using for it.  */
4518   if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
4519     warning (OPT_Waggregate_return, "function returns an aggregate");
4520 }
4521
4522 /* Make sure all values used by the optimization passes have sane defaults.  */
4523 unsigned int
4524 init_function_for_compilation (void)
4525 {
4526   reg_renumber = 0;
4527   return 0;
4528 }
4529
4530 struct rtl_opt_pass pass_init_function =
4531 {
4532  {
4533   RTL_PASS,
4534   "*init_function",                     /* name */
4535   NULL,                                 /* gate */
4536   init_function_for_compilation,        /* execute */
4537   NULL,                                 /* sub */
4538   NULL,                                 /* next */
4539   0,                                    /* static_pass_number */
4540   TV_NONE,                              /* tv_id */
4541   0,                                    /* properties_required */
4542   0,                                    /* properties_provided */
4543   0,                                    /* properties_destroyed */
4544   0,                                    /* todo_flags_start */
4545   0                                     /* todo_flags_finish */
4546  }
4547 };
4548
4549
4550 void
4551 expand_main_function (void)
4552 {
4553 #if (defined(INVOKE__main)                              \
4554      || (!defined(HAS_INIT_SECTION)                     \
4555          && !defined(INIT_SECTION_ASM_OP)               \
4556          && !defined(INIT_ARRAY_SECTION_ASM_OP)))
4557   emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
4558 #endif
4559 }
4560 \f
4561 /* Expand code to initialize the stack_protect_guard.  This is invoked at
4562    the beginning of a function to be protected.  */
4563
4564 #ifndef HAVE_stack_protect_set
4565 # define HAVE_stack_protect_set         0
4566 # define gen_stack_protect_set(x,y)     (gcc_unreachable (), NULL_RTX)
4567 #endif
4568
4569 void
4570 stack_protect_prologue (void)
4571 {
4572   tree guard_decl = targetm.stack_protect_guard ();
4573   rtx x, y;
4574
4575   x = expand_normal (crtl->stack_protect_guard);
4576   y = expand_normal (guard_decl);
4577
4578   /* Allow the target to copy from Y to X without leaking Y into a
4579      register.  */
4580   if (HAVE_stack_protect_set)
4581     {
4582       rtx insn = gen_stack_protect_set (x, y);
4583       if (insn)
4584         {
4585           emit_insn (insn);
4586           return;
4587         }
4588     }
4589
4590   /* Otherwise do a straight move.  */
4591   emit_move_insn (x, y);
4592 }
4593
4594 /* Expand code to verify the stack_protect_guard.  This is invoked at
4595    the end of a function to be protected.  */
4596
4597 #ifndef HAVE_stack_protect_test
4598 # define HAVE_stack_protect_test                0
4599 # define gen_stack_protect_test(x, y, z)        (gcc_unreachable (), NULL_RTX)
4600 #endif
4601
4602 void
4603 stack_protect_epilogue (void)
4604 {
4605   tree guard_decl = targetm.stack_protect_guard ();
4606   rtx label = gen_label_rtx ();
4607   rtx x, y, tmp;
4608
4609   x = expand_normal (crtl->stack_protect_guard);
4610   y = expand_normal (guard_decl);
4611
4612   /* Allow the target to compare Y with X without leaking either into
4613      a register.  */
4614   switch (HAVE_stack_protect_test != 0)
4615     {
4616     case 1:
4617       tmp = gen_stack_protect_test (x, y, label);
4618       if (tmp)
4619         {
4620           emit_insn (tmp);
4621           break;
4622         }
4623       /* FALLTHRU */
4624
4625     default:
4626       emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
4627       break;
4628     }
4629
4630   /* The noreturn predictor has been moved to the tree level.  The rtl-level
4631      predictors estimate this branch about 20%, which isn't enough to get
4632      things moved out of line.  Since this is the only extant case of adding
4633      a noreturn function at the rtl level, it doesn't seem worth doing ought
4634      except adding the prediction by hand.  */
4635   tmp = get_last_insn ();
4636   if (JUMP_P (tmp))
4637     predict_insn_def (tmp, PRED_NORETURN, TAKEN);
4638
4639   expand_expr_stmt (targetm.stack_protect_fail ());
4640   emit_label (label);
4641 }
4642 \f
4643 /* Start the RTL for a new function, and set variables used for
4644    emitting RTL.
4645    SUBR is the FUNCTION_DECL node.
4646    PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
4647    the function's parameters, which must be run at any return statement.  */
4648
4649 void
4650 expand_function_start (tree subr)
4651 {
4652   /* Make sure volatile mem refs aren't considered
4653      valid operands of arithmetic insns.  */
4654   init_recog_no_volatile ();
4655
4656   crtl->profile
4657     = (profile_flag
4658        && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
4659
4660   crtl->limit_stack
4661     = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
4662
4663   /* Make the label for return statements to jump to.  Do not special
4664      case machines with special return instructions -- they will be
4665      handled later during jump, ifcvt, or epilogue creation.  */
4666   return_label = gen_label_rtx ();
4667
4668   /* Initialize rtx used to return the value.  */
4669   /* Do this before assign_parms so that we copy the struct value address
4670      before any library calls that assign parms might generate.  */
4671
4672   /* Decide whether to return the value in memory or in a register.  */
4673   if (aggregate_value_p (DECL_RESULT (subr), subr))
4674     {
4675       /* Returning something that won't go in a register.  */
4676       rtx value_address = 0;
4677
4678 #ifdef PCC_STATIC_STRUCT_RETURN
4679       if (cfun->returns_pcc_struct)
4680         {
4681           int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
4682           value_address = assemble_static_space (size);
4683         }
4684       else
4685 #endif
4686         {
4687           rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
4688           /* Expect to be passed the address of a place to store the value.
4689              If it is passed as an argument, assign_parms will take care of
4690              it.  */
4691           if (sv)
4692             {
4693               value_address = gen_reg_rtx (Pmode);
4694               emit_move_insn (value_address, sv);
4695             }
4696         }
4697       if (value_address)
4698         {
4699           rtx x = value_address;
4700           if (!DECL_BY_REFERENCE (DECL_RESULT (subr)))
4701             {
4702               x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), x);
4703               set_mem_attributes (x, DECL_RESULT (subr), 1);
4704             }
4705           SET_DECL_RTL (DECL_RESULT (subr), x);
4706         }
4707     }
4708   else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
4709     /* If return mode is void, this decl rtl should not be used.  */
4710     SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
4711   else
4712     {
4713       /* Compute the return values into a pseudo reg, which we will copy
4714          into the true return register after the cleanups are done.  */
4715       tree return_type = TREE_TYPE (DECL_RESULT (subr));
4716       if (TYPE_MODE (return_type) != BLKmode
4717           && targetm.calls.return_in_msb (return_type))
4718         /* expand_function_end will insert the appropriate padding in
4719            this case.  Use the return value's natural (unpadded) mode
4720            within the function proper.  */
4721         SET_DECL_RTL (DECL_RESULT (subr),
4722                       gen_reg_rtx (TYPE_MODE (return_type)));
4723       else
4724         {
4725           /* In order to figure out what mode to use for the pseudo, we
4726              figure out what the mode of the eventual return register will
4727              actually be, and use that.  */
4728           rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
4729
4730           /* Structures that are returned in registers are not
4731              aggregate_value_p, so we may see a PARALLEL or a REG.  */
4732           if (REG_P (hard_reg))
4733             SET_DECL_RTL (DECL_RESULT (subr),
4734                           gen_reg_rtx (GET_MODE (hard_reg)));
4735           else
4736             {
4737               gcc_assert (GET_CODE (hard_reg) == PARALLEL);
4738               SET_DECL_RTL (DECL_RESULT (subr), gen_group_rtx (hard_reg));
4739             }
4740         }
4741
4742       /* Set DECL_REGISTER flag so that expand_function_end will copy the
4743          result to the real return register(s).  */
4744       DECL_REGISTER (DECL_RESULT (subr)) = 1;
4745     }
4746
4747   /* Initialize rtx for parameters and local variables.
4748      In some cases this requires emitting insns.  */
4749   assign_parms (subr);
4750
4751   /* If function gets a static chain arg, store it.  */
4752   if (cfun->static_chain_decl)
4753     {
4754       tree parm = cfun->static_chain_decl;
4755       rtx local, chain, insn;
4756
4757       local = gen_reg_rtx (Pmode);
4758       chain = targetm.calls.static_chain (current_function_decl, true);
4759
4760       set_decl_incoming_rtl (parm, chain, false);
4761       SET_DECL_RTL (parm, local);
4762       mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4763
4764       insn = emit_move_insn (local, chain);
4765
4766       /* Mark the register as eliminable, similar to parameters.  */
4767       if (MEM_P (chain)
4768           && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
4769         set_unique_reg_note (insn, REG_EQUIV, chain);
4770     }
4771
4772   /* If the function receives a non-local goto, then store the
4773      bits we need to restore the frame pointer.  */
4774   if (cfun->nonlocal_goto_save_area)
4775     {
4776       tree t_save;
4777       rtx r_save;
4778
4779       /* ??? We need to do this save early.  Unfortunately here is
4780          before the frame variable gets declared.  Help out...  */
4781       tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
4782       if (!DECL_RTL_SET_P (var))
4783         expand_decl (var);
4784
4785       t_save = build4 (ARRAY_REF,
4786                        TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
4787                        cfun->nonlocal_goto_save_area,
4788                        integer_zero_node, NULL_TREE, NULL_TREE);
4789       r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
4790       gcc_assert (GET_MODE (r_save) == Pmode);
4791
4792       emit_move_insn (r_save, targetm.builtin_setjmp_frame_value ());
4793       update_nonlocal_goto_save_area ();
4794     }
4795
4796   /* The following was moved from init_function_start.
4797      The move is supposed to make sdb output more accurate.  */
4798   /* Indicate the beginning of the function body,
4799      as opposed to parm setup.  */
4800   emit_note (NOTE_INSN_FUNCTION_BEG);
4801
4802   gcc_assert (NOTE_P (get_last_insn ()));
4803
4804   parm_birth_insn = get_last_insn ();
4805
4806   if (crtl->profile)
4807     {
4808 #ifdef PROFILE_HOOK
4809       PROFILE_HOOK (current_function_funcdef_no);
4810 #endif
4811     }
4812
4813   /* If we are doing generic stack checking, the probe should go here.  */
4814   if (flag_stack_check == GENERIC_STACK_CHECK)
4815     stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
4816
4817   /* Make sure there is a line number after the function entry setup code.  */
4818   force_next_line_note ();
4819 }
4820 \f
4821 /* Undo the effects of init_dummy_function_start.  */
4822 void
4823 expand_dummy_function_end (void)
4824 {
4825   gcc_assert (in_dummy_function);
4826
4827   /* End any sequences that failed to be closed due to syntax errors.  */
4828   while (in_sequence_p ())
4829     end_sequence ();
4830
4831   /* Outside function body, can't compute type's actual size
4832      until next function's body starts.  */
4833
4834   free_after_parsing (cfun);
4835   free_after_compilation (cfun);
4836   pop_cfun ();
4837   in_dummy_function = false;
4838 }
4839
4840 /* Call DOIT for each hard register used as a return value from
4841    the current function.  */
4842
4843 void
4844 diddle_return_value (void (*doit) (rtx, void *), void *arg)
4845 {
4846   rtx outgoing = crtl->return_rtx;
4847
4848   if (! outgoing)
4849     return;
4850
4851   if (REG_P (outgoing))
4852     (*doit) (outgoing, arg);
4853   else if (GET_CODE (outgoing) == PARALLEL)
4854     {
4855       int i;
4856
4857       for (i = 0; i < XVECLEN (outgoing, 0); i++)
4858         {
4859           rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
4860
4861           if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
4862             (*doit) (x, arg);
4863         }
4864     }
4865 }
4866
4867 static void
4868 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4869 {
4870   emit_clobber (reg);
4871 }
4872
4873 void
4874 clobber_return_register (void)
4875 {
4876   diddle_return_value (do_clobber_return_reg, NULL);
4877
4878   /* In case we do use pseudo to return value, clobber it too.  */
4879   if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
4880     {
4881       tree decl_result = DECL_RESULT (current_function_decl);
4882       rtx decl_rtl = DECL_RTL (decl_result);
4883       if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
4884         {
4885           do_clobber_return_reg (decl_rtl, NULL);
4886         }
4887     }
4888 }
4889
4890 static void
4891 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4892 {
4893   emit_use (reg);
4894 }
4895
4896 static void
4897 use_return_register (void)
4898 {
4899   diddle_return_value (do_use_return_reg, NULL);
4900 }
4901
4902 /* Possibly warn about unused parameters.  */
4903 void
4904 do_warn_unused_parameter (tree fn)
4905 {
4906   tree decl;
4907
4908   for (decl = DECL_ARGUMENTS (fn);
4909        decl; decl = DECL_CHAIN (decl))
4910     if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
4911         && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
4912         && !TREE_NO_WARNING (decl))
4913       warning (OPT_Wunused_parameter, "unused parameter %q+D", decl);
4914 }
4915
4916 static GTY(()) rtx initial_trampoline;
4917
4918 /* Generate RTL for the end of the current function.  */
4919
4920 void
4921 expand_function_end (void)
4922 {
4923   rtx clobber_after;
4924
4925   /* If arg_pointer_save_area was referenced only from a nested
4926      function, we will not have initialized it yet.  Do that now.  */
4927   if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
4928     get_arg_pointer_save_area ();
4929
4930   /* If we are doing generic stack checking and this function makes calls,
4931      do a stack probe at the start of the function to ensure we have enough
4932      space for another stack frame.  */
4933   if (flag_stack_check == GENERIC_STACK_CHECK)
4934     {
4935       rtx insn, seq;
4936
4937       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4938         if (CALL_P (insn))
4939           {
4940             rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
4941             start_sequence ();
4942             if (STACK_CHECK_MOVING_SP)
4943               anti_adjust_stack_and_probe (max_frame_size, true);
4944             else
4945               probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
4946             seq = get_insns ();
4947             end_sequence ();
4948             set_insn_locators (seq, prologue_locator);
4949             emit_insn_before (seq, stack_check_probe_note);
4950             break;
4951           }
4952     }
4953
4954   /* End any sequences that failed to be closed due to syntax errors.  */
4955   while (in_sequence_p ())
4956     end_sequence ();
4957
4958   clear_pending_stack_adjust ();
4959   do_pending_stack_adjust ();
4960
4961   /* Output a linenumber for the end of the function.
4962      SDB depends on this.  */
4963   force_next_line_note ();
4964   set_curr_insn_source_location (input_location);
4965
4966   /* Before the return label (if any), clobber the return
4967      registers so that they are not propagated live to the rest of
4968      the function.  This can only happen with functions that drop
4969      through; if there had been a return statement, there would
4970      have either been a return rtx, or a jump to the return label.
4971
4972      We delay actual code generation after the current_function_value_rtx
4973      is computed.  */
4974   clobber_after = get_last_insn ();
4975
4976   /* Output the label for the actual return from the function.  */
4977   emit_label (return_label);
4978
4979   if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
4980     {
4981       /* Let except.c know where it should emit the call to unregister
4982          the function context for sjlj exceptions.  */
4983       if (flag_exceptions)
4984         sjlj_emit_function_exit_after (get_last_insn ());
4985     }
4986   else
4987     {
4988       /* We want to ensure that instructions that may trap are not
4989          moved into the epilogue by scheduling, because we don't
4990          always emit unwind information for the epilogue.  */
4991       if (cfun->can_throw_non_call_exceptions)
4992         emit_insn (gen_blockage ());
4993     }
4994
4995   /* If this is an implementation of throw, do what's necessary to
4996      communicate between __builtin_eh_return and the epilogue.  */
4997   expand_eh_return ();
4998
4999   /* If scalar return value was computed in a pseudo-reg, or was a named
5000      return value that got dumped to the stack, copy that to the hard
5001      return register.  */
5002   if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5003     {
5004       tree decl_result = DECL_RESULT (current_function_decl);
5005       rtx decl_rtl = DECL_RTL (decl_result);
5006
5007       if (REG_P (decl_rtl)
5008           ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5009           : DECL_REGISTER (decl_result))
5010         {
5011           rtx real_decl_rtl = crtl->return_rtx;
5012
5013           /* This should be set in assign_parms.  */
5014           gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
5015
5016           /* If this is a BLKmode structure being returned in registers,
5017              then use the mode computed in expand_return.  Note that if
5018              decl_rtl is memory, then its mode may have been changed,
5019              but that crtl->return_rtx has not.  */
5020           if (GET_MODE (real_decl_rtl) == BLKmode)
5021             PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
5022
5023           /* If a non-BLKmode return value should be padded at the least
5024              significant end of the register, shift it left by the appropriate
5025              amount.  BLKmode results are handled using the group load/store
5026              machinery.  */
5027           if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
5028               && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
5029             {
5030               emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
5031                                            REGNO (real_decl_rtl)),
5032                               decl_rtl);
5033               shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
5034             }
5035           /* If a named return value dumped decl_return to memory, then
5036              we may need to re-do the PROMOTE_MODE signed/unsigned
5037              extension.  */
5038           else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
5039             {
5040               int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
5041               promote_function_mode (TREE_TYPE (decl_result),
5042                                      GET_MODE (decl_rtl), &unsignedp,
5043                                      TREE_TYPE (current_function_decl), 1);
5044
5045               convert_move (real_decl_rtl, decl_rtl, unsignedp);
5046             }
5047           else if (GET_CODE (real_decl_rtl) == PARALLEL)
5048             {
5049               /* If expand_function_start has created a PARALLEL for decl_rtl,
5050                  move the result to the real return registers.  Otherwise, do
5051                  a group load from decl_rtl for a named return.  */
5052               if (GET_CODE (decl_rtl) == PARALLEL)
5053                 emit_group_move (real_decl_rtl, decl_rtl);
5054               else
5055                 emit_group_load (real_decl_rtl, decl_rtl,
5056                                  TREE_TYPE (decl_result),
5057                                  int_size_in_bytes (TREE_TYPE (decl_result)));
5058             }
5059           /* In the case of complex integer modes smaller than a word, we'll
5060              need to generate some non-trivial bitfield insertions.  Do that
5061              on a pseudo and not the hard register.  */
5062           else if (GET_CODE (decl_rtl) == CONCAT
5063                    && GET_MODE_CLASS (GET_MODE (decl_rtl)) == MODE_COMPLEX_INT
5064                    && GET_MODE_BITSIZE (GET_MODE (decl_rtl)) <= BITS_PER_WORD)
5065             {
5066               int old_generating_concat_p;
5067               rtx tmp;
5068
5069               old_generating_concat_p = generating_concat_p;
5070               generating_concat_p = 0;
5071               tmp = gen_reg_rtx (GET_MODE (decl_rtl));
5072               generating_concat_p = old_generating_concat_p;
5073
5074               emit_move_insn (tmp, decl_rtl);
5075               emit_move_insn (real_decl_rtl, tmp);
5076             }
5077           else
5078             emit_move_insn (real_decl_rtl, decl_rtl);
5079         }
5080     }
5081
5082   /* If returning a structure, arrange to return the address of the value
5083      in a place where debuggers expect to find it.
5084
5085      If returning a structure PCC style,
5086      the caller also depends on this value.
5087      And cfun->returns_pcc_struct is not necessarily set.  */
5088   if (cfun->returns_struct
5089       || cfun->returns_pcc_struct)
5090     {
5091       rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
5092       tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5093       rtx outgoing;
5094
5095       if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
5096         type = TREE_TYPE (type);
5097       else
5098         value_address = XEXP (value_address, 0);
5099
5100       outgoing = targetm.calls.function_value (build_pointer_type (type),
5101                                                current_function_decl, true);
5102
5103       /* Mark this as a function return value so integrate will delete the
5104          assignment and USE below when inlining this function.  */
5105       REG_FUNCTION_VALUE_P (outgoing) = 1;
5106
5107       /* The address may be ptr_mode and OUTGOING may be Pmode.  */
5108       value_address = convert_memory_address (GET_MODE (outgoing),
5109                                               value_address);
5110
5111       emit_move_insn (outgoing, value_address);
5112
5113       /* Show return register used to hold result (in this case the address
5114          of the result.  */
5115       crtl->return_rtx = outgoing;
5116     }
5117
5118   /* Emit the actual code to clobber return register.  */
5119   {
5120     rtx seq;
5121
5122     start_sequence ();
5123     clobber_return_register ();
5124     seq = get_insns ();
5125     end_sequence ();
5126
5127     emit_insn_after (seq, clobber_after);
5128   }
5129
5130   /* Output the label for the naked return from the function.  */
5131   if (naked_return_label)
5132     emit_label (naked_return_label);
5133
5134   /* @@@ This is a kludge.  We want to ensure that instructions that
5135      may trap are not moved into the epilogue by scheduling, because
5136      we don't always emit unwind information for the epilogue.  */
5137   if (cfun->can_throw_non_call_exceptions
5138       && targetm_common.except_unwind_info (&global_options) != UI_SJLJ)
5139     emit_insn (gen_blockage ());
5140
5141   /* If stack protection is enabled for this function, check the guard.  */
5142   if (crtl->stack_protect_guard)
5143     stack_protect_epilogue ();
5144
5145   /* If we had calls to alloca, and this machine needs
5146      an accurate stack pointer to exit the function,
5147      insert some code to save and restore the stack pointer.  */
5148   if (! EXIT_IGNORE_STACK
5149       && cfun->calls_alloca)
5150     {
5151       rtx tem = 0, seq;
5152
5153       start_sequence ();
5154       emit_stack_save (SAVE_FUNCTION, &tem);
5155       seq = get_insns ();
5156       end_sequence ();
5157       emit_insn_before (seq, parm_birth_insn);
5158
5159       emit_stack_restore (SAVE_FUNCTION, tem);
5160     }
5161
5162   /* ??? This should no longer be necessary since stupid is no longer with
5163      us, but there are some parts of the compiler (eg reload_combine, and
5164      sh mach_dep_reorg) that still try and compute their own lifetime info
5165      instead of using the general framework.  */
5166   use_return_register ();
5167 }
5168
5169 rtx
5170 get_arg_pointer_save_area (void)
5171 {
5172   rtx ret = arg_pointer_save_area;
5173
5174   if (! ret)
5175     {
5176       ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5177       arg_pointer_save_area = ret;
5178     }
5179
5180   if (! crtl->arg_pointer_save_area_init)
5181     {
5182       rtx seq;
5183
5184       /* Save the arg pointer at the beginning of the function.  The
5185          generated stack slot may not be a valid memory address, so we
5186          have to check it and fix it if necessary.  */
5187       start_sequence ();
5188       emit_move_insn (validize_mem (ret),
5189                       crtl->args.internal_arg_pointer);
5190       seq = get_insns ();
5191       end_sequence ();
5192
5193       push_topmost_sequence ();
5194       emit_insn_after (seq, entry_of_function ());
5195       pop_topmost_sequence ();
5196
5197       crtl->arg_pointer_save_area_init = true;
5198     }
5199
5200   return ret;
5201 }
5202 \f
5203 /* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
5204    for the first time.  */
5205
5206 static void
5207 record_insns (rtx insns, rtx end, htab_t *hashp)
5208 {
5209   rtx tmp;
5210   htab_t hash = *hashp;
5211
5212   if (hash == NULL)
5213     *hashp = hash
5214       = htab_create_ggc (17, htab_hash_pointer, htab_eq_pointer, NULL);
5215
5216   for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5217     {
5218       void **slot = htab_find_slot (hash, tmp, INSERT);
5219       gcc_assert (*slot == NULL);
5220       *slot = tmp;
5221     }
5222 }
5223
5224 /* INSN has been duplicated or replaced by as COPY, perhaps by duplicating a
5225    basic block, splitting or peepholes.  If INSN is a prologue or epilogue
5226    insn, then record COPY as well.  */
5227
5228 void
5229 maybe_copy_prologue_epilogue_insn (rtx insn, rtx copy)
5230 {
5231   htab_t hash;
5232   void **slot;
5233
5234   hash = epilogue_insn_hash;
5235   if (!hash || !htab_find (hash, insn))
5236     {
5237       hash = prologue_insn_hash;
5238       if (!hash || !htab_find (hash, insn))
5239         return;
5240     }
5241
5242   slot = htab_find_slot (hash, copy, INSERT);
5243   gcc_assert (*slot == NULL);
5244   *slot = copy;
5245 }
5246
5247 /* Set the locator of the insn chain starting at INSN to LOC.  */
5248 static void
5249 set_insn_locators (rtx insn, int loc)
5250 {
5251   while (insn != NULL_RTX)
5252     {
5253       if (INSN_P (insn))
5254         INSN_LOCATOR (insn) = loc;
5255       insn = NEXT_INSN (insn);
5256     }
5257 }
5258
5259 /* Determine if any INSNs in HASH are, or are part of, INSN.  Because
5260    we can be running after reorg, SEQUENCE rtl is possible.  */
5261
5262 static bool
5263 contains (const_rtx insn, htab_t hash)
5264 {
5265   if (hash == NULL)
5266     return false;
5267
5268   if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5269     {
5270       int i;
5271       for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
5272         if (htab_find (hash, XVECEXP (PATTERN (insn), 0, i)))
5273           return true;
5274       return false;
5275     }
5276
5277   return htab_find (hash, insn) != NULL;
5278 }
5279
5280 int
5281 prologue_epilogue_contains (const_rtx insn)
5282 {
5283   if (contains (insn, prologue_insn_hash))
5284     return 1;
5285   if (contains (insn, epilogue_insn_hash))
5286     return 1;
5287   return 0;
5288 }
5289
5290 #ifdef HAVE_simple_return
5291
5292 /* A for_each_rtx subroutine of record_hard_reg_sets.  */
5293 static int
5294 record_hard_reg_uses_1 (rtx *px, void *data)
5295 {
5296   rtx x = *px;
5297   HARD_REG_SET *pused = (HARD_REG_SET *)data;
5298
5299   if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
5300     add_to_hard_reg_set (pused, GET_MODE (x), REGNO (x));
5301   return 0;
5302 }
5303
5304 /* Like record_hard_reg_sets, but called through note_uses.  */
5305 static void
5306 record_hard_reg_uses (rtx *px, void *data)
5307 {
5308   for_each_rtx (px, record_hard_reg_uses_1, data);
5309 }
5310
5311 /* Return true if INSN requires the stack frame to be set up.
5312    PROLOGUE_USED contains the hard registers used in the function
5313    prologue.  SET_UP_BY_PROLOGUE is the set of registers we expect the
5314    prologue to set up for the function.  */
5315 static bool
5316 requires_stack_frame_p (rtx insn, HARD_REG_SET prologue_used,
5317                         HARD_REG_SET set_up_by_prologue)
5318 {
5319   df_ref *df_rec;
5320   HARD_REG_SET hardregs;
5321   unsigned regno;
5322
5323   if (!INSN_P (insn) || DEBUG_INSN_P (insn))
5324     return false;
5325   if (CALL_P (insn))
5326     return !SIBLING_CALL_P (insn);
5327
5328   CLEAR_HARD_REG_SET (hardregs);
5329   for (df_rec = DF_INSN_DEFS (insn); *df_rec; df_rec++)
5330     {
5331       rtx dreg = DF_REF_REG (*df_rec);
5332
5333       if (!REG_P (dreg))
5334         continue;
5335
5336       add_to_hard_reg_set (&hardregs, GET_MODE (dreg),
5337                            REGNO (dreg));
5338     }
5339   if (hard_reg_set_intersect_p (hardregs, prologue_used))
5340     return true;
5341   AND_COMPL_HARD_REG_SET (hardregs, call_used_reg_set);
5342   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5343     if (TEST_HARD_REG_BIT (hardregs, regno)
5344         && df_regs_ever_live_p (regno))
5345       return true;
5346
5347   for (df_rec = DF_INSN_USES (insn); *df_rec; df_rec++)
5348     {
5349       rtx reg = DF_REF_REG (*df_rec);
5350
5351       if (!REG_P (reg))
5352         continue;
5353
5354       add_to_hard_reg_set (&hardregs, GET_MODE (reg),
5355                            REGNO (reg));
5356     }
5357   if (hard_reg_set_intersect_p (hardregs, set_up_by_prologue))
5358     return true;
5359
5360   return false;
5361 }
5362
5363 /* Look for sets of call-saved registers in the first block of the
5364    function, and move them down into successor blocks if the register
5365    is used only on one path.  This exposes more opportunities for
5366    shrink-wrapping.
5367    These kinds of sets often occur when incoming argument registers are
5368    moved to call-saved registers because their values are live across
5369    one or more calls during the function.  */
5370
5371 static void
5372 prepare_shrink_wrap (basic_block entry_block)
5373 {
5374   rtx insn, curr;
5375   FOR_BB_INSNS_SAFE (entry_block, insn, curr)
5376     {
5377       basic_block next_bb;
5378       edge e, live_edge;
5379       edge_iterator ei;
5380       rtx set, scan;
5381       unsigned destreg, srcreg;
5382
5383       if (!NONDEBUG_INSN_P (insn))
5384         continue;
5385       set = single_set (insn);
5386       if (!set)
5387         continue;
5388
5389       if (!REG_P (SET_SRC (set)) || !REG_P (SET_DEST (set)))
5390         continue;
5391       srcreg = REGNO (SET_SRC (set));
5392       destreg = REGNO (SET_DEST (set));
5393       if (hard_regno_nregs[srcreg][GET_MODE (SET_SRC (set))] > 1
5394           || hard_regno_nregs[destreg][GET_MODE (SET_DEST (set))] > 1)
5395         continue;
5396
5397       next_bb = entry_block;
5398       scan = insn;
5399
5400       for (;;)
5401         {
5402           live_edge = NULL;
5403           /* Try to find a single edge across which the register is live.
5404              If we find one, we'll try to move the set across this edge.  */
5405           FOR_EACH_EDGE (e, ei, next_bb->succs)
5406             {
5407               if (REGNO_REG_SET_P (df_get_live_in (e->dest), destreg))
5408                 {
5409                   if (live_edge)
5410                     {
5411                       live_edge = NULL;
5412                       break;
5413                     }
5414                   live_edge = e;
5415                 }
5416             }
5417           if (!live_edge)
5418             break;
5419           /* We can sometimes encounter dead code.  Don't try to move it
5420              into the exit block.  */
5421           if (live_edge->dest == EXIT_BLOCK_PTR)
5422             break;
5423           if (EDGE_COUNT (live_edge->dest->preds) > 1)
5424             break;
5425           while (scan != BB_END (next_bb))
5426             {
5427               scan = NEXT_INSN (scan);
5428               if (NONDEBUG_INSN_P (scan))
5429                 {
5430                   rtx link;
5431                   HARD_REG_SET set_regs;
5432
5433                   CLEAR_HARD_REG_SET (set_regs);
5434                   note_stores (PATTERN (scan), record_hard_reg_sets,
5435                                &set_regs);
5436                   if (CALL_P (scan))
5437                     IOR_HARD_REG_SET (set_regs, call_used_reg_set);
5438                   for (link = REG_NOTES (scan); link; link = XEXP (link, 1))
5439                     if (REG_NOTE_KIND (link) == REG_INC)
5440                       record_hard_reg_sets (XEXP (link, 0), NULL, &set_regs);
5441
5442                   if (TEST_HARD_REG_BIT (set_regs, srcreg)
5443                       || reg_referenced_p (SET_DEST (set),
5444                                            PATTERN (scan)))
5445                     {
5446                       scan = NULL_RTX;
5447                       break;
5448                     }
5449                   if (CALL_P (scan))
5450                     {
5451                       rtx link = CALL_INSN_FUNCTION_USAGE (scan);
5452                       while (link)
5453                         {
5454                           rtx tmp = XEXP (link, 0);
5455                           if (GET_CODE (tmp) == USE
5456                               && reg_referenced_p (SET_DEST (set), tmp))
5457                             break;
5458                           link = XEXP (link, 1);
5459                         }
5460                       if (link)
5461                         {
5462                           scan = NULL_RTX;
5463                           break;
5464                         }
5465                     }
5466                 }
5467             }
5468           if (!scan)
5469             break;
5470           next_bb = live_edge->dest;
5471         }
5472
5473       if (next_bb != entry_block)
5474         {
5475           rtx after = BB_HEAD (next_bb);
5476           while (!NOTE_P (after)
5477                  || NOTE_KIND (after) != NOTE_INSN_BASIC_BLOCK)
5478             after = NEXT_INSN (after);
5479           emit_insn_after (PATTERN (insn), after);
5480           delete_insn (insn);
5481         }
5482     }
5483 }
5484
5485 #endif
5486
5487 #ifdef HAVE_return
5488 /* Insert use of return register before the end of BB.  */
5489
5490 static void
5491 emit_use_return_register_into_block (basic_block bb)
5492 {
5493   rtx seq;
5494   start_sequence ();
5495   use_return_register ();
5496   seq = get_insns ();
5497   end_sequence ();
5498   emit_insn_before (seq, BB_END (bb));
5499 }
5500
5501
5502 /* Create a return pattern, either simple_return or return, depending on
5503    simple_p.  */
5504
5505 static rtx
5506 gen_return_pattern (bool simple_p)
5507 {
5508 #ifdef HAVE_simple_return
5509   return simple_p ? gen_simple_return () : gen_return ();
5510 #else
5511   gcc_assert (!simple_p);
5512   return gen_return ();
5513 #endif
5514 }
5515
5516 /* Insert an appropriate return pattern at the end of block BB.  This
5517    also means updating block_for_insn appropriately.  SIMPLE_P is
5518    the same as in gen_return_pattern and passed to it.  */
5519
5520 static void
5521 emit_return_into_block (bool simple_p, basic_block bb)
5522 {
5523   rtx jump, pat;
5524   jump = emit_jump_insn_after (gen_return_pattern (simple_p), BB_END (bb));
5525   pat = PATTERN (jump);
5526   if (GET_CODE (pat) == PARALLEL)
5527     pat = XVECEXP (pat, 0, 0);
5528   gcc_assert (ANY_RETURN_P (pat));
5529   JUMP_LABEL (jump) = pat;
5530 }
5531 #endif
5532
5533 /* Set JUMP_LABEL for a return insn.  */
5534
5535 void
5536 set_return_jump_label (rtx returnjump)
5537 {
5538   rtx pat = PATTERN (returnjump);
5539   if (GET_CODE (pat) == PARALLEL)
5540     pat = XVECEXP (pat, 0, 0);
5541   if (ANY_RETURN_P (pat))
5542     JUMP_LABEL (returnjump) = pat;
5543   else
5544     JUMP_LABEL (returnjump) = ret_rtx;
5545 }
5546
5547 /* Return true if BB has any active insns.  */
5548 static bool
5549 bb_active_p (basic_block bb)
5550 {
5551   rtx label;
5552
5553   /* Test whether there are active instructions in BB.  */
5554   label = BB_END (bb);
5555   while (label && !LABEL_P (label))
5556     {
5557       if (active_insn_p (label))
5558         break;
5559       label = PREV_INSN (label);
5560     }
5561   return BB_HEAD (bb) != label || !LABEL_P (label);
5562 }
5563
5564 /* Generate the prologue and epilogue RTL if the machine supports it.  Thread
5565    this into place with notes indicating where the prologue ends and where
5566    the epilogue begins.  Update the basic block information when possible.
5567
5568    Notes on epilogue placement:
5569    There are several kinds of edges to the exit block:
5570    * a single fallthru edge from LAST_BB
5571    * possibly, edges from blocks containing sibcalls
5572    * possibly, fake edges from infinite loops
5573
5574    The epilogue is always emitted on the fallthru edge from the last basic
5575    block in the function, LAST_BB, into the exit block.
5576
5577    If LAST_BB is empty except for a label, it is the target of every
5578    other basic block in the function that ends in a return.  If a
5579    target has a return or simple_return pattern (possibly with
5580    conditional variants), these basic blocks can be changed so that a
5581    return insn is emitted into them, and their target is adjusted to
5582    the real exit block.
5583
5584    Notes on shrink wrapping: We implement a fairly conservative
5585    version of shrink-wrapping rather than the textbook one.  We only
5586    generate a single prologue and a single epilogue.  This is
5587    sufficient to catch a number of interesting cases involving early
5588    exits.
5589
5590    First, we identify the blocks that require the prologue to occur before
5591    them.  These are the ones that modify a call-saved register, or reference
5592    any of the stack or frame pointer registers.  To simplify things, we then
5593    mark everything reachable from these blocks as also requiring a prologue.
5594    This takes care of loops automatically, and avoids the need to examine
5595    whether MEMs reference the frame, since it is sufficient to check for
5596    occurrences of the stack or frame pointer.
5597
5598    We then compute the set of blocks for which the need for a prologue
5599    is anticipatable (borrowing terminology from the shrink-wrapping
5600    description in Muchnick's book).  These are the blocks which either
5601    require a prologue themselves, or those that have only successors
5602    where the prologue is anticipatable.  The prologue needs to be
5603    inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
5604    is not.  For the moment, we ensure that only one such edge exists.
5605
5606    The epilogue is placed as described above, but we make a
5607    distinction between inserting return and simple_return patterns
5608    when modifying other blocks that end in a return.  Blocks that end
5609    in a sibcall omit the sibcall_epilogue if the block is not in
5610    ANTIC.  */
5611
5612 static void
5613 thread_prologue_and_epilogue_insns (void)
5614 {
5615   bool inserted;
5616   basic_block last_bb;
5617   bool last_bb_active ATTRIBUTE_UNUSED;
5618 #ifdef HAVE_simple_return
5619   VEC (rtx, heap) *unconverted_simple_returns = NULL;
5620   basic_block simple_return_block_hot = NULL;
5621   basic_block simple_return_block_cold = NULL;
5622   bool nonempty_prologue;
5623 #endif
5624   rtx returnjump ATTRIBUTE_UNUSED;
5625   rtx seq ATTRIBUTE_UNUSED, epilogue_end ATTRIBUTE_UNUSED;
5626   rtx prologue_seq ATTRIBUTE_UNUSED, split_prologue_seq ATTRIBUTE_UNUSED;
5627   edge e, entry_edge, orig_entry_edge, exit_fallthru_edge;
5628   edge_iterator ei;
5629   bitmap_head bb_flags;
5630
5631   df_analyze ();
5632
5633   rtl_profile_for_bb (ENTRY_BLOCK_PTR);
5634
5635   inserted = false;
5636   seq = NULL_RTX;
5637   epilogue_end = NULL_RTX;
5638   returnjump = NULL_RTX;
5639
5640   /* Can't deal with multiple successors of the entry block at the
5641      moment.  Function should always have at least one entry
5642      point.  */
5643   gcc_assert (single_succ_p (ENTRY_BLOCK_PTR));
5644   entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
5645   orig_entry_edge = entry_edge;
5646
5647   exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR->preds);
5648   if (exit_fallthru_edge != NULL)
5649     {
5650       last_bb = exit_fallthru_edge->src;
5651       last_bb_active = bb_active_p (last_bb);
5652     }
5653   else
5654     {
5655       last_bb = NULL;
5656       last_bb_active = false;
5657     }
5658
5659   split_prologue_seq = NULL_RTX;
5660   if (flag_split_stack
5661       && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
5662           == NULL))
5663     {
5664 #ifndef HAVE_split_stack_prologue
5665       gcc_unreachable ();
5666 #else
5667       gcc_assert (HAVE_split_stack_prologue);
5668
5669       start_sequence ();
5670       emit_insn (gen_split_stack_prologue ());
5671       split_prologue_seq = get_insns ();
5672       end_sequence ();
5673
5674       record_insns (split_prologue_seq, NULL, &prologue_insn_hash);
5675       set_insn_locators (split_prologue_seq, prologue_locator);
5676 #endif
5677     }
5678
5679   prologue_seq = NULL_RTX;
5680 #ifdef HAVE_prologue
5681   if (HAVE_prologue)
5682     {
5683       start_sequence ();
5684       seq = gen_prologue ();
5685       emit_insn (seq);
5686
5687       /* Insert an explicit USE for the frame pointer
5688          if the profiling is on and the frame pointer is required.  */
5689       if (crtl->profile && frame_pointer_needed)
5690         emit_use (hard_frame_pointer_rtx);
5691
5692       /* Retain a map of the prologue insns.  */
5693       record_insns (seq, NULL, &prologue_insn_hash);
5694       emit_note (NOTE_INSN_PROLOGUE_END);
5695
5696       /* Ensure that instructions are not moved into the prologue when
5697          profiling is on.  The call to the profiling routine can be
5698          emitted within the live range of a call-clobbered register.  */
5699       if (!targetm.profile_before_prologue () && crtl->profile)
5700         emit_insn (gen_blockage ());
5701
5702       prologue_seq = get_insns ();
5703       end_sequence ();
5704       set_insn_locators (prologue_seq, prologue_locator);
5705     }
5706 #endif
5707
5708   bitmap_initialize (&bb_flags, &bitmap_default_obstack);
5709
5710 #ifdef HAVE_simple_return
5711   /* Try to perform a kind of shrink-wrapping, making sure the
5712      prologue/epilogue is emitted only around those parts of the
5713      function that require it.  */
5714
5715   nonempty_prologue = false;
5716   for (seq = prologue_seq; seq; seq = NEXT_INSN (seq))
5717     if (!NOTE_P (seq) || NOTE_KIND (seq) != NOTE_INSN_PROLOGUE_END)
5718       {
5719         nonempty_prologue = true;
5720         break;
5721       }
5722       
5723   if (flag_shrink_wrap && HAVE_simple_return
5724       && (targetm.profile_before_prologue () || !crtl->profile)
5725       && nonempty_prologue && !crtl->calls_eh_return)
5726     {
5727       HARD_REG_SET prologue_clobbered, prologue_used, live_on_edge;
5728       HARD_REG_SET set_up_by_prologue;
5729       rtx p_insn;
5730
5731       VEC(basic_block, heap) *vec;
5732       basic_block bb;
5733       bitmap_head bb_antic_flags;
5734       bitmap_head bb_on_list;
5735
5736       if (dump_file)
5737         fprintf (dump_file, "Attempting shrink-wrapping optimization.\n");
5738
5739       /* Compute the registers set and used in the prologue.  */
5740       CLEAR_HARD_REG_SET (prologue_clobbered);
5741       CLEAR_HARD_REG_SET (prologue_used);
5742       for (p_insn = prologue_seq; p_insn; p_insn = NEXT_INSN (p_insn))
5743         {
5744           HARD_REG_SET this_used;
5745           if (!NONDEBUG_INSN_P (p_insn))
5746             continue;
5747
5748           CLEAR_HARD_REG_SET (this_used);
5749           note_uses (&PATTERN (p_insn), record_hard_reg_uses,
5750                      &this_used);
5751           AND_COMPL_HARD_REG_SET (this_used, prologue_clobbered);
5752           IOR_HARD_REG_SET (prologue_used, this_used);
5753           note_stores (PATTERN (p_insn), record_hard_reg_sets,
5754                        &prologue_clobbered);
5755         }
5756
5757       prepare_shrink_wrap (entry_edge->dest);
5758
5759       /* That may have inserted instructions into the last block.  */
5760       if (last_bb && !last_bb_active)
5761         last_bb_active = bb_active_p (last_bb);
5762
5763       bitmap_initialize (&bb_antic_flags, &bitmap_default_obstack);
5764       bitmap_initialize (&bb_on_list, &bitmap_default_obstack);
5765
5766       /* Find the set of basic blocks that require a stack frame.  */
5767
5768       vec = VEC_alloc (basic_block, heap, n_basic_blocks);
5769
5770       CLEAR_HARD_REG_SET (set_up_by_prologue);
5771       add_to_hard_reg_set (&set_up_by_prologue, Pmode, STACK_POINTER_REGNUM);
5772       add_to_hard_reg_set (&set_up_by_prologue, Pmode, ARG_POINTER_REGNUM);
5773       if (frame_pointer_needed)
5774         add_to_hard_reg_set (&set_up_by_prologue, Pmode,
5775                              HARD_FRAME_POINTER_REGNUM);
5776       if (pic_offset_table_rtx)
5777         add_to_hard_reg_set (&set_up_by_prologue, Pmode,
5778                              PIC_OFFSET_TABLE_REGNUM);
5779
5780       FOR_EACH_BB (bb)
5781         {
5782           rtx insn;
5783           /* As a special case, check for jumps to the last bb that
5784              cannot successfully be converted to simple_returns later
5785              on, and mark them as requiring a frame.  These are
5786              conditional jumps that jump to their fallthru block, so
5787              it's not a case that is expected to occur often.  */
5788           if (JUMP_P (BB_END (bb)) && any_condjump_p (BB_END (bb))
5789               && single_succ_p (bb)
5790               && !last_bb_active
5791               && single_succ (bb) == last_bb)
5792             {
5793               bitmap_set_bit (&bb_flags, bb->index);
5794               VEC_quick_push (basic_block, vec, bb);
5795             }
5796           else
5797             FOR_BB_INSNS (bb, insn)
5798               if (requires_stack_frame_p (insn, prologue_used,
5799                                           set_up_by_prologue))
5800                 {
5801                   bitmap_set_bit (&bb_flags, bb->index);
5802                   VEC_quick_push (basic_block, vec, bb);
5803                   break;
5804                 }
5805         }
5806
5807       /* For every basic block that needs a prologue, mark all blocks
5808          reachable from it, so as to ensure they are also seen as
5809          requiring a prologue.  */
5810       while (!VEC_empty (basic_block, vec))
5811         {
5812           basic_block tmp_bb = VEC_pop (basic_block, vec);
5813           edge e;
5814           edge_iterator ei;
5815           FOR_EACH_EDGE (e, ei, tmp_bb->succs)
5816             if (e->dest != EXIT_BLOCK_PTR
5817                 && bitmap_set_bit (&bb_flags, e->dest->index))
5818               VEC_quick_push (basic_block, vec, e->dest);
5819         }
5820       /* If the last basic block contains only a label, we'll be able
5821          to convert jumps to it to (potentially conditional) return
5822          insns later.  This means we don't necessarily need a prologue
5823          for paths reaching it.  */
5824       if (last_bb)
5825         {
5826           if (!last_bb_active)
5827             bitmap_clear_bit (&bb_flags, last_bb->index);
5828           else if (!bitmap_bit_p (&bb_flags, last_bb->index))
5829             goto fail_shrinkwrap;
5830         }
5831
5832       /* Now walk backwards from every block that is marked as needing
5833          a prologue to compute the bb_antic_flags bitmap.  */
5834       bitmap_copy (&bb_antic_flags, &bb_flags);
5835       FOR_EACH_BB (bb)
5836         {
5837           edge e;
5838           edge_iterator ei;
5839           if (!bitmap_bit_p (&bb_flags, bb->index))
5840             continue;
5841           FOR_EACH_EDGE (e, ei, bb->preds)
5842             if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
5843                 && bitmap_set_bit (&bb_on_list, e->src->index))
5844               VEC_quick_push (basic_block, vec, e->src);
5845         }
5846       while (!VEC_empty (basic_block, vec))
5847         {
5848           basic_block tmp_bb = VEC_pop (basic_block, vec);
5849           edge e;
5850           edge_iterator ei;
5851           bool all_set = true;
5852
5853           bitmap_clear_bit (&bb_on_list, tmp_bb->index);
5854           FOR_EACH_EDGE (e, ei, tmp_bb->succs)
5855             if (!bitmap_bit_p (&bb_antic_flags, e->dest->index))
5856               {
5857                 all_set = false;
5858                 break;
5859               }
5860
5861           if (all_set)
5862             {
5863               bitmap_set_bit (&bb_antic_flags, tmp_bb->index);
5864               FOR_EACH_EDGE (e, ei, tmp_bb->preds)
5865                 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
5866                     && bitmap_set_bit (&bb_on_list, e->src->index))
5867                   VEC_quick_push (basic_block, vec, e->src);
5868             }
5869         }
5870       /* Find exactly one edge that leads to a block in ANTIC from
5871          a block that isn't.  */
5872       if (!bitmap_bit_p (&bb_antic_flags, entry_edge->dest->index))
5873         FOR_EACH_BB (bb)
5874           {
5875             if (!bitmap_bit_p (&bb_antic_flags, bb->index))
5876               continue;
5877             FOR_EACH_EDGE (e, ei, bb->preds)
5878               if (!bitmap_bit_p (&bb_antic_flags, e->src->index))
5879                 {
5880                   if (entry_edge != orig_entry_edge)
5881                     {
5882                       entry_edge = orig_entry_edge;
5883                       if (dump_file)
5884                         fprintf (dump_file, "More than one candidate edge.\n");
5885                       goto fail_shrinkwrap;
5886                     }
5887                   if (dump_file)
5888                     fprintf (dump_file, "Found candidate edge for "
5889                              "shrink-wrapping, %d->%d.\n", e->src->index,
5890                              e->dest->index);
5891                   entry_edge = e;
5892                 }
5893           }
5894
5895       /* Test whether the prologue is known to clobber any register
5896          (other than FP or SP) which are live on the edge.  */
5897       CLEAR_HARD_REG_BIT (prologue_clobbered, STACK_POINTER_REGNUM);
5898       if (frame_pointer_needed)
5899         CLEAR_HARD_REG_BIT (prologue_clobbered, HARD_FRAME_POINTER_REGNUM);
5900       CLEAR_HARD_REG_SET (live_on_edge);
5901       reg_set_to_hard_reg_set (&live_on_edge,
5902                                df_get_live_in (entry_edge->dest));
5903       if (hard_reg_set_intersect_p (live_on_edge, prologue_clobbered))
5904         {
5905           entry_edge = orig_entry_edge;
5906           if (dump_file)
5907             fprintf (dump_file, "Shrink-wrapping aborted due to clobber.\n");
5908         }
5909       else if (entry_edge != orig_entry_edge)
5910         {
5911           crtl->shrink_wrapped = true;
5912           if (dump_file)
5913             fprintf (dump_file, "Performing shrink-wrapping.\n");
5914         }
5915
5916     fail_shrinkwrap:
5917       bitmap_clear (&bb_antic_flags);
5918       bitmap_clear (&bb_on_list);
5919       VEC_free (basic_block, heap, vec);
5920     }
5921 #endif
5922
5923   if (split_prologue_seq != NULL_RTX)
5924     {
5925       insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
5926       inserted = true;
5927     }
5928   if (prologue_seq != NULL_RTX)
5929     {
5930       insert_insn_on_edge (prologue_seq, entry_edge);
5931       inserted = true;
5932     }
5933
5934   /* If the exit block has no non-fake predecessors, we don't need
5935      an epilogue.  */
5936   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5937     if ((e->flags & EDGE_FAKE) == 0)
5938       break;
5939   if (e == NULL)
5940     goto epilogue_done;
5941
5942   rtl_profile_for_bb (EXIT_BLOCK_PTR);
5943
5944 #ifdef HAVE_return
5945   /* If we're allowed to generate a simple return instruction, then by
5946      definition we don't need a full epilogue.  If the last basic
5947      block before the exit block does not contain active instructions,
5948      examine its predecessors and try to emit (conditional) return
5949      instructions.  */
5950   if (optimize && !last_bb_active
5951       && (HAVE_return || entry_edge != orig_entry_edge))
5952     {
5953       edge_iterator ei2;
5954       int i;
5955       basic_block bb;
5956       rtx label;
5957       VEC(basic_block,heap) *src_bbs;
5958
5959       if (exit_fallthru_edge == NULL)
5960         goto epilogue_done;
5961       label = BB_HEAD (last_bb);
5962
5963       src_bbs = VEC_alloc (basic_block, heap, EDGE_COUNT (last_bb->preds));
5964       FOR_EACH_EDGE (e, ei2, last_bb->preds)
5965         if (e->src != ENTRY_BLOCK_PTR)
5966           VEC_quick_push (basic_block, src_bbs, e->src);
5967
5968       FOR_EACH_VEC_ELT (basic_block, src_bbs, i, bb)
5969         {
5970           bool simple_p;
5971           rtx jump;
5972           e = find_edge (bb, last_bb);
5973
5974           jump = BB_END (bb);
5975
5976 #ifdef HAVE_simple_return
5977           simple_p = (entry_edge != orig_entry_edge
5978                       && !bitmap_bit_p (&bb_flags, bb->index));
5979 #else
5980           simple_p = false;
5981 #endif
5982
5983           if (!simple_p
5984               && (!HAVE_return || !JUMP_P (jump)
5985                   || JUMP_LABEL (jump) != label))
5986             continue;
5987
5988           /* If we have an unconditional jump, we can replace that
5989              with a simple return instruction.  */
5990           if (!JUMP_P (jump))
5991             {
5992               emit_barrier_after (BB_END (bb));
5993               emit_return_into_block (simple_p, bb);
5994             }
5995           else if (simplejump_p (jump))
5996             {
5997               /* The use of the return register might be present in the exit
5998                  fallthru block.  Either:
5999                  - removing the use is safe, and we should remove the use in
6000                    the exit fallthru block, or
6001                  - removing the use is not safe, and we should add it here.
6002                  For now, we conservatively choose the latter.  Either of the
6003                  2 helps in crossjumping.  */
6004               emit_use_return_register_into_block (bb);
6005
6006               emit_return_into_block (simple_p, bb);
6007               delete_insn (jump);
6008             }
6009           else if (condjump_p (jump) && JUMP_LABEL (jump) != label)
6010             {
6011               basic_block new_bb;
6012               edge new_e;
6013
6014               gcc_assert (simple_p);
6015               new_bb = split_edge (e);
6016               emit_barrier_after (BB_END (new_bb));
6017               emit_return_into_block (simple_p, new_bb);
6018 #ifdef HAVE_simple_return
6019               if (BB_PARTITION (new_bb) == BB_HOT_PARTITION)
6020                 simple_return_block_hot = new_bb;
6021               else
6022                 simple_return_block_cold = new_bb;
6023 #endif
6024               new_e = single_succ_edge (new_bb);
6025               redirect_edge_succ (new_e, EXIT_BLOCK_PTR);
6026
6027               continue;
6028             }
6029           /* If we have a conditional jump branching to the last
6030              block, we can try to replace that with a conditional
6031              return instruction.  */
6032           else if (condjump_p (jump))
6033             {
6034               rtx dest;
6035               if (simple_p)
6036                 dest = simple_return_rtx;
6037               else
6038                 dest = ret_rtx;
6039               if (! redirect_jump (jump, dest, 0))
6040                 {
6041 #ifdef HAVE_simple_return
6042                   if (simple_p)
6043                     VEC_safe_push (rtx, heap,
6044                                    unconverted_simple_returns, jump);
6045 #endif
6046                   continue;
6047                 }
6048
6049               /* See comment in simple_jump_p case above.  */
6050               emit_use_return_register_into_block (bb);
6051
6052               /* If this block has only one successor, it both jumps
6053                  and falls through to the fallthru block, so we can't
6054                  delete the edge.  */
6055               if (single_succ_p (bb))
6056                 continue;
6057             }
6058           else
6059             {
6060 #ifdef HAVE_simple_return
6061               if (simple_p)
6062                 VEC_safe_push (rtx, heap,
6063                                unconverted_simple_returns, jump);
6064 #endif
6065               continue;
6066             }
6067
6068           /* Fix up the CFG for the successful change we just made.  */
6069           redirect_edge_succ (e, EXIT_BLOCK_PTR);
6070         }
6071       VEC_free (basic_block, heap, src_bbs);
6072
6073       if (HAVE_return)
6074         {
6075           /* Emit a return insn for the exit fallthru block.  Whether
6076              this is still reachable will be determined later.  */
6077
6078           emit_barrier_after (BB_END (last_bb));
6079           emit_return_into_block (false, last_bb);
6080           epilogue_end = BB_END (last_bb);
6081           if (JUMP_P (epilogue_end))
6082             set_return_jump_label (epilogue_end);
6083           single_succ_edge (last_bb)->flags &= ~EDGE_FALLTHRU;
6084           goto epilogue_done;
6085         }
6086     }
6087 #endif
6088
6089   /* A small fib -- epilogue is not yet completed, but we wish to re-use
6090      this marker for the splits of EH_RETURN patterns, and nothing else
6091      uses the flag in the meantime.  */
6092   epilogue_completed = 1;
6093
6094 #ifdef HAVE_eh_return
6095   /* Find non-fallthru edges that end with EH_RETURN instructions.  On
6096      some targets, these get split to a special version of the epilogue
6097      code.  In order to be able to properly annotate these with unwind
6098      info, try to split them now.  If we get a valid split, drop an
6099      EPILOGUE_BEG note and mark the insns as epilogue insns.  */
6100   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6101     {
6102       rtx prev, last, trial;
6103
6104       if (e->flags & EDGE_FALLTHRU)
6105         continue;
6106       last = BB_END (e->src);
6107       if (!eh_returnjump_p (last))
6108         continue;
6109
6110       prev = PREV_INSN (last);
6111       trial = try_split (PATTERN (last), last, 1);
6112       if (trial == last)
6113         continue;
6114
6115       record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
6116       emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
6117     }
6118 #endif
6119
6120   /* If nothing falls through into the exit block, we don't need an
6121      epilogue.  */
6122
6123   if (exit_fallthru_edge == NULL)
6124     goto epilogue_done;
6125
6126 #ifdef HAVE_epilogue
6127   if (HAVE_epilogue)
6128     {
6129       start_sequence ();
6130       epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
6131       seq = gen_epilogue ();
6132       if (seq)
6133         emit_jump_insn (seq);
6134
6135       /* Retain a map of the epilogue insns.  */
6136       record_insns (seq, NULL, &epilogue_insn_hash);
6137       set_insn_locators (seq, epilogue_locator);
6138
6139       seq = get_insns ();
6140       returnjump = get_last_insn ();
6141       end_sequence ();
6142
6143       insert_insn_on_edge (seq, exit_fallthru_edge);
6144       inserted = true;
6145
6146       if (JUMP_P (returnjump))
6147         set_return_jump_label (returnjump);
6148     }
6149   else
6150 #endif
6151     {
6152       basic_block cur_bb;
6153
6154       if (! next_active_insn (BB_END (exit_fallthru_edge->src)))
6155         goto epilogue_done;
6156       /* We have a fall-through edge to the exit block, the source is not
6157          at the end of the function, and there will be an assembler epilogue
6158          at the end of the function.
6159          We can't use force_nonfallthru here, because that would try to
6160          use return.  Inserting a jump 'by hand' is extremely messy, so
6161          we take advantage of cfg_layout_finalize using
6162          fixup_fallthru_exit_predecessor.  */
6163       cfg_layout_initialize (0);
6164       FOR_EACH_BB (cur_bb)
6165         if (cur_bb->index >= NUM_FIXED_BLOCKS
6166             && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
6167           cur_bb->aux = cur_bb->next_bb;
6168       cfg_layout_finalize ();
6169     }
6170
6171 epilogue_done:
6172
6173   default_rtl_profile ();
6174
6175   if (inserted)
6176     {
6177       sbitmap blocks;
6178
6179       commit_edge_insertions ();
6180
6181       /* Look for basic blocks within the prologue insns.  */
6182       blocks = sbitmap_alloc (last_basic_block);
6183       sbitmap_zero (blocks);
6184       SET_BIT (blocks, entry_edge->dest->index);
6185       SET_BIT (blocks, orig_entry_edge->dest->index);
6186       find_many_sub_basic_blocks (blocks);
6187       sbitmap_free (blocks);
6188
6189       /* The epilogue insns we inserted may cause the exit edge to no longer
6190          be fallthru.  */
6191       FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6192         {
6193           if (((e->flags & EDGE_FALLTHRU) != 0)
6194               && returnjump_p (BB_END (e->src)))
6195             e->flags &= ~EDGE_FALLTHRU;
6196         }
6197     }
6198
6199 #ifdef HAVE_simple_return
6200   /* If there were branches to an empty LAST_BB which we tried to
6201      convert to conditional simple_returns, but couldn't for some
6202      reason, create a block to hold a simple_return insn and redirect
6203      those remaining edges.  */
6204   if (!VEC_empty (rtx, unconverted_simple_returns))
6205     {
6206       basic_block exit_pred = EXIT_BLOCK_PTR->prev_bb;
6207       rtx jump;
6208       int i;
6209
6210       gcc_assert (entry_edge != orig_entry_edge);
6211
6212       /* See if we can reuse the last insn that was emitted for the
6213          epilogue.  */
6214       if (returnjump != NULL_RTX
6215           && JUMP_LABEL (returnjump) == simple_return_rtx)
6216         {
6217           edge e = split_block (exit_fallthru_edge->src,
6218                                 PREV_INSN (returnjump));
6219           if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
6220             simple_return_block_hot = e->dest;
6221           else
6222             simple_return_block_cold = e->dest;
6223         }
6224
6225       FOR_EACH_VEC_ELT (rtx, unconverted_simple_returns, i, jump)
6226         {
6227           basic_block src_bb = BLOCK_FOR_INSN (jump);
6228           edge e = find_edge (src_bb, last_bb);
6229           basic_block *pdest_bb;
6230
6231           if (BB_PARTITION (src_bb) == BB_HOT_PARTITION)
6232             pdest_bb = &simple_return_block_hot;
6233           else
6234             pdest_bb = &simple_return_block_cold;
6235           if (*pdest_bb == NULL)
6236             {
6237               basic_block bb;
6238               rtx start;
6239
6240               bb = create_basic_block (NULL, NULL, exit_pred);
6241               BB_COPY_PARTITION (bb, e->src);
6242               start = emit_jump_insn_after (gen_simple_return (),
6243                                             BB_END (bb));
6244               JUMP_LABEL (start) = simple_return_rtx;
6245               emit_barrier_after (start);
6246
6247               *pdest_bb = bb;
6248               make_edge (bb, EXIT_BLOCK_PTR, 0);
6249             }
6250           redirect_edge_and_branch_force (e, *pdest_bb);
6251         }
6252       VEC_free (rtx, heap, unconverted_simple_returns);
6253     }
6254 #endif
6255
6256 #ifdef HAVE_sibcall_epilogue
6257   /* Emit sibling epilogues before any sibling call sites.  */
6258   for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
6259     {
6260       basic_block bb = e->src;
6261       rtx insn = BB_END (bb);
6262       rtx ep_seq;
6263
6264       if (!CALL_P (insn)
6265           || ! SIBLING_CALL_P (insn)
6266           || (entry_edge != orig_entry_edge
6267               && !bitmap_bit_p (&bb_flags, bb->index)))
6268         {
6269           ei_next (&ei);
6270           continue;
6271         }
6272
6273       ep_seq = gen_sibcall_epilogue ();
6274       if (ep_seq)
6275         {
6276           start_sequence ();
6277           emit_note (NOTE_INSN_EPILOGUE_BEG);
6278           emit_insn (ep_seq);
6279           seq = get_insns ();
6280           end_sequence ();
6281
6282           /* Retain a map of the epilogue insns.  Used in life analysis to
6283              avoid getting rid of sibcall epilogue insns.  Do this before we
6284              actually emit the sequence.  */
6285           record_insns (seq, NULL, &epilogue_insn_hash);
6286           set_insn_locators (seq, epilogue_locator);
6287
6288           emit_insn_before (seq, insn);
6289         }
6290       ei_next (&ei);
6291     }
6292 #endif
6293
6294 #ifdef HAVE_epilogue
6295   if (epilogue_end)
6296     {
6297       rtx insn, next;
6298
6299       /* Similarly, move any line notes that appear after the epilogue.
6300          There is no need, however, to be quite so anal about the existence
6301          of such a note.  Also possibly move
6302          NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
6303          info generation.  */
6304       for (insn = epilogue_end; insn; insn = next)
6305         {
6306           next = NEXT_INSN (insn);
6307           if (NOTE_P (insn)
6308               && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
6309             reorder_insns (insn, insn, PREV_INSN (epilogue_end));
6310         }
6311     }
6312 #endif
6313
6314   bitmap_clear (&bb_flags);
6315
6316   /* Threading the prologue and epilogue changes the artificial refs
6317      in the entry and exit blocks.  */
6318   epilogue_completed = 1;
6319   df_update_entry_exit_and_calls ();
6320 }
6321
6322 /* Reposition the prologue-end and epilogue-begin notes after
6323    instruction scheduling.  */
6324
6325 void
6326 reposition_prologue_and_epilogue_notes (void)
6327 {
6328 #if defined (HAVE_prologue) || defined (HAVE_epilogue) \
6329     || defined (HAVE_sibcall_epilogue)
6330   /* Since the hash table is created on demand, the fact that it is
6331      non-null is a signal that it is non-empty.  */
6332   if (prologue_insn_hash != NULL)
6333     {
6334       size_t len = htab_elements (prologue_insn_hash);
6335       rtx insn, last = NULL, note = NULL;
6336
6337       /* Scan from the beginning until we reach the last prologue insn.  */
6338       /* ??? While we do have the CFG intact, there are two problems:
6339          (1) The prologue can contain loops (typically probing the stack),
6340              which means that the end of the prologue isn't in the first bb.
6341          (2) Sometimes the PROLOGUE_END note gets pushed into the next bb.  */
6342       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6343         {
6344           if (NOTE_P (insn))
6345             {
6346               if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
6347                 note = insn;
6348             }
6349           else if (contains (insn, prologue_insn_hash))
6350             {
6351               last = insn;
6352               if (--len == 0)
6353                 break;
6354             }
6355         }
6356
6357       if (last)
6358         {
6359           if (note == NULL)
6360             {
6361               /* Scan forward looking for the PROLOGUE_END note.  It should
6362                  be right at the beginning of the block, possibly with other
6363                  insn notes that got moved there.  */
6364               for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
6365                 {
6366                   if (NOTE_P (note)
6367                       && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
6368                     break;
6369                 }
6370             }
6371
6372           /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note.  */
6373           if (LABEL_P (last))
6374             last = NEXT_INSN (last);
6375           reorder_insns (note, note, last);
6376         }
6377     }
6378
6379   if (epilogue_insn_hash != NULL)
6380     {
6381       edge_iterator ei;
6382       edge e;
6383
6384       FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6385         {
6386           rtx insn, first = NULL, note = NULL;
6387           basic_block bb = e->src;
6388
6389           /* Scan from the beginning until we reach the first epilogue insn. */
6390           FOR_BB_INSNS (bb, insn)
6391             {
6392               if (NOTE_P (insn))
6393                 {
6394                   if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
6395                     {
6396                       note = insn;
6397                       if (first != NULL)
6398                         break;
6399                     }
6400                 }
6401               else if (first == NULL && contains (insn, epilogue_insn_hash))
6402                 {
6403                   first = insn;
6404                   if (note != NULL)
6405                     break;
6406                 }
6407             }
6408
6409           if (note)
6410             {
6411               /* If the function has a single basic block, and no real
6412                  epilogue insns (e.g. sibcall with no cleanup), the
6413                  epilogue note can get scheduled before the prologue
6414                  note.  If we have frame related prologue insns, having
6415                  them scanned during the epilogue will result in a crash.
6416                  In this case re-order the epilogue note to just before
6417                  the last insn in the block.  */
6418               if (first == NULL)
6419                 first = BB_END (bb);
6420
6421               if (PREV_INSN (first) != note)
6422                 reorder_insns (note, note, PREV_INSN (first));
6423             }
6424         }
6425     }
6426 #endif /* HAVE_prologue or HAVE_epilogue */
6427 }
6428
6429 /* Returns the name of the current function.  */
6430 const char *
6431 current_function_name (void)
6432 {
6433   if (cfun == NULL)
6434     return "<none>";
6435   return lang_hooks.decl_printable_name (cfun->decl, 2);
6436 }
6437 \f
6438
6439 static unsigned int
6440 rest_of_handle_check_leaf_regs (void)
6441 {
6442 #ifdef LEAF_REGISTERS
6443   current_function_uses_only_leaf_regs
6444     = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
6445 #endif
6446   return 0;
6447 }
6448
6449 /* Insert a TYPE into the used types hash table of CFUN.  */
6450
6451 static void
6452 used_types_insert_helper (tree type, struct function *func)
6453 {
6454   if (type != NULL && func != NULL)
6455     {
6456       void **slot;
6457
6458       if (func->used_types_hash == NULL)
6459         func->used_types_hash = htab_create_ggc (37, htab_hash_pointer,
6460                                                  htab_eq_pointer, NULL);
6461       slot = htab_find_slot (func->used_types_hash, type, INSERT);
6462       if (*slot == NULL)
6463         *slot = type;
6464     }
6465 }
6466
6467 /* Given a type, insert it into the used hash table in cfun.  */
6468 void
6469 used_types_insert (tree t)
6470 {
6471   while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
6472     if (TYPE_NAME (t))
6473       break;
6474     else
6475       t = TREE_TYPE (t);
6476   if (TREE_CODE (t) == ERROR_MARK)
6477     return;
6478   if (TYPE_NAME (t) == NULL_TREE
6479       || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
6480     t = TYPE_MAIN_VARIANT (t);
6481   if (debug_info_level > DINFO_LEVEL_NONE)
6482     {
6483       if (cfun)
6484         used_types_insert_helper (t, cfun);
6485       else
6486         /* So this might be a type referenced by a global variable.
6487            Record that type so that we can later decide to emit its debug
6488            information.  */
6489         VEC_safe_push (tree, gc, types_used_by_cur_var_decl, t);
6490     }
6491 }
6492
6493 /* Helper to Hash a struct types_used_by_vars_entry.  */
6494
6495 static hashval_t
6496 hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
6497 {
6498   gcc_assert (entry && entry->var_decl && entry->type);
6499
6500   return iterative_hash_object (entry->type,
6501                                 iterative_hash_object (entry->var_decl, 0));
6502 }
6503
6504 /* Hash function of the types_used_by_vars_entry hash table.  */
6505
6506 hashval_t
6507 types_used_by_vars_do_hash (const void *x)
6508 {
6509   const struct types_used_by_vars_entry *entry =
6510     (const struct types_used_by_vars_entry *) x;
6511
6512   return hash_types_used_by_vars_entry (entry);
6513 }
6514
6515 /*Equality function of the types_used_by_vars_entry hash table.  */
6516
6517 int
6518 types_used_by_vars_eq (const void *x1, const void *x2)
6519 {
6520   const struct types_used_by_vars_entry *e1 =
6521     (const struct types_used_by_vars_entry *) x1;
6522   const struct types_used_by_vars_entry *e2 =
6523     (const struct types_used_by_vars_entry *)x2;
6524
6525   return (e1->var_decl == e2->var_decl && e1->type == e2->type);
6526 }
6527
6528 /* Inserts an entry into the types_used_by_vars_hash hash table. */
6529
6530 void
6531 types_used_by_var_decl_insert (tree type, tree var_decl)
6532 {
6533   if (type != NULL && var_decl != NULL)
6534     {
6535       void **slot;
6536       struct types_used_by_vars_entry e;
6537       e.var_decl = var_decl;
6538       e.type = type;
6539       if (types_used_by_vars_hash == NULL)
6540         types_used_by_vars_hash =
6541           htab_create_ggc (37, types_used_by_vars_do_hash,
6542                            types_used_by_vars_eq, NULL);
6543       slot = htab_find_slot_with_hash (types_used_by_vars_hash, &e,
6544                                        hash_types_used_by_vars_entry (&e), INSERT);
6545       if (*slot == NULL)
6546         {
6547           struct types_used_by_vars_entry *entry;
6548           entry = ggc_alloc_types_used_by_vars_entry ();
6549           entry->type = type;
6550           entry->var_decl = var_decl;
6551           *slot = entry;
6552         }
6553     }
6554 }
6555
6556 struct rtl_opt_pass pass_leaf_regs =
6557 {
6558  {
6559   RTL_PASS,
6560   "*leaf_regs",                         /* name */
6561   NULL,                                 /* gate */
6562   rest_of_handle_check_leaf_regs,       /* execute */
6563   NULL,                                 /* sub */
6564   NULL,                                 /* next */
6565   0,                                    /* static_pass_number */
6566   TV_NONE,                              /* tv_id */
6567   0,                                    /* properties_required */
6568   0,                                    /* properties_provided */
6569   0,                                    /* properties_destroyed */
6570   0,                                    /* todo_flags_start */
6571   0                                     /* todo_flags_finish */
6572  }
6573 };
6574
6575 static unsigned int
6576 rest_of_handle_thread_prologue_and_epilogue (void)
6577 {
6578   if (optimize)
6579     cleanup_cfg (CLEANUP_EXPENSIVE);
6580
6581   /* On some machines, the prologue and epilogue code, or parts thereof,
6582      can be represented as RTL.  Doing so lets us schedule insns between
6583      it and the rest of the code and also allows delayed branch
6584      scheduling to operate in the epilogue.  */
6585   thread_prologue_and_epilogue_insns ();
6586
6587   /* The stack usage info is finalized during prologue expansion.  */
6588   if (flag_stack_usage_info)
6589     output_stack_usage ();
6590
6591   return 0;
6592 }
6593
6594 struct rtl_opt_pass pass_thread_prologue_and_epilogue =
6595 {
6596  {
6597   RTL_PASS,
6598   "pro_and_epilogue",                   /* name */
6599   NULL,                                 /* gate */
6600   rest_of_handle_thread_prologue_and_epilogue, /* execute */
6601   NULL,                                 /* sub */
6602   NULL,                                 /* next */
6603   0,                                    /* static_pass_number */
6604   TV_THREAD_PROLOGUE_AND_EPILOGUE,      /* tv_id */
6605   0,                                    /* properties_required */
6606   0,                                    /* properties_provided */
6607   0,                                    /* properties_destroyed */
6608   TODO_verify_flow,                     /* todo_flags_start */
6609   TODO_df_verify |
6610   TODO_df_finish | TODO_verify_rtl_sharing |
6611   TODO_ggc_collect                      /* todo_flags_finish */
6612  }
6613 };
6614 \f
6615
6616 /* This mini-pass fixes fall-out from SSA in asm statements that have
6617    in-out constraints.  Say you start with
6618
6619      orig = inout;
6620      asm ("": "+mr" (inout));
6621      use (orig);
6622
6623    which is transformed very early to use explicit output and match operands:
6624
6625      orig = inout;
6626      asm ("": "=mr" (inout) : "0" (inout));
6627      use (orig);
6628
6629    Or, after SSA and copyprop,
6630
6631      asm ("": "=mr" (inout_2) : "0" (inout_1));
6632      use (inout_1);
6633
6634    Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
6635    they represent two separate values, so they will get different pseudo
6636    registers during expansion.  Then, since the two operands need to match
6637    per the constraints, but use different pseudo registers, reload can
6638    only register a reload for these operands.  But reloads can only be
6639    satisfied by hardregs, not by memory, so we need a register for this
6640    reload, just because we are presented with non-matching operands.
6641    So, even though we allow memory for this operand, no memory can be
6642    used for it, just because the two operands don't match.  This can
6643    cause reload failures on register-starved targets.
6644
6645    So it's a symptom of reload not being able to use memory for reloads
6646    or, alternatively it's also a symptom of both operands not coming into
6647    reload as matching (in which case the pseudo could go to memory just
6648    fine, as the alternative allows it, and no reload would be necessary).
6649    We fix the latter problem here, by transforming
6650
6651      asm ("": "=mr" (inout_2) : "0" (inout_1));
6652
6653    back to
6654
6655      inout_2 = inout_1;
6656      asm ("": "=mr" (inout_2) : "0" (inout_2));  */
6657
6658 static void
6659 match_asm_constraints_1 (rtx insn, rtx *p_sets, int noutputs)
6660 {
6661   int i;
6662   bool changed = false;
6663   rtx op = SET_SRC (p_sets[0]);
6664   int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
6665   rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
6666   bool *output_matched = XALLOCAVEC (bool, noutputs);
6667
6668   memset (output_matched, 0, noutputs * sizeof (bool));
6669   for (i = 0; i < ninputs; i++)
6670     {
6671       rtx input, output, insns;
6672       const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
6673       char *end;
6674       int match, j;
6675
6676       if (*constraint == '%')
6677         constraint++;
6678
6679       match = strtoul (constraint, &end, 10);
6680       if (end == constraint)
6681         continue;
6682
6683       gcc_assert (match < noutputs);
6684       output = SET_DEST (p_sets[match]);
6685       input = RTVEC_ELT (inputs, i);
6686       /* Only do the transformation for pseudos.  */
6687       if (! REG_P (output)
6688           || rtx_equal_p (output, input)
6689           || (GET_MODE (input) != VOIDmode
6690               && GET_MODE (input) != GET_MODE (output)))
6691         continue;
6692
6693       /* We can't do anything if the output is also used as input,
6694          as we're going to overwrite it.  */
6695       for (j = 0; j < ninputs; j++)
6696         if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
6697           break;
6698       if (j != ninputs)
6699         continue;
6700
6701       /* Avoid changing the same input several times.  For
6702          asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
6703          only change in once (to out1), rather than changing it
6704          first to out1 and afterwards to out2.  */
6705       if (i > 0)
6706         {
6707           for (j = 0; j < noutputs; j++)
6708             if (output_matched[j] && input == SET_DEST (p_sets[j]))
6709               break;
6710           if (j != noutputs)
6711             continue;
6712         }
6713       output_matched[match] = true;
6714
6715       start_sequence ();
6716       emit_move_insn (output, input);
6717       insns = get_insns ();
6718       end_sequence ();
6719       emit_insn_before (insns, insn);
6720
6721       /* Now replace all mentions of the input with output.  We can't
6722          just replace the occurrence in inputs[i], as the register might
6723          also be used in some other input (or even in an address of an
6724          output), which would mean possibly increasing the number of
6725          inputs by one (namely 'output' in addition), which might pose
6726          a too complicated problem for reload to solve.  E.g. this situation:
6727
6728            asm ("" : "=r" (output), "=m" (input) : "0" (input))
6729
6730          Here 'input' is used in two occurrences as input (once for the
6731          input operand, once for the address in the second output operand).
6732          If we would replace only the occurrence of the input operand (to
6733          make the matching) we would be left with this:
6734
6735            output = input
6736            asm ("" : "=r" (output), "=m" (input) : "0" (output))
6737
6738          Now we suddenly have two different input values (containing the same
6739          value, but different pseudos) where we formerly had only one.
6740          With more complicated asms this might lead to reload failures
6741          which wouldn't have happen without this pass.  So, iterate over
6742          all operands and replace all occurrences of the register used.  */
6743       for (j = 0; j < noutputs; j++)
6744         if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
6745             && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
6746           SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
6747                                               input, output);
6748       for (j = 0; j < ninputs; j++)
6749         if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
6750           RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
6751                                                input, output);
6752
6753       changed = true;
6754     }
6755
6756   if (changed)
6757     df_insn_rescan (insn);
6758 }
6759
6760 static unsigned
6761 rest_of_match_asm_constraints (void)
6762 {
6763   basic_block bb;
6764   rtx insn, pat, *p_sets;
6765   int noutputs;
6766
6767   if (!crtl->has_asm_statement)
6768     return 0;
6769
6770   df_set_flags (DF_DEFER_INSN_RESCAN);
6771   FOR_EACH_BB (bb)
6772     {
6773       FOR_BB_INSNS (bb, insn)
6774         {
6775           if (!INSN_P (insn))
6776             continue;
6777
6778           pat = PATTERN (insn);
6779           if (GET_CODE (pat) == PARALLEL)
6780             p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
6781           else if (GET_CODE (pat) == SET)
6782             p_sets = &PATTERN (insn), noutputs = 1;
6783           else
6784             continue;
6785
6786           if (GET_CODE (*p_sets) == SET
6787               && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
6788             match_asm_constraints_1 (insn, p_sets, noutputs);
6789          }
6790     }
6791
6792   return TODO_df_finish;
6793 }
6794
6795 struct rtl_opt_pass pass_match_asm_constraints =
6796 {
6797  {
6798   RTL_PASS,
6799   "asmcons",                            /* name */
6800   NULL,                                 /* gate */
6801   rest_of_match_asm_constraints,        /* execute */
6802   NULL,                                 /* sub */
6803   NULL,                                 /* next */
6804   0,                                    /* static_pass_number */
6805   TV_NONE,                              /* tv_id */
6806   0,                                    /* properties_required */
6807   0,                                    /* properties_provided */
6808   0,                                    /* properties_destroyed */
6809   0,                                    /* todo_flags_start */
6810   0                                     /* todo_flags_finish */
6811  }
6812 };
6813
6814
6815 #include "gt-function.h"