OSDN Git Service

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