OSDN Git Service

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