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