OSDN Git Service

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