OSDN Git Service

* tree-vrp.c (debug_value_range, debug_all_value_ranges,
[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 tree types_used_by_cur_var_decl = NULL;
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_NEW (struct 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_NEW (struct 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_NEW (struct 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_NEW (struct 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 = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
3373                                                  crtl->args.size);
3374
3375   /* For stdarg.h function, save info about
3376      regs and stack space used by the named args.  */
3377
3378   crtl->args.info = all.args_so_far;
3379
3380   /* Set the rtx used for the function return value.  Put this in its
3381      own variable so any optimizers that need this information don't have
3382      to include tree.h.  Do this here so it gets done when an inlined
3383      function gets output.  */
3384
3385   crtl->return_rtx
3386     = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3387        ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3388
3389   /* If scalar return value was computed in a pseudo-reg, or was a named
3390      return value that got dumped to the stack, copy that to the hard
3391      return register.  */
3392   if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3393     {
3394       tree decl_result = DECL_RESULT (fndecl);
3395       rtx decl_rtl = DECL_RTL (decl_result);
3396
3397       if (REG_P (decl_rtl)
3398           ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3399           : DECL_REGISTER (decl_result))
3400         {
3401           rtx real_decl_rtl;
3402
3403           real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3404                                                         fndecl, true);
3405           REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3406           /* The delay slot scheduler assumes that crtl->return_rtx
3407              holds the hard register containing the return value, not a
3408              temporary pseudo.  */
3409           crtl->return_rtx = real_decl_rtl;
3410         }
3411     }
3412 }
3413
3414 /* A subroutine of gimplify_parameters, invoked via walk_tree.
3415    For all seen types, gimplify their sizes.  */
3416
3417 static tree
3418 gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3419 {
3420   tree t = *tp;
3421
3422   *walk_subtrees = 0;
3423   if (TYPE_P (t))
3424     {
3425       if (POINTER_TYPE_P (t))
3426         *walk_subtrees = 1;
3427       else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3428                && !TYPE_SIZES_GIMPLIFIED (t))
3429         {
3430           gimplify_type_sizes (t, (gimple_seq *) data);
3431           *walk_subtrees = 1;
3432         }
3433     }
3434
3435   return NULL;
3436 }
3437
3438 /* Gimplify the parameter list for current_function_decl.  This involves
3439    evaluating SAVE_EXPRs of variable sized parameters and generating code
3440    to implement callee-copies reference parameters.  Returns a sequence of
3441    statements to add to the beginning of the function.  */
3442
3443 gimple_seq
3444 gimplify_parameters (void)
3445 {
3446   struct assign_parm_data_all all;
3447   tree parm;
3448   gimple_seq stmts = NULL;
3449   VEC(tree, heap) *fnargs;
3450   unsigned i;
3451
3452   assign_parms_initialize_all (&all);
3453   fnargs = assign_parms_augmented_arg_list (&all);
3454
3455   for (i = 0; VEC_iterate (tree, fnargs, i, parm); ++i)
3456     {
3457       struct assign_parm_data_one data;
3458
3459       /* Extract the type of PARM; adjust it according to ABI.  */
3460       assign_parm_find_data_types (&all, parm, &data);
3461
3462       /* Early out for errors and void parameters.  */
3463       if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3464         continue;
3465
3466       /* Update info on where next arg arrives in registers.  */
3467       FUNCTION_ARG_ADVANCE (all.args_so_far, data.promoted_mode,
3468                             data.passed_type, data.named_arg);
3469
3470       /* ??? Once upon a time variable_size stuffed parameter list
3471          SAVE_EXPRs (amongst others) onto a pending sizes list.  This
3472          turned out to be less than manageable in the gimple world.
3473          Now we have to hunt them down ourselves.  */
3474       walk_tree_without_duplicates (&data.passed_type,
3475                                     gimplify_parm_type, &stmts);
3476
3477       if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
3478         {
3479           gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3480           gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3481         }
3482
3483       if (data.passed_pointer)
3484         {
3485           tree type = TREE_TYPE (data.passed_type);
3486           if (reference_callee_copied (&all.args_so_far, TYPE_MODE (type),
3487                                        type, data.named_arg))
3488             {
3489               tree local, t;
3490
3491               /* For constant-sized objects, this is trivial; for
3492                  variable-sized objects, we have to play games.  */
3493               if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3494                   && !(flag_stack_check == GENERIC_STACK_CHECK
3495                        && compare_tree_int (DECL_SIZE_UNIT (parm),
3496                                             STACK_CHECK_MAX_VAR_SIZE) > 0))
3497                 {
3498                   local = create_tmp_var (type, get_name (parm));
3499                   DECL_IGNORED_P (local) = 0;
3500                   /* If PARM was addressable, move that flag over
3501                      to the local copy, as its address will be taken,
3502                      not the PARMs.  */
3503                   if (TREE_ADDRESSABLE (parm))
3504                     {
3505                       TREE_ADDRESSABLE (parm) = 0;
3506                       TREE_ADDRESSABLE (local) = 1;
3507                     }
3508                 }
3509               else
3510                 {
3511                   tree ptr_type, addr;
3512
3513                   ptr_type = build_pointer_type (type);
3514                   addr = create_tmp_var (ptr_type, get_name (parm));
3515                   DECL_IGNORED_P (addr) = 0;
3516                   local = build_fold_indirect_ref (addr);
3517
3518                   t = built_in_decls[BUILT_IN_ALLOCA];
3519                   t = build_call_expr (t, 1, DECL_SIZE_UNIT (parm));
3520                   t = fold_convert (ptr_type, t);
3521                   t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
3522                   gimplify_and_add (t, &stmts);
3523                 }
3524
3525               gimplify_assign (local, parm, &stmts);
3526
3527               SET_DECL_VALUE_EXPR (parm, local);
3528               DECL_HAS_VALUE_EXPR_P (parm) = 1;
3529             }
3530         }
3531     }
3532
3533   VEC_free (tree, heap, fnargs);
3534
3535   return stmts;
3536 }
3537 \f
3538 /* Compute the size and offset from the start of the stacked arguments for a
3539    parm passed in mode PASSED_MODE and with type TYPE.
3540
3541    INITIAL_OFFSET_PTR points to the current offset into the stacked
3542    arguments.
3543
3544    The starting offset and size for this parm are returned in
3545    LOCATE->OFFSET and LOCATE->SIZE, respectively.  When IN_REGS is
3546    nonzero, the offset is that of stack slot, which is returned in
3547    LOCATE->SLOT_OFFSET.  LOCATE->ALIGNMENT_PAD is the amount of
3548    padding required from the initial offset ptr to the stack slot.
3549
3550    IN_REGS is nonzero if the argument will be passed in registers.  It will
3551    never be set if REG_PARM_STACK_SPACE is not defined.
3552
3553    FNDECL is the function in which the argument was defined.
3554
3555    There are two types of rounding that are done.  The first, controlled by
3556    FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
3557    list to be aligned to the specific boundary (in bits).  This rounding
3558    affects the initial and starting offsets, but not the argument size.
3559
3560    The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
3561    optionally rounds the size of the parm to PARM_BOUNDARY.  The
3562    initial offset is not affected by this rounding, while the size always
3563    is and the starting offset may be.  */
3564
3565 /*  LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
3566     INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
3567     callers pass in the total size of args so far as
3568     INITIAL_OFFSET_PTR.  LOCATE->SIZE is always positive.  */
3569
3570 void
3571 locate_and_pad_parm (enum machine_mode passed_mode, tree type, int in_regs,
3572                      int partial, tree fndecl ATTRIBUTE_UNUSED,
3573                      struct args_size *initial_offset_ptr,
3574                      struct locate_and_pad_arg_data *locate)
3575 {
3576   tree sizetree;
3577   enum direction where_pad;
3578   unsigned int boundary;
3579   int reg_parm_stack_space = 0;
3580   int part_size_in_regs;
3581
3582 #ifdef REG_PARM_STACK_SPACE
3583   reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
3584
3585   /* If we have found a stack parm before we reach the end of the
3586      area reserved for registers, skip that area.  */
3587   if (! in_regs)
3588     {
3589       if (reg_parm_stack_space > 0)
3590         {
3591           if (initial_offset_ptr->var)
3592             {
3593               initial_offset_ptr->var
3594                 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
3595                               ssize_int (reg_parm_stack_space));
3596               initial_offset_ptr->constant = 0;
3597             }
3598           else if (initial_offset_ptr->constant < reg_parm_stack_space)
3599             initial_offset_ptr->constant = reg_parm_stack_space;
3600         }
3601     }
3602 #endif /* REG_PARM_STACK_SPACE */
3603
3604   part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
3605
3606   sizetree
3607     = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
3608   where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
3609   boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
3610   locate->where_pad = where_pad;
3611
3612   /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT.  */
3613   if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
3614     boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
3615
3616   locate->boundary = boundary;
3617
3618   if (SUPPORTS_STACK_ALIGNMENT)
3619     {
3620       /* stack_alignment_estimated can't change after stack has been
3621          realigned.  */
3622       if (crtl->stack_alignment_estimated < boundary)
3623         {
3624           if (!crtl->stack_realign_processed)
3625             crtl->stack_alignment_estimated = boundary;
3626           else
3627             {
3628               /* If stack is realigned and stack alignment value
3629                  hasn't been finalized, it is OK not to increase
3630                  stack_alignment_estimated.  The bigger alignment
3631                  requirement is recorded in stack_alignment_needed
3632                  below.  */
3633               gcc_assert (!crtl->stack_realign_finalized
3634                           && crtl->stack_realign_needed);
3635             }
3636         }
3637     }
3638
3639   /* Remember if the outgoing parameter requires extra alignment on the
3640      calling function side.  */
3641   if (crtl->stack_alignment_needed < boundary)
3642     crtl->stack_alignment_needed = boundary;
3643   if (crtl->preferred_stack_boundary < boundary)
3644     crtl->preferred_stack_boundary = boundary;
3645
3646 #ifdef ARGS_GROW_DOWNWARD
3647   locate->slot_offset.constant = -initial_offset_ptr->constant;
3648   if (initial_offset_ptr->var)
3649     locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
3650                                           initial_offset_ptr->var);
3651
3652   {
3653     tree s2 = sizetree;
3654     if (where_pad != none
3655         && (!host_integerp (sizetree, 1)
3656             || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
3657       s2 = round_up (s2, PARM_BOUNDARY / BITS_PER_UNIT);
3658     SUB_PARM_SIZE (locate->slot_offset, s2);
3659   }
3660
3661   locate->slot_offset.constant += part_size_in_regs;
3662
3663   if (!in_regs
3664 #ifdef REG_PARM_STACK_SPACE
3665       || REG_PARM_STACK_SPACE (fndecl) > 0
3666 #endif
3667      )
3668     pad_to_arg_alignment (&locate->slot_offset, boundary,
3669                           &locate->alignment_pad);
3670
3671   locate->size.constant = (-initial_offset_ptr->constant
3672                            - locate->slot_offset.constant);
3673   if (initial_offset_ptr->var)
3674     locate->size.var = size_binop (MINUS_EXPR,
3675                                    size_binop (MINUS_EXPR,
3676                                                ssize_int (0),
3677                                                initial_offset_ptr->var),
3678                                    locate->slot_offset.var);
3679
3680   /* Pad_below needs the pre-rounded size to know how much to pad
3681      below.  */
3682   locate->offset = locate->slot_offset;
3683   if (where_pad == downward)
3684     pad_below (&locate->offset, passed_mode, sizetree);
3685
3686 #else /* !ARGS_GROW_DOWNWARD */
3687   if (!in_regs
3688 #ifdef REG_PARM_STACK_SPACE
3689       || REG_PARM_STACK_SPACE (fndecl) > 0
3690 #endif
3691       )
3692     pad_to_arg_alignment (initial_offset_ptr, boundary,
3693                           &locate->alignment_pad);
3694   locate->slot_offset = *initial_offset_ptr;
3695
3696 #ifdef PUSH_ROUNDING
3697   if (passed_mode != BLKmode)
3698     sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
3699 #endif
3700
3701   /* Pad_below needs the pre-rounded size to know how much to pad below
3702      so this must be done before rounding up.  */
3703   locate->offset = locate->slot_offset;
3704   if (where_pad == downward)
3705     pad_below (&locate->offset, passed_mode, sizetree);
3706
3707   if (where_pad != none
3708       && (!host_integerp (sizetree, 1)
3709           || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
3710     sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
3711
3712   ADD_PARM_SIZE (locate->size, sizetree);
3713
3714   locate->size.constant -= part_size_in_regs;
3715 #endif /* ARGS_GROW_DOWNWARD */
3716
3717 #ifdef FUNCTION_ARG_OFFSET
3718   locate->offset.constant += FUNCTION_ARG_OFFSET (passed_mode, type);
3719 #endif
3720 }
3721
3722 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
3723    BOUNDARY is measured in bits, but must be a multiple of a storage unit.  */
3724
3725 static void
3726 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
3727                       struct args_size *alignment_pad)
3728 {
3729   tree save_var = NULL_TREE;
3730   HOST_WIDE_INT save_constant = 0;
3731   int boundary_in_bytes = boundary / BITS_PER_UNIT;
3732   HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
3733
3734 #ifdef SPARC_STACK_BOUNDARY_HACK
3735   /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
3736      the real alignment of %sp.  However, when it does this, the
3737      alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY.  */
3738   if (SPARC_STACK_BOUNDARY_HACK)
3739     sp_offset = 0;
3740 #endif
3741
3742   if (boundary > PARM_BOUNDARY)
3743     {
3744       save_var = offset_ptr->var;
3745       save_constant = offset_ptr->constant;
3746     }
3747
3748   alignment_pad->var = NULL_TREE;
3749   alignment_pad->constant = 0;
3750
3751   if (boundary > BITS_PER_UNIT)
3752     {
3753       if (offset_ptr->var)
3754         {
3755           tree sp_offset_tree = ssize_int (sp_offset);
3756           tree offset = size_binop (PLUS_EXPR,
3757                                     ARGS_SIZE_TREE (*offset_ptr),
3758                                     sp_offset_tree);
3759 #ifdef ARGS_GROW_DOWNWARD
3760           tree rounded = round_down (offset, boundary / BITS_PER_UNIT);
3761 #else
3762           tree rounded = round_up   (offset, boundary / BITS_PER_UNIT);
3763 #endif
3764
3765           offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
3766           /* ARGS_SIZE_TREE includes constant term.  */
3767           offset_ptr->constant = 0;
3768           if (boundary > PARM_BOUNDARY)
3769             alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
3770                                              save_var);
3771         }
3772       else
3773         {
3774           offset_ptr->constant = -sp_offset +
3775 #ifdef ARGS_GROW_DOWNWARD
3776             FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3777 #else
3778             CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3779 #endif
3780             if (boundary > PARM_BOUNDARY)
3781               alignment_pad->constant = offset_ptr->constant - save_constant;
3782         }
3783     }
3784 }
3785
3786 static void
3787 pad_below (struct args_size *offset_ptr, enum machine_mode passed_mode, tree sizetree)
3788 {
3789   if (passed_mode != BLKmode)
3790     {
3791       if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
3792         offset_ptr->constant
3793           += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
3794                / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
3795               - GET_MODE_SIZE (passed_mode));
3796     }
3797   else
3798     {
3799       if (TREE_CODE (sizetree) != INTEGER_CST
3800           || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
3801         {
3802           /* Round the size up to multiple of PARM_BOUNDARY bits.  */
3803           tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
3804           /* Add it in.  */
3805           ADD_PARM_SIZE (*offset_ptr, s2);
3806           SUB_PARM_SIZE (*offset_ptr, sizetree);
3807         }
3808     }
3809 }
3810 \f
3811
3812 /* True if register REGNO was alive at a place where `setjmp' was
3813    called and was set more than once or is an argument.  Such regs may
3814    be clobbered by `longjmp'.  */
3815
3816 static bool
3817 regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
3818 {
3819   /* There appear to be cases where some local vars never reach the
3820      backend but have bogus regnos.  */
3821   if (regno >= max_reg_num ())
3822     return false;
3823
3824   return ((REG_N_SETS (regno) > 1
3825            || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR), regno))
3826           && REGNO_REG_SET_P (setjmp_crosses, regno));
3827 }
3828
3829 /* Walk the tree of blocks describing the binding levels within a
3830    function and warn about variables the might be killed by setjmp or
3831    vfork.  This is done after calling flow_analysis before register
3832    allocation since that will clobber the pseudo-regs to hard
3833    regs.  */
3834
3835 static void
3836 setjmp_vars_warning (bitmap setjmp_crosses, tree block)
3837 {
3838   tree decl, sub;
3839
3840   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
3841     {
3842       if (TREE_CODE (decl) == VAR_DECL
3843           && DECL_RTL_SET_P (decl)
3844           && REG_P (DECL_RTL (decl))
3845           && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
3846         warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
3847                  " %<longjmp%> or %<vfork%>", decl);
3848     }
3849
3850   for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
3851     setjmp_vars_warning (setjmp_crosses, sub);
3852 }
3853
3854 /* Do the appropriate part of setjmp_vars_warning
3855    but for arguments instead of local variables.  */
3856
3857 static void
3858 setjmp_args_warning (bitmap setjmp_crosses)
3859 {
3860   tree decl;
3861   for (decl = DECL_ARGUMENTS (current_function_decl);
3862        decl; decl = TREE_CHAIN (decl))
3863     if (DECL_RTL (decl) != 0
3864         && REG_P (DECL_RTL (decl))
3865         && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
3866       warning (OPT_Wclobbered,
3867                "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
3868                decl);
3869 }
3870
3871 /* Generate warning messages for variables live across setjmp.  */
3872
3873 void
3874 generate_setjmp_warnings (void)
3875 {
3876   bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
3877
3878   if (n_basic_blocks == NUM_FIXED_BLOCKS
3879       || bitmap_empty_p (setjmp_crosses))
3880     return;
3881
3882   setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
3883   setjmp_args_warning (setjmp_crosses);
3884 }
3885
3886 \f
3887 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
3888    and create duplicate blocks.  */
3889 /* ??? Need an option to either create block fragments or to create
3890    abstract origin duplicates of a source block.  It really depends
3891    on what optimization has been performed.  */
3892
3893 void
3894 reorder_blocks (void)
3895 {
3896   tree block = DECL_INITIAL (current_function_decl);
3897   VEC(tree,heap) *block_stack;
3898
3899   if (block == NULL_TREE)
3900     return;
3901
3902   block_stack = VEC_alloc (tree, heap, 10);
3903
3904   /* Reset the TREE_ASM_WRITTEN bit for all blocks.  */
3905   clear_block_marks (block);
3906
3907   /* Prune the old trees away, so that they don't get in the way.  */
3908   BLOCK_SUBBLOCKS (block) = NULL_TREE;
3909   BLOCK_CHAIN (block) = NULL_TREE;
3910
3911   /* Recreate the block tree from the note nesting.  */
3912   reorder_blocks_1 (get_insns (), block, &block_stack);
3913   BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
3914
3915   VEC_free (tree, heap, block_stack);
3916 }
3917
3918 /* Helper function for reorder_blocks.  Reset TREE_ASM_WRITTEN.  */
3919
3920 void
3921 clear_block_marks (tree block)
3922 {
3923   while (block)
3924     {
3925       TREE_ASM_WRITTEN (block) = 0;
3926       clear_block_marks (BLOCK_SUBBLOCKS (block));
3927       block = BLOCK_CHAIN (block);
3928     }
3929 }
3930
3931 static void
3932 reorder_blocks_1 (rtx insns, tree current_block, VEC(tree,heap) **p_block_stack)
3933 {
3934   rtx insn;
3935
3936   for (insn = insns; insn; insn = NEXT_INSN (insn))
3937     {
3938       if (NOTE_P (insn))
3939         {
3940           if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
3941             {
3942               tree block = NOTE_BLOCK (insn);
3943               tree origin;
3944
3945               origin = (BLOCK_FRAGMENT_ORIGIN (block)
3946                         ? BLOCK_FRAGMENT_ORIGIN (block)
3947                         : block);
3948
3949               /* If we have seen this block before, that means it now
3950                  spans multiple address regions.  Create a new fragment.  */
3951               if (TREE_ASM_WRITTEN (block))
3952                 {
3953                   tree new_block = copy_node (block);
3954
3955                   BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
3956                   BLOCK_FRAGMENT_CHAIN (new_block)
3957                     = BLOCK_FRAGMENT_CHAIN (origin);
3958                   BLOCK_FRAGMENT_CHAIN (origin) = new_block;
3959
3960                   NOTE_BLOCK (insn) = new_block;
3961                   block = new_block;
3962                 }
3963
3964               BLOCK_SUBBLOCKS (block) = 0;
3965               TREE_ASM_WRITTEN (block) = 1;
3966               /* When there's only one block for the entire function,
3967                  current_block == block and we mustn't do this, it
3968                  will cause infinite recursion.  */
3969               if (block != current_block)
3970                 {
3971                   if (block != origin)
3972                     gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block);
3973
3974                   BLOCK_SUPERCONTEXT (block) = current_block;
3975                   BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
3976                   BLOCK_SUBBLOCKS (current_block) = block;
3977                   current_block = origin;
3978                 }
3979               VEC_safe_push (tree, heap, *p_block_stack, block);
3980             }
3981           else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
3982             {
3983               NOTE_BLOCK (insn) = VEC_pop (tree, *p_block_stack);
3984               BLOCK_SUBBLOCKS (current_block)
3985                 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
3986               current_block = BLOCK_SUPERCONTEXT (current_block);
3987             }
3988         }
3989     }
3990 }
3991
3992 /* Reverse the order of elements in the chain T of blocks,
3993    and return the new head of the chain (old last element).  */
3994
3995 tree
3996 blocks_nreverse (tree t)
3997 {
3998   tree prev = 0, decl, next;
3999   for (decl = t; decl; decl = next)
4000     {
4001       next = BLOCK_CHAIN (decl);
4002       BLOCK_CHAIN (decl) = prev;
4003       prev = decl;
4004     }
4005   return prev;
4006 }
4007
4008 /* Count the subblocks of the list starting with BLOCK.  If VECTOR is
4009    non-NULL, list them all into VECTOR, in a depth-first preorder
4010    traversal of the block tree.  Also clear TREE_ASM_WRITTEN in all
4011    blocks.  */
4012
4013 static int
4014 all_blocks (tree block, tree *vector)
4015 {
4016   int n_blocks = 0;
4017
4018   while (block)
4019     {
4020       TREE_ASM_WRITTEN (block) = 0;
4021
4022       /* Record this block.  */
4023       if (vector)
4024         vector[n_blocks] = block;
4025
4026       ++n_blocks;
4027
4028       /* Record the subblocks, and their subblocks...  */
4029       n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4030                               vector ? vector + n_blocks : 0);
4031       block = BLOCK_CHAIN (block);
4032     }
4033
4034   return n_blocks;
4035 }
4036
4037 /* Return a vector containing all the blocks rooted at BLOCK.  The
4038    number of elements in the vector is stored in N_BLOCKS_P.  The
4039    vector is dynamically allocated; it is the caller's responsibility
4040    to call `free' on the pointer returned.  */
4041
4042 static tree *
4043 get_block_vector (tree block, int *n_blocks_p)
4044 {
4045   tree *block_vector;
4046
4047   *n_blocks_p = all_blocks (block, NULL);
4048   block_vector = XNEWVEC (tree, *n_blocks_p);
4049   all_blocks (block, block_vector);
4050
4051   return block_vector;
4052 }
4053
4054 static GTY(()) int next_block_index = 2;
4055
4056 /* Set BLOCK_NUMBER for all the blocks in FN.  */
4057
4058 void
4059 number_blocks (tree fn)
4060 {
4061   int i;
4062   int n_blocks;
4063   tree *block_vector;
4064
4065   /* For SDB and XCOFF debugging output, we start numbering the blocks
4066      from 1 within each function, rather than keeping a running
4067      count.  */
4068 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
4069   if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
4070     next_block_index = 1;
4071 #endif
4072
4073   block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4074
4075   /* The top-level BLOCK isn't numbered at all.  */
4076   for (i = 1; i < n_blocks; ++i)
4077     /* We number the blocks from two.  */
4078     BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4079
4080   free (block_vector);
4081
4082   return;
4083 }
4084
4085 /* If VAR is present in a subblock of BLOCK, return the subblock.  */
4086
4087 DEBUG_FUNCTION tree
4088 debug_find_var_in_block_tree (tree var, tree block)
4089 {
4090   tree t;
4091
4092   for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4093     if (t == var)
4094       return block;
4095
4096   for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4097     {
4098       tree ret = debug_find_var_in_block_tree (var, t);
4099       if (ret)
4100         return ret;
4101     }
4102
4103   return NULL_TREE;
4104 }
4105 \f
4106 /* Keep track of whether we're in a dummy function context.  If we are,
4107    we don't want to invoke the set_current_function hook, because we'll
4108    get into trouble if the hook calls target_reinit () recursively or
4109    when the initial initialization is not yet complete.  */
4110
4111 static bool in_dummy_function;
4112
4113 /* Invoke the target hook when setting cfun.  Update the optimization options
4114    if the function uses different options than the default.  */
4115
4116 static void
4117 invoke_set_current_function_hook (tree fndecl)
4118 {
4119   if (!in_dummy_function)
4120     {
4121       tree opts = ((fndecl)
4122                    ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4123                    : optimization_default_node);
4124
4125       if (!opts)
4126         opts = optimization_default_node;
4127
4128       /* Change optimization options if needed.  */
4129       if (optimization_current_node != opts)
4130         {
4131           optimization_current_node = opts;
4132           cl_optimization_restore (TREE_OPTIMIZATION (opts));
4133         }
4134
4135       targetm.set_current_function (fndecl);
4136     }
4137 }
4138
4139 /* cfun should never be set directly; use this function.  */
4140
4141 void
4142 set_cfun (struct function *new_cfun)
4143 {
4144   if (cfun != new_cfun)
4145     {
4146       cfun = new_cfun;
4147       invoke_set_current_function_hook (new_cfun ? new_cfun->decl : NULL_TREE);
4148     }
4149 }
4150
4151 /* Initialized with NOGC, making this poisonous to the garbage collector.  */
4152
4153 static VEC(function_p,heap) *cfun_stack;
4154
4155 /* Push the current cfun onto the stack, and set cfun to new_cfun.  */
4156
4157 void
4158 push_cfun (struct function *new_cfun)
4159 {
4160   VEC_safe_push (function_p, heap, cfun_stack, cfun);
4161   set_cfun (new_cfun);
4162 }
4163
4164 /* Pop cfun from the stack.  */
4165
4166 void
4167 pop_cfun (void)
4168 {
4169   struct function *new_cfun = VEC_pop (function_p, cfun_stack);
4170   set_cfun (new_cfun);
4171 }
4172
4173 /* Return value of funcdef and increase it.  */
4174 int
4175 get_next_funcdef_no (void)
4176 {
4177   return funcdef_no++;
4178 }
4179
4180 /* Allocate a function structure for FNDECL and set its contents
4181    to the defaults.  Set cfun to the newly-allocated object.
4182    Some of the helper functions invoked during initialization assume
4183    that cfun has already been set.  Therefore, assign the new object
4184    directly into cfun and invoke the back end hook explicitly at the
4185    very end, rather than initializing a temporary and calling set_cfun
4186    on it.
4187
4188    ABSTRACT_P is true if this is a function that will never be seen by
4189    the middle-end.  Such functions are front-end concepts (like C++
4190    function templates) that do not correspond directly to functions
4191    placed in object files.  */
4192
4193 void
4194 allocate_struct_function (tree fndecl, bool abstract_p)
4195 {
4196   tree result;
4197   tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4198
4199   cfun = GGC_CNEW (struct function);
4200
4201   init_eh_for_function ();
4202
4203   if (init_machine_status)
4204     cfun->machine = (*init_machine_status) ();
4205
4206 #ifdef OVERRIDE_ABI_FORMAT
4207   OVERRIDE_ABI_FORMAT (fndecl);
4208 #endif
4209
4210   invoke_set_current_function_hook (fndecl);
4211
4212   if (fndecl != NULL_TREE)
4213     {
4214       DECL_STRUCT_FUNCTION (fndecl) = cfun;
4215       cfun->decl = fndecl;
4216       current_function_funcdef_no = get_next_funcdef_no ();
4217
4218       result = DECL_RESULT (fndecl);
4219       if (!abstract_p && aggregate_value_p (result, fndecl))
4220         {
4221 #ifdef PCC_STATIC_STRUCT_RETURN
4222           cfun->returns_pcc_struct = 1;
4223 #endif
4224           cfun->returns_struct = 1;
4225         }
4226
4227       cfun->stdarg
4228         = (fntype
4229            && TYPE_ARG_TYPES (fntype) != 0
4230            && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4231                != void_type_node));
4232
4233       /* Assume all registers in stdarg functions need to be saved.  */
4234       cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4235       cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4236
4237       /* ??? This could be set on a per-function basis by the front-end
4238          but is this worth the hassle?  */
4239       cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4240     }
4241 }
4242
4243 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4244    instead of just setting it.  */
4245
4246 void
4247 push_struct_function (tree fndecl)
4248 {
4249   VEC_safe_push (function_p, heap, cfun_stack, cfun);
4250   allocate_struct_function (fndecl, false);
4251 }
4252
4253 /* Reset crtl and other non-struct-function variables to defaults as
4254    appropriate for emitting rtl at the start of a function.  */
4255
4256 static void
4257 prepare_function_start (void)
4258 {
4259   gcc_assert (!crtl->emit.x_last_insn);
4260   init_temp_slots ();
4261   init_emit ();
4262   init_varasm_status ();
4263   init_expr ();
4264   default_rtl_profile ();
4265
4266   cse_not_expected = ! optimize;
4267
4268   /* Caller save not needed yet.  */
4269   caller_save_needed = 0;
4270
4271   /* We haven't done register allocation yet.  */
4272   reg_renumber = 0;
4273
4274   /* Indicate that we have not instantiated virtual registers yet.  */
4275   virtuals_instantiated = 0;
4276
4277   /* Indicate that we want CONCATs now.  */
4278   generating_concat_p = 1;
4279
4280   /* Indicate we have no need of a frame pointer yet.  */
4281   frame_pointer_needed = 0;
4282 }
4283
4284 /* Initialize the rtl expansion mechanism so that we can do simple things
4285    like generate sequences.  This is used to provide a context during global
4286    initialization of some passes.  You must call expand_dummy_function_end
4287    to exit this context.  */
4288
4289 void
4290 init_dummy_function_start (void)
4291 {
4292   gcc_assert (!in_dummy_function);
4293   in_dummy_function = true;
4294   push_struct_function (NULL_TREE);
4295   prepare_function_start ();
4296 }
4297
4298 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4299    and initialize static variables for generating RTL for the statements
4300    of the function.  */
4301
4302 void
4303 init_function_start (tree subr)
4304 {
4305   if (subr && DECL_STRUCT_FUNCTION (subr))
4306     set_cfun (DECL_STRUCT_FUNCTION (subr));
4307   else
4308     allocate_struct_function (subr, false);
4309   prepare_function_start ();
4310
4311   /* Warn if this value is an aggregate type,
4312      regardless of which calling convention we are using for it.  */
4313   if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
4314     warning (OPT_Waggregate_return, "function returns an aggregate");
4315 }
4316
4317 /* Make sure all values used by the optimization passes have sane defaults.  */
4318 unsigned int
4319 init_function_for_compilation (void)
4320 {
4321   reg_renumber = 0;
4322   return 0;
4323 }
4324
4325 struct rtl_opt_pass pass_init_function =
4326 {
4327  {
4328   RTL_PASS,
4329   "*init_function",                     /* name */
4330   NULL,                                 /* gate */
4331   init_function_for_compilation,        /* execute */
4332   NULL,                                 /* sub */
4333   NULL,                                 /* next */
4334   0,                                    /* static_pass_number */
4335   TV_NONE,                              /* tv_id */
4336   0,                                    /* properties_required */
4337   0,                                    /* properties_provided */
4338   0,                                    /* properties_destroyed */
4339   0,                                    /* todo_flags_start */
4340   0                                     /* todo_flags_finish */
4341  }
4342 };
4343
4344
4345 void
4346 expand_main_function (void)
4347 {
4348 #if (defined(INVOKE__main)                              \
4349      || (!defined(HAS_INIT_SECTION)                     \
4350          && !defined(INIT_SECTION_ASM_OP)               \
4351          && !defined(INIT_ARRAY_SECTION_ASM_OP)))
4352   emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
4353 #endif
4354 }
4355 \f
4356 /* Expand code to initialize the stack_protect_guard.  This is invoked at
4357    the beginning of a function to be protected.  */
4358
4359 #ifndef HAVE_stack_protect_set
4360 # define HAVE_stack_protect_set         0
4361 # define gen_stack_protect_set(x,y)     (gcc_unreachable (), NULL_RTX)
4362 #endif
4363
4364 void
4365 stack_protect_prologue (void)
4366 {
4367   tree guard_decl = targetm.stack_protect_guard ();
4368   rtx x, y;
4369
4370   x = expand_normal (crtl->stack_protect_guard);
4371   y = expand_normal (guard_decl);
4372
4373   /* Allow the target to copy from Y to X without leaking Y into a
4374      register.  */
4375   if (HAVE_stack_protect_set)
4376     {
4377       rtx insn = gen_stack_protect_set (x, y);
4378       if (insn)
4379         {
4380           emit_insn (insn);
4381           return;
4382         }
4383     }
4384
4385   /* Otherwise do a straight move.  */
4386   emit_move_insn (x, y);
4387 }
4388
4389 /* Expand code to verify the stack_protect_guard.  This is invoked at
4390    the end of a function to be protected.  */
4391
4392 #ifndef HAVE_stack_protect_test
4393 # define HAVE_stack_protect_test                0
4394 # define gen_stack_protect_test(x, y, z)        (gcc_unreachable (), NULL_RTX)
4395 #endif
4396
4397 void
4398 stack_protect_epilogue (void)
4399 {
4400   tree guard_decl = targetm.stack_protect_guard ();
4401   rtx label = gen_label_rtx ();
4402   rtx x, y, tmp;
4403
4404   x = expand_normal (crtl->stack_protect_guard);
4405   y = expand_normal (guard_decl);
4406
4407   /* Allow the target to compare Y with X without leaking either into
4408      a register.  */
4409   switch (HAVE_stack_protect_test != 0)
4410     {
4411     case 1:
4412       tmp = gen_stack_protect_test (x, y, label);
4413       if (tmp)
4414         {
4415           emit_insn (tmp);
4416           break;
4417         }
4418       /* FALLTHRU */
4419
4420     default:
4421       emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
4422       break;
4423     }
4424
4425   /* The noreturn predictor has been moved to the tree level.  The rtl-level
4426      predictors estimate this branch about 20%, which isn't enough to get
4427      things moved out of line.  Since this is the only extant case of adding
4428      a noreturn function at the rtl level, it doesn't seem worth doing ought
4429      except adding the prediction by hand.  */
4430   tmp = get_last_insn ();
4431   if (JUMP_P (tmp))
4432     predict_insn_def (tmp, PRED_NORETURN, TAKEN);
4433
4434   expand_expr_stmt (targetm.stack_protect_fail ());
4435   emit_label (label);
4436 }
4437 \f
4438 /* Start the RTL for a new function, and set variables used for
4439    emitting RTL.
4440    SUBR is the FUNCTION_DECL node.
4441    PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
4442    the function's parameters, which must be run at any return statement.  */
4443
4444 void
4445 expand_function_start (tree subr)
4446 {
4447   /* Make sure volatile mem refs aren't considered
4448      valid operands of arithmetic insns.  */
4449   init_recog_no_volatile ();
4450
4451   crtl->profile
4452     = (profile_flag
4453        && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
4454
4455   crtl->limit_stack
4456     = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
4457
4458   /* Make the label for return statements to jump to.  Do not special
4459      case machines with special return instructions -- they will be
4460      handled later during jump, ifcvt, or epilogue creation.  */
4461   return_label = gen_label_rtx ();
4462
4463   /* Initialize rtx used to return the value.  */
4464   /* Do this before assign_parms so that we copy the struct value address
4465      before any library calls that assign parms might generate.  */
4466
4467   /* Decide whether to return the value in memory or in a register.  */
4468   if (aggregate_value_p (DECL_RESULT (subr), subr))
4469     {
4470       /* Returning something that won't go in a register.  */
4471       rtx value_address = 0;
4472
4473 #ifdef PCC_STATIC_STRUCT_RETURN
4474       if (cfun->returns_pcc_struct)
4475         {
4476           int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
4477           value_address = assemble_static_space (size);
4478         }
4479       else
4480 #endif
4481         {
4482           rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
4483           /* Expect to be passed the address of a place to store the value.
4484              If it is passed as an argument, assign_parms will take care of
4485              it.  */
4486           if (sv)
4487             {
4488               value_address = gen_reg_rtx (Pmode);
4489               emit_move_insn (value_address, sv);
4490             }
4491         }
4492       if (value_address)
4493         {
4494           rtx x = value_address;
4495           if (!DECL_BY_REFERENCE (DECL_RESULT (subr)))
4496             {
4497               x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), x);
4498               set_mem_attributes (x, DECL_RESULT (subr), 1);
4499             }
4500           SET_DECL_RTL (DECL_RESULT (subr), x);
4501         }
4502     }
4503   else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
4504     /* If return mode is void, this decl rtl should not be used.  */
4505     SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
4506   else
4507     {
4508       /* Compute the return values into a pseudo reg, which we will copy
4509          into the true return register after the cleanups are done.  */
4510       tree return_type = TREE_TYPE (DECL_RESULT (subr));
4511       if (TYPE_MODE (return_type) != BLKmode
4512           && targetm.calls.return_in_msb (return_type))
4513         /* expand_function_end will insert the appropriate padding in
4514            this case.  Use the return value's natural (unpadded) mode
4515            within the function proper.  */
4516         SET_DECL_RTL (DECL_RESULT (subr),
4517                       gen_reg_rtx (TYPE_MODE (return_type)));
4518       else
4519         {
4520           /* In order to figure out what mode to use for the pseudo, we
4521              figure out what the mode of the eventual return register will
4522              actually be, and use that.  */
4523           rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
4524
4525           /* Structures that are returned in registers are not
4526              aggregate_value_p, so we may see a PARALLEL or a REG.  */
4527           if (REG_P (hard_reg))
4528             SET_DECL_RTL (DECL_RESULT (subr),
4529                           gen_reg_rtx (GET_MODE (hard_reg)));
4530           else
4531             {
4532               gcc_assert (GET_CODE (hard_reg) == PARALLEL);
4533               SET_DECL_RTL (DECL_RESULT (subr), gen_group_rtx (hard_reg));
4534             }
4535         }
4536
4537       /* Set DECL_REGISTER flag so that expand_function_end will copy the
4538          result to the real return register(s).  */
4539       DECL_REGISTER (DECL_RESULT (subr)) = 1;
4540     }
4541
4542   /* Initialize rtx for parameters and local variables.
4543      In some cases this requires emitting insns.  */
4544   assign_parms (subr);
4545
4546   /* If function gets a static chain arg, store it.  */
4547   if (cfun->static_chain_decl)
4548     {
4549       tree parm = cfun->static_chain_decl;
4550       rtx local, chain, insn;
4551
4552       local = gen_reg_rtx (Pmode);
4553       chain = targetm.calls.static_chain (current_function_decl, true);
4554
4555       set_decl_incoming_rtl (parm, chain, false);
4556       SET_DECL_RTL (parm, local);
4557       mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4558
4559       insn = emit_move_insn (local, chain);
4560
4561       /* Mark the register as eliminable, similar to parameters.  */
4562       if (MEM_P (chain)
4563           && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
4564         set_unique_reg_note (insn, REG_EQUIV, chain);
4565     }
4566
4567   /* If the function receives a non-local goto, then store the
4568      bits we need to restore the frame pointer.  */
4569   if (cfun->nonlocal_goto_save_area)
4570     {
4571       tree t_save;
4572       rtx r_save;
4573
4574       /* ??? We need to do this save early.  Unfortunately here is
4575          before the frame variable gets declared.  Help out...  */
4576       tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
4577       if (!DECL_RTL_SET_P (var))
4578         expand_decl (var);
4579
4580       t_save = build4 (ARRAY_REF, ptr_type_node,
4581                        cfun->nonlocal_goto_save_area,
4582                        integer_zero_node, NULL_TREE, NULL_TREE);
4583       r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
4584       r_save = convert_memory_address (Pmode, r_save);
4585
4586       emit_move_insn (r_save, targetm.builtin_setjmp_frame_value ());
4587       update_nonlocal_goto_save_area ();
4588     }
4589
4590   /* The following was moved from init_function_start.
4591      The move is supposed to make sdb output more accurate.  */
4592   /* Indicate the beginning of the function body,
4593      as opposed to parm setup.  */
4594   emit_note (NOTE_INSN_FUNCTION_BEG);
4595
4596   gcc_assert (NOTE_P (get_last_insn ()));
4597
4598   parm_birth_insn = get_last_insn ();
4599
4600   if (crtl->profile)
4601     {
4602 #ifdef PROFILE_HOOK
4603       PROFILE_HOOK (current_function_funcdef_no);
4604 #endif
4605     }
4606
4607   /* After the display initializations is where the stack checking
4608      probe should go.  */
4609   if(flag_stack_check)
4610     stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
4611
4612   /* Make sure there is a line number after the function entry setup code.  */
4613   force_next_line_note ();
4614 }
4615 \f
4616 /* Undo the effects of init_dummy_function_start.  */
4617 void
4618 expand_dummy_function_end (void)
4619 {
4620   gcc_assert (in_dummy_function);
4621
4622   /* End any sequences that failed to be closed due to syntax errors.  */
4623   while (in_sequence_p ())
4624     end_sequence ();
4625
4626   /* Outside function body, can't compute type's actual size
4627      until next function's body starts.  */
4628
4629   free_after_parsing (cfun);
4630   free_after_compilation (cfun);
4631   pop_cfun ();
4632   in_dummy_function = false;
4633 }
4634
4635 /* Call DOIT for each hard register used as a return value from
4636    the current function.  */
4637
4638 void
4639 diddle_return_value (void (*doit) (rtx, void *), void *arg)
4640 {
4641   rtx outgoing = crtl->return_rtx;
4642
4643   if (! outgoing)
4644     return;
4645
4646   if (REG_P (outgoing))
4647     (*doit) (outgoing, arg);
4648   else if (GET_CODE (outgoing) == PARALLEL)
4649     {
4650       int i;
4651
4652       for (i = 0; i < XVECLEN (outgoing, 0); i++)
4653         {
4654           rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
4655
4656           if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
4657             (*doit) (x, arg);
4658         }
4659     }
4660 }
4661
4662 static void
4663 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4664 {
4665   emit_clobber (reg);
4666 }
4667
4668 void
4669 clobber_return_register (void)
4670 {
4671   diddle_return_value (do_clobber_return_reg, NULL);
4672
4673   /* In case we do use pseudo to return value, clobber it too.  */
4674   if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
4675     {
4676       tree decl_result = DECL_RESULT (current_function_decl);
4677       rtx decl_rtl = DECL_RTL (decl_result);
4678       if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
4679         {
4680           do_clobber_return_reg (decl_rtl, NULL);
4681         }
4682     }
4683 }
4684
4685 static void
4686 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4687 {
4688   emit_use (reg);
4689 }
4690
4691 static void
4692 use_return_register (void)
4693 {
4694   diddle_return_value (do_use_return_reg, NULL);
4695 }
4696
4697 /* Possibly warn about unused parameters.  */
4698 void
4699 do_warn_unused_parameter (tree fn)
4700 {
4701   tree decl;
4702
4703   for (decl = DECL_ARGUMENTS (fn);
4704        decl; decl = TREE_CHAIN (decl))
4705     if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
4706         && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
4707         && !TREE_NO_WARNING (decl))
4708       warning (OPT_Wunused_parameter, "unused parameter %q+D", decl);
4709 }
4710
4711 static GTY(()) rtx initial_trampoline;
4712
4713 /* Generate RTL for the end of the current function.  */
4714
4715 void
4716 expand_function_end (void)
4717 {
4718   rtx clobber_after;
4719
4720   /* If arg_pointer_save_area was referenced only from a nested
4721      function, we will not have initialized it yet.  Do that now.  */
4722   if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
4723     get_arg_pointer_save_area ();
4724
4725   /* If we are doing generic stack checking and this function makes calls,
4726      do a stack probe at the start of the function to ensure we have enough
4727      space for another stack frame.  */
4728   if (flag_stack_check == GENERIC_STACK_CHECK)
4729     {
4730       rtx insn, seq;
4731
4732       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4733         if (CALL_P (insn))
4734           {
4735             rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
4736             start_sequence ();
4737             if (STACK_CHECK_MOVING_SP)
4738               anti_adjust_stack_and_probe (max_frame_size, true);
4739             else
4740               probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
4741             seq = get_insns ();
4742             end_sequence ();
4743             emit_insn_before (seq, stack_check_probe_note);
4744             break;
4745           }
4746     }
4747
4748   /* End any sequences that failed to be closed due to syntax errors.  */
4749   while (in_sequence_p ())
4750     end_sequence ();
4751
4752   clear_pending_stack_adjust ();
4753   do_pending_stack_adjust ();
4754
4755   /* Output a linenumber for the end of the function.
4756      SDB depends on this.  */
4757   force_next_line_note ();
4758   set_curr_insn_source_location (input_location);
4759
4760   /* Before the return label (if any), clobber the return
4761      registers so that they are not propagated live to the rest of
4762      the function.  This can only happen with functions that drop
4763      through; if there had been a return statement, there would
4764      have either been a return rtx, or a jump to the return label.
4765
4766      We delay actual code generation after the current_function_value_rtx
4767      is computed.  */
4768   clobber_after = get_last_insn ();
4769
4770   /* Output the label for the actual return from the function.  */
4771   emit_label (return_label);
4772
4773   if (USING_SJLJ_EXCEPTIONS)
4774     {
4775       /* Let except.c know where it should emit the call to unregister
4776          the function context for sjlj exceptions.  */
4777       if (flag_exceptions)
4778         sjlj_emit_function_exit_after (get_last_insn ());
4779     }
4780   else
4781     {
4782       /* We want to ensure that instructions that may trap are not
4783          moved into the epilogue by scheduling, because we don't
4784          always emit unwind information for the epilogue.  */
4785       if (cfun->can_throw_non_call_exceptions)
4786         emit_insn (gen_blockage ());
4787     }
4788
4789   /* If this is an implementation of throw, do what's necessary to
4790      communicate between __builtin_eh_return and the epilogue.  */
4791   expand_eh_return ();
4792
4793   /* If scalar return value was computed in a pseudo-reg, or was a named
4794      return value that got dumped to the stack, copy that to the hard
4795      return register.  */
4796   if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
4797     {
4798       tree decl_result = DECL_RESULT (current_function_decl);
4799       rtx decl_rtl = DECL_RTL (decl_result);
4800
4801       if (REG_P (decl_rtl)
4802           ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
4803           : DECL_REGISTER (decl_result))
4804         {
4805           rtx real_decl_rtl = crtl->return_rtx;
4806
4807           /* This should be set in assign_parms.  */
4808           gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
4809
4810           /* If this is a BLKmode structure being returned in registers,
4811              then use the mode computed in expand_return.  Note that if
4812              decl_rtl is memory, then its mode may have been changed,
4813              but that crtl->return_rtx has not.  */
4814           if (GET_MODE (real_decl_rtl) == BLKmode)
4815             PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
4816
4817           /* If a non-BLKmode return value should be padded at the least
4818              significant end of the register, shift it left by the appropriate
4819              amount.  BLKmode results are handled using the group load/store
4820              machinery.  */
4821           if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
4822               && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
4823             {
4824               emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
4825                                            REGNO (real_decl_rtl)),
4826                               decl_rtl);
4827               shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
4828             }
4829           /* If a named return value dumped decl_return to memory, then
4830              we may need to re-do the PROMOTE_MODE signed/unsigned
4831              extension.  */
4832           else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
4833             {
4834               int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
4835               promote_function_mode (TREE_TYPE (decl_result),
4836                                      GET_MODE (decl_rtl), &unsignedp,
4837                                      TREE_TYPE (current_function_decl), 1);
4838
4839               convert_move (real_decl_rtl, decl_rtl, unsignedp);
4840             }
4841           else if (GET_CODE (real_decl_rtl) == PARALLEL)
4842             {
4843               /* If expand_function_start has created a PARALLEL for decl_rtl,
4844                  move the result to the real return registers.  Otherwise, do
4845                  a group load from decl_rtl for a named return.  */
4846               if (GET_CODE (decl_rtl) == PARALLEL)
4847                 emit_group_move (real_decl_rtl, decl_rtl);
4848               else
4849                 emit_group_load (real_decl_rtl, decl_rtl,
4850                                  TREE_TYPE (decl_result),
4851                                  int_size_in_bytes (TREE_TYPE (decl_result)));
4852             }
4853           /* In the case of complex integer modes smaller than a word, we'll
4854              need to generate some non-trivial bitfield insertions.  Do that
4855              on a pseudo and not the hard register.  */
4856           else if (GET_CODE (decl_rtl) == CONCAT
4857                    && GET_MODE_CLASS (GET_MODE (decl_rtl)) == MODE_COMPLEX_INT
4858                    && GET_MODE_BITSIZE (GET_MODE (decl_rtl)) <= BITS_PER_WORD)
4859             {
4860               int old_generating_concat_p;
4861               rtx tmp;
4862
4863               old_generating_concat_p = generating_concat_p;
4864               generating_concat_p = 0;
4865               tmp = gen_reg_rtx (GET_MODE (decl_rtl));
4866               generating_concat_p = old_generating_concat_p;
4867
4868               emit_move_insn (tmp, decl_rtl);
4869               emit_move_insn (real_decl_rtl, tmp);
4870             }
4871           else
4872             emit_move_insn (real_decl_rtl, decl_rtl);
4873         }
4874     }
4875
4876   /* If returning a structure, arrange to return the address of the value
4877      in a place where debuggers expect to find it.
4878
4879      If returning a structure PCC style,
4880      the caller also depends on this value.
4881      And cfun->returns_pcc_struct is not necessarily set.  */
4882   if (cfun->returns_struct
4883       || cfun->returns_pcc_struct)
4884     {
4885       rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
4886       tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
4887       rtx outgoing;
4888
4889       if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
4890         type = TREE_TYPE (type);
4891       else
4892         value_address = XEXP (value_address, 0);
4893
4894       outgoing = targetm.calls.function_value (build_pointer_type (type),
4895                                                current_function_decl, true);
4896
4897       /* Mark this as a function return value so integrate will delete the
4898          assignment and USE below when inlining this function.  */
4899       REG_FUNCTION_VALUE_P (outgoing) = 1;
4900
4901       /* The address may be ptr_mode and OUTGOING may be Pmode.  */
4902       value_address = convert_memory_address (GET_MODE (outgoing),
4903                                               value_address);
4904
4905       emit_move_insn (outgoing, value_address);
4906
4907       /* Show return register used to hold result (in this case the address
4908          of the result.  */
4909       crtl->return_rtx = outgoing;
4910     }
4911
4912   /* Emit the actual code to clobber return register.  */
4913   {
4914     rtx seq;
4915
4916     start_sequence ();
4917     clobber_return_register ();
4918     seq = get_insns ();
4919     end_sequence ();
4920
4921     emit_insn_after (seq, clobber_after);
4922   }
4923
4924   /* Output the label for the naked return from the function.  */
4925   if (naked_return_label)
4926     emit_label (naked_return_label);
4927
4928   /* @@@ This is a kludge.  We want to ensure that instructions that
4929      may trap are not moved into the epilogue by scheduling, because
4930      we don't always emit unwind information for the epilogue.  */
4931   if (!USING_SJLJ_EXCEPTIONS && cfun->can_throw_non_call_exceptions)
4932     emit_insn (gen_blockage ());
4933
4934   /* If stack protection is enabled for this function, check the guard.  */
4935   if (crtl->stack_protect_guard)
4936     stack_protect_epilogue ();
4937
4938   /* If we had calls to alloca, and this machine needs
4939      an accurate stack pointer to exit the function,
4940      insert some code to save and restore the stack pointer.  */
4941   if (! EXIT_IGNORE_STACK
4942       && cfun->calls_alloca)
4943     {
4944       rtx tem = 0;
4945
4946       emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
4947       emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
4948     }
4949
4950   /* ??? This should no longer be necessary since stupid is no longer with
4951      us, but there are some parts of the compiler (eg reload_combine, and
4952      sh mach_dep_reorg) that still try and compute their own lifetime info
4953      instead of using the general framework.  */
4954   use_return_register ();
4955 }
4956
4957 rtx
4958 get_arg_pointer_save_area (void)
4959 {
4960   rtx ret = arg_pointer_save_area;
4961
4962   if (! ret)
4963     {
4964       ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
4965       arg_pointer_save_area = ret;
4966     }
4967
4968   if (! crtl->arg_pointer_save_area_init)
4969     {
4970       rtx seq;
4971
4972       /* Save the arg pointer at the beginning of the function.  The
4973          generated stack slot may not be a valid memory address, so we
4974          have to check it and fix it if necessary.  */
4975       start_sequence ();
4976       emit_move_insn (validize_mem (ret),
4977                       crtl->args.internal_arg_pointer);
4978       seq = get_insns ();
4979       end_sequence ();
4980
4981       push_topmost_sequence ();
4982       emit_insn_after (seq, entry_of_function ());
4983       pop_topmost_sequence ();
4984     }
4985
4986   return ret;
4987 }
4988 \f
4989 /* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
4990    for the first time.  */
4991
4992 static void
4993 record_insns (rtx insns, rtx end, htab_t *hashp)
4994 {
4995   rtx tmp;
4996   htab_t hash = *hashp;
4997
4998   if (hash == NULL)
4999     *hashp = hash
5000       = htab_create_ggc (17, htab_hash_pointer, htab_eq_pointer, NULL);
5001
5002   for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5003     {
5004       void **slot = htab_find_slot (hash, tmp, INSERT);
5005       gcc_assert (*slot == NULL);
5006       *slot = tmp;
5007     }
5008 }
5009
5010 /* INSN has been duplicated as COPY, as part of duping a basic block.
5011    If INSN is an epilogue insn, then record COPY as epilogue as well.  */
5012
5013 void
5014 maybe_copy_epilogue_insn (rtx insn, rtx copy)
5015 {
5016   void **slot;
5017
5018   if (epilogue_insn_hash == NULL
5019       || htab_find (epilogue_insn_hash, insn) == NULL)
5020     return;
5021
5022   slot = htab_find_slot (epilogue_insn_hash, copy, INSERT);
5023   gcc_assert (*slot == NULL);
5024   *slot = copy;
5025 }
5026
5027 /* Set the locator of the insn chain starting at INSN to LOC.  */
5028 static void
5029 set_insn_locators (rtx insn, int loc)
5030 {
5031   while (insn != NULL_RTX)
5032     {
5033       if (INSN_P (insn))
5034         INSN_LOCATOR (insn) = loc;
5035       insn = NEXT_INSN (insn);
5036     }
5037 }
5038
5039 /* Determine if any INSNs in HASH are, or are part of, INSN.  Because
5040    we can be running after reorg, SEQUENCE rtl is possible.  */
5041
5042 static bool
5043 contains (const_rtx insn, htab_t hash)
5044 {
5045   if (hash == NULL)
5046     return false;
5047
5048   if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5049     {
5050       int i;
5051       for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
5052         if (htab_find (hash, XVECEXP (PATTERN (insn), 0, i)))
5053           return true;
5054       return false;
5055     }
5056
5057   return htab_find (hash, insn) != NULL;
5058 }
5059
5060 int
5061 prologue_epilogue_contains (const_rtx insn)
5062 {
5063   if (contains (insn, prologue_insn_hash))
5064     return 1;
5065   if (contains (insn, epilogue_insn_hash))
5066     return 1;
5067   return 0;
5068 }
5069
5070 #ifdef HAVE_return
5071 /* Insert gen_return at the end of block BB.  This also means updating
5072    block_for_insn appropriately.  */
5073
5074 static void
5075 emit_return_into_block (basic_block bb)
5076 {
5077   emit_jump_insn_after (gen_return (), BB_END (bb));
5078 }
5079 #endif /* HAVE_return */
5080
5081 /* Generate the prologue and epilogue RTL if the machine supports it.  Thread
5082    this into place with notes indicating where the prologue ends and where
5083    the epilogue begins.  Update the basic block information when possible.  */
5084
5085 static void
5086 thread_prologue_and_epilogue_insns (void)
5087 {
5088   int inserted = 0;
5089   edge e;
5090 #if defined (HAVE_sibcall_epilogue) || defined (HAVE_epilogue) || defined (HAVE_return) || defined (HAVE_prologue)
5091   rtx seq;
5092 #endif
5093 #if defined (HAVE_epilogue) || defined(HAVE_return)
5094   rtx epilogue_end = NULL_RTX;
5095 #endif
5096   edge_iterator ei;
5097
5098   rtl_profile_for_bb (ENTRY_BLOCK_PTR);
5099 #ifdef HAVE_prologue
5100   if (HAVE_prologue)
5101     {
5102       start_sequence ();
5103       seq = gen_prologue ();
5104       emit_insn (seq);
5105
5106       /* Insert an explicit USE for the frame pointer
5107          if the profiling is on and the frame pointer is required.  */
5108       if (crtl->profile && frame_pointer_needed)
5109         emit_use (hard_frame_pointer_rtx);
5110
5111       /* Retain a map of the prologue insns.  */
5112       record_insns (seq, NULL, &prologue_insn_hash);
5113       emit_note (NOTE_INSN_PROLOGUE_END);
5114
5115 #ifndef PROFILE_BEFORE_PROLOGUE
5116       /* Ensure that instructions are not moved into the prologue when
5117          profiling is on.  The call to the profiling routine can be
5118          emitted within the live range of a call-clobbered register.  */
5119       if (crtl->profile)
5120         emit_insn (gen_blockage ());
5121 #endif
5122
5123       seq = get_insns ();
5124       end_sequence ();
5125       set_insn_locators (seq, prologue_locator);
5126
5127       /* Can't deal with multiple successors of the entry block
5128          at the moment.  Function should always have at least one
5129          entry point.  */
5130       gcc_assert (single_succ_p (ENTRY_BLOCK_PTR));
5131
5132       insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
5133       inserted = 1;
5134     }
5135 #endif
5136
5137   /* If the exit block has no non-fake predecessors, we don't need
5138      an epilogue.  */
5139   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5140     if ((e->flags & EDGE_FAKE) == 0)
5141       break;
5142   if (e == NULL)
5143     goto epilogue_done;
5144
5145   rtl_profile_for_bb (EXIT_BLOCK_PTR);
5146 #ifdef HAVE_return
5147   if (optimize && HAVE_return)
5148     {
5149       /* If we're allowed to generate a simple return instruction,
5150          then by definition we don't need a full epilogue.  Examine
5151          the block that falls through to EXIT.   If it does not
5152          contain any code, examine its predecessors and try to
5153          emit (conditional) return instructions.  */
5154
5155       basic_block last;
5156       rtx label;
5157
5158       FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5159         if (e->flags & EDGE_FALLTHRU)
5160           break;
5161       if (e == NULL)
5162         goto epilogue_done;
5163       last = e->src;
5164
5165       /* Verify that there are no active instructions in the last block.  */
5166       label = BB_END (last);
5167       while (label && !LABEL_P (label))
5168         {
5169           if (active_insn_p (label))
5170             break;
5171           label = PREV_INSN (label);
5172         }
5173
5174       if (BB_HEAD (last) == label && LABEL_P (label))
5175         {
5176           edge_iterator ei2;
5177
5178           for (ei2 = ei_start (last->preds); (e = ei_safe_edge (ei2)); )
5179             {
5180               basic_block bb = e->src;
5181               rtx jump;
5182
5183               if (bb == ENTRY_BLOCK_PTR)
5184                 {
5185                   ei_next (&ei2);
5186                   continue;
5187                 }
5188
5189               jump = BB_END (bb);
5190               if (!JUMP_P (jump) || JUMP_LABEL (jump) != label)
5191                 {
5192                   ei_next (&ei2);
5193                   continue;
5194                 }
5195
5196               /* If we have an unconditional jump, we can replace that
5197                  with a simple return instruction.  */
5198               if (simplejump_p (jump))
5199                 {
5200                   emit_return_into_block (bb);
5201                   delete_insn (jump);
5202                 }
5203
5204               /* If we have a conditional jump, we can try to replace
5205                  that with a conditional return instruction.  */
5206               else if (condjump_p (jump))
5207                 {
5208                   if (! redirect_jump (jump, 0, 0))
5209                     {
5210                       ei_next (&ei2);
5211                       continue;
5212                     }
5213
5214                   /* If this block has only one successor, it both jumps
5215                      and falls through to the fallthru block, so we can't
5216                      delete the edge.  */
5217                   if (single_succ_p (bb))
5218                     {
5219                       ei_next (&ei2);
5220                       continue;
5221                     }
5222                 }
5223               else
5224                 {
5225                   ei_next (&ei2);
5226                   continue;
5227                 }
5228
5229               /* Fix up the CFG for the successful change we just made.  */
5230               redirect_edge_succ (e, EXIT_BLOCK_PTR);
5231             }
5232
5233           /* Emit a return insn for the exit fallthru block.  Whether
5234              this is still reachable will be determined later.  */
5235
5236           emit_barrier_after (BB_END (last));
5237           emit_return_into_block (last);
5238           epilogue_end = BB_END (last);
5239           single_succ_edge (last)->flags &= ~EDGE_FALLTHRU;
5240           goto epilogue_done;
5241         }
5242     }
5243 #endif
5244
5245   /* A small fib -- epilogue is not yet completed, but we wish to re-use
5246      this marker for the splits of EH_RETURN patterns, and nothing else
5247      uses the flag in the meantime.  */
5248   epilogue_completed = 1;
5249
5250 #ifdef HAVE_eh_return
5251   /* Find non-fallthru edges that end with EH_RETURN instructions.  On
5252      some targets, these get split to a special version of the epilogue
5253      code.  In order to be able to properly annotate these with unwind
5254      info, try to split them now.  If we get a valid split, drop an
5255      EPILOGUE_BEG note and mark the insns as epilogue insns.  */
5256   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5257     {
5258       rtx prev, last, trial;
5259
5260       if (e->flags & EDGE_FALLTHRU)
5261         continue;
5262       last = BB_END (e->src);
5263       if (!eh_returnjump_p (last))
5264         continue;
5265
5266       prev = PREV_INSN (last);
5267       trial = try_split (PATTERN (last), last, 1);
5268       if (trial == last)
5269         continue;
5270
5271       record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
5272       emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
5273     }
5274 #endif
5275
5276   /* Find the edge that falls through to EXIT.  Other edges may exist
5277      due to RETURN instructions, but those don't need epilogues.
5278      There really shouldn't be a mixture -- either all should have
5279      been converted or none, however...  */
5280
5281   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5282     if (e->flags & EDGE_FALLTHRU)
5283       break;
5284   if (e == NULL)
5285     goto epilogue_done;
5286
5287 #ifdef HAVE_epilogue
5288   if (HAVE_epilogue)
5289     {
5290       start_sequence ();
5291       epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
5292       seq = gen_epilogue ();
5293       emit_jump_insn (seq);
5294
5295       /* Retain a map of the epilogue insns.  */
5296       record_insns (seq, NULL, &epilogue_insn_hash);
5297       set_insn_locators (seq, epilogue_locator);
5298
5299       seq = get_insns ();
5300       end_sequence ();
5301
5302       insert_insn_on_edge (seq, e);
5303       inserted = 1;
5304     }
5305   else
5306 #endif
5307     {
5308       basic_block cur_bb;
5309
5310       if (! next_active_insn (BB_END (e->src)))
5311         goto epilogue_done;
5312       /* We have a fall-through edge to the exit block, the source is not
5313          at the end of the function, and there will be an assembler epilogue
5314          at the end of the function.
5315          We can't use force_nonfallthru here, because that would try to
5316          use return.  Inserting a jump 'by hand' is extremely messy, so
5317          we take advantage of cfg_layout_finalize using
5318         fixup_fallthru_exit_predecessor.  */
5319       cfg_layout_initialize (0);
5320       FOR_EACH_BB (cur_bb)
5321         if (cur_bb->index >= NUM_FIXED_BLOCKS
5322             && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
5323           cur_bb->aux = cur_bb->next_bb;
5324       cfg_layout_finalize ();
5325     }
5326 epilogue_done:
5327   default_rtl_profile ();
5328
5329   if (inserted)
5330     {
5331       commit_edge_insertions ();
5332
5333       /* The epilogue insns we inserted may cause the exit edge to no longer
5334          be fallthru.  */
5335       FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5336         {
5337           if (((e->flags & EDGE_FALLTHRU) != 0)
5338               && returnjump_p (BB_END (e->src)))
5339             e->flags &= ~EDGE_FALLTHRU;
5340         }
5341     }
5342
5343 #ifdef HAVE_sibcall_epilogue
5344   /* Emit sibling epilogues before any sibling call sites.  */
5345   for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
5346     {
5347       basic_block bb = e->src;
5348       rtx insn = BB_END (bb);
5349
5350       if (!CALL_P (insn)
5351           || ! SIBLING_CALL_P (insn))
5352         {
5353           ei_next (&ei);
5354           continue;
5355         }
5356
5357       start_sequence ();
5358       emit_note (NOTE_INSN_EPILOGUE_BEG);
5359       emit_insn (gen_sibcall_epilogue ());
5360       seq = get_insns ();
5361       end_sequence ();
5362
5363       /* Retain a map of the epilogue insns.  Used in life analysis to
5364          avoid getting rid of sibcall epilogue insns.  Do this before we
5365          actually emit the sequence.  */
5366       record_insns (seq, NULL, &epilogue_insn_hash);
5367       set_insn_locators (seq, epilogue_locator);
5368
5369       emit_insn_before (seq, insn);
5370       ei_next (&ei);
5371     }
5372 #endif
5373
5374 #ifdef HAVE_epilogue
5375   if (epilogue_end)
5376     {
5377       rtx insn, next;
5378
5379       /* Similarly, move any line notes that appear after the epilogue.
5380          There is no need, however, to be quite so anal about the existence
5381          of such a note.  Also possibly move
5382          NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
5383          info generation.  */
5384       for (insn = epilogue_end; insn; insn = next)
5385         {
5386           next = NEXT_INSN (insn);
5387           if (NOTE_P (insn)
5388               && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
5389             reorder_insns (insn, insn, PREV_INSN (epilogue_end));
5390         }
5391     }
5392 #endif
5393
5394   /* Threading the prologue and epilogue changes the artificial refs
5395      in the entry and exit blocks.  */
5396   epilogue_completed = 1;
5397   df_update_entry_exit_and_calls ();
5398 }
5399
5400 /* Reposition the prologue-end and epilogue-begin notes after
5401    instruction scheduling.  */
5402
5403 void
5404 reposition_prologue_and_epilogue_notes (void)
5405 {
5406 #if defined (HAVE_prologue) || defined (HAVE_epilogue) \
5407     || defined (HAVE_sibcall_epilogue)
5408   /* Since the hash table is created on demand, the fact that it is
5409      non-null is a signal that it is non-empty.  */
5410   if (prologue_insn_hash != NULL)
5411     {
5412       size_t len = htab_elements (prologue_insn_hash);
5413       rtx insn, last = NULL, note = NULL;
5414
5415       /* Scan from the beginning until we reach the last prologue insn.  */
5416       /* ??? While we do have the CFG intact, there are two problems:
5417          (1) The prologue can contain loops (typically probing the stack),
5418              which means that the end of the prologue isn't in the first bb.
5419          (2) Sometimes the PROLOGUE_END note gets pushed into the next bb.  */
5420       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5421         {
5422           if (NOTE_P (insn))
5423             {
5424               if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
5425                 note = insn;
5426             }
5427           else if (contains (insn, prologue_insn_hash))
5428             {
5429               last = insn;
5430               if (--len == 0)
5431                 break;
5432             }
5433         }
5434
5435       if (last)
5436         {
5437           if (note == NULL)
5438             {
5439               /* Scan forward looking for the PROLOGUE_END note.  It should
5440                  be right at the beginning of the block, possibly with other
5441                  insn notes that got moved there.  */
5442               for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
5443                 {
5444                   if (NOTE_P (note)
5445                       && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
5446                     break;
5447                 }
5448             }
5449
5450           /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note.  */
5451           if (LABEL_P (last))
5452             last = NEXT_INSN (last);
5453           reorder_insns (note, note, last);
5454         }
5455     }
5456
5457   if (epilogue_insn_hash != NULL)
5458     {
5459       edge_iterator ei;
5460       edge e;
5461
5462       FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5463         {
5464           rtx insn, first = NULL, note = NULL;
5465           basic_block bb = e->src;
5466
5467           /* Scan from the beginning until we reach the first epilogue insn. */
5468           FOR_BB_INSNS (bb, insn)
5469             {
5470               if (NOTE_P (insn))
5471                 {
5472                   if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
5473                     {
5474                       note = insn;
5475                       if (first != NULL)
5476                         break;
5477                     }
5478                 }
5479               else if (first == NULL && contains (insn, epilogue_insn_hash))
5480                 {
5481                   first = insn;
5482                   if (note != NULL)
5483                     break;
5484                 }
5485             }
5486
5487           if (note)
5488             {
5489               /* If the function has a single basic block, and no real
5490                  epilogue insns (e.g. sibcall with no cleanup), the
5491                  epilogue note can get scheduled before the prologue
5492                  note.  If we have frame related prologue insns, having
5493                  them scanned during the epilogue will result in a crash.
5494                  In this case re-order the epilogue note to just before
5495                  the last insn in the block.  */
5496               if (first == NULL)
5497                 first = BB_END (bb);
5498
5499               if (PREV_INSN (first) != note)
5500                 reorder_insns (note, note, PREV_INSN (first));
5501             }
5502         }
5503     }
5504 #endif /* HAVE_prologue or HAVE_epilogue */
5505 }
5506
5507 /* Returns the name of the current function.  */
5508 const char *
5509 current_function_name (void)
5510 {
5511   if (cfun == NULL)
5512     return "<none>";
5513   return lang_hooks.decl_printable_name (cfun->decl, 2);
5514 }
5515 \f
5516
5517 static unsigned int
5518 rest_of_handle_check_leaf_regs (void)
5519 {
5520 #ifdef LEAF_REGISTERS
5521   current_function_uses_only_leaf_regs
5522     = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
5523 #endif
5524   return 0;
5525 }
5526
5527 /* Insert a TYPE into the used types hash table of CFUN.  */
5528
5529 static void
5530 used_types_insert_helper (tree type, struct function *func)
5531 {
5532   if (type != NULL && func != NULL)
5533     {
5534       void **slot;
5535
5536       if (func->used_types_hash == NULL)
5537         func->used_types_hash = htab_create_ggc (37, htab_hash_pointer,
5538                                                  htab_eq_pointer, NULL);
5539       slot = htab_find_slot (func->used_types_hash, type, INSERT);
5540       if (*slot == NULL)
5541         *slot = type;
5542     }
5543 }
5544
5545 /* Given a type, insert it into the used hash table in cfun.  */
5546 void
5547 used_types_insert (tree t)
5548 {
5549   while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
5550     if (TYPE_NAME (t))
5551       break;
5552     else
5553       t = TREE_TYPE (t);
5554   if (TYPE_NAME (t) == NULL_TREE
5555       || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
5556     t = TYPE_MAIN_VARIANT (t);
5557   if (debug_info_level > DINFO_LEVEL_NONE)
5558     {
5559       if (cfun)
5560         used_types_insert_helper (t, cfun);
5561       else
5562         /* So this might be a type referenced by a global variable.
5563            Record that type so that we can later decide to emit its debug
5564            information.  */
5565         types_used_by_cur_var_decl =
5566           tree_cons (t, NULL, types_used_by_cur_var_decl);
5567
5568     }
5569 }
5570
5571 /* Helper to Hash a struct types_used_by_vars_entry.  */
5572
5573 static hashval_t
5574 hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
5575 {
5576   gcc_assert (entry && entry->var_decl && entry->type);
5577
5578   return iterative_hash_object (entry->type,
5579                                 iterative_hash_object (entry->var_decl, 0));
5580 }
5581
5582 /* Hash function of the types_used_by_vars_entry hash table.  */
5583
5584 hashval_t
5585 types_used_by_vars_do_hash (const void *x)
5586 {
5587   const struct types_used_by_vars_entry *entry =
5588     (const struct types_used_by_vars_entry *) x;
5589
5590   return hash_types_used_by_vars_entry (entry);
5591 }
5592
5593 /*Equality function of the types_used_by_vars_entry hash table.  */
5594
5595 int
5596 types_used_by_vars_eq (const void *x1, const void *x2)
5597 {
5598   const struct types_used_by_vars_entry *e1 =
5599     (const struct types_used_by_vars_entry *) x1;
5600   const struct types_used_by_vars_entry *e2 =
5601     (const struct types_used_by_vars_entry *)x2;
5602
5603   return (e1->var_decl == e2->var_decl && e1->type == e2->type);
5604 }
5605
5606 /* Inserts an entry into the types_used_by_vars_hash hash table. */
5607
5608 void
5609 types_used_by_var_decl_insert (tree type, tree var_decl)
5610 {
5611   if (type != NULL && var_decl != NULL)
5612     {
5613       void **slot;
5614       struct types_used_by_vars_entry e;
5615       e.var_decl = var_decl;
5616       e.type = type;
5617       if (types_used_by_vars_hash == NULL)
5618         types_used_by_vars_hash =
5619           htab_create_ggc (37, types_used_by_vars_do_hash,
5620                            types_used_by_vars_eq, NULL);
5621       slot = htab_find_slot_with_hash (types_used_by_vars_hash, &e,
5622                                        hash_types_used_by_vars_entry (&e), INSERT);
5623       if (*slot == NULL)
5624         {
5625           struct types_used_by_vars_entry *entry;
5626           entry = (struct types_used_by_vars_entry*) ggc_alloc
5627                     (sizeof (struct types_used_by_vars_entry));
5628           entry->type = type;
5629           entry->var_decl = var_decl;
5630           *slot = entry;
5631         }
5632     }
5633 }
5634
5635 struct rtl_opt_pass pass_leaf_regs =
5636 {
5637  {
5638   RTL_PASS,
5639   "*leaf_regs",                         /* name */
5640   NULL,                                 /* gate */
5641   rest_of_handle_check_leaf_regs,       /* execute */
5642   NULL,                                 /* sub */
5643   NULL,                                 /* next */
5644   0,                                    /* static_pass_number */
5645   TV_NONE,                              /* tv_id */
5646   0,                                    /* properties_required */
5647   0,                                    /* properties_provided */
5648   0,                                    /* properties_destroyed */
5649   0,                                    /* todo_flags_start */
5650   0                                     /* todo_flags_finish */
5651  }
5652 };
5653
5654 static unsigned int
5655 rest_of_handle_thread_prologue_and_epilogue (void)
5656 {
5657   if (optimize)
5658     cleanup_cfg (CLEANUP_EXPENSIVE);
5659   /* On some machines, the prologue and epilogue code, or parts thereof,
5660      can be represented as RTL.  Doing so lets us schedule insns between
5661      it and the rest of the code and also allows delayed branch
5662      scheduling to operate in the epilogue.  */
5663
5664   thread_prologue_and_epilogue_insns ();
5665   return 0;
5666 }
5667
5668 struct rtl_opt_pass pass_thread_prologue_and_epilogue =
5669 {
5670  {
5671   RTL_PASS,
5672   "pro_and_epilogue",                   /* name */
5673   NULL,                                 /* gate */
5674   rest_of_handle_thread_prologue_and_epilogue, /* execute */
5675   NULL,                                 /* sub */
5676   NULL,                                 /* next */
5677   0,                                    /* static_pass_number */
5678   TV_THREAD_PROLOGUE_AND_EPILOGUE,      /* tv_id */
5679   0,                                    /* properties_required */
5680   0,                                    /* properties_provided */
5681   0,                                    /* properties_destroyed */
5682   TODO_verify_flow,                     /* todo_flags_start */
5683   TODO_dump_func |
5684   TODO_df_verify |
5685   TODO_df_finish | TODO_verify_rtl_sharing |
5686   TODO_ggc_collect                      /* todo_flags_finish */
5687  }
5688 };
5689 \f
5690
5691 /* This mini-pass fixes fall-out from SSA in asm statements that have
5692    in-out constraints.  Say you start with
5693
5694      orig = inout;
5695      asm ("": "+mr" (inout));
5696      use (orig);
5697
5698    which is transformed very early to use explicit output and match operands:
5699
5700      orig = inout;
5701      asm ("": "=mr" (inout) : "0" (inout));
5702      use (orig);
5703
5704    Or, after SSA and copyprop,
5705
5706      asm ("": "=mr" (inout_2) : "0" (inout_1));
5707      use (inout_1);
5708
5709    Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
5710    they represent two separate values, so they will get different pseudo
5711    registers during expansion.  Then, since the two operands need to match
5712    per the constraints, but use different pseudo registers, reload can
5713    only register a reload for these operands.  But reloads can only be
5714    satisfied by hardregs, not by memory, so we need a register for this
5715    reload, just because we are presented with non-matching operands.
5716    So, even though we allow memory for this operand, no memory can be
5717    used for it, just because the two operands don't match.  This can
5718    cause reload failures on register-starved targets.
5719
5720    So it's a symptom of reload not being able to use memory for reloads
5721    or, alternatively it's also a symptom of both operands not coming into
5722    reload as matching (in which case the pseudo could go to memory just
5723    fine, as the alternative allows it, and no reload would be necessary).
5724    We fix the latter problem here, by transforming
5725
5726      asm ("": "=mr" (inout_2) : "0" (inout_1));
5727
5728    back to
5729
5730      inout_2 = inout_1;
5731      asm ("": "=mr" (inout_2) : "0" (inout_2));  */
5732
5733 static void
5734 match_asm_constraints_1 (rtx insn, rtx *p_sets, int noutputs)
5735 {
5736   int i;
5737   bool changed = false;
5738   rtx op = SET_SRC (p_sets[0]);
5739   int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
5740   rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
5741   bool *output_matched = XALLOCAVEC (bool, noutputs);
5742
5743   memset (output_matched, 0, noutputs * sizeof (bool));
5744   for (i = 0; i < ninputs; i++)
5745     {
5746       rtx input, output, insns;
5747       const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
5748       char *end;
5749       int match, j;
5750
5751       if (*constraint == '%')
5752         constraint++;
5753
5754       match = strtoul (constraint, &end, 10);
5755       if (end == constraint)
5756         continue;
5757
5758       gcc_assert (match < noutputs);
5759       output = SET_DEST (p_sets[match]);
5760       input = RTVEC_ELT (inputs, i);
5761       /* Only do the transformation for pseudos.  */
5762       if (! REG_P (output)
5763           || rtx_equal_p (output, input)
5764           || (GET_MODE (input) != VOIDmode
5765               && GET_MODE (input) != GET_MODE (output)))
5766         continue;
5767
5768       /* We can't do anything if the output is also used as input,
5769          as we're going to overwrite it.  */
5770       for (j = 0; j < ninputs; j++)
5771         if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
5772           break;
5773       if (j != ninputs)
5774         continue;
5775
5776       /* Avoid changing the same input several times.  For
5777          asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
5778          only change in once (to out1), rather than changing it
5779          first to out1 and afterwards to out2.  */
5780       if (i > 0)
5781         {
5782           for (j = 0; j < noutputs; j++)
5783             if (output_matched[j] && input == SET_DEST (p_sets[j]))
5784               break;
5785           if (j != noutputs)
5786             continue;
5787         }
5788       output_matched[match] = true;
5789
5790       start_sequence ();
5791       emit_move_insn (output, input);
5792       insns = get_insns ();
5793       end_sequence ();
5794       emit_insn_before (insns, insn);
5795
5796       /* Now replace all mentions of the input with output.  We can't
5797          just replace the occurrence in inputs[i], as the register might
5798          also be used in some other input (or even in an address of an
5799          output), which would mean possibly increasing the number of
5800          inputs by one (namely 'output' in addition), which might pose
5801          a too complicated problem for reload to solve.  E.g. this situation:
5802
5803            asm ("" : "=r" (output), "=m" (input) : "0" (input))
5804
5805          Here 'input' is used in two occurrences as input (once for the
5806          input operand, once for the address in the second output operand).
5807          If we would replace only the occurrence of the input operand (to
5808          make the matching) we would be left with this:
5809
5810            output = input
5811            asm ("" : "=r" (output), "=m" (input) : "0" (output))
5812
5813          Now we suddenly have two different input values (containing the same
5814          value, but different pseudos) where we formerly had only one.
5815          With more complicated asms this might lead to reload failures
5816          which wouldn't have happen without this pass.  So, iterate over
5817          all operands and replace all occurrences of the register used.  */
5818       for (j = 0; j < noutputs; j++)
5819         if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
5820             && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
5821           SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
5822                                               input, output);
5823       for (j = 0; j < ninputs; j++)
5824         if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
5825           RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
5826                                                input, output);
5827
5828       changed = true;
5829     }
5830
5831   if (changed)
5832     df_insn_rescan (insn);
5833 }
5834
5835 static unsigned
5836 rest_of_match_asm_constraints (void)
5837 {
5838   basic_block bb;
5839   rtx insn, pat, *p_sets;
5840   int noutputs;
5841
5842   if (!crtl->has_asm_statement)
5843     return 0;
5844
5845   df_set_flags (DF_DEFER_INSN_RESCAN);
5846   FOR_EACH_BB (bb)
5847     {
5848       FOR_BB_INSNS (bb, insn)
5849         {
5850           if (!INSN_P (insn))
5851             continue;
5852
5853           pat = PATTERN (insn);
5854           if (GET_CODE (pat) == PARALLEL)
5855             p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
5856           else if (GET_CODE (pat) == SET)
5857             p_sets = &PATTERN (insn), noutputs = 1;
5858           else
5859             continue;
5860
5861           if (GET_CODE (*p_sets) == SET
5862               && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
5863             match_asm_constraints_1 (insn, p_sets, noutputs);
5864          }
5865     }
5866
5867   return TODO_df_finish;
5868 }
5869
5870 struct rtl_opt_pass pass_match_asm_constraints =
5871 {
5872  {
5873   RTL_PASS,
5874   "asmcons",                            /* name */
5875   NULL,                                 /* gate */
5876   rest_of_match_asm_constraints,        /* execute */
5877   NULL,                                 /* sub */
5878   NULL,                                 /* next */
5879   0,                                    /* static_pass_number */
5880   TV_NONE,                              /* tv_id */
5881   0,                                    /* properties_required */
5882   0,                                    /* properties_provided */
5883   0,                                    /* properties_destroyed */
5884   0,                                    /* todo_flags_start */
5885   TODO_dump_func                       /* todo_flags_finish */
5886  }
5887 };
5888
5889
5890 #include "gt-function.h"