OSDN Git Service

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