OSDN Git Service

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