OSDN Git Service

* toplev.c (process_options, parse_options_and_default_flags):
[pf3gnuchains/gcc-fork.git] / gcc / function.c
1 /* Expands front end tree to back end RTL for GNU C-Compiler
2    Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3    1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
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    Call `put_var_into_stack' when you learn, belatedly, that a variable
37    previously given a pseudo-register must in fact go in the stack.
38    This function changes the DECL_RTL to be a stack slot instead of a reg
39    then scans all the RTL instructions so far generated to correct them.  */
40
41 #include "config.h"
42 #include "system.h"
43 #include "rtl.h"
44 #include "tree.h"
45 #include "flags.h"
46 #include "except.h"
47 #include "function.h"
48 #include "expr.h"
49 #include "libfuncs.h"
50 #include "regs.h"
51 #include "hard-reg-set.h"
52 #include "insn-config.h"
53 #include "recog.h"
54 #include "output.h"
55 #include "basic-block.h"
56 #include "obstack.h"
57 #include "toplev.h"
58 #include "hash.h"
59 #include "ggc.h"
60 #include "tm_p.h"
61 #include "integrate.h"
62
63 #ifndef TRAMPOLINE_ALIGNMENT
64 #define TRAMPOLINE_ALIGNMENT FUNCTION_BOUNDARY
65 #endif
66
67 #ifndef LOCAL_ALIGNMENT
68 #define LOCAL_ALIGNMENT(TYPE, ALIGNMENT) ALIGNMENT
69 #endif
70
71 /* Some systems use __main in a way incompatible with its use in gcc, in these
72    cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
73    give the same symbol without quotes for an alternative entry point.  You
74    must define both, or neither.  */
75 #ifndef NAME__MAIN
76 #define NAME__MAIN "__main"
77 #define SYMBOL__MAIN __main
78 #endif
79
80 /* Round a value to the lowest integer less than it that is a multiple of
81    the required alignment.  Avoid using division in case the value is
82    negative.  Assume the alignment is a power of two.  */
83 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
84
85 /* Similar, but round to the next highest integer that meets the
86    alignment.  */
87 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
88
89 /* NEED_SEPARATE_AP means that we cannot derive ap from the value of fp
90    during rtl generation.  If they are different register numbers, this is
91    always true.  It may also be true if
92    FIRST_PARM_OFFSET - STARTING_FRAME_OFFSET is not a constant during rtl
93    generation.  See fix_lexical_addr for details.  */
94
95 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
96 #define NEED_SEPARATE_AP
97 #endif
98
99 /* Nonzero if function being compiled doesn't contain any calls
100    (ignoring the prologue and epilogue).  This is set prior to
101    local register allocation and is valid for the remaining
102    compiler passes.  */
103 int current_function_is_leaf;
104
105 /* Nonzero if function being compiled doesn't contain any instructions
106    that can throw an exception.  This is set prior to final.  */
107
108 int current_function_nothrow;
109
110 /* Nonzero if function being compiled doesn't modify the stack pointer
111    (ignoring the prologue and epilogue).  This is only valid after
112    life_analysis has run.  */
113 int current_function_sp_is_unchanging;
114
115 /* Nonzero if the function being compiled is a leaf function which only
116    uses leaf registers.  This is valid after reload (specifically after
117    sched2) and is useful only if the port defines LEAF_REGISTERS.  */
118 int current_function_uses_only_leaf_regs;
119
120 /* Nonzero once virtual register instantiation has been done.
121    assign_stack_local uses frame_pointer_rtx when this is nonzero.
122    calls.c:emit_library_call_value_1 uses it to set up
123    post-instantiation libcalls.  */
124 int virtuals_instantiated;
125
126 /* These variables hold pointers to functions to create and destroy
127    target specific, per-function data structures.  */
128 void (*init_machine_status) PARAMS ((struct function *));
129 void (*free_machine_status) PARAMS ((struct function *));
130 /* This variable holds a pointer to a function to register any
131    data items in the target specific, per-function data structure
132    that will need garbage collection.  */
133 void (*mark_machine_status) PARAMS ((struct function *));
134
135 /* Likewise, but for language-specific data.  */
136 void (*init_lang_status) PARAMS ((struct function *));
137 void (*save_lang_status) PARAMS ((struct function *));
138 void (*restore_lang_status) PARAMS ((struct function *));
139 void (*mark_lang_status) PARAMS ((struct function *));
140 void (*free_lang_status) PARAMS ((struct function *));
141
142 /* The FUNCTION_DECL for an inline function currently being expanded.  */
143 tree inline_function_decl;
144
145 /* The currently compiled function.  */
146 struct function *cfun = 0;
147
148 /* These arrays record the INSN_UIDs of the prologue and epilogue insns.  */
149 static varray_type prologue;
150 static varray_type epilogue;
151
152 /* Array of INSN_UIDs to hold the INSN_UIDs for each sibcall epilogue
153    in this function.  */
154 static varray_type sibcall_epilogue;
155 \f
156 /* In order to evaluate some expressions, such as function calls returning
157    structures in memory, we need to temporarily allocate stack locations.
158    We record each allocated temporary in the following structure.
159
160    Associated with each temporary slot is a nesting level.  When we pop up
161    one level, all temporaries associated with the previous level are freed.
162    Normally, all temporaries are freed after the execution of the statement
163    in which they were created.  However, if we are inside a ({...}) grouping,
164    the result may be in a temporary and hence must be preserved.  If the
165    result could be in a temporary, we preserve it if we can determine which
166    one it is in.  If we cannot determine which temporary may contain the
167    result, all temporaries are preserved.  A temporary is preserved by
168    pretending it was allocated at the previous nesting level.
169
170    Automatic variables are also assigned temporary slots, at the nesting
171    level where they are defined.  They are marked a "kept" so that
172    free_temp_slots will not free them.  */
173
174 struct temp_slot
175 {
176   /* Points to next temporary slot.  */
177   struct temp_slot *next;
178   /* The rtx to used to reference the slot.  */
179   rtx slot;
180   /* The rtx used to represent the address if not the address of the
181      slot above.  May be an EXPR_LIST if multiple addresses exist.  */
182   rtx address;
183   /* The alignment (in bits) of the slot.  */
184   unsigned int align;
185   /* The size, in units, of the slot.  */
186   HOST_WIDE_INT size;
187   /* The type of the object in the slot, or zero if it doesn't correspond
188      to a type.  We use this to determine whether a slot can be reused.
189      It can be reused if objects of the type of the new slot will always
190      conflict with objects of the type of the old slot.  */
191   tree type;
192   /* The value of `sequence_rtl_expr' when this temporary is allocated.  */
193   tree rtl_expr;
194   /* Non-zero if this temporary is currently in use.  */
195   char in_use;
196   /* Non-zero if this temporary has its address taken.  */
197   char addr_taken;
198   /* Nesting level at which this slot is being used.  */
199   int level;
200   /* Non-zero if this should survive a call to free_temp_slots.  */
201   int keep;
202   /* The offset of the slot from the frame_pointer, including extra space
203      for alignment.  This info is for combine_temp_slots.  */
204   HOST_WIDE_INT base_offset;
205   /* The size of the slot, including extra space for alignment.  This
206      info is for combine_temp_slots.  */
207   HOST_WIDE_INT full_size;
208 };
209 \f
210 /* This structure is used to record MEMs or pseudos used to replace VAR, any
211    SUBREGs of VAR, and any MEMs containing VAR as an address.  We need to
212    maintain this list in case two operands of an insn were required to match;
213    in that case we must ensure we use the same replacement.  */
214
215 struct fixup_replacement
216 {
217   rtx old;
218   rtx new;
219   struct fixup_replacement *next;
220 };
221
222 struct insns_for_mem_entry
223 {
224   /* The KEY in HE will be a MEM.  */
225   struct hash_entry he;
226   /* These are the INSNS which reference the MEM.  */
227   rtx insns;
228 };
229
230 /* Forward declarations.  */
231
232 static rtx assign_stack_local_1 PARAMS ((enum machine_mode, HOST_WIDE_INT,
233                                          int, struct function *));
234 static struct temp_slot *find_temp_slot_from_address  PARAMS ((rtx));
235 static void put_reg_into_stack  PARAMS ((struct function *, rtx, tree,
236                                          enum machine_mode, enum machine_mode,
237                                          int, unsigned int, int,
238                                          struct hash_table *));
239 static void schedule_fixup_var_refs PARAMS ((struct function *, rtx, tree,
240                                              enum machine_mode,
241                                              struct hash_table *));
242 static void fixup_var_refs      PARAMS ((rtx, enum machine_mode, int,
243                                          struct hash_table *));
244 static struct fixup_replacement
245   *find_fixup_replacement       PARAMS ((struct fixup_replacement **, rtx));
246 static void fixup_var_refs_insns PARAMS ((rtx, rtx, enum machine_mode,
247                                           int, int));
248 static void fixup_var_refs_insns_with_hash
249                                 PARAMS ((struct hash_table *, rtx,
250                                          enum machine_mode, int));
251 static void fixup_var_refs_insn PARAMS ((rtx, rtx, enum machine_mode,
252                                          int, int));
253 static void fixup_var_refs_1    PARAMS ((rtx, enum machine_mode, rtx *, rtx,
254                                          struct fixup_replacement **));
255 static rtx fixup_memory_subreg  PARAMS ((rtx, rtx, int));
256 static rtx walk_fixup_memory_subreg  PARAMS ((rtx, rtx, int));
257 static rtx fixup_stack_1        PARAMS ((rtx, rtx));
258 static void optimize_bit_field  PARAMS ((rtx, rtx, rtx *));
259 static void instantiate_decls   PARAMS ((tree, int));
260 static void instantiate_decls_1 PARAMS ((tree, int));
261 static void instantiate_decl    PARAMS ((rtx, HOST_WIDE_INT, int));
262 static rtx instantiate_new_reg  PARAMS ((rtx, HOST_WIDE_INT *));
263 static int instantiate_virtual_regs_1 PARAMS ((rtx *, rtx, int));
264 static void delete_handlers     PARAMS ((void));
265 static void pad_to_arg_alignment PARAMS ((struct args_size *, int,
266                                           struct args_size *));
267 #ifndef ARGS_GROW_DOWNWARD
268 static void pad_below           PARAMS ((struct args_size *, enum machine_mode,
269                                          tree));
270 #endif
271 static rtx round_trampoline_addr PARAMS ((rtx));
272 static rtx adjust_trampoline_addr PARAMS ((rtx));
273 static tree *identify_blocks_1  PARAMS ((rtx, tree *, tree *, tree *));
274 static void reorder_blocks_0    PARAMS ((tree));
275 static void reorder_blocks_1    PARAMS ((rtx, tree, varray_type *));
276 static void reorder_fix_fragments PARAMS ((tree));
277 static tree blocks_nreverse     PARAMS ((tree));
278 static int all_blocks           PARAMS ((tree, tree *));
279 static tree *get_block_vector   PARAMS ((tree, int *));
280 /* We always define `record_insns' even if its not used so that we
281    can always export `prologue_epilogue_contains'.  */
282 static void record_insns        PARAMS ((rtx, varray_type *)) ATTRIBUTE_UNUSED;
283 static int contains             PARAMS ((rtx, varray_type));
284 #ifdef HAVE_return
285 static void emit_return_into_block PARAMS ((basic_block, rtx));
286 #endif
287 static void put_addressof_into_stack PARAMS ((rtx, struct hash_table *));
288 static bool purge_addressof_1 PARAMS ((rtx *, rtx, int, int,
289                                           struct hash_table *));
290 static void purge_single_hard_subreg_set PARAMS ((rtx));
291 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
292 static rtx keep_stack_depressed PARAMS ((rtx));
293 #endif
294 static int is_addressof         PARAMS ((rtx *, void *));
295 static struct hash_entry *insns_for_mem_newfunc PARAMS ((struct hash_entry *,
296                                                          struct hash_table *,
297                                                          hash_table_key));
298 static unsigned long insns_for_mem_hash PARAMS ((hash_table_key));
299 static bool insns_for_mem_comp PARAMS ((hash_table_key, hash_table_key));
300 static int insns_for_mem_walk   PARAMS ((rtx *, void *));
301 static void compute_insns_for_mem PARAMS ((rtx, rtx, struct hash_table *));
302 static void mark_function_status PARAMS ((struct function *));
303 static void maybe_mark_struct_function PARAMS ((void *));
304 static void prepare_function_start PARAMS ((void));
305 static void do_clobber_return_reg PARAMS ((rtx, void *));
306 static void do_use_return_reg PARAMS ((rtx, void *));
307 \f
308 /* Pointer to chain of `struct function' for containing functions.  */
309 static struct function *outer_function_chain;
310
311 /* Given a function decl for a containing function,
312    return the `struct function' for it.  */
313
314 struct function *
315 find_function_data (decl)
316      tree decl;
317 {
318   struct function *p;
319
320   for (p = outer_function_chain; p; p = p->outer)
321     if (p->decl == decl)
322       return p;
323
324   abort ();
325 }
326
327 /* Save the current context for compilation of a nested function.
328    This is called from language-specific code.  The caller should use
329    the save_lang_status callback to save any language-specific state,
330    since this function knows only about language-independent
331    variables.  */
332
333 void
334 push_function_context_to (context)
335      tree context;
336 {
337   struct function *p;
338
339   if (context)
340     {
341       if (context == current_function_decl)
342         cfun->contains_functions = 1;
343       else
344         {
345           struct function *containing = find_function_data (context);
346           containing->contains_functions = 1;
347         }
348     }
349
350   if (cfun == 0)
351     init_dummy_function_start ();
352   p = cfun;
353
354   p->outer = outer_function_chain;
355   outer_function_chain = p;
356   p->fixup_var_refs_queue = 0;
357
358   if (save_lang_status)
359     (*save_lang_status) (p);
360
361   cfun = 0;
362 }
363
364 void
365 push_function_context ()
366 {
367   push_function_context_to (current_function_decl);
368 }
369
370 /* Restore the last saved context, at the end of a nested function.
371    This function is called from language-specific code.  */
372
373 void
374 pop_function_context_from (context)
375      tree context ATTRIBUTE_UNUSED;
376 {
377   struct function *p = outer_function_chain;
378   struct var_refs_queue *queue;
379
380   cfun = p;
381   outer_function_chain = p->outer;
382
383   current_function_decl = p->decl;
384   reg_renumber = 0;
385
386   restore_emit_status (p);
387
388   if (restore_lang_status)
389     (*restore_lang_status) (p);
390
391   /* Finish doing put_var_into_stack for any of our variables
392      which became addressable during the nested function.  */
393   for (queue = p->fixup_var_refs_queue; queue; queue = queue->next)
394     fixup_var_refs (queue->modified, queue->promoted_mode,
395                     queue->unsignedp, 0);
396
397   p->fixup_var_refs_queue = 0;
398
399   /* Reset variables that have known state during rtx generation.  */
400   rtx_equal_function_value_matters = 1;
401   virtuals_instantiated = 0;
402   generating_concat_p = 1;
403 }
404
405 void
406 pop_function_context ()
407 {
408   pop_function_context_from (current_function_decl);
409 }
410
411 /* Clear out all parts of the state in F that can safely be discarded
412    after the function has been parsed, but not compiled, to let
413    garbage collection reclaim the memory.  */
414
415 void
416 free_after_parsing (f)
417      struct function *f;
418 {
419   /* f->expr->forced_labels is used by code generation.  */
420   /* f->emit->regno_reg_rtx is used by code generation.  */
421   /* f->varasm is used by code generation.  */
422   /* f->eh->eh_return_stub_label is used by code generation.  */
423
424   if (free_lang_status)
425     (*free_lang_status) (f);
426   free_stmt_status (f);
427 }
428
429 /* Clear out all parts of the state in F that can safely be discarded
430    after the function has been compiled, to let garbage collection
431    reclaim the memory.  */
432
433 void
434 free_after_compilation (f)
435      struct function *f;
436 {
437   free_eh_status (f);
438   free_expr_status (f);
439   free_emit_status (f);
440   free_varasm_status (f);
441
442   if (free_machine_status)
443     (*free_machine_status) (f);
444
445   if (f->x_parm_reg_stack_loc)
446     free (f->x_parm_reg_stack_loc);
447
448   f->x_temp_slots = NULL;
449   f->arg_offset_rtx = NULL;
450   f->return_rtx = NULL;
451   f->internal_arg_pointer = NULL;
452   f->x_nonlocal_labels = NULL;
453   f->x_nonlocal_goto_handler_slots = NULL;
454   f->x_nonlocal_goto_handler_labels = NULL;
455   f->x_nonlocal_goto_stack_level = NULL;
456   f->x_cleanup_label = NULL;
457   f->x_return_label = NULL;
458   f->x_save_expr_regs = NULL;
459   f->x_stack_slot_list = NULL;
460   f->x_rtl_expr_chain = NULL;
461   f->x_tail_recursion_label = NULL;
462   f->x_tail_recursion_reentry = NULL;
463   f->x_arg_pointer_save_area = NULL;
464   f->x_clobber_return_insn = NULL;
465   f->x_context_display = NULL;
466   f->x_trampoline_list = NULL;
467   f->x_parm_birth_insn = NULL;
468   f->x_last_parm_insn = NULL;
469   f->x_parm_reg_stack_loc = NULL;
470   f->fixup_var_refs_queue = NULL;
471   f->original_arg_vector = NULL;
472   f->original_decl_initial = NULL;
473   f->inl_last_parm_insn = NULL;
474   f->epilogue_delay_list = NULL;
475 }
476 \f
477 /* Allocate fixed slots in the stack frame of the current function.  */
478
479 /* Return size needed for stack frame based on slots so far allocated in
480    function F.
481    This size counts from zero.  It is not rounded to PREFERRED_STACK_BOUNDARY;
482    the caller may have to do that.  */
483
484 HOST_WIDE_INT
485 get_func_frame_size (f)
486      struct function *f;
487 {
488 #ifdef FRAME_GROWS_DOWNWARD
489   return -f->x_frame_offset;
490 #else
491   return f->x_frame_offset;
492 #endif
493 }
494
495 /* Return size needed for stack frame based on slots so far allocated.
496    This size counts from zero.  It is not rounded to PREFERRED_STACK_BOUNDARY;
497    the caller may have to do that.  */
498 HOST_WIDE_INT
499 get_frame_size ()
500 {
501   return get_func_frame_size (cfun);
502 }
503
504 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
505    with machine mode MODE.
506
507    ALIGN controls the amount of alignment for the address of the slot:
508    0 means according to MODE,
509    -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
510    positive specifies alignment boundary in bits.
511
512    We do not round to stack_boundary here.
513
514    FUNCTION specifies the function to allocate in.  */
515
516 static rtx
517 assign_stack_local_1 (mode, size, align, function)
518      enum machine_mode mode;
519      HOST_WIDE_INT size;
520      int align;
521      struct function *function;
522 {
523   rtx x, addr;
524   int bigend_correction = 0;
525   int alignment;
526
527   if (align == 0)
528     {
529       tree type;
530
531       if (mode == BLKmode)
532         alignment = BIGGEST_ALIGNMENT;
533       else
534         alignment = GET_MODE_ALIGNMENT (mode);
535
536       /* Allow the target to (possibly) increase the alignment of this
537          stack slot.  */
538       type = type_for_mode (mode, 0);
539       if (type)
540         alignment = LOCAL_ALIGNMENT (type, alignment);
541
542       alignment /= BITS_PER_UNIT;
543     }
544   else if (align == -1)
545     {
546       alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
547       size = CEIL_ROUND (size, alignment);
548     }
549   else
550     alignment = align / BITS_PER_UNIT;
551
552 #ifdef FRAME_GROWS_DOWNWARD
553   function->x_frame_offset -= size;
554 #endif
555
556   /* Ignore alignment we can't do with expected alignment of the boundary.  */
557   if (alignment * BITS_PER_UNIT > PREFERRED_STACK_BOUNDARY)
558     alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
559
560   if (function->stack_alignment_needed < alignment * BITS_PER_UNIT)
561     function->stack_alignment_needed = alignment * BITS_PER_UNIT;
562
563   /* Round frame offset to that alignment.
564      We must be careful here, since FRAME_OFFSET might be negative and
565      division with a negative dividend isn't as well defined as we might
566      like.  So we instead assume that ALIGNMENT is a power of two and
567      use logical operations which are unambiguous.  */
568 #ifdef FRAME_GROWS_DOWNWARD
569   function->x_frame_offset = FLOOR_ROUND (function->x_frame_offset, alignment);
570 #else
571   function->x_frame_offset = CEIL_ROUND (function->x_frame_offset, alignment);
572 #endif
573
574   /* On a big-endian machine, if we are allocating more space than we will use,
575      use the least significant bytes of those that are allocated.  */
576   if (BYTES_BIG_ENDIAN && mode != BLKmode)
577     bigend_correction = size - GET_MODE_SIZE (mode);
578
579   /* If we have already instantiated virtual registers, return the actual
580      address relative to the frame pointer.  */
581   if (function == cfun && virtuals_instantiated)
582     addr = plus_constant (frame_pointer_rtx,
583                           (frame_offset + bigend_correction
584                            + STARTING_FRAME_OFFSET));
585   else
586     addr = plus_constant (virtual_stack_vars_rtx,
587                           function->x_frame_offset + bigend_correction);
588
589 #ifndef FRAME_GROWS_DOWNWARD
590   function->x_frame_offset += size;
591 #endif
592
593   x = gen_rtx_MEM (mode, addr);
594
595   function->x_stack_slot_list
596     = gen_rtx_EXPR_LIST (VOIDmode, x, function->x_stack_slot_list);
597
598   return x;
599 }
600
601 /* Wrapper around assign_stack_local_1;  assign a local stack slot for the
602    current function.  */
603
604 rtx
605 assign_stack_local (mode, size, align)
606      enum machine_mode mode;
607      HOST_WIDE_INT size;
608      int align;
609 {
610   return assign_stack_local_1 (mode, size, align, cfun);
611 }
612 \f
613 /* Allocate a temporary stack slot and record it for possible later
614    reuse.
615
616    MODE is the machine mode to be given to the returned rtx.
617
618    SIZE is the size in units of the space required.  We do no rounding here
619    since assign_stack_local will do any required rounding.
620
621    KEEP is 1 if this slot is to be retained after a call to
622    free_temp_slots.  Automatic variables for a block are allocated
623    with this flag.  KEEP is 2 if we allocate a longer term temporary,
624    whose lifetime is controlled by CLEANUP_POINT_EXPRs.  KEEP is 3
625    if we are to allocate something at an inner level to be treated as
626    a variable in the block (e.g., a SAVE_EXPR).
627
628    TYPE is the type that will be used for the stack slot.  */
629
630 rtx
631 assign_stack_temp_for_type (mode, size, keep, type)
632      enum machine_mode mode;
633      HOST_WIDE_INT size;
634      int keep;
635      tree type;
636 {
637   unsigned int align;
638   struct temp_slot *p, *best_p = 0;
639
640   /* If SIZE is -1 it means that somebody tried to allocate a temporary
641      of a variable size.  */
642   if (size == -1)
643     abort ();
644
645   if (mode == BLKmode)
646     align = BIGGEST_ALIGNMENT;
647   else
648     align = GET_MODE_ALIGNMENT (mode);
649
650   if (! type)
651     type = type_for_mode (mode, 0);
652
653   if (type)
654     align = LOCAL_ALIGNMENT (type, align);
655
656   /* Try to find an available, already-allocated temporary of the proper
657      mode which meets the size and alignment requirements.  Choose the
658      smallest one with the closest alignment.  */
659   for (p = temp_slots; p; p = p->next)
660     if (p->align >= align && p->size >= size && GET_MODE (p->slot) == mode
661         && ! p->in_use
662         && objects_must_conflict_p (p->type, type)
663         && (best_p == 0 || best_p->size > p->size
664             || (best_p->size == p->size && best_p->align > p->align)))
665       {
666         if (p->align == align && p->size == size)
667           {
668             best_p = 0;
669             break;
670           }
671         best_p = p;
672       }
673
674   /* Make our best, if any, the one to use.  */
675   if (best_p)
676     {
677       /* If there are enough aligned bytes left over, make them into a new
678          temp_slot so that the extra bytes don't get wasted.  Do this only
679          for BLKmode slots, so that we can be sure of the alignment.  */
680       if (GET_MODE (best_p->slot) == BLKmode)
681         {
682           int alignment = best_p->align / BITS_PER_UNIT;
683           HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
684
685           if (best_p->size - rounded_size >= alignment)
686             {
687               p = (struct temp_slot *) ggc_alloc (sizeof (struct temp_slot));
688               p->in_use = p->addr_taken = 0;
689               p->size = best_p->size - rounded_size;
690               p->base_offset = best_p->base_offset + rounded_size;
691               p->full_size = best_p->full_size - rounded_size;
692               p->slot = gen_rtx_MEM (BLKmode,
693                                      plus_constant (XEXP (best_p->slot, 0),
694                                                     rounded_size));
695               p->align = best_p->align;
696               p->address = 0;
697               p->rtl_expr = 0;
698               p->type = best_p->type;
699               p->next = temp_slots;
700               temp_slots = p;
701
702               stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
703                                                    stack_slot_list);
704
705               best_p->size = rounded_size;
706               best_p->full_size = rounded_size;
707             }
708         }
709
710       p = best_p;
711     }
712
713   /* If we still didn't find one, make a new temporary.  */
714   if (p == 0)
715     {
716       HOST_WIDE_INT frame_offset_old = frame_offset;
717
718       p = (struct temp_slot *) ggc_alloc (sizeof (struct temp_slot));
719
720       /* We are passing an explicit alignment request to assign_stack_local.
721          One side effect of that is assign_stack_local will not round SIZE
722          to ensure the frame offset remains suitably aligned.
723
724          So for requests which depended on the rounding of SIZE, we go ahead
725          and round it now.  We also make sure ALIGNMENT is at least
726          BIGGEST_ALIGNMENT.  */
727       if (mode == BLKmode && align < BIGGEST_ALIGNMENT)
728         abort();
729       p->slot = assign_stack_local (mode,
730                                     (mode == BLKmode
731                                      ? CEIL_ROUND (size, align / BITS_PER_UNIT)
732                                      : size),
733                                     align);
734
735       p->align = align;
736
737       /* The following slot size computation is necessary because we don't
738          know the actual size of the temporary slot until assign_stack_local
739          has performed all the frame alignment and size rounding for the
740          requested temporary.  Note that extra space added for alignment
741          can be either above or below this stack slot depending on which
742          way the frame grows.  We include the extra space if and only if it
743          is above this slot.  */
744 #ifdef FRAME_GROWS_DOWNWARD
745       p->size = frame_offset_old - frame_offset;
746 #else
747       p->size = size;
748 #endif
749
750       /* Now define the fields used by combine_temp_slots.  */
751 #ifdef FRAME_GROWS_DOWNWARD
752       p->base_offset = frame_offset;
753       p->full_size = frame_offset_old - frame_offset;
754 #else
755       p->base_offset = frame_offset_old;
756       p->full_size = frame_offset - frame_offset_old;
757 #endif
758       p->address = 0;
759       p->next = temp_slots;
760       temp_slots = p;
761     }
762
763   p->in_use = 1;
764   p->addr_taken = 0;
765   p->rtl_expr = seq_rtl_expr;
766   p->type = type;
767
768   if (keep == 2)
769     {
770       p->level = target_temp_slot_level;
771       p->keep = 0;
772     }
773   else if (keep == 3)
774     {
775       p->level = var_temp_slot_level;
776       p->keep = 0;
777     }
778   else
779     {
780       p->level = temp_slot_level;
781       p->keep = keep;
782     }
783
784   /* We may be reusing an old slot, so clear any MEM flags that may have been
785      set from before.  */
786   RTX_UNCHANGING_P (p->slot) = 0;
787   MEM_IN_STRUCT_P (p->slot) = 0;
788   MEM_SCALAR_P (p->slot) = 0;
789   MEM_VOLATILE_P (p->slot) = 0;
790   set_mem_alias_set (p->slot, 0);
791
792   /* If we know the alias set for the memory that will be used, use
793      it.  If there's no TYPE, then we don't know anything about the
794      alias set for the memory.  */
795   set_mem_alias_set (p->slot, type ? get_alias_set (type) : 0);
796   set_mem_align (p->slot, align);
797
798   /* If a type is specified, set the relevant flags.  */
799   if (type != 0)
800     {
801       RTX_UNCHANGING_P (p->slot) = TYPE_READONLY (type);
802       MEM_VOLATILE_P (p->slot) = TYPE_VOLATILE (type);
803       MEM_SET_IN_STRUCT_P (p->slot, AGGREGATE_TYPE_P (type));
804     }
805
806   return p->slot;
807 }
808
809 /* Allocate a temporary stack slot and record it for possible later
810    reuse.  First three arguments are same as in preceding function.  */
811
812 rtx
813 assign_stack_temp (mode, size, keep)
814      enum machine_mode mode;
815      HOST_WIDE_INT size;
816      int keep;
817 {
818   return assign_stack_temp_for_type (mode, size, keep, NULL_TREE);
819 }
820 \f
821 /* Assign a temporary of given TYPE.
822    KEEP is as for assign_stack_temp.
823    MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
824    it is 0 if a register is OK.
825    DONT_PROMOTE is 1 if we should not promote values in register
826    to wider modes.  */
827
828 rtx
829 assign_temp (type, keep, memory_required, dont_promote)
830      tree type;
831      int keep;
832      int memory_required;
833      int dont_promote ATTRIBUTE_UNUSED;
834 {
835   enum machine_mode mode = TYPE_MODE (type);
836 #ifndef PROMOTE_FOR_CALL_ONLY
837   int unsignedp = TREE_UNSIGNED (type);
838 #endif
839
840   if (mode == BLKmode || memory_required)
841     {
842       HOST_WIDE_INT size = int_size_in_bytes (type);
843       rtx tmp;
844
845       /* Zero sized arrays are GNU C extension.  Set size to 1 to avoid
846          problems with allocating the stack space.  */
847       if (size == 0)
848         size = 1;
849
850       /* Unfortunately, we don't yet know how to allocate variable-sized
851          temporaries.  However, sometimes we have a fixed upper limit on
852          the size (which is stored in TYPE_ARRAY_MAX_SIZE) and can use that
853          instead.  This is the case for Chill variable-sized strings.  */
854       if (size == -1 && TREE_CODE (type) == ARRAY_TYPE
855           && TYPE_ARRAY_MAX_SIZE (type) != NULL_TREE
856           && host_integerp (TYPE_ARRAY_MAX_SIZE (type), 1))
857         size = tree_low_cst (TYPE_ARRAY_MAX_SIZE (type), 1);
858
859       tmp = assign_stack_temp_for_type (mode, size, keep, type);
860       return tmp;
861     }
862
863 #ifndef PROMOTE_FOR_CALL_ONLY
864   if (! dont_promote)
865     mode = promote_mode (type, mode, &unsignedp, 0);
866 #endif
867
868   return gen_reg_rtx (mode);
869 }
870 \f
871 /* Combine temporary stack slots which are adjacent on the stack.
872
873    This allows for better use of already allocated stack space.  This is only
874    done for BLKmode slots because we can be sure that we won't have alignment
875    problems in this case.  */
876
877 void
878 combine_temp_slots ()
879 {
880   struct temp_slot *p, *q;
881   struct temp_slot *prev_p, *prev_q;
882   int num_slots;
883
884   /* We can't combine slots, because the information about which slot
885      is in which alias set will be lost.  */
886   if (flag_strict_aliasing)
887     return;
888
889   /* If there are a lot of temp slots, don't do anything unless
890      high levels of optimization.  */
891   if (! flag_expensive_optimizations)
892     for (p = temp_slots, num_slots = 0; p; p = p->next, num_slots++)
893       if (num_slots > 100 || (num_slots > 10 && optimize == 0))
894         return;
895
896   for (p = temp_slots, prev_p = 0; p; p = prev_p ? prev_p->next : temp_slots)
897     {
898       int delete_p = 0;
899
900       if (! p->in_use && GET_MODE (p->slot) == BLKmode)
901         for (q = p->next, prev_q = p; q; q = prev_q->next)
902           {
903             int delete_q = 0;
904             if (! q->in_use && GET_MODE (q->slot) == BLKmode)
905               {
906                 if (p->base_offset + p->full_size == q->base_offset)
907                   {
908                     /* Q comes after P; combine Q into P.  */
909                     p->size += q->size;
910                     p->full_size += q->full_size;
911                     delete_q = 1;
912                   }
913                 else if (q->base_offset + q->full_size == p->base_offset)
914                   {
915                     /* P comes after Q; combine P into Q.  */
916                     q->size += p->size;
917                     q->full_size += p->full_size;
918                     delete_p = 1;
919                     break;
920                   }
921               }
922             /* Either delete Q or advance past it.  */
923             if (delete_q)
924               prev_q->next = q->next;
925             else
926               prev_q = q;
927           }
928       /* Either delete P or advance past it.  */
929       if (delete_p)
930         {
931           if (prev_p)
932             prev_p->next = p->next;
933           else
934             temp_slots = p->next;
935         }
936       else
937         prev_p = p;
938     }
939 }
940 \f
941 /* Find the temp slot corresponding to the object at address X.  */
942
943 static struct temp_slot *
944 find_temp_slot_from_address (x)
945      rtx x;
946 {
947   struct temp_slot *p;
948   rtx next;
949
950   for (p = temp_slots; p; p = p->next)
951     {
952       if (! p->in_use)
953         continue;
954
955       else if (XEXP (p->slot, 0) == x
956                || p->address == x
957                || (GET_CODE (x) == PLUS
958                    && XEXP (x, 0) == virtual_stack_vars_rtx
959                    && GET_CODE (XEXP (x, 1)) == CONST_INT
960                    && INTVAL (XEXP (x, 1)) >= p->base_offset
961                    && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size))
962         return p;
963
964       else if (p->address != 0 && GET_CODE (p->address) == EXPR_LIST)
965         for (next = p->address; next; next = XEXP (next, 1))
966           if (XEXP (next, 0) == x)
967             return p;
968     }
969
970   /* If we have a sum involving a register, see if it points to a temp
971      slot.  */
972   if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == REG
973       && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
974     return p;
975   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG
976            && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
977     return p;
978
979   return 0;
980 }
981
982 /* Indicate that NEW is an alternate way of referring to the temp slot
983    that previously was known by OLD.  */
984
985 void
986 update_temp_slot_address (old, new)
987      rtx old, new;
988 {
989   struct temp_slot *p;
990
991   if (rtx_equal_p (old, new))
992     return;
993
994   p = find_temp_slot_from_address (old);
995
996   /* If we didn't find one, see if both OLD is a PLUS.  If so, and NEW
997      is a register, see if one operand of the PLUS is a temporary
998      location.  If so, NEW points into it.  Otherwise, if both OLD and
999      NEW are a PLUS and if there is a register in common between them.
1000      If so, try a recursive call on those values.  */
1001   if (p == 0)
1002     {
1003       if (GET_CODE (old) != PLUS)
1004         return;
1005
1006       if (GET_CODE (new) == REG)
1007         {
1008           update_temp_slot_address (XEXP (old, 0), new);
1009           update_temp_slot_address (XEXP (old, 1), new);
1010           return;
1011         }
1012       else if (GET_CODE (new) != PLUS)
1013         return;
1014
1015       if (rtx_equal_p (XEXP (old, 0), XEXP (new, 0)))
1016         update_temp_slot_address (XEXP (old, 1), XEXP (new, 1));
1017       else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 0)))
1018         update_temp_slot_address (XEXP (old, 0), XEXP (new, 1));
1019       else if (rtx_equal_p (XEXP (old, 0), XEXP (new, 1)))
1020         update_temp_slot_address (XEXP (old, 1), XEXP (new, 0));
1021       else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 1)))
1022         update_temp_slot_address (XEXP (old, 0), XEXP (new, 0));
1023
1024       return;
1025     }
1026
1027   /* Otherwise add an alias for the temp's address.  */
1028   else if (p->address == 0)
1029     p->address = new;
1030   else
1031     {
1032       if (GET_CODE (p->address) != EXPR_LIST)
1033         p->address = gen_rtx_EXPR_LIST (VOIDmode, p->address, NULL_RTX);
1034
1035       p->address = gen_rtx_EXPR_LIST (VOIDmode, new, p->address);
1036     }
1037 }
1038
1039 /* If X could be a reference to a temporary slot, mark the fact that its
1040    address was taken.  */
1041
1042 void
1043 mark_temp_addr_taken (x)
1044      rtx x;
1045 {
1046   struct temp_slot *p;
1047
1048   if (x == 0)
1049     return;
1050
1051   /* If X is not in memory or is at a constant address, it cannot be in
1052      a temporary slot.  */
1053   if (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1054     return;
1055
1056   p = find_temp_slot_from_address (XEXP (x, 0));
1057   if (p != 0)
1058     p->addr_taken = 1;
1059 }
1060
1061 /* If X could be a reference to a temporary slot, mark that slot as
1062    belonging to the to one level higher than the current level.  If X
1063    matched one of our slots, just mark that one.  Otherwise, we can't
1064    easily predict which it is, so upgrade all of them.  Kept slots
1065    need not be touched.
1066
1067    This is called when an ({...}) construct occurs and a statement
1068    returns a value in memory.  */
1069
1070 void
1071 preserve_temp_slots (x)
1072      rtx x;
1073 {
1074   struct temp_slot *p = 0;
1075
1076   /* If there is no result, we still might have some objects whose address
1077      were taken, so we need to make sure they stay around.  */
1078   if (x == 0)
1079     {
1080       for (p = temp_slots; p; p = p->next)
1081         if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1082           p->level--;
1083
1084       return;
1085     }
1086
1087   /* If X is a register that is being used as a pointer, see if we have
1088      a temporary slot we know it points to.  To be consistent with
1089      the code below, we really should preserve all non-kept slots
1090      if we can't find a match, but that seems to be much too costly.  */
1091   if (GET_CODE (x) == REG && REG_POINTER (x))
1092     p = find_temp_slot_from_address (x);
1093
1094   /* If X is not in memory or is at a constant address, it cannot be in
1095      a temporary slot, but it can contain something whose address was
1096      taken.  */
1097   if (p == 0 && (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0))))
1098     {
1099       for (p = temp_slots; p; p = p->next)
1100         if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1101           p->level--;
1102
1103       return;
1104     }
1105
1106   /* First see if we can find a match.  */
1107   if (p == 0)
1108     p = find_temp_slot_from_address (XEXP (x, 0));
1109
1110   if (p != 0)
1111     {
1112       /* Move everything at our level whose address was taken to our new
1113          level in case we used its address.  */
1114       struct temp_slot *q;
1115
1116       if (p->level == temp_slot_level)
1117         {
1118           for (q = temp_slots; q; q = q->next)
1119             if (q != p && q->addr_taken && q->level == p->level)
1120               q->level--;
1121
1122           p->level--;
1123           p->addr_taken = 0;
1124         }
1125       return;
1126     }
1127
1128   /* Otherwise, preserve all non-kept slots at this level.  */
1129   for (p = temp_slots; p; p = p->next)
1130     if (p->in_use && p->level == temp_slot_level && ! p->keep)
1131       p->level--;
1132 }
1133
1134 /* X is the result of an RTL_EXPR.  If it is a temporary slot associated
1135    with that RTL_EXPR, promote it into a temporary slot at the present
1136    level so it will not be freed when we free slots made in the
1137    RTL_EXPR.  */
1138
1139 void
1140 preserve_rtl_expr_result (x)
1141      rtx x;
1142 {
1143   struct temp_slot *p;
1144
1145   /* If X is not in memory or is at a constant address, it cannot be in
1146      a temporary slot.  */
1147   if (x == 0 || GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1148     return;
1149
1150   /* If we can find a match, move it to our level unless it is already at
1151      an upper level.  */
1152   p = find_temp_slot_from_address (XEXP (x, 0));
1153   if (p != 0)
1154     {
1155       p->level = MIN (p->level, temp_slot_level);
1156       p->rtl_expr = 0;
1157     }
1158
1159   return;
1160 }
1161
1162 /* Free all temporaries used so far.  This is normally called at the end
1163    of generating code for a statement.  Don't free any temporaries
1164    currently in use for an RTL_EXPR that hasn't yet been emitted.
1165    We could eventually do better than this since it can be reused while
1166    generating the same RTL_EXPR, but this is complex and probably not
1167    worthwhile.  */
1168
1169 void
1170 free_temp_slots ()
1171 {
1172   struct temp_slot *p;
1173
1174   for (p = temp_slots; p; p = p->next)
1175     if (p->in_use && p->level == temp_slot_level && ! p->keep
1176         && p->rtl_expr == 0)
1177       p->in_use = 0;
1178
1179   combine_temp_slots ();
1180 }
1181
1182 /* Free all temporary slots used in T, an RTL_EXPR node.  */
1183
1184 void
1185 free_temps_for_rtl_expr (t)
1186      tree t;
1187 {
1188   struct temp_slot *p;
1189
1190   for (p = temp_slots; p; p = p->next)
1191     if (p->rtl_expr == t)
1192       {
1193         /* If this slot is below the current TEMP_SLOT_LEVEL, then it
1194            needs to be preserved.  This can happen if a temporary in
1195            the RTL_EXPR was addressed; preserve_temp_slots will move
1196            the temporary into a higher level.  */
1197         if (temp_slot_level <= p->level)
1198           p->in_use = 0;
1199         else
1200           p->rtl_expr = NULL_TREE;
1201       }
1202
1203   combine_temp_slots ();
1204 }
1205
1206 /* Mark all temporaries ever allocated in this function as not suitable
1207    for reuse until the current level is exited.  */
1208
1209 void
1210 mark_all_temps_used ()
1211 {
1212   struct temp_slot *p;
1213
1214   for (p = temp_slots; p; p = p->next)
1215     {
1216       p->in_use = p->keep = 1;
1217       p->level = MIN (p->level, temp_slot_level);
1218     }
1219 }
1220
1221 /* Push deeper into the nesting level for stack temporaries.  */
1222
1223 void
1224 push_temp_slots ()
1225 {
1226   temp_slot_level++;
1227 }
1228
1229 /* Likewise, but save the new level as the place to allocate variables
1230    for blocks.  */
1231
1232 #if 0
1233 void
1234 push_temp_slots_for_block ()
1235 {
1236   push_temp_slots ();
1237
1238   var_temp_slot_level = temp_slot_level;
1239 }
1240
1241 /* Likewise, but save the new level as the place to allocate temporaries
1242    for TARGET_EXPRs.  */
1243
1244 void
1245 push_temp_slots_for_target ()
1246 {
1247   push_temp_slots ();
1248
1249   target_temp_slot_level = temp_slot_level;
1250 }
1251
1252 /* Set and get the value of target_temp_slot_level.  The only
1253    permitted use of these functions is to save and restore this value.  */
1254
1255 int
1256 get_target_temp_slot_level ()
1257 {
1258   return target_temp_slot_level;
1259 }
1260
1261 void
1262 set_target_temp_slot_level (level)
1263      int level;
1264 {
1265   target_temp_slot_level = level;
1266 }
1267 #endif
1268
1269 /* Pop a temporary nesting level.  All slots in use in the current level
1270    are freed.  */
1271
1272 void
1273 pop_temp_slots ()
1274 {
1275   struct temp_slot *p;
1276
1277   for (p = temp_slots; p; p = p->next)
1278     if (p->in_use && p->level == temp_slot_level && p->rtl_expr == 0)
1279       p->in_use = 0;
1280
1281   combine_temp_slots ();
1282
1283   temp_slot_level--;
1284 }
1285
1286 /* Initialize temporary slots.  */
1287
1288 void
1289 init_temp_slots ()
1290 {
1291   /* We have not allocated any temporaries yet.  */
1292   temp_slots = 0;
1293   temp_slot_level = 0;
1294   var_temp_slot_level = 0;
1295   target_temp_slot_level = 0;
1296 }
1297 \f
1298 /* Retroactively move an auto variable from a register to a stack slot.
1299    This is done when an address-reference to the variable is seen.  */
1300
1301 void
1302 put_var_into_stack (decl)
1303      tree decl;
1304 {
1305   rtx reg;
1306   enum machine_mode promoted_mode, decl_mode;
1307   struct function *function = 0;
1308   tree context;
1309   int can_use_addressof;
1310   int volatilep = TREE_CODE (decl) != SAVE_EXPR && TREE_THIS_VOLATILE (decl);
1311   int usedp = (TREE_USED (decl)
1312                || (TREE_CODE (decl) != SAVE_EXPR && DECL_INITIAL (decl) != 0));
1313
1314   context = decl_function_context (decl);
1315
1316   /* Get the current rtl used for this object and its original mode.  */
1317   reg = (TREE_CODE (decl) == SAVE_EXPR 
1318          ? SAVE_EXPR_RTL (decl) 
1319          : DECL_RTL_IF_SET (decl));
1320
1321   /* No need to do anything if decl has no rtx yet
1322      since in that case caller is setting TREE_ADDRESSABLE
1323      and a stack slot will be assigned when the rtl is made.  */
1324   if (reg == 0)
1325     return;
1326
1327   /* Get the declared mode for this object.  */
1328   decl_mode = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
1329                : DECL_MODE (decl));
1330   /* Get the mode it's actually stored in.  */
1331   promoted_mode = GET_MODE (reg);
1332
1333   /* If this variable comes from an outer function, find that
1334      function's saved context.  Don't use find_function_data here,
1335      because it might not be in any active function.
1336      FIXME: Is that really supposed to happen?
1337      It does in ObjC at least.  */
1338   if (context != current_function_decl && context != inline_function_decl)
1339     for (function = outer_function_chain; function; function = function->outer)
1340       if (function->decl == context)
1341         break;
1342
1343   /* If this is a variable-size object with a pseudo to address it,
1344      put that pseudo into the stack, if the var is nonlocal.  */
1345   if (TREE_CODE (decl) != SAVE_EXPR && DECL_NONLOCAL (decl)
1346       && GET_CODE (reg) == MEM
1347       && GET_CODE (XEXP (reg, 0)) == REG
1348       && REGNO (XEXP (reg, 0)) > LAST_VIRTUAL_REGISTER)
1349     {
1350       reg = XEXP (reg, 0);
1351       decl_mode = promoted_mode = GET_MODE (reg);
1352     }
1353
1354   can_use_addressof
1355     = (function == 0
1356        && optimize > 0
1357        /* FIXME make it work for promoted modes too */
1358        && decl_mode == promoted_mode
1359 #ifdef NON_SAVING_SETJMP
1360        && ! (NON_SAVING_SETJMP && current_function_calls_setjmp)
1361 #endif
1362        );
1363
1364   /* If we can't use ADDRESSOF, make sure we see through one we already
1365      generated.  */
1366   if (! can_use_addressof && GET_CODE (reg) == MEM
1367       && GET_CODE (XEXP (reg, 0)) == ADDRESSOF)
1368     reg = XEXP (XEXP (reg, 0), 0);
1369
1370   /* Now we should have a value that resides in one or more pseudo regs.  */
1371
1372   if (GET_CODE (reg) == REG)
1373     {
1374       /* If this variable lives in the current function and we don't need
1375          to put things in the stack for the sake of setjmp, try to keep it
1376          in a register until we know we actually need the address.  */
1377       if (can_use_addressof)
1378         gen_mem_addressof (reg, decl);
1379       else
1380         put_reg_into_stack (function, reg, TREE_TYPE (decl), promoted_mode,
1381                             decl_mode, volatilep, 0, usedp, 0);
1382     }
1383   else if (GET_CODE (reg) == CONCAT)
1384     {
1385       /* A CONCAT contains two pseudos; put them both in the stack.
1386          We do it so they end up consecutive.
1387          We fixup references to the parts only after we fixup references
1388          to the whole CONCAT, lest we do double fixups for the latter
1389          references.  */
1390       enum machine_mode part_mode = GET_MODE (XEXP (reg, 0));
1391       tree part_type = type_for_mode (part_mode, 0);
1392       rtx lopart = XEXP (reg, 0);
1393       rtx hipart = XEXP (reg, 1);
1394 #ifdef FRAME_GROWS_DOWNWARD
1395       /* Since part 0 should have a lower address, do it second.  */
1396       put_reg_into_stack (function, hipart, part_type, part_mode,
1397                           part_mode, volatilep, 0, 0, 0);
1398       put_reg_into_stack (function, lopart, part_type, part_mode,
1399                           part_mode, volatilep, 0, 0, 0);
1400 #else
1401       put_reg_into_stack (function, lopart, part_type, part_mode,
1402                           part_mode, volatilep, 0, 0, 0);
1403       put_reg_into_stack (function, hipart, part_type, part_mode,
1404                           part_mode, volatilep, 0, 0, 0);
1405 #endif
1406
1407       /* Change the CONCAT into a combined MEM for both parts.  */
1408       PUT_CODE (reg, MEM);
1409       MEM_ATTRS (reg) = 0;
1410
1411       /* set_mem_attributes uses DECL_RTL to avoid re-generating of
1412          already computed alias sets.  Here we want to re-generate.  */
1413       if (DECL_P (decl))
1414         SET_DECL_RTL (decl, NULL);
1415       set_mem_attributes (reg, decl, 1);
1416       if (DECL_P (decl))
1417         SET_DECL_RTL (decl, reg);
1418
1419       /* The two parts are in memory order already.
1420          Use the lower parts address as ours.  */
1421       XEXP (reg, 0) = XEXP (XEXP (reg, 0), 0);
1422       /* Prevent sharing of rtl that might lose.  */
1423       if (GET_CODE (XEXP (reg, 0)) == PLUS)
1424         XEXP (reg, 0) = copy_rtx (XEXP (reg, 0));
1425       if (usedp)
1426         {
1427           schedule_fixup_var_refs (function, reg, TREE_TYPE (decl),
1428                                    promoted_mode, 0);
1429           schedule_fixup_var_refs (function, lopart, part_type, part_mode, 0);
1430           schedule_fixup_var_refs (function, hipart, part_type, part_mode, 0);
1431         }
1432     }
1433   else
1434     return;
1435 }
1436
1437 /* Subroutine of put_var_into_stack.  This puts a single pseudo reg REG
1438    into the stack frame of FUNCTION (0 means the current function).
1439    DECL_MODE is the machine mode of the user-level data type.
1440    PROMOTED_MODE is the machine mode of the register.
1441    VOLATILE_P is nonzero if this is for a "volatile" decl.
1442    USED_P is nonzero if this reg might have already been used in an insn.  */
1443
1444 static void
1445 put_reg_into_stack (function, reg, type, promoted_mode, decl_mode, volatile_p,
1446                     original_regno, used_p, ht)
1447      struct function *function;
1448      rtx reg;
1449      tree type;
1450      enum machine_mode promoted_mode, decl_mode;
1451      int volatile_p;
1452      unsigned int original_regno;
1453      int used_p;
1454      struct hash_table *ht;
1455 {
1456   struct function *func = function ? function : cfun;
1457   rtx new = 0;
1458   unsigned int regno = original_regno;
1459
1460   if (regno == 0)
1461     regno = REGNO (reg);
1462
1463   if (regno < func->x_max_parm_reg)
1464     new = func->x_parm_reg_stack_loc[regno];
1465
1466   if (new == 0)
1467     new = assign_stack_local_1 (decl_mode, GET_MODE_SIZE (decl_mode), 0, func);
1468
1469   PUT_CODE (reg, MEM);
1470   PUT_MODE (reg, decl_mode);
1471   XEXP (reg, 0) = XEXP (new, 0);
1472   MEM_ATTRS (reg) = 0;
1473   /* `volatil' bit means one thing for MEMs, another entirely for REGs.  */
1474   MEM_VOLATILE_P (reg) = volatile_p;
1475
1476   /* If this is a memory ref that contains aggregate components,
1477      mark it as such for cse and loop optimize.  If we are reusing a
1478      previously generated stack slot, then we need to copy the bit in
1479      case it was set for other reasons.  For instance, it is set for
1480      __builtin_va_alist.  */
1481   if (type)
1482     {
1483       MEM_SET_IN_STRUCT_P (reg,
1484                            AGGREGATE_TYPE_P (type) || MEM_IN_STRUCT_P (new));
1485       set_mem_alias_set (reg, get_alias_set (type));
1486     }
1487
1488   if (used_p)
1489     schedule_fixup_var_refs (function, reg, type, promoted_mode, ht);
1490 }
1491
1492 /* Make sure that all refs to the variable, previously made
1493    when it was a register, are fixed up to be valid again.
1494    See function above for meaning of arguments.  */
1495
1496 static void
1497 schedule_fixup_var_refs (function, reg, type, promoted_mode, ht)
1498      struct function *function;
1499      rtx reg;
1500      tree type;
1501      enum machine_mode promoted_mode;
1502      struct hash_table *ht;
1503 {
1504   int unsigned_p = type ? TREE_UNSIGNED (type) : 0;
1505
1506   if (function != 0)
1507     {
1508       struct var_refs_queue *temp;
1509
1510       temp
1511         = (struct var_refs_queue *) ggc_alloc (sizeof (struct var_refs_queue));
1512       temp->modified = reg;
1513       temp->promoted_mode = promoted_mode;
1514       temp->unsignedp = unsigned_p;
1515       temp->next = function->fixup_var_refs_queue;
1516       function->fixup_var_refs_queue = temp;
1517     }
1518   else
1519     /* Variable is local; fix it up now.  */
1520     fixup_var_refs (reg, promoted_mode, unsigned_p, ht);
1521 }
1522 \f
1523 static void
1524 fixup_var_refs (var, promoted_mode, unsignedp, ht)
1525      rtx var;
1526      enum machine_mode promoted_mode;
1527      int unsignedp;
1528      struct hash_table *ht;
1529 {
1530   tree pending;
1531   rtx first_insn = get_insns ();
1532   struct sequence_stack *stack = seq_stack;
1533   tree rtl_exps = rtl_expr_chain;
1534
1535   /* If there's a hash table, it must record all uses of VAR.  */
1536   if (ht)
1537     {
1538       if (stack != 0)
1539         abort ();
1540       fixup_var_refs_insns_with_hash (ht, var, promoted_mode, unsignedp);
1541       return;
1542     }
1543
1544   fixup_var_refs_insns (first_insn, var, promoted_mode, unsignedp,
1545                         stack == 0);
1546
1547   /* Scan all pending sequences too.  */
1548   for (; stack; stack = stack->next)
1549     {
1550       push_to_full_sequence (stack->first, stack->last);
1551       fixup_var_refs_insns (stack->first, var, promoted_mode, unsignedp,
1552                             stack->next != 0);
1553       /* Update remembered end of sequence
1554          in case we added an insn at the end.  */
1555       stack->last = get_last_insn ();
1556       end_sequence ();
1557     }
1558
1559   /* Scan all waiting RTL_EXPRs too.  */
1560   for (pending = rtl_exps; pending; pending = TREE_CHAIN (pending))
1561     {
1562       rtx seq = RTL_EXPR_SEQUENCE (TREE_VALUE (pending));
1563       if (seq != const0_rtx && seq != 0)
1564         {
1565           push_to_sequence (seq);
1566           fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0);
1567           end_sequence ();
1568         }
1569     }
1570 }
1571 \f
1572 /* REPLACEMENTS is a pointer to a list of the struct fixup_replacement and X is
1573    some part of an insn.  Return a struct fixup_replacement whose OLD
1574    value is equal to X.  Allocate a new structure if no such entry exists.  */
1575
1576 static struct fixup_replacement *
1577 find_fixup_replacement (replacements, x)
1578      struct fixup_replacement **replacements;
1579      rtx x;
1580 {
1581   struct fixup_replacement *p;
1582
1583   /* See if we have already replaced this.  */
1584   for (p = *replacements; p != 0 && ! rtx_equal_p (p->old, x); p = p->next)
1585     ;
1586
1587   if (p == 0)
1588     {
1589       p = (struct fixup_replacement *) xmalloc (sizeof (struct fixup_replacement));
1590       p->old = x;
1591       p->new = 0;
1592       p->next = *replacements;
1593       *replacements = p;
1594     }
1595
1596   return p;
1597 }
1598
1599 /* Scan the insn-chain starting with INSN for refs to VAR
1600    and fix them up.  TOPLEVEL is nonzero if this chain is the
1601    main chain of insns for the current function.  */
1602
1603 static void
1604 fixup_var_refs_insns (insn, var, promoted_mode, unsignedp, toplevel)
1605      rtx insn;
1606      rtx var;
1607      enum machine_mode promoted_mode;
1608      int unsignedp;
1609      int toplevel;
1610 {
1611   while (insn)
1612     {
1613       /* fixup_var_refs_insn might modify insn, so save its next
1614          pointer now.  */
1615       rtx next = NEXT_INSN (insn);
1616
1617       /* CALL_PLACEHOLDERs are special; we have to switch into each of
1618          the three sequences they (potentially) contain, and process
1619          them recursively.  The CALL_INSN itself is not interesting.  */
1620
1621       if (GET_CODE (insn) == CALL_INSN
1622           && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1623         {
1624           int i;
1625
1626           /* Look at the Normal call, sibling call and tail recursion
1627              sequences attached to the CALL_PLACEHOLDER.  */
1628           for (i = 0; i < 3; i++)
1629             {
1630               rtx seq = XEXP (PATTERN (insn), i);
1631               if (seq)
1632                 {
1633                   push_to_sequence (seq);
1634                   fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0);
1635                   XEXP (PATTERN (insn), i) = get_insns ();
1636                   end_sequence ();
1637                 }
1638             }
1639         }
1640
1641       else if (INSN_P (insn))
1642         fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, toplevel);
1643
1644       insn = next;
1645     }
1646 }
1647
1648 /* Look up the insns which reference VAR in HT and fix them up.  Other
1649    arguments are the same as fixup_var_refs_insns.
1650
1651    N.B. No need for special processing of CALL_PLACEHOLDERs here,
1652    because the hash table will point straight to the interesting insn
1653    (inside the CALL_PLACEHOLDER).  */
1654
1655 static void
1656 fixup_var_refs_insns_with_hash (ht, var, promoted_mode, unsignedp)
1657      struct hash_table *ht;
1658      rtx var;
1659      enum machine_mode promoted_mode;
1660      int unsignedp;
1661 {
1662   struct insns_for_mem_entry *ime = (struct insns_for_mem_entry *)
1663     hash_lookup (ht, var, /*create=*/0, /*copy=*/0);
1664   rtx insn_list = ime->insns;
1665
1666   while (insn_list)
1667     {
1668       rtx insn = XEXP (insn_list, 0);
1669         
1670       if (INSN_P (insn))
1671         fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, 1);
1672
1673       insn_list = XEXP (insn_list, 1);
1674     }
1675 }
1676
1677
1678 /* Per-insn processing by fixup_var_refs_insns(_with_hash).  INSN is
1679    the insn under examination, VAR is the variable to fix up
1680    references to, PROMOTED_MODE and UNSIGNEDP describe VAR, and
1681    TOPLEVEL is nonzero if this is the main insn chain for this
1682    function.  */
1683
1684 static void
1685 fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, toplevel)
1686      rtx insn;
1687      rtx var;
1688      enum machine_mode promoted_mode;
1689      int unsignedp;
1690      int toplevel;
1691 {
1692   rtx call_dest = 0;
1693   rtx set, prev, prev_set;
1694   rtx note;
1695
1696   /* Remember the notes in case we delete the insn.  */
1697   note = REG_NOTES (insn);
1698
1699   /* If this is a CLOBBER of VAR, delete it.
1700
1701      If it has a REG_LIBCALL note, delete the REG_LIBCALL
1702      and REG_RETVAL notes too.  */
1703   if (GET_CODE (PATTERN (insn)) == CLOBBER
1704       && (XEXP (PATTERN (insn), 0) == var
1705           || (GET_CODE (XEXP (PATTERN (insn), 0)) == CONCAT
1706               && (XEXP (XEXP (PATTERN (insn), 0), 0) == var
1707                   || XEXP (XEXP (PATTERN (insn), 0), 1) == var))))
1708     {
1709       if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0)
1710         /* The REG_LIBCALL note will go away since we are going to
1711            turn INSN into a NOTE, so just delete the
1712            corresponding REG_RETVAL note.  */
1713         remove_note (XEXP (note, 0),
1714                      find_reg_note (XEXP (note, 0), REG_RETVAL,
1715                                     NULL_RTX));
1716
1717       delete_insn (insn);
1718     }
1719
1720   /* The insn to load VAR from a home in the arglist
1721      is now a no-op.  When we see it, just delete it.
1722      Similarly if this is storing VAR from a register from which
1723      it was loaded in the previous insn.  This will occur
1724      when an ADDRESSOF was made for an arglist slot.  */
1725   else if (toplevel
1726            && (set = single_set (insn)) != 0
1727            && SET_DEST (set) == var
1728            /* If this represents the result of an insn group,
1729               don't delete the insn.  */
1730            && find_reg_note (insn, REG_RETVAL, NULL_RTX) == 0
1731            && (rtx_equal_p (SET_SRC (set), var)
1732                || (GET_CODE (SET_SRC (set)) == REG
1733                    && (prev = prev_nonnote_insn (insn)) != 0
1734                    && (prev_set = single_set (prev)) != 0
1735                    && SET_DEST (prev_set) == SET_SRC (set)
1736                    && rtx_equal_p (SET_SRC (prev_set), var))))
1737     {
1738       delete_insn (insn);
1739     }
1740   else
1741     {
1742       struct fixup_replacement *replacements = 0;
1743       rtx next_insn = NEXT_INSN (insn);
1744
1745       if (SMALL_REGISTER_CLASSES)
1746         {
1747           /* If the insn that copies the results of a CALL_INSN
1748              into a pseudo now references VAR, we have to use an
1749              intermediate pseudo since we want the life of the
1750              return value register to be only a single insn.
1751
1752              If we don't use an intermediate pseudo, such things as
1753              address computations to make the address of VAR valid
1754              if it is not can be placed between the CALL_INSN and INSN.
1755
1756              To make sure this doesn't happen, we record the destination
1757              of the CALL_INSN and see if the next insn uses both that
1758              and VAR.  */
1759
1760           if (call_dest != 0 && GET_CODE (insn) == INSN
1761               && reg_mentioned_p (var, PATTERN (insn))
1762               && reg_mentioned_p (call_dest, PATTERN (insn)))
1763             {
1764               rtx temp = gen_reg_rtx (GET_MODE (call_dest));
1765
1766               emit_insn_before (gen_move_insn (temp, call_dest), insn);
1767
1768               PATTERN (insn) = replace_rtx (PATTERN (insn),
1769                                             call_dest, temp);
1770             }
1771
1772           if (GET_CODE (insn) == CALL_INSN
1773               && GET_CODE (PATTERN (insn)) == SET)
1774             call_dest = SET_DEST (PATTERN (insn));
1775           else if (GET_CODE (insn) == CALL_INSN
1776                    && GET_CODE (PATTERN (insn)) == PARALLEL
1777                    && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1778             call_dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1779           else
1780             call_dest = 0;
1781         }
1782
1783       /* See if we have to do anything to INSN now that VAR is in
1784          memory.  If it needs to be loaded into a pseudo, use a single
1785          pseudo for the entire insn in case there is a MATCH_DUP
1786          between two operands.  We pass a pointer to the head of
1787          a list of struct fixup_replacements.  If fixup_var_refs_1
1788          needs to allocate pseudos or replacement MEMs (for SUBREGs),
1789          it will record them in this list.
1790
1791          If it allocated a pseudo for any replacement, we copy into
1792          it here.  */
1793
1794       fixup_var_refs_1 (var, promoted_mode, &PATTERN (insn), insn,
1795                         &replacements);
1796
1797       /* If this is last_parm_insn, and any instructions were output
1798          after it to fix it up, then we must set last_parm_insn to
1799          the last such instruction emitted.  */
1800       if (insn == last_parm_insn)
1801         last_parm_insn = PREV_INSN (next_insn);
1802
1803       while (replacements)
1804         {
1805           struct fixup_replacement *next;
1806
1807           if (GET_CODE (replacements->new) == REG)
1808             {
1809               rtx insert_before;
1810               rtx seq;
1811
1812               /* OLD might be a (subreg (mem)).  */
1813               if (GET_CODE (replacements->old) == SUBREG)
1814                 replacements->old
1815                   = fixup_memory_subreg (replacements->old, insn, 0);
1816               else
1817                 replacements->old
1818                   = fixup_stack_1 (replacements->old, insn);
1819
1820               insert_before = insn;
1821
1822               /* If we are changing the mode, do a conversion.
1823                  This might be wasteful, but combine.c will
1824                  eliminate much of the waste.  */
1825
1826               if (GET_MODE (replacements->new)
1827                   != GET_MODE (replacements->old))
1828                 {
1829                   start_sequence ();
1830                   convert_move (replacements->new,
1831                                 replacements->old, unsignedp);
1832                   seq = gen_sequence ();
1833                   end_sequence ();
1834                 }
1835               else
1836                 seq = gen_move_insn (replacements->new,
1837                                      replacements->old);
1838
1839               emit_insn_before (seq, insert_before);
1840             }
1841
1842           next = replacements->next;
1843           free (replacements);
1844           replacements = next;
1845         }
1846     }
1847
1848   /* Also fix up any invalid exprs in the REG_NOTES of this insn.
1849      But don't touch other insns referred to by reg-notes;
1850      we will get them elsewhere.  */
1851   while (note)
1852     {
1853       if (GET_CODE (note) != INSN_LIST)
1854         XEXP (note, 0)
1855           = walk_fixup_memory_subreg (XEXP (note, 0), insn, 1);
1856       note = XEXP (note, 1);
1857     }
1858 }
1859 \f
1860 /* VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
1861    See if the rtx expression at *LOC in INSN needs to be changed.
1862
1863    REPLACEMENTS is a pointer to a list head that starts out zero, but may
1864    contain a list of original rtx's and replacements. If we find that we need
1865    to modify this insn by replacing a memory reference with a pseudo or by
1866    making a new MEM to implement a SUBREG, we consult that list to see if
1867    we have already chosen a replacement. If none has already been allocated,
1868    we allocate it and update the list.  fixup_var_refs_insn will copy VAR
1869    or the SUBREG, as appropriate, to the pseudo.  */
1870
1871 static void
1872 fixup_var_refs_1 (var, promoted_mode, loc, insn, replacements)
1873      rtx var;
1874      enum machine_mode promoted_mode;
1875      rtx *loc;
1876      rtx insn;
1877      struct fixup_replacement **replacements;
1878 {
1879   int i;
1880   rtx x = *loc;
1881   RTX_CODE code = GET_CODE (x);
1882   const char *fmt;
1883   rtx tem, tem1;
1884   struct fixup_replacement *replacement;
1885
1886   switch (code)
1887     {
1888     case ADDRESSOF:
1889       if (XEXP (x, 0) == var)
1890         {
1891           /* Prevent sharing of rtl that might lose.  */
1892           rtx sub = copy_rtx (XEXP (var, 0));
1893
1894           if (! validate_change (insn, loc, sub, 0))
1895             {
1896               rtx y = gen_reg_rtx (GET_MODE (sub));
1897               rtx seq, new_insn;
1898
1899               /* We should be able to replace with a register or all is lost.
1900                  Note that we can't use validate_change to verify this, since
1901                  we're not caring for replacing all dups simultaneously.  */
1902               if (! validate_replace_rtx (*loc, y, insn))
1903                 abort ();
1904
1905               /* Careful!  First try to recognize a direct move of the
1906                  value, mimicking how things are done in gen_reload wrt
1907                  PLUS.  Consider what happens when insn is a conditional
1908                  move instruction and addsi3 clobbers flags.  */
1909
1910               start_sequence ();
1911               new_insn = emit_insn (gen_rtx_SET (VOIDmode, y, sub));
1912               seq = gen_sequence ();
1913               end_sequence ();
1914
1915               if (recog_memoized (new_insn) < 0)
1916                 {
1917                   /* That failed.  Fall back on force_operand and hope.  */
1918
1919                   start_sequence ();
1920                   sub = force_operand (sub, y);
1921                   if (sub != y)
1922                     emit_insn (gen_move_insn (y, sub));
1923                   seq = gen_sequence ();
1924                   end_sequence ();
1925                 }
1926
1927 #ifdef HAVE_cc0
1928               /* Don't separate setter from user.  */
1929               if (PREV_INSN (insn) && sets_cc0_p (PREV_INSN (insn)))
1930                 insn = PREV_INSN (insn);
1931 #endif
1932
1933               emit_insn_before (seq, insn);
1934             }
1935         }
1936       return;
1937
1938     case MEM:
1939       if (var == x)
1940         {
1941           /* If we already have a replacement, use it.  Otherwise,
1942              try to fix up this address in case it is invalid.  */
1943
1944           replacement = find_fixup_replacement (replacements, var);
1945           if (replacement->new)
1946             {
1947               *loc = replacement->new;
1948               return;
1949             }
1950
1951           *loc = replacement->new = x = fixup_stack_1 (x, insn);
1952
1953           /* Unless we are forcing memory to register or we changed the mode,
1954              we can leave things the way they are if the insn is valid.  */
1955
1956           INSN_CODE (insn) = -1;
1957           if (! flag_force_mem && GET_MODE (x) == promoted_mode
1958               && recog_memoized (insn) >= 0)
1959             return;
1960
1961           *loc = replacement->new = gen_reg_rtx (promoted_mode);
1962           return;
1963         }
1964
1965       /* If X contains VAR, we need to unshare it here so that we update
1966          each occurrence separately.  But all identical MEMs in one insn
1967          must be replaced with the same rtx because of the possibility of
1968          MATCH_DUPs.  */
1969
1970       if (reg_mentioned_p (var, x))
1971         {
1972           replacement = find_fixup_replacement (replacements, x);
1973           if (replacement->new == 0)
1974             replacement->new = copy_most_rtx (x, var);
1975
1976           *loc = x = replacement->new;
1977           code = GET_CODE (x);
1978         }
1979       break;
1980
1981     case REG:
1982     case CC0:
1983     case PC:
1984     case CONST_INT:
1985     case CONST:
1986     case SYMBOL_REF:
1987     case LABEL_REF:
1988     case CONST_DOUBLE:
1989       return;
1990
1991     case SIGN_EXTRACT:
1992     case ZERO_EXTRACT:
1993       /* Note that in some cases those types of expressions are altered
1994          by optimize_bit_field, and do not survive to get here.  */
1995       if (XEXP (x, 0) == var
1996           || (GET_CODE (XEXP (x, 0)) == SUBREG
1997               && SUBREG_REG (XEXP (x, 0)) == var))
1998         {
1999           /* Get TEM as a valid MEM in the mode presently in the insn.
2000
2001              We don't worry about the possibility of MATCH_DUP here; it
2002              is highly unlikely and would be tricky to handle.  */
2003
2004           tem = XEXP (x, 0);
2005           if (GET_CODE (tem) == SUBREG)
2006             {
2007               if (GET_MODE_BITSIZE (GET_MODE (tem))
2008                   > GET_MODE_BITSIZE (GET_MODE (var)))
2009                 {
2010                   replacement = find_fixup_replacement (replacements, var);
2011                   if (replacement->new == 0)
2012                     replacement->new = gen_reg_rtx (GET_MODE (var));
2013                   SUBREG_REG (tem) = replacement->new;
2014
2015                   /* The following code works only if we have a MEM, so we
2016                      need to handle the subreg here.  We directly substitute
2017                      it assuming that a subreg must be OK here.  We already
2018                      scheduled a replacement to copy the mem into the
2019                      subreg.  */
2020                   XEXP (x, 0) = tem;
2021                   return;
2022                 }
2023               else
2024                 tem = fixup_memory_subreg (tem, insn, 0);
2025             }
2026           else
2027             tem = fixup_stack_1 (tem, insn);
2028
2029           /* Unless we want to load from memory, get TEM into the proper mode
2030              for an extract from memory.  This can only be done if the
2031              extract is at a constant position and length.  */
2032
2033           if (! flag_force_mem && GET_CODE (XEXP (x, 1)) == CONST_INT
2034               && GET_CODE (XEXP (x, 2)) == CONST_INT
2035               && ! mode_dependent_address_p (XEXP (tem, 0))
2036               && ! MEM_VOLATILE_P (tem))
2037             {
2038               enum machine_mode wanted_mode = VOIDmode;
2039               enum machine_mode is_mode = GET_MODE (tem);
2040               HOST_WIDE_INT pos = INTVAL (XEXP (x, 2));
2041
2042               if (GET_CODE (x) == ZERO_EXTRACT)
2043                 {
2044                   enum machine_mode new_mode
2045                     = mode_for_extraction (EP_extzv, 1);
2046                   if (new_mode != MAX_MACHINE_MODE)
2047                     wanted_mode = new_mode;
2048                 }
2049               else if (GET_CODE (x) == SIGN_EXTRACT)
2050                 {
2051                   enum machine_mode new_mode
2052                     = mode_for_extraction (EP_extv, 1);
2053                   if (new_mode != MAX_MACHINE_MODE)
2054                     wanted_mode = new_mode;
2055                 }
2056
2057               /* If we have a narrower mode, we can do something.  */
2058               if (wanted_mode != VOIDmode
2059                   && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2060                 {
2061                   HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2062                   rtx old_pos = XEXP (x, 2);
2063                   rtx newmem;
2064
2065                   /* If the bytes and bits are counted differently, we
2066                      must adjust the offset.  */
2067                   if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2068                     offset = (GET_MODE_SIZE (is_mode)
2069                               - GET_MODE_SIZE (wanted_mode) - offset);
2070
2071                   pos %= GET_MODE_BITSIZE (wanted_mode);
2072
2073                   newmem = adjust_address_nv (tem, wanted_mode, offset);
2074
2075                   /* Make the change and see if the insn remains valid.  */
2076                   INSN_CODE (insn) = -1;
2077                   XEXP (x, 0) = newmem;
2078                   XEXP (x, 2) = GEN_INT (pos);
2079
2080                   if (recog_memoized (insn) >= 0)
2081                     return;
2082
2083                   /* Otherwise, restore old position.  XEXP (x, 0) will be
2084                      restored later.  */
2085                   XEXP (x, 2) = old_pos;
2086                 }
2087             }
2088
2089           /* If we get here, the bitfield extract insn can't accept a memory
2090              reference.  Copy the input into a register.  */
2091
2092           tem1 = gen_reg_rtx (GET_MODE (tem));
2093           emit_insn_before (gen_move_insn (tem1, tem), insn);
2094           XEXP (x, 0) = tem1;
2095           return;
2096         }
2097       break;
2098
2099     case SUBREG:
2100       if (SUBREG_REG (x) == var)
2101         {
2102           /* If this is a special SUBREG made because VAR was promoted
2103              from a wider mode, replace it with VAR and call ourself
2104              recursively, this time saying that the object previously
2105              had its current mode (by virtue of the SUBREG).  */
2106
2107           if (SUBREG_PROMOTED_VAR_P (x))
2108             {
2109               *loc = var;
2110               fixup_var_refs_1 (var, GET_MODE (var), loc, insn, replacements);
2111               return;
2112             }
2113
2114           /* If this SUBREG makes VAR wider, it has become a paradoxical
2115              SUBREG with VAR in memory, but these aren't allowed at this
2116              stage of the compilation.  So load VAR into a pseudo and take
2117              a SUBREG of that pseudo.  */
2118           if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (var)))
2119             {
2120               replacement = find_fixup_replacement (replacements, var);
2121               if (replacement->new == 0)
2122                 replacement->new = gen_reg_rtx (promoted_mode);
2123               SUBREG_REG (x) = replacement->new;
2124               return;
2125             }
2126
2127           /* See if we have already found a replacement for this SUBREG.
2128              If so, use it.  Otherwise, make a MEM and see if the insn
2129              is recognized.  If not, or if we should force MEM into a register,
2130              make a pseudo for this SUBREG.  */
2131           replacement = find_fixup_replacement (replacements, x);
2132           if (replacement->new)
2133             {
2134               *loc = replacement->new;
2135               return;
2136             }
2137
2138           replacement->new = *loc = fixup_memory_subreg (x, insn, 0);
2139
2140           INSN_CODE (insn) = -1;
2141           if (! flag_force_mem && recog_memoized (insn) >= 0)
2142             return;
2143
2144           *loc = replacement->new = gen_reg_rtx (GET_MODE (x));
2145           return;
2146         }
2147       break;
2148
2149     case SET:
2150       /* First do special simplification of bit-field references.  */
2151       if (GET_CODE (SET_DEST (x)) == SIGN_EXTRACT
2152           || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2153         optimize_bit_field (x, insn, 0);
2154       if (GET_CODE (SET_SRC (x)) == SIGN_EXTRACT
2155           || GET_CODE (SET_SRC (x)) == ZERO_EXTRACT)
2156         optimize_bit_field (x, insn, 0);
2157
2158       /* For a paradoxical SUBREG inside a ZERO_EXTRACT, load the object
2159          into a register and then store it back out.  */
2160       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2161           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG
2162           && SUBREG_REG (XEXP (SET_DEST (x), 0)) == var
2163           && (GET_MODE_SIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2164               > GET_MODE_SIZE (GET_MODE (var))))
2165         {
2166           replacement = find_fixup_replacement (replacements, var);
2167           if (replacement->new == 0)
2168             replacement->new = gen_reg_rtx (GET_MODE (var));
2169
2170           SUBREG_REG (XEXP (SET_DEST (x), 0)) = replacement->new;
2171           emit_insn_after (gen_move_insn (var, replacement->new), insn);
2172         }
2173
2174       /* If SET_DEST is now a paradoxical SUBREG, put the result of this
2175          insn into a pseudo and store the low part of the pseudo into VAR.  */
2176       if (GET_CODE (SET_DEST (x)) == SUBREG
2177           && SUBREG_REG (SET_DEST (x)) == var
2178           && (GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
2179               > GET_MODE_SIZE (GET_MODE (var))))
2180         {
2181           SET_DEST (x) = tem = gen_reg_rtx (GET_MODE (SET_DEST (x)));
2182           emit_insn_after (gen_move_insn (var, gen_lowpart (GET_MODE (var),
2183                                                             tem)),
2184                            insn);
2185           break;
2186         }
2187
2188       {
2189         rtx dest = SET_DEST (x);
2190         rtx src = SET_SRC (x);
2191         rtx outerdest = dest;
2192
2193         while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2194                || GET_CODE (dest) == SIGN_EXTRACT
2195                || GET_CODE (dest) == ZERO_EXTRACT)
2196           dest = XEXP (dest, 0);
2197
2198         if (GET_CODE (src) == SUBREG)
2199           src = SUBREG_REG (src);
2200
2201         /* If VAR does not appear at the top level of the SET
2202            just scan the lower levels of the tree.  */
2203
2204         if (src != var && dest != var)
2205           break;
2206
2207         /* We will need to rerecognize this insn.  */
2208         INSN_CODE (insn) = -1;
2209
2210         if (GET_CODE (outerdest) == ZERO_EXTRACT && dest == var
2211             && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
2212           {
2213             /* Since this case will return, ensure we fixup all the
2214                operands here.  */
2215             fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 1),
2216                               insn, replacements);
2217             fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 2),
2218                               insn, replacements);
2219             fixup_var_refs_1 (var, promoted_mode, &SET_SRC (x),
2220                               insn, replacements);
2221
2222             tem = XEXP (outerdest, 0);
2223
2224             /* Clean up (SUBREG:SI (MEM:mode ...) 0)
2225                that may appear inside a ZERO_EXTRACT.
2226                This was legitimate when the MEM was a REG.  */
2227             if (GET_CODE (tem) == SUBREG
2228                 && SUBREG_REG (tem) == var)
2229               tem = fixup_memory_subreg (tem, insn, 0);
2230             else
2231               tem = fixup_stack_1 (tem, insn);
2232
2233             if (GET_CODE (XEXP (outerdest, 1)) == CONST_INT
2234                 && GET_CODE (XEXP (outerdest, 2)) == CONST_INT
2235                 && ! mode_dependent_address_p (XEXP (tem, 0))
2236                 && ! MEM_VOLATILE_P (tem))
2237               {
2238                 enum machine_mode wanted_mode;
2239                 enum machine_mode is_mode = GET_MODE (tem);
2240                 HOST_WIDE_INT pos = INTVAL (XEXP (outerdest, 2));
2241
2242                 wanted_mode = mode_for_extraction (EP_insv, 0);
2243
2244                 /* If we have a narrower mode, we can do something.  */
2245                 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2246                   {
2247                     HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2248                     rtx old_pos = XEXP (outerdest, 2);
2249                     rtx newmem;
2250
2251                     if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2252                       offset = (GET_MODE_SIZE (is_mode)
2253                                 - GET_MODE_SIZE (wanted_mode) - offset);
2254
2255                     pos %= GET_MODE_BITSIZE (wanted_mode);
2256
2257                     newmem = adjust_address_nv (tem, wanted_mode, offset);
2258
2259                     /* Make the change and see if the insn remains valid.  */
2260                     INSN_CODE (insn) = -1;
2261                     XEXP (outerdest, 0) = newmem;
2262                     XEXP (outerdest, 2) = GEN_INT (pos);
2263
2264                     if (recog_memoized (insn) >= 0)
2265                       return;
2266
2267                     /* Otherwise, restore old position.  XEXP (x, 0) will be
2268                        restored later.  */
2269                     XEXP (outerdest, 2) = old_pos;
2270                   }
2271               }
2272
2273             /* If we get here, the bit-field store doesn't allow memory
2274                or isn't located at a constant position.  Load the value into
2275                a register, do the store, and put it back into memory.  */
2276
2277             tem1 = gen_reg_rtx (GET_MODE (tem));
2278             emit_insn_before (gen_move_insn (tem1, tem), insn);
2279             emit_insn_after (gen_move_insn (tem, tem1), insn);
2280             XEXP (outerdest, 0) = tem1;
2281             return;
2282           }
2283
2284         /* STRICT_LOW_PART is a no-op on memory references
2285            and it can cause combinations to be unrecognizable,
2286            so eliminate it.  */
2287
2288         if (dest == var && GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2289           SET_DEST (x) = XEXP (SET_DEST (x), 0);
2290
2291         /* A valid insn to copy VAR into or out of a register
2292            must be left alone, to avoid an infinite loop here.
2293            If the reference to VAR is by a subreg, fix that up,
2294            since SUBREG is not valid for a memref.
2295            Also fix up the address of the stack slot.
2296
2297            Note that we must not try to recognize the insn until
2298            after we know that we have valid addresses and no
2299            (subreg (mem ...) ...) constructs, since these interfere
2300            with determining the validity of the insn.  */
2301
2302         if ((SET_SRC (x) == var
2303              || (GET_CODE (SET_SRC (x)) == SUBREG
2304                  && SUBREG_REG (SET_SRC (x)) == var))
2305             && (GET_CODE (SET_DEST (x)) == REG
2306                 || (GET_CODE (SET_DEST (x)) == SUBREG
2307                     && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG))
2308             && GET_MODE (var) == promoted_mode
2309             && x == single_set (insn))
2310           {
2311             rtx pat, last;
2312
2313             replacement = find_fixup_replacement (replacements, SET_SRC (x));
2314             if (replacement->new)
2315               SET_SRC (x) = replacement->new;
2316             else if (GET_CODE (SET_SRC (x)) == SUBREG)
2317               SET_SRC (x) = replacement->new
2318                 = fixup_memory_subreg (SET_SRC (x), insn, 0);
2319             else
2320               SET_SRC (x) = replacement->new
2321                 = fixup_stack_1 (SET_SRC (x), insn);
2322
2323             if (recog_memoized (insn) >= 0)
2324               return;
2325
2326             /* INSN is not valid, but we know that we want to
2327                copy SET_SRC (x) to SET_DEST (x) in some way.  So
2328                we generate the move and see whether it requires more
2329                than one insn.  If it does, we emit those insns and
2330                delete INSN.  Otherwise, we an just replace the pattern
2331                of INSN; we have already verified above that INSN has
2332                no other function that to do X.  */
2333
2334             pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2335             if (GET_CODE (pat) == SEQUENCE)
2336               {
2337                 last = emit_insn_before (pat, insn);
2338
2339                 /* INSN might have REG_RETVAL or other important notes, so
2340                    we need to store the pattern of the last insn in the
2341                    sequence into INSN similarly to the normal case.  LAST
2342                    should not have REG_NOTES, but we allow them if INSN has
2343                    no REG_NOTES.  */
2344                 if (REG_NOTES (last) && REG_NOTES (insn))
2345                   abort ();
2346                 if (REG_NOTES (last))
2347                   REG_NOTES (insn) = REG_NOTES (last);
2348                 PATTERN (insn) = PATTERN (last);
2349
2350                 delete_insn (last);
2351               }
2352             else
2353               PATTERN (insn) = pat;
2354
2355             return;
2356           }
2357
2358         if ((SET_DEST (x) == var
2359              || (GET_CODE (SET_DEST (x)) == SUBREG
2360                  && SUBREG_REG (SET_DEST (x)) == var))
2361             && (GET_CODE (SET_SRC (x)) == REG
2362                 || (GET_CODE (SET_SRC (x)) == SUBREG
2363                     && GET_CODE (SUBREG_REG (SET_SRC (x))) == REG))
2364             && GET_MODE (var) == promoted_mode
2365             && x == single_set (insn))
2366           {
2367             rtx pat, last;
2368
2369             if (GET_CODE (SET_DEST (x)) == SUBREG)
2370               SET_DEST (x) = fixup_memory_subreg (SET_DEST (x), insn, 0);
2371             else
2372               SET_DEST (x) = fixup_stack_1 (SET_DEST (x), insn);
2373
2374             if (recog_memoized (insn) >= 0)
2375               return;
2376
2377             pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2378             if (GET_CODE (pat) == SEQUENCE)
2379               {
2380                 last = emit_insn_before (pat, insn);
2381
2382                 /* INSN might have REG_RETVAL or other important notes, so
2383                    we need to store the pattern of the last insn in the
2384                    sequence into INSN similarly to the normal case.  LAST
2385                    should not have REG_NOTES, but we allow them if INSN has
2386                    no REG_NOTES.  */
2387                 if (REG_NOTES (last) && REG_NOTES (insn))
2388                   abort ();
2389                 if (REG_NOTES (last))
2390                   REG_NOTES (insn) = REG_NOTES (last);
2391                 PATTERN (insn) = PATTERN (last);
2392
2393                 delete_insn (last);
2394               }
2395             else
2396               PATTERN (insn) = pat;
2397
2398             return;
2399           }
2400
2401         /* Otherwise, storing into VAR must be handled specially
2402            by storing into a temporary and copying that into VAR
2403            with a new insn after this one.  Note that this case
2404            will be used when storing into a promoted scalar since
2405            the insn will now have different modes on the input
2406            and output and hence will be invalid (except for the case
2407            of setting it to a constant, which does not need any
2408            change if it is valid).  We generate extra code in that case,
2409            but combine.c will eliminate it.  */
2410
2411         if (dest == var)
2412           {
2413             rtx temp;
2414             rtx fixeddest = SET_DEST (x);
2415
2416             /* STRICT_LOW_PART can be discarded, around a MEM.  */
2417             if (GET_CODE (fixeddest) == STRICT_LOW_PART)
2418               fixeddest = XEXP (fixeddest, 0);
2419             /* Convert (SUBREG (MEM)) to a MEM in a changed mode.  */
2420             if (GET_CODE (fixeddest) == SUBREG)
2421               {
2422                 fixeddest = fixup_memory_subreg (fixeddest, insn, 0);
2423                 promoted_mode = GET_MODE (fixeddest);
2424               }
2425             else
2426               fixeddest = fixup_stack_1 (fixeddest, insn);
2427
2428             temp = gen_reg_rtx (promoted_mode);
2429
2430             emit_insn_after (gen_move_insn (fixeddest,
2431                                             gen_lowpart (GET_MODE (fixeddest),
2432                                                          temp)),
2433                              insn);
2434
2435             SET_DEST (x) = temp;
2436           }
2437       }
2438
2439     default:
2440       break;
2441     }
2442
2443   /* Nothing special about this RTX; fix its operands.  */
2444
2445   fmt = GET_RTX_FORMAT (code);
2446   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2447     {
2448       if (fmt[i] == 'e')
2449         fixup_var_refs_1 (var, promoted_mode, &XEXP (x, i), insn, replacements);
2450       else if (fmt[i] == 'E')
2451         {
2452           int j;
2453           for (j = 0; j < XVECLEN (x, i); j++)
2454             fixup_var_refs_1 (var, promoted_mode, &XVECEXP (x, i, j),
2455                               insn, replacements);
2456         }
2457     }
2458 }
2459 \f
2460 /* Given X, an rtx of the form (SUBREG:m1 (MEM:m2 addr)),
2461    return an rtx (MEM:m1 newaddr) which is equivalent.
2462    If any insns must be emitted to compute NEWADDR, put them before INSN.
2463
2464    UNCRITICAL nonzero means accept paradoxical subregs.
2465    This is used for subregs found inside REG_NOTES.  */
2466
2467 static rtx
2468 fixup_memory_subreg (x, insn, uncritical)
2469      rtx x;
2470      rtx insn;
2471      int uncritical;
2472 {
2473   int offset = SUBREG_BYTE (x);
2474   rtx addr = XEXP (SUBREG_REG (x), 0);
2475   enum machine_mode mode = GET_MODE (x);
2476   rtx result;
2477
2478   /* Paradoxical SUBREGs are usually invalid during RTL generation.  */
2479   if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
2480       && ! uncritical)
2481     abort ();
2482
2483   if (!flag_force_addr
2484       && memory_address_p (mode, plus_constant (addr, offset)))
2485     /* Shortcut if no insns need be emitted.  */
2486     return adjust_address (SUBREG_REG (x), mode, offset);
2487
2488   start_sequence ();
2489   result = adjust_address (SUBREG_REG (x), mode, offset);
2490   emit_insn_before (gen_sequence (), insn);
2491   end_sequence ();
2492   return result;
2493 }
2494
2495 /* Do fixup_memory_subreg on all (SUBREG (MEM ...) ...) contained in X.
2496    Replace subexpressions of X in place.
2497    If X itself is a (SUBREG (MEM ...) ...), return the replacement expression.
2498    Otherwise return X, with its contents possibly altered.
2499
2500    If any insns must be emitted to compute NEWADDR, put them before INSN.
2501
2502    UNCRITICAL is as in fixup_memory_subreg.  */
2503
2504 static rtx
2505 walk_fixup_memory_subreg (x, insn, uncritical)
2506      rtx x;
2507      rtx insn;
2508      int uncritical;
2509 {
2510   enum rtx_code code;
2511   const char *fmt;
2512   int i;
2513
2514   if (x == 0)
2515     return 0;
2516
2517   code = GET_CODE (x);
2518
2519   if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
2520     return fixup_memory_subreg (x, insn, uncritical);
2521
2522   /* Nothing special about this RTX; fix its operands.  */
2523
2524   fmt = GET_RTX_FORMAT (code);
2525   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2526     {
2527       if (fmt[i] == 'e')
2528         XEXP (x, i) = walk_fixup_memory_subreg (XEXP (x, i), insn, uncritical);
2529       else if (fmt[i] == 'E')
2530         {
2531           int j;
2532           for (j = 0; j < XVECLEN (x, i); j++)
2533             XVECEXP (x, i, j)
2534               = walk_fixup_memory_subreg (XVECEXP (x, i, j), insn, uncritical);
2535         }
2536     }
2537   return x;
2538 }
2539 \f
2540 /* For each memory ref within X, if it refers to a stack slot
2541    with an out of range displacement, put the address in a temp register
2542    (emitting new insns before INSN to load these registers)
2543    and alter the memory ref to use that register.
2544    Replace each such MEM rtx with a copy, to avoid clobberage.  */
2545
2546 static rtx
2547 fixup_stack_1 (x, insn)
2548      rtx x;
2549      rtx insn;
2550 {
2551   int i;
2552   RTX_CODE code = GET_CODE (x);
2553   const char *fmt;
2554
2555   if (code == MEM)
2556     {
2557       rtx ad = XEXP (x, 0);
2558       /* If we have address of a stack slot but it's not valid
2559          (displacement is too large), compute the sum in a register.  */
2560       if (GET_CODE (ad) == PLUS
2561           && GET_CODE (XEXP (ad, 0)) == REG
2562           && ((REGNO (XEXP (ad, 0)) >= FIRST_VIRTUAL_REGISTER
2563                && REGNO (XEXP (ad, 0)) <= LAST_VIRTUAL_REGISTER)
2564               || REGNO (XEXP (ad, 0)) == FRAME_POINTER_REGNUM
2565 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2566               || REGNO (XEXP (ad, 0)) == HARD_FRAME_POINTER_REGNUM
2567 #endif
2568               || REGNO (XEXP (ad, 0)) == STACK_POINTER_REGNUM
2569               || REGNO (XEXP (ad, 0)) == ARG_POINTER_REGNUM
2570               || XEXP (ad, 0) == current_function_internal_arg_pointer)
2571           && GET_CODE (XEXP (ad, 1)) == CONST_INT)
2572         {
2573           rtx temp, seq;
2574           if (memory_address_p (GET_MODE (x), ad))
2575             return x;
2576
2577           start_sequence ();
2578           temp = copy_to_reg (ad);
2579           seq = gen_sequence ();
2580           end_sequence ();
2581           emit_insn_before (seq, insn);
2582           return replace_equiv_address (x, temp);
2583         }
2584       return x;
2585     }
2586
2587   fmt = GET_RTX_FORMAT (code);
2588   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2589     {
2590       if (fmt[i] == 'e')
2591         XEXP (x, i) = fixup_stack_1 (XEXP (x, i), insn);
2592       else if (fmt[i] == 'E')
2593         {
2594           int j;
2595           for (j = 0; j < XVECLEN (x, i); j++)
2596             XVECEXP (x, i, j) = fixup_stack_1 (XVECEXP (x, i, j), insn);
2597         }
2598     }
2599   return x;
2600 }
2601 \f
2602 /* Optimization: a bit-field instruction whose field
2603    happens to be a byte or halfword in memory
2604    can be changed to a move instruction.
2605
2606    We call here when INSN is an insn to examine or store into a bit-field.
2607    BODY is the SET-rtx to be altered.
2608
2609    EQUIV_MEM is the table `reg_equiv_mem' if that is available; else 0.
2610    (Currently this is called only from function.c, and EQUIV_MEM
2611    is always 0.)  */
2612
2613 static void
2614 optimize_bit_field (body, insn, equiv_mem)
2615      rtx body;
2616      rtx insn;
2617      rtx *equiv_mem;
2618 {
2619   rtx bitfield;
2620   int destflag;
2621   rtx seq = 0;
2622   enum machine_mode mode;
2623
2624   if (GET_CODE (SET_DEST (body)) == SIGN_EXTRACT
2625       || GET_CODE (SET_DEST (body)) == ZERO_EXTRACT)
2626     bitfield = SET_DEST (body), destflag = 1;
2627   else
2628     bitfield = SET_SRC (body), destflag = 0;
2629
2630   /* First check that the field being stored has constant size and position
2631      and is in fact a byte or halfword suitably aligned.  */
2632
2633   if (GET_CODE (XEXP (bitfield, 1)) == CONST_INT
2634       && GET_CODE (XEXP (bitfield, 2)) == CONST_INT
2635       && ((mode = mode_for_size (INTVAL (XEXP (bitfield, 1)), MODE_INT, 1))
2636           != BLKmode)
2637       && INTVAL (XEXP (bitfield, 2)) % INTVAL (XEXP (bitfield, 1)) == 0)
2638     {
2639       rtx memref = 0;
2640
2641       /* Now check that the containing word is memory, not a register,
2642          and that it is safe to change the machine mode.  */
2643
2644       if (GET_CODE (XEXP (bitfield, 0)) == MEM)
2645         memref = XEXP (bitfield, 0);
2646       else if (GET_CODE (XEXP (bitfield, 0)) == REG
2647                && equiv_mem != 0)
2648         memref = equiv_mem[REGNO (XEXP (bitfield, 0))];
2649       else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2650                && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == MEM)
2651         memref = SUBREG_REG (XEXP (bitfield, 0));
2652       else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2653                && equiv_mem != 0
2654                && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == REG)
2655         memref = equiv_mem[REGNO (SUBREG_REG (XEXP (bitfield, 0)))];
2656
2657       if (memref
2658           && ! mode_dependent_address_p (XEXP (memref, 0))
2659           && ! MEM_VOLATILE_P (memref))
2660         {
2661           /* Now adjust the address, first for any subreg'ing
2662              that we are now getting rid of,
2663              and then for which byte of the word is wanted.  */
2664
2665           HOST_WIDE_INT offset = INTVAL (XEXP (bitfield, 2));
2666           rtx insns;
2667
2668           /* Adjust OFFSET to count bits from low-address byte.  */
2669           if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
2670             offset = (GET_MODE_BITSIZE (GET_MODE (XEXP (bitfield, 0)))
2671                       - offset - INTVAL (XEXP (bitfield, 1)));
2672
2673           /* Adjust OFFSET to count bytes from low-address byte.  */
2674           offset /= BITS_PER_UNIT;
2675           if (GET_CODE (XEXP (bitfield, 0)) == SUBREG)
2676             {
2677               offset += (SUBREG_BYTE (XEXP (bitfield, 0))
2678                          / UNITS_PER_WORD) * UNITS_PER_WORD;
2679               if (BYTES_BIG_ENDIAN)
2680                 offset -= (MIN (UNITS_PER_WORD,
2681                                 GET_MODE_SIZE (GET_MODE (XEXP (bitfield, 0))))
2682                            - MIN (UNITS_PER_WORD,
2683                                   GET_MODE_SIZE (GET_MODE (memref))));
2684             }
2685
2686           start_sequence ();
2687           memref = adjust_address (memref, mode, offset);
2688           insns = get_insns ();
2689           end_sequence ();
2690           emit_insns_before (insns, insn);
2691
2692           /* Store this memory reference where
2693              we found the bit field reference.  */
2694
2695           if (destflag)
2696             {
2697               validate_change (insn, &SET_DEST (body), memref, 1);
2698               if (! CONSTANT_ADDRESS_P (SET_SRC (body)))
2699                 {
2700                   rtx src = SET_SRC (body);
2701                   while (GET_CODE (src) == SUBREG
2702                          && SUBREG_BYTE (src) == 0)
2703                     src = SUBREG_REG (src);
2704                   if (GET_MODE (src) != GET_MODE (memref))
2705                     src = gen_lowpart (GET_MODE (memref), SET_SRC (body));
2706                   validate_change (insn, &SET_SRC (body), src, 1);
2707                 }
2708               else if (GET_MODE (SET_SRC (body)) != VOIDmode
2709                        && GET_MODE (SET_SRC (body)) != GET_MODE (memref))
2710                 /* This shouldn't happen because anything that didn't have
2711                    one of these modes should have got converted explicitly
2712                    and then referenced through a subreg.
2713                    This is so because the original bit-field was
2714                    handled by agg_mode and so its tree structure had
2715                    the same mode that memref now has.  */
2716                 abort ();
2717             }
2718           else
2719             {
2720               rtx dest = SET_DEST (body);
2721
2722               while (GET_CODE (dest) == SUBREG
2723                      && SUBREG_BYTE (dest) == 0
2724                      && (GET_MODE_CLASS (GET_MODE (dest))
2725                          == GET_MODE_CLASS (GET_MODE (SUBREG_REG (dest))))
2726                      && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
2727                          <= UNITS_PER_WORD))
2728                 dest = SUBREG_REG (dest);
2729
2730               validate_change (insn, &SET_DEST (body), dest, 1);
2731
2732               if (GET_MODE (dest) == GET_MODE (memref))
2733                 validate_change (insn, &SET_SRC (body), memref, 1);
2734               else
2735                 {
2736                   /* Convert the mem ref to the destination mode.  */
2737                   rtx newreg = gen_reg_rtx (GET_MODE (dest));
2738
2739                   start_sequence ();
2740                   convert_move (newreg, memref,
2741                                 GET_CODE (SET_SRC (body)) == ZERO_EXTRACT);
2742                   seq = get_insns ();
2743                   end_sequence ();
2744
2745                   validate_change (insn, &SET_SRC (body), newreg, 1);
2746                 }
2747             }
2748
2749           /* See if we can convert this extraction or insertion into
2750              a simple move insn.  We might not be able to do so if this
2751              was, for example, part of a PARALLEL.
2752
2753              If we succeed, write out any needed conversions.  If we fail,
2754              it is hard to guess why we failed, so don't do anything
2755              special; just let the optimization be suppressed.  */
2756
2757           if (apply_change_group () && seq)
2758             emit_insns_before (seq, insn);
2759         }
2760     }
2761 }
2762 \f
2763 /* These routines are responsible for converting virtual register references
2764    to the actual hard register references once RTL generation is complete.
2765
2766    The following four variables are used for communication between the
2767    routines.  They contain the offsets of the virtual registers from their
2768    respective hard registers.  */
2769
2770 static int in_arg_offset;
2771 static int var_offset;
2772 static int dynamic_offset;
2773 static int out_arg_offset;
2774 static int cfa_offset;
2775
2776 /* In most machines, the stack pointer register is equivalent to the bottom
2777    of the stack.  */
2778
2779 #ifndef STACK_POINTER_OFFSET
2780 #define STACK_POINTER_OFFSET    0
2781 #endif
2782
2783 /* If not defined, pick an appropriate default for the offset of dynamically
2784    allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
2785    REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE.  */
2786
2787 #ifndef STACK_DYNAMIC_OFFSET
2788
2789 /* The bottom of the stack points to the actual arguments.  If
2790    REG_PARM_STACK_SPACE is defined, this includes the space for the register
2791    parameters.  However, if OUTGOING_REG_PARM_STACK space is not defined,
2792    stack space for register parameters is not pushed by the caller, but
2793    rather part of the fixed stack areas and hence not included in
2794    `current_function_outgoing_args_size'.  Nevertheless, we must allow
2795    for it when allocating stack dynamic objects.  */
2796
2797 #if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
2798 #define STACK_DYNAMIC_OFFSET(FNDECL)    \
2799 ((ACCUMULATE_OUTGOING_ARGS                                                    \
2800   ? (current_function_outgoing_args_size + REG_PARM_STACK_SPACE (FNDECL)) : 0)\
2801  + (STACK_POINTER_OFFSET))                                                    \
2802
2803 #else
2804 #define STACK_DYNAMIC_OFFSET(FNDECL)    \
2805 ((ACCUMULATE_OUTGOING_ARGS ? current_function_outgoing_args_size : 0)         \
2806  + (STACK_POINTER_OFFSET))
2807 #endif
2808 #endif
2809
2810 /* On most machines, the CFA coincides with the first incoming parm.  */
2811
2812 #ifndef ARG_POINTER_CFA_OFFSET
2813 #define ARG_POINTER_CFA_OFFSET(FNDECL) FIRST_PARM_OFFSET (FNDECL)
2814 #endif
2815
2816 /* Build up a (MEM (ADDRESSOF (REG))) rtx for a register REG that just had
2817    its address taken.  DECL is the decl for the object stored in the
2818    register, for later use if we do need to force REG into the stack.
2819    REG is overwritten by the MEM like in put_reg_into_stack.  */
2820
2821 rtx
2822 gen_mem_addressof (reg, decl)
2823      rtx reg;
2824      tree decl;
2825 {
2826   rtx r = gen_rtx_ADDRESSOF (Pmode, gen_reg_rtx (GET_MODE (reg)),
2827                              REGNO (reg), decl);
2828
2829   /* Calculate this before we start messing with decl's RTL.  */
2830   HOST_WIDE_INT set = decl ? get_alias_set (decl) : 0;
2831
2832   /* If the original REG was a user-variable, then so is the REG whose
2833      address is being taken.  Likewise for unchanging.  */
2834   REG_USERVAR_P (XEXP (r, 0)) = REG_USERVAR_P (reg);
2835   RTX_UNCHANGING_P (XEXP (r, 0)) = RTX_UNCHANGING_P (reg);
2836
2837   PUT_CODE (reg, MEM);
2838   MEM_ATTRS (reg) = 0;
2839   XEXP (reg, 0) = r;
2840
2841   if (decl)
2842     {
2843       tree type = TREE_TYPE (decl);
2844       enum machine_mode decl_mode
2845         = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
2846            : DECL_MODE (decl));
2847       rtx decl_rtl = decl ? DECL_RTL_IF_SET (decl) : 0;
2848
2849       PUT_MODE (reg, decl_mode);
2850
2851       /* Clear DECL_RTL momentarily so functions below will work
2852          properly, then set it again.  */
2853       if (decl_rtl == reg)
2854         SET_DECL_RTL (decl, 0);
2855
2856       set_mem_attributes (reg, decl, 1);
2857       set_mem_alias_set (reg, set);
2858
2859       if (decl_rtl == reg)
2860         SET_DECL_RTL (decl, reg);
2861
2862       if (TREE_USED (decl) || DECL_INITIAL (decl) != 0)
2863         fixup_var_refs (reg, GET_MODE (reg), TREE_UNSIGNED (type), 0);
2864     }
2865   else
2866     fixup_var_refs (reg, GET_MODE (reg), 0, 0);
2867
2868   return reg;
2869 }
2870
2871 /* If DECL has an RTL that is an ADDRESSOF rtx, put it into the stack.  */
2872
2873 void
2874 flush_addressof (decl)
2875      tree decl;
2876 {
2877   if ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL)
2878       && DECL_RTL (decl) != 0
2879       && GET_CODE (DECL_RTL (decl)) == MEM
2880       && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF
2881       && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == REG)
2882     put_addressof_into_stack (XEXP (DECL_RTL (decl), 0), 0);
2883 }
2884
2885 /* Force the register pointed to by R, an ADDRESSOF rtx, into the stack.  */
2886
2887 static void
2888 put_addressof_into_stack (r, ht)
2889      rtx r;
2890      struct hash_table *ht;
2891 {
2892   tree decl, type;
2893   int volatile_p, used_p;
2894
2895   rtx reg = XEXP (r, 0);
2896
2897   if (GET_CODE (reg) != REG)
2898     abort ();
2899
2900   decl = ADDRESSOF_DECL (r);
2901   if (decl)
2902     {
2903       type = TREE_TYPE (decl);
2904       volatile_p = (TREE_CODE (decl) != SAVE_EXPR
2905                     && TREE_THIS_VOLATILE (decl));
2906       used_p = (TREE_USED (decl)
2907                 || (TREE_CODE (decl) != SAVE_EXPR
2908                     && DECL_INITIAL (decl) != 0));
2909     }
2910   else
2911     {
2912       type = NULL_TREE;
2913       volatile_p = 0;
2914       used_p = 1;
2915     }
2916
2917   put_reg_into_stack (0, reg, type, GET_MODE (reg), GET_MODE (reg),
2918                       volatile_p, ADDRESSOF_REGNO (r), used_p, ht);
2919 }
2920
2921 /* List of replacements made below in purge_addressof_1 when creating
2922    bitfield insertions.  */
2923 static rtx purge_bitfield_addressof_replacements;
2924
2925 /* List of replacements made below in purge_addressof_1 for patterns
2926    (MEM (ADDRESSOF (REG ...))).  The key of the list entry is the
2927    corresponding (ADDRESSOF (REG ...)) and value is a substitution for
2928    the all pattern.  List PURGE_BITFIELD_ADDRESSOF_REPLACEMENTS is not
2929    enough in complex cases, e.g. when some field values can be
2930    extracted by usage MEM with narrower mode.  */
2931 static rtx purge_addressof_replacements;
2932
2933 /* Helper function for purge_addressof.  See if the rtx expression at *LOC
2934    in INSN needs to be changed.  If FORCE, always put any ADDRESSOFs into
2935    the stack.  If the function returns FALSE then the replacement could not
2936    be made.  */
2937
2938 static bool
2939 purge_addressof_1 (loc, insn, force, store, ht)
2940      rtx *loc;
2941      rtx insn;
2942      int force, store;
2943      struct hash_table *ht;
2944 {
2945   rtx x;
2946   RTX_CODE code;
2947   int i, j;
2948   const char *fmt;
2949   bool result = true;
2950
2951   /* Re-start here to avoid recursion in common cases.  */
2952  restart:
2953
2954   x = *loc;
2955   if (x == 0)
2956     return true;
2957
2958   code = GET_CODE (x);
2959
2960   /* If we don't return in any of the cases below, we will recurse inside
2961      the RTX, which will normally result in any ADDRESSOF being forced into
2962      memory.  */
2963   if (code == SET)
2964     {
2965       result = purge_addressof_1 (&SET_DEST (x), insn, force, 1, ht);
2966       result &= purge_addressof_1 (&SET_SRC (x), insn, force, 0, ht);
2967       return result;
2968     }
2969   else if (code == ADDRESSOF)
2970     {
2971       rtx sub, insns;
2972
2973       if (GET_CODE (XEXP (x, 0)) != MEM)
2974         {
2975           put_addressof_into_stack (x, ht);
2976           return true;
2977         }
2978           
2979       /* We must create a copy of the rtx because it was created by
2980          overwriting a REG rtx which is always shared.  */
2981       sub = copy_rtx (XEXP (XEXP (x, 0), 0));
2982       if (validate_change (insn, loc, sub, 0)
2983           || validate_replace_rtx (x, sub, insn))
2984         return true;
2985
2986       start_sequence ();
2987       sub = force_operand (sub, NULL_RTX);
2988       if (! validate_change (insn, loc, sub, 0)
2989           && ! validate_replace_rtx (x, sub, insn))
2990         abort ();
2991
2992       insns = gen_sequence ();
2993       end_sequence ();
2994       emit_insn_before (insns, insn);
2995       return true;
2996     }
2997
2998   else if (code == MEM && GET_CODE (XEXP (x, 0)) == ADDRESSOF && ! force)
2999     {
3000       rtx sub = XEXP (XEXP (x, 0), 0);
3001
3002       if (GET_CODE (sub) == MEM)
3003         sub = adjust_address_nv (sub, GET_MODE (x), 0);
3004       else if (GET_CODE (sub) == REG
3005                && (MEM_VOLATILE_P (x) || GET_MODE (x) == BLKmode))
3006         ;
3007       else if (GET_CODE (sub) == REG && GET_MODE (x) != GET_MODE (sub))
3008         {
3009           int size_x, size_sub;
3010
3011           if (!insn)
3012             {
3013               /* When processing REG_NOTES look at the list of
3014                  replacements done on the insn to find the register that X
3015                  was replaced by.  */
3016               rtx tem;
3017
3018               for (tem = purge_bitfield_addressof_replacements;
3019                    tem != NULL_RTX;
3020                    tem = XEXP (XEXP (tem, 1), 1))
3021                 if (rtx_equal_p (x, XEXP (tem, 0)))
3022                   {
3023                     *loc = XEXP (XEXP (tem, 1), 0);
3024                     return true;
3025                   }
3026
3027               /* See comment for purge_addressof_replacements.  */
3028               for (tem = purge_addressof_replacements;
3029                    tem != NULL_RTX;
3030                    tem = XEXP (XEXP (tem, 1), 1))
3031                 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3032                   {
3033                     rtx z = XEXP (XEXP (tem, 1), 0);
3034
3035                     if (GET_MODE (x) == GET_MODE (z)
3036                         || (GET_CODE (XEXP (XEXP (tem, 1), 0)) != REG
3037                             && GET_CODE (XEXP (XEXP (tem, 1), 0)) != SUBREG))
3038                       abort ();
3039
3040                     /* It can happen that the note may speak of things
3041                        in a wider (or just different) mode than the
3042                        code did.  This is especially true of
3043                        REG_RETVAL.  */
3044
3045                     if (GET_CODE (z) == SUBREG && SUBREG_BYTE (z) == 0)
3046                       z = SUBREG_REG (z);
3047
3048                     if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3049                         && (GET_MODE_SIZE (GET_MODE (x))
3050                             > GET_MODE_SIZE (GET_MODE (z))))
3051                       {
3052                         /* This can occur as a result in invalid
3053                            pointer casts, e.g. float f; ...
3054                            *(long long int *)&f.
3055                            ??? We could emit a warning here, but
3056                            without a line number that wouldn't be
3057                            very helpful.  */
3058                         z = gen_rtx_SUBREG (GET_MODE (x), z, 0);
3059                       }
3060                     else
3061                       z = gen_lowpart (GET_MODE (x), z);
3062
3063                     *loc = z;
3064                     return true;
3065                   }
3066
3067               /* Sometimes we may not be able to find the replacement.  For
3068                  example when the original insn was a MEM in a wider mode,
3069                  and the note is part of a sign extension of a narrowed
3070                  version of that MEM.  Gcc testcase compile/990829-1.c can
3071                  generate an example of this situation.  Rather than complain
3072                  we return false, which will prompt our caller to remove the
3073                  offending note.  */
3074               return false;
3075             }
3076
3077           size_x = GET_MODE_BITSIZE (GET_MODE (x));
3078           size_sub = GET_MODE_BITSIZE (GET_MODE (sub));
3079
3080           /* Don't even consider working with paradoxical subregs,
3081              or the moral equivalent seen here.  */
3082           if (size_x <= size_sub
3083               && int_mode_for_mode (GET_MODE (sub)) != BLKmode)
3084             {
3085               /* Do a bitfield insertion to mirror what would happen
3086                  in memory.  */
3087
3088               rtx val, seq;
3089
3090               if (store)
3091                 {
3092                   rtx p = PREV_INSN (insn);
3093
3094                   start_sequence ();
3095                   val = gen_reg_rtx (GET_MODE (x));
3096                   if (! validate_change (insn, loc, val, 0))
3097                     {
3098                       /* Discard the current sequence and put the
3099                          ADDRESSOF on stack.  */
3100                       end_sequence ();
3101                       goto give_up;
3102                     }
3103                   seq = gen_sequence ();
3104                   end_sequence ();
3105                   emit_insn_before (seq, insn);
3106                   compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3107                                          insn, ht);
3108
3109                   start_sequence ();
3110                   store_bit_field (sub, size_x, 0, GET_MODE (x),
3111                                    val, GET_MODE_SIZE (GET_MODE (sub)));
3112
3113                   /* Make sure to unshare any shared rtl that store_bit_field
3114                      might have created.  */
3115                   unshare_all_rtl_again (get_insns ());
3116
3117                   seq = gen_sequence ();
3118                   end_sequence ();
3119                   p = emit_insn_after (seq, insn);
3120                   if (NEXT_INSN (insn))
3121                     compute_insns_for_mem (NEXT_INSN (insn),
3122                                            p ? NEXT_INSN (p) : NULL_RTX,
3123                                            ht);
3124                 }
3125               else
3126                 {
3127                   rtx p = PREV_INSN (insn);
3128
3129                   start_sequence ();
3130                   val = extract_bit_field (sub, size_x, 0, 1, NULL_RTX,
3131                                            GET_MODE (x), GET_MODE (x),
3132                                            GET_MODE_SIZE (GET_MODE (sub)));
3133
3134                   if (! validate_change (insn, loc, val, 0))
3135                     {
3136                       /* Discard the current sequence and put the
3137                          ADDRESSOF on stack.  */
3138                       end_sequence ();
3139                       goto give_up;
3140                     }
3141
3142                   seq = gen_sequence ();
3143                   end_sequence ();
3144                   emit_insn_before (seq, insn);
3145                   compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3146                                          insn, ht);
3147                 }
3148
3149               /* Remember the replacement so that the same one can be done
3150                  on the REG_NOTES.  */
3151               purge_bitfield_addressof_replacements
3152                 = gen_rtx_EXPR_LIST (VOIDmode, x,
3153                                      gen_rtx_EXPR_LIST
3154                                      (VOIDmode, val,
3155                                       purge_bitfield_addressof_replacements));
3156
3157               /* We replaced with a reg -- all done.  */
3158               return true;
3159             }
3160         }
3161
3162       else if (validate_change (insn, loc, sub, 0))
3163         {
3164           /* Remember the replacement so that the same one can be done
3165              on the REG_NOTES.  */
3166           if (GET_CODE (sub) == REG || GET_CODE (sub) == SUBREG)
3167             {
3168               rtx tem;
3169
3170               for (tem = purge_addressof_replacements;
3171                    tem != NULL_RTX;
3172                    tem = XEXP (XEXP (tem, 1), 1))
3173                 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3174                   {
3175                     XEXP (XEXP (tem, 1), 0) = sub;
3176                     return true;
3177                   }
3178               purge_addressof_replacements
3179                 = gen_rtx (EXPR_LIST, VOIDmode, XEXP (x, 0),
3180                            gen_rtx_EXPR_LIST (VOIDmode, sub,
3181                                               purge_addressof_replacements));
3182               return true;
3183             }
3184           goto restart;
3185         }
3186     }
3187
3188  give_up:
3189   /* Scan all subexpressions.  */
3190   fmt = GET_RTX_FORMAT (code);
3191   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3192     {
3193       if (*fmt == 'e')
3194         result &= purge_addressof_1 (&XEXP (x, i), insn, force, 0, ht);
3195       else if (*fmt == 'E')
3196         for (j = 0; j < XVECLEN (x, i); j++)
3197           result &= purge_addressof_1 (&XVECEXP (x, i, j), insn, force, 0, ht);
3198     }
3199
3200   return result;
3201 }
3202
3203 /* Return a new hash table entry in HT.  */
3204
3205 static struct hash_entry *
3206 insns_for_mem_newfunc (he, ht, k)
3207      struct hash_entry *he;
3208      struct hash_table *ht;
3209      hash_table_key k ATTRIBUTE_UNUSED;
3210 {
3211   struct insns_for_mem_entry *ifmhe;
3212   if (he)
3213     return he;
3214
3215   ifmhe = ((struct insns_for_mem_entry *)
3216            hash_allocate (ht, sizeof (struct insns_for_mem_entry)));
3217   ifmhe->insns = NULL_RTX;
3218
3219   return &ifmhe->he;
3220 }
3221
3222 /* Return a hash value for K, a REG.  */
3223
3224 static unsigned long
3225 insns_for_mem_hash (k)
3226      hash_table_key k;
3227 {
3228   /* K is really a RTX.  Just use the address as the hash value.  */
3229   return (unsigned long) k;
3230 }
3231
3232 /* Return non-zero if K1 and K2 (two REGs) are the same.  */
3233
3234 static bool
3235 insns_for_mem_comp (k1, k2)
3236      hash_table_key k1;
3237      hash_table_key k2;
3238 {
3239   return k1 == k2;
3240 }
3241
3242 struct insns_for_mem_walk_info
3243 {
3244   /* The hash table that we are using to record which INSNs use which
3245      MEMs.  */
3246   struct hash_table *ht;
3247
3248   /* The INSN we are currently processing.  */
3249   rtx insn;
3250
3251   /* Zero if we are walking to find ADDRESSOFs, one if we are walking
3252      to find the insns that use the REGs in the ADDRESSOFs.  */
3253   int pass;
3254 };
3255
3256 /* Called from compute_insns_for_mem via for_each_rtx.  If R is a REG
3257    that might be used in an ADDRESSOF expression, record this INSN in
3258    the hash table given by DATA (which is really a pointer to an
3259    insns_for_mem_walk_info structure).  */
3260
3261 static int
3262 insns_for_mem_walk (r, data)
3263      rtx *r;
3264      void *data;
3265 {
3266   struct insns_for_mem_walk_info *ifmwi
3267     = (struct insns_for_mem_walk_info *) data;
3268
3269   if (ifmwi->pass == 0 && *r && GET_CODE (*r) == ADDRESSOF
3270       && GET_CODE (XEXP (*r, 0)) == REG)
3271     hash_lookup (ifmwi->ht, XEXP (*r, 0), /*create=*/1, /*copy=*/0);
3272   else if (ifmwi->pass == 1 && *r && GET_CODE (*r) == REG)
3273     {
3274       /* Lookup this MEM in the hashtable, creating it if necessary.  */
3275       struct insns_for_mem_entry *ifme
3276         = (struct insns_for_mem_entry *) hash_lookup (ifmwi->ht,
3277                                                       *r,
3278                                                       /*create=*/0,
3279                                                       /*copy=*/0);
3280
3281       /* If we have not already recorded this INSN, do so now.  Since
3282          we process the INSNs in order, we know that if we have
3283          recorded it it must be at the front of the list.  */
3284       if (ifme && (!ifme->insns || XEXP (ifme->insns, 0) != ifmwi->insn))
3285         ifme->insns = gen_rtx_EXPR_LIST (VOIDmode, ifmwi->insn,
3286                                          ifme->insns);
3287     }
3288
3289   return 0;
3290 }
3291
3292 /* Walk the INSNS, until we reach LAST_INSN, recording which INSNs use
3293    which REGs in HT.  */
3294
3295 static void
3296 compute_insns_for_mem (insns, last_insn, ht)
3297      rtx insns;
3298      rtx last_insn;
3299      struct hash_table *ht;
3300 {
3301   rtx insn;
3302   struct insns_for_mem_walk_info ifmwi;
3303   ifmwi.ht = ht;
3304
3305   for (ifmwi.pass = 0; ifmwi.pass < 2; ++ifmwi.pass)
3306     for (insn = insns; insn != last_insn; insn = NEXT_INSN (insn))
3307       if (INSN_P (insn))
3308         {
3309           ifmwi.insn = insn;
3310           for_each_rtx (&insn, insns_for_mem_walk, &ifmwi);
3311         }
3312 }
3313
3314 /* Helper function for purge_addressof called through for_each_rtx.
3315    Returns true iff the rtl is an ADDRESSOF.  */
3316
3317 static int
3318 is_addressof (rtl, data)
3319      rtx *rtl;
3320      void *data ATTRIBUTE_UNUSED;
3321 {
3322   return GET_CODE (*rtl) == ADDRESSOF;
3323 }
3324
3325 /* Eliminate all occurrences of ADDRESSOF from INSNS.  Elide any remaining
3326    (MEM (ADDRESSOF)) patterns, and force any needed registers into the
3327    stack.  */
3328
3329 void
3330 purge_addressof (insns)
3331      rtx insns;
3332 {
3333   rtx insn;
3334   struct hash_table ht;
3335
3336   /* When we actually purge ADDRESSOFs, we turn REGs into MEMs.  That
3337      requires a fixup pass over the instruction stream to correct
3338      INSNs that depended on the REG being a REG, and not a MEM.  But,
3339      these fixup passes are slow.  Furthermore, most MEMs are not
3340      mentioned in very many instructions.  So, we speed up the process
3341      by pre-calculating which REGs occur in which INSNs; that allows
3342      us to perform the fixup passes much more quickly.  */
3343   hash_table_init (&ht,
3344                    insns_for_mem_newfunc,
3345                    insns_for_mem_hash,
3346                    insns_for_mem_comp);
3347   compute_insns_for_mem (insns, NULL_RTX, &ht);
3348
3349   for (insn = insns; insn; insn = NEXT_INSN (insn))
3350     if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3351         || GET_CODE (insn) == CALL_INSN)
3352       {
3353         if (! purge_addressof_1 (&PATTERN (insn), insn,
3354                                  asm_noperands (PATTERN (insn)) > 0, 0, &ht))
3355           /* If we could not replace the ADDRESSOFs in the insn,
3356              something is wrong.  */
3357           abort ();
3358
3359         if (! purge_addressof_1 (&REG_NOTES (insn), NULL_RTX, 0, 0, &ht))
3360           {
3361             /* If we could not replace the ADDRESSOFs in the insn's notes,
3362                we can just remove the offending notes instead.  */
3363             rtx note;
3364
3365             for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3366               {
3367                 /* If we find a REG_RETVAL note then the insn is a libcall.
3368                    Such insns must have REG_EQUAL notes as well, in order
3369                    for later passes of the compiler to work.  So it is not
3370                    safe to delete the notes here, and instead we abort.  */
3371                 if (REG_NOTE_KIND (note) == REG_RETVAL)
3372                   abort ();
3373                 if (for_each_rtx (&note, is_addressof, NULL))
3374                   remove_note (insn, note);
3375               }
3376           }
3377       }
3378
3379   /* Clean up.  */
3380   hash_table_free (&ht);
3381   purge_bitfield_addressof_replacements = 0;
3382   purge_addressof_replacements = 0;
3383
3384   /* REGs are shared.  purge_addressof will destructively replace a REG
3385      with a MEM, which creates shared MEMs.
3386
3387      Unfortunately, the children of put_reg_into_stack assume that MEMs
3388      referring to the same stack slot are shared (fixup_var_refs and
3389      the associated hash table code).
3390
3391      So, we have to do another unsharing pass after we have flushed any
3392      REGs that had their address taken into the stack.
3393
3394      It may be worth tracking whether or not we converted any REGs into
3395      MEMs to avoid this overhead when it is not needed.  */
3396   unshare_all_rtl_again (get_insns ());
3397 }
3398 \f
3399 /* Convert a SET of a hard subreg to a set of the appropriate hard
3400    register.  A subroutine of purge_hard_subreg_sets.  */
3401
3402 static void
3403 purge_single_hard_subreg_set (pattern)
3404      rtx pattern;
3405 {
3406   rtx reg = SET_DEST (pattern);
3407   enum machine_mode mode = GET_MODE (SET_DEST (pattern));
3408   int offset = 0;
3409
3410   if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG
3411       && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
3412     {
3413       offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
3414                                     GET_MODE (SUBREG_REG (reg)),
3415                                     SUBREG_BYTE (reg),
3416                                     GET_MODE (reg));
3417       reg = SUBREG_REG (reg);
3418     }
3419
3420                   
3421   if (GET_CODE (reg) == REG && REGNO (reg) < FIRST_PSEUDO_REGISTER)
3422     {
3423       reg = gen_rtx_REG (mode, REGNO (reg) + offset);
3424       SET_DEST (pattern) = reg;
3425     }
3426 }
3427
3428 /* Eliminate all occurrences of SETs of hard subregs from INSNS.  The
3429    only such SETs that we expect to see are those left in because
3430    integrate can't handle sets of parts of a return value register.
3431
3432    We don't use alter_subreg because we only want to eliminate subregs
3433    of hard registers.  */
3434
3435 void
3436 purge_hard_subreg_sets (insn)
3437      rtx insn;
3438 {
3439   for (; insn; insn = NEXT_INSN (insn))
3440     {
3441       if (INSN_P (insn))
3442         {
3443           rtx pattern = PATTERN (insn);
3444           switch (GET_CODE (pattern))
3445             {
3446             case SET:
3447               if (GET_CODE (SET_DEST (pattern)) == SUBREG)
3448                 purge_single_hard_subreg_set (pattern);
3449               break;          
3450             case PARALLEL:
3451               {
3452                 int j;
3453                 for (j = XVECLEN (pattern, 0) - 1; j >= 0; j--)
3454                   {
3455                     rtx inner_pattern = XVECEXP (pattern, 0, j);
3456                     if (GET_CODE (inner_pattern) == SET
3457                         && GET_CODE (SET_DEST (inner_pattern)) == SUBREG)
3458                       purge_single_hard_subreg_set (inner_pattern);
3459                   }
3460               }
3461               break;
3462             default:
3463               break;
3464             }
3465         }
3466     }
3467 }
3468 \f
3469 /* Pass through the INSNS of function FNDECL and convert virtual register
3470    references to hard register references.  */
3471
3472 void
3473 instantiate_virtual_regs (fndecl, insns)
3474      tree fndecl;
3475      rtx insns;
3476 {
3477   rtx insn;
3478   unsigned int i;
3479
3480   /* Compute the offsets to use for this function.  */
3481   in_arg_offset = FIRST_PARM_OFFSET (fndecl);
3482   var_offset = STARTING_FRAME_OFFSET;
3483   dynamic_offset = STACK_DYNAMIC_OFFSET (fndecl);
3484   out_arg_offset = STACK_POINTER_OFFSET;
3485   cfa_offset = ARG_POINTER_CFA_OFFSET (fndecl);
3486
3487   /* Scan all variables and parameters of this function.  For each that is
3488      in memory, instantiate all virtual registers if the result is a valid
3489      address.  If not, we do it later.  That will handle most uses of virtual
3490      regs on many machines.  */
3491   instantiate_decls (fndecl, 1);
3492
3493   /* Initialize recognition, indicating that volatile is OK.  */
3494   init_recog ();
3495
3496   /* Scan through all the insns, instantiating every virtual register still
3497      present.  */
3498   for (insn = insns; insn; insn = NEXT_INSN (insn))
3499     if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3500         || GET_CODE (insn) == CALL_INSN)
3501       {
3502         instantiate_virtual_regs_1 (&PATTERN (insn), insn, 1);
3503         instantiate_virtual_regs_1 (&REG_NOTES (insn), NULL_RTX, 0);
3504         /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE.  */
3505         if (GET_CODE (insn) == CALL_INSN)
3506           instantiate_virtual_regs_1 (&CALL_INSN_FUNCTION_USAGE (insn),
3507                                       NULL_RTX, 0);
3508       }
3509
3510   /* Instantiate the stack slots for the parm registers, for later use in
3511      addressof elimination.  */
3512   for (i = 0; i < max_parm_reg; ++i)
3513     if (parm_reg_stack_loc[i])
3514       instantiate_virtual_regs_1 (&parm_reg_stack_loc[i], NULL_RTX, 0);
3515
3516   /* Now instantiate the remaining register equivalences for debugging info.
3517      These will not be valid addresses.  */
3518   instantiate_decls (fndecl, 0);
3519
3520   /* Indicate that, from now on, assign_stack_local should use
3521      frame_pointer_rtx.  */
3522   virtuals_instantiated = 1;
3523 }
3524
3525 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
3526    all virtual registers in their DECL_RTL's.
3527
3528    If VALID_ONLY, do this only if the resulting address is still valid.
3529    Otherwise, always do it.  */
3530
3531 static void
3532 instantiate_decls (fndecl, valid_only)
3533      tree fndecl;
3534      int valid_only;
3535 {
3536   tree decl;
3537
3538   /* Process all parameters of the function.  */
3539   for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
3540     {
3541       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
3542       HOST_WIDE_INT size_rtl;
3543
3544       instantiate_decl (DECL_RTL (decl), size, valid_only);
3545
3546       /* If the parameter was promoted, then the incoming RTL mode may be
3547          larger than the declared type size.  We must use the larger of
3548          the two sizes.  */
3549       size_rtl = GET_MODE_SIZE (GET_MODE (DECL_INCOMING_RTL (decl)));
3550       size = MAX (size_rtl, size);
3551       instantiate_decl (DECL_INCOMING_RTL (decl), size, valid_only);
3552     }
3553
3554   /* Now process all variables defined in the function or its subblocks.  */
3555   instantiate_decls_1 (DECL_INITIAL (fndecl), valid_only);
3556 }
3557
3558 /* Subroutine of instantiate_decls: Process all decls in the given
3559    BLOCK node and all its subblocks.  */
3560
3561 static void
3562 instantiate_decls_1 (let, valid_only)
3563      tree let;
3564      int valid_only;
3565 {
3566   tree t;
3567
3568   for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
3569     if (DECL_RTL_SET_P (t))
3570       instantiate_decl (DECL_RTL (t), 
3571                         int_size_in_bytes (TREE_TYPE (t)),
3572                         valid_only);
3573
3574   /* Process all subblocks.  */
3575   for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
3576     instantiate_decls_1 (t, valid_only);
3577 }
3578
3579 /* Subroutine of the preceding procedures: Given RTL representing a
3580    decl and the size of the object, do any instantiation required.
3581
3582    If VALID_ONLY is non-zero, it means that the RTL should only be
3583    changed if the new address is valid.  */
3584
3585 static void
3586 instantiate_decl (x, size, valid_only)
3587      rtx x;
3588      HOST_WIDE_INT size;
3589      int valid_only;
3590 {
3591   enum machine_mode mode;
3592   rtx addr;
3593
3594   /* If this is not a MEM, no need to do anything.  Similarly if the
3595      address is a constant or a register that is not a virtual register.  */
3596
3597   if (x == 0 || GET_CODE (x) != MEM)
3598     return;
3599
3600   addr = XEXP (x, 0);
3601   if (CONSTANT_P (addr)
3602       || (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == REG)
3603       || (GET_CODE (addr) == REG
3604           && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
3605               || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
3606     return;
3607
3608   /* If we should only do this if the address is valid, copy the address.
3609      We need to do this so we can undo any changes that might make the
3610      address invalid.  This copy is unfortunate, but probably can't be
3611      avoided.  */
3612
3613   if (valid_only)
3614     addr = copy_rtx (addr);
3615
3616   instantiate_virtual_regs_1 (&addr, NULL_RTX, 0);
3617
3618   if (valid_only && size >= 0)
3619     {
3620       unsigned HOST_WIDE_INT decl_size = size;
3621
3622       /* Now verify that the resulting address is valid for every integer or
3623          floating-point mode up to and including SIZE bytes long.  We do this
3624          since the object might be accessed in any mode and frame addresses
3625          are shared.  */
3626
3627       for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3628            mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3629            mode = GET_MODE_WIDER_MODE (mode))
3630         if (! memory_address_p (mode, addr))
3631           return;
3632
3633       for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
3634            mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3635            mode = GET_MODE_WIDER_MODE (mode))
3636         if (! memory_address_p (mode, addr))
3637           return;
3638     }
3639
3640   /* Put back the address now that we have updated it and we either know
3641      it is valid or we don't care whether it is valid.  */
3642
3643   XEXP (x, 0) = addr;
3644 }
3645 \f
3646 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
3647    is a virtual register, return the equivalent hard register and set the
3648    offset indirectly through the pointer.  Otherwise, return 0.  */
3649
3650 static rtx
3651 instantiate_new_reg (x, poffset)
3652      rtx x;
3653      HOST_WIDE_INT *poffset;
3654 {
3655   rtx new;
3656   HOST_WIDE_INT offset;
3657
3658   if (x == virtual_incoming_args_rtx)
3659     new = arg_pointer_rtx, offset = in_arg_offset;
3660   else if (x == virtual_stack_vars_rtx)
3661     new = frame_pointer_rtx, offset = var_offset;
3662   else if (x == virtual_stack_dynamic_rtx)
3663     new = stack_pointer_rtx, offset = dynamic_offset;
3664   else if (x == virtual_outgoing_args_rtx)
3665     new = stack_pointer_rtx, offset = out_arg_offset;
3666   else if (x == virtual_cfa_rtx)
3667     new = arg_pointer_rtx, offset = cfa_offset;
3668   else
3669     return 0;
3670
3671   *poffset = offset;
3672   return new;
3673 }
3674 \f
3675 /* Given a pointer to a piece of rtx and an optional pointer to the
3676    containing object, instantiate any virtual registers present in it.
3677
3678    If EXTRA_INSNS, we always do the replacement and generate
3679    any extra insns before OBJECT.  If it zero, we do nothing if replacement
3680    is not valid.
3681
3682    Return 1 if we either had nothing to do or if we were able to do the
3683    needed replacement.  Return 0 otherwise; we only return zero if
3684    EXTRA_INSNS is zero.
3685
3686    We first try some simple transformations to avoid the creation of extra
3687    pseudos.  */
3688
3689 static int
3690 instantiate_virtual_regs_1 (loc, object, extra_insns)
3691      rtx *loc;
3692      rtx object;
3693      int extra_insns;
3694 {
3695   rtx x;
3696   RTX_CODE code;
3697   rtx new = 0;
3698   HOST_WIDE_INT offset = 0;
3699   rtx temp;
3700   rtx seq;
3701   int i, j;
3702   const char *fmt;
3703
3704   /* Re-start here to avoid recursion in common cases.  */
3705  restart:
3706
3707   x = *loc;
3708   if (x == 0)
3709     return 1;
3710
3711   code = GET_CODE (x);
3712
3713   /* Check for some special cases.  */
3714   switch (code)
3715     {
3716     case CONST_INT:
3717     case CONST_DOUBLE:
3718     case CONST:
3719     case SYMBOL_REF:
3720     case CODE_LABEL:
3721     case PC:
3722     case CC0:
3723     case ASM_INPUT:
3724     case ADDR_VEC:
3725     case ADDR_DIFF_VEC:
3726     case RETURN:
3727       return 1;
3728
3729     case SET:
3730       /* We are allowed to set the virtual registers.  This means that
3731          the actual register should receive the source minus the
3732          appropriate offset.  This is used, for example, in the handling
3733          of non-local gotos.  */
3734       if ((new = instantiate_new_reg (SET_DEST (x), &offset)) != 0)
3735         {
3736           rtx src = SET_SRC (x);
3737
3738           /* We are setting the register, not using it, so the relevant
3739              offset is the negative of the offset to use were we using
3740              the register.  */
3741           offset = - offset;
3742           instantiate_virtual_regs_1 (&src, NULL_RTX, 0);
3743
3744           /* The only valid sources here are PLUS or REG.  Just do
3745              the simplest possible thing to handle them.  */
3746           if (GET_CODE (src) != REG && GET_CODE (src) != PLUS)
3747             abort ();
3748
3749           start_sequence ();
3750           if (GET_CODE (src) != REG)
3751             temp = force_operand (src, NULL_RTX);
3752           else
3753             temp = src;
3754           temp = force_operand (plus_constant (temp, offset), NULL_RTX);
3755           seq = get_insns ();
3756           end_sequence ();
3757
3758           emit_insns_before (seq, object);
3759           SET_DEST (x) = new;
3760
3761           if (! validate_change (object, &SET_SRC (x), temp, 0)
3762               || ! extra_insns)
3763             abort ();
3764
3765           return 1;
3766         }
3767
3768       instantiate_virtual_regs_1 (&SET_DEST (x), object, extra_insns);
3769       loc = &SET_SRC (x);
3770       goto restart;
3771
3772     case PLUS:
3773       /* Handle special case of virtual register plus constant.  */
3774       if (CONSTANT_P (XEXP (x, 1)))
3775         {
3776           rtx old, new_offset;
3777
3778           /* Check for (plus (plus VIRT foo) (const_int)) first.  */
3779           if (GET_CODE (XEXP (x, 0)) == PLUS)
3780             {
3781               if ((new = instantiate_new_reg (XEXP (XEXP (x, 0), 0), &offset)))
3782                 {
3783                   instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 1), object,
3784                                               extra_insns);
3785                   new = gen_rtx_PLUS (Pmode, new, XEXP (XEXP (x, 0), 1));
3786                 }
3787               else
3788                 {
3789                   loc = &XEXP (x, 0);
3790                   goto restart;
3791                 }
3792             }
3793
3794 #ifdef POINTERS_EXTEND_UNSIGNED
3795           /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
3796              we can commute the PLUS and SUBREG because pointers into the
3797              frame are well-behaved.  */
3798           else if (GET_CODE (XEXP (x, 0)) == SUBREG && GET_MODE (x) == ptr_mode
3799                    && GET_CODE (XEXP (x, 1)) == CONST_INT
3800                    && 0 != (new
3801                             = instantiate_new_reg (SUBREG_REG (XEXP (x, 0)),
3802                                                    &offset))
3803                    && validate_change (object, loc,
3804                                        plus_constant (gen_lowpart (ptr_mode,
3805                                                                    new),
3806                                                       offset
3807                                                       + INTVAL (XEXP (x, 1))),
3808                                        0))
3809                 return 1;
3810 #endif
3811           else if ((new = instantiate_new_reg (XEXP (x, 0), &offset)) == 0)
3812             {
3813               /* We know the second operand is a constant.  Unless the
3814                  first operand is a REG (which has been already checked),
3815                  it needs to be checked.  */
3816               if (GET_CODE (XEXP (x, 0)) != REG)
3817                 {
3818                   loc = &XEXP (x, 0);
3819                   goto restart;
3820                 }
3821               return 1;
3822             }
3823
3824           new_offset = plus_constant (XEXP (x, 1), offset);
3825
3826           /* If the new constant is zero, try to replace the sum with just
3827              the register.  */
3828           if (new_offset == const0_rtx
3829               && validate_change (object, loc, new, 0))
3830             return 1;
3831
3832           /* Next try to replace the register and new offset.
3833              There are two changes to validate here and we can't assume that
3834              in the case of old offset equals new just changing the register
3835              will yield a valid insn.  In the interests of a little efficiency,
3836              however, we only call validate change once (we don't queue up the
3837              changes and then call apply_change_group).  */
3838
3839           old = XEXP (x, 0);
3840           if (offset == 0
3841               ? ! validate_change (object, &XEXP (x, 0), new, 0)
3842               : (XEXP (x, 0) = new,
3843                  ! validate_change (object, &XEXP (x, 1), new_offset, 0)))
3844             {
3845               if (! extra_insns)
3846                 {
3847                   XEXP (x, 0) = old;
3848                   return 0;
3849                 }
3850
3851               /* Otherwise copy the new constant into a register and replace
3852                  constant with that register.  */
3853               temp = gen_reg_rtx (Pmode);
3854               XEXP (x, 0) = new;
3855               if (validate_change (object, &XEXP (x, 1), temp, 0))
3856                 emit_insn_before (gen_move_insn (temp, new_offset), object);
3857               else
3858                 {
3859                   /* If that didn't work, replace this expression with a
3860                      register containing the sum.  */
3861
3862                   XEXP (x, 0) = old;
3863                   new = gen_rtx_PLUS (Pmode, new, new_offset);
3864
3865                   start_sequence ();
3866                   temp = force_operand (new, NULL_RTX);
3867                   seq = get_insns ();
3868                   end_sequence ();
3869
3870                   emit_insns_before (seq, object);
3871                   if (! validate_change (object, loc, temp, 0)
3872                       && ! validate_replace_rtx (x, temp, object))
3873                     abort ();
3874                 }
3875             }
3876
3877           return 1;
3878         }
3879
3880       /* Fall through to generic two-operand expression case.  */
3881     case EXPR_LIST:
3882     case CALL:
3883     case COMPARE:
3884     case MINUS:
3885     case MULT:
3886     case DIV:      case UDIV:
3887     case MOD:      case UMOD:
3888     case AND:      case IOR:      case XOR:
3889     case ROTATERT: case ROTATE:
3890     case ASHIFTRT: case LSHIFTRT: case ASHIFT:
3891     case NE:       case EQ:
3892     case GE:       case GT:       case GEU:    case GTU:
3893     case LE:       case LT:       case LEU:    case LTU:
3894       if (XEXP (x, 1) && ! CONSTANT_P (XEXP (x, 1)))
3895         instantiate_virtual_regs_1 (&XEXP (x, 1), object, extra_insns);
3896       loc = &XEXP (x, 0);
3897       goto restart;
3898
3899     case MEM:
3900       /* Most cases of MEM that convert to valid addresses have already been
3901          handled by our scan of decls.  The only special handling we
3902          need here is to make a copy of the rtx to ensure it isn't being
3903          shared if we have to change it to a pseudo.
3904
3905          If the rtx is a simple reference to an address via a virtual register,
3906          it can potentially be shared.  In such cases, first try to make it
3907          a valid address, which can also be shared.  Otherwise, copy it and
3908          proceed normally.
3909
3910          First check for common cases that need no processing.  These are
3911          usually due to instantiation already being done on a previous instance
3912          of a shared rtx.  */
3913
3914       temp = XEXP (x, 0);
3915       if (CONSTANT_ADDRESS_P (temp)
3916 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3917           || temp == arg_pointer_rtx
3918 #endif
3919 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3920           || temp == hard_frame_pointer_rtx
3921 #endif
3922           || temp == frame_pointer_rtx)
3923         return 1;
3924
3925       if (GET_CODE (temp) == PLUS
3926           && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3927           && (XEXP (temp, 0) == frame_pointer_rtx
3928 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3929               || XEXP (temp, 0) == hard_frame_pointer_rtx
3930 #endif
3931 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3932               || XEXP (temp, 0) == arg_pointer_rtx
3933 #endif
3934               ))
3935         return 1;
3936
3937       if (temp == virtual_stack_vars_rtx
3938           || temp == virtual_incoming_args_rtx
3939           || (GET_CODE (temp) == PLUS
3940               && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3941               && (XEXP (temp, 0) == virtual_stack_vars_rtx
3942                   || XEXP (temp, 0) == virtual_incoming_args_rtx)))
3943         {
3944           /* This MEM may be shared.  If the substitution can be done without
3945              the need to generate new pseudos, we want to do it in place
3946              so all copies of the shared rtx benefit.  The call below will
3947              only make substitutions if the resulting address is still
3948              valid.
3949
3950              Note that we cannot pass X as the object in the recursive call
3951              since the insn being processed may not allow all valid
3952              addresses.  However, if we were not passed on object, we can
3953              only modify X without copying it if X will have a valid
3954              address.
3955
3956              ??? Also note that this can still lose if OBJECT is an insn that
3957              has less restrictions on an address that some other insn.
3958              In that case, we will modify the shared address.  This case
3959              doesn't seem very likely, though.  One case where this could
3960              happen is in the case of a USE or CLOBBER reference, but we
3961              take care of that below.  */
3962
3963           if (instantiate_virtual_regs_1 (&XEXP (x, 0),
3964                                           object ? object : x, 0))
3965             return 1;
3966
3967           /* Otherwise make a copy and process that copy.  We copy the entire
3968              RTL expression since it might be a PLUS which could also be
3969              shared.  */
3970           *loc = x = copy_rtx (x);
3971         }
3972
3973       /* Fall through to generic unary operation case.  */
3974     case PREFETCH:
3975     case SUBREG:
3976     case STRICT_LOW_PART:
3977     case NEG:          case NOT:
3978     case PRE_DEC:      case PRE_INC:      case POST_DEC:    case POST_INC:
3979     case SIGN_EXTEND:  case ZERO_EXTEND:
3980     case TRUNCATE:     case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3981     case FLOAT:        case FIX:
3982     case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3983     case ABS:
3984     case SQRT:
3985     case FFS:
3986       /* These case either have just one operand or we know that we need not
3987          check the rest of the operands.  */
3988       loc = &XEXP (x, 0);
3989       goto restart;
3990
3991     case USE:
3992     case CLOBBER:
3993       /* If the operand is a MEM, see if the change is a valid MEM.  If not,
3994          go ahead and make the invalid one, but do it to a copy.  For a REG,
3995          just make the recursive call, since there's no chance of a problem.  */
3996
3997       if ((GET_CODE (XEXP (x, 0)) == MEM
3998            && instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), XEXP (x, 0),
3999                                           0))
4000           || (GET_CODE (XEXP (x, 0)) == REG
4001               && instantiate_virtual_regs_1 (&XEXP (x, 0), object, 0)))
4002         return 1;
4003
4004       XEXP (x, 0) = copy_rtx (XEXP (x, 0));
4005       loc = &XEXP (x, 0);
4006       goto restart;
4007
4008     case REG:
4009       /* Try to replace with a PLUS.  If that doesn't work, compute the sum
4010          in front of this insn and substitute the temporary.  */
4011       if ((new = instantiate_new_reg (x, &offset)) != 0)
4012         {
4013           temp = plus_constant (new, offset);
4014           if (!validate_change (object, loc, temp, 0))
4015             {
4016               if (! extra_insns)
4017                 return 0;
4018
4019               start_sequence ();
4020               temp = force_operand (temp, NULL_RTX);
4021               seq = get_insns ();
4022               end_sequence ();
4023
4024               emit_insns_before (seq, object);
4025               if (! validate_change (object, loc, temp, 0)
4026                   && ! validate_replace_rtx (x, temp, object))
4027                 abort ();
4028             }
4029         }
4030
4031       return 1;
4032
4033     case ADDRESSOF:
4034       if (GET_CODE (XEXP (x, 0)) == REG)
4035         return 1;
4036
4037       else if (GET_CODE (XEXP (x, 0)) == MEM)
4038         {
4039           /* If we have a (addressof (mem ..)), do any instantiation inside
4040              since we know we'll be making the inside valid when we finally
4041              remove the ADDRESSOF.  */
4042           instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), NULL_RTX, 0);
4043           return 1;
4044         }
4045       break;
4046
4047     default:
4048       break;
4049     }
4050
4051   /* Scan all subexpressions.  */
4052   fmt = GET_RTX_FORMAT (code);
4053   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
4054     if (*fmt == 'e')
4055       {
4056         if (!instantiate_virtual_regs_1 (&XEXP (x, i), object, extra_insns))
4057           return 0;
4058       }
4059     else if (*fmt == 'E')
4060       for (j = 0; j < XVECLEN (x, i); j++)
4061         if (! instantiate_virtual_regs_1 (&XVECEXP (x, i, j), object,
4062                                           extra_insns))
4063           return 0;
4064
4065   return 1;
4066 }
4067 \f
4068 /* Optimization: assuming this function does not receive nonlocal gotos,
4069    delete the handlers for such, as well as the insns to establish
4070    and disestablish them.  */
4071
4072 static void
4073 delete_handlers ()
4074 {
4075   rtx insn;
4076   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4077     {
4078       /* Delete the handler by turning off the flag that would
4079          prevent jump_optimize from deleting it.
4080          Also permit deletion of the nonlocal labels themselves
4081          if nothing local refers to them.  */
4082       if (GET_CODE (insn) == CODE_LABEL)
4083         {
4084           tree t, last_t;
4085
4086           LABEL_PRESERVE_P (insn) = 0;
4087
4088           /* Remove it from the nonlocal_label list, to avoid confusing
4089              flow.  */
4090           for (t = nonlocal_labels, last_t = 0; t;
4091                last_t = t, t = TREE_CHAIN (t))
4092             if (DECL_RTL (TREE_VALUE (t)) == insn)
4093               break;
4094           if (t)
4095             {
4096               if (! last_t)
4097                 nonlocal_labels = TREE_CHAIN (nonlocal_labels);
4098               else
4099                 TREE_CHAIN (last_t) = TREE_CHAIN (t);
4100             }
4101         }
4102       if (GET_CODE (insn) == INSN)
4103         {
4104           int can_delete = 0;
4105           rtx t;
4106           for (t = nonlocal_goto_handler_slots; t != 0; t = XEXP (t, 1))
4107             if (reg_mentioned_p (t, PATTERN (insn)))
4108               {
4109                 can_delete = 1;
4110                 break;
4111               }
4112           if (can_delete
4113               || (nonlocal_goto_stack_level != 0
4114                   && reg_mentioned_p (nonlocal_goto_stack_level,
4115                                       PATTERN (insn))))
4116             delete_related_insns (insn);
4117         }
4118     }
4119 }
4120 \f
4121 int
4122 max_parm_reg_num ()
4123 {
4124   return max_parm_reg;
4125 }
4126
4127 /* Return the first insn following those generated by `assign_parms'.  */
4128
4129 rtx
4130 get_first_nonparm_insn ()
4131 {
4132   if (last_parm_insn)
4133     return NEXT_INSN (last_parm_insn);
4134   return get_insns ();
4135 }
4136
4137 /* Return the first NOTE_INSN_BLOCK_BEG note in the function.
4138    Crash if there is none.  */
4139
4140 rtx
4141 get_first_block_beg ()
4142 {
4143   rtx searcher;
4144   rtx insn = get_first_nonparm_insn ();
4145
4146   for (searcher = insn; searcher; searcher = NEXT_INSN (searcher))
4147     if (GET_CODE (searcher) == NOTE
4148         && NOTE_LINE_NUMBER (searcher) == NOTE_INSN_BLOCK_BEG)
4149       return searcher;
4150
4151   abort ();     /* Invalid call to this function.  (See comments above.)  */
4152   return NULL_RTX;
4153 }
4154
4155 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
4156    This means a type for which function calls must pass an address to the
4157    function or get an address back from the function.
4158    EXP may be a type node or an expression (whose type is tested).  */
4159
4160 int
4161 aggregate_value_p (exp)
4162      tree exp;
4163 {
4164   int i, regno, nregs;
4165   rtx reg;
4166
4167   tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
4168
4169   if (TREE_CODE (type) == VOID_TYPE)
4170     return 0;
4171   if (RETURN_IN_MEMORY (type))
4172     return 1;
4173   /* Types that are TREE_ADDRESSABLE must be constructed in memory,
4174      and thus can't be returned in registers.  */
4175   if (TREE_ADDRESSABLE (type))
4176     return 1;
4177   if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
4178     return 1;
4179   /* Make sure we have suitable call-clobbered regs to return
4180      the value in; if not, we must return it in memory.  */
4181   reg = hard_function_value (type, 0, 0);
4182
4183   /* If we have something other than a REG (e.g. a PARALLEL), then assume
4184      it is OK.  */
4185   if (GET_CODE (reg) != REG)
4186     return 0;
4187
4188   regno = REGNO (reg);
4189   nregs = HARD_REGNO_NREGS (regno, TYPE_MODE (type));
4190   for (i = 0; i < nregs; i++)
4191     if (! call_used_regs[regno + i])
4192       return 1;
4193   return 0;
4194 }
4195 \f
4196 /* Assign RTL expressions to the function's parameters.
4197    This may involve copying them into registers and using
4198    those registers as the RTL for them.  */
4199
4200 void
4201 assign_parms (fndecl)
4202      tree fndecl;
4203 {
4204   tree parm;
4205   rtx entry_parm = 0;
4206   rtx stack_parm = 0;
4207   CUMULATIVE_ARGS args_so_far;
4208   enum machine_mode promoted_mode, passed_mode;
4209   enum machine_mode nominal_mode, promoted_nominal_mode;
4210   int unsignedp;
4211   /* Total space needed so far for args on the stack,
4212      given as a constant and a tree-expression.  */
4213   struct args_size stack_args_size;
4214   tree fntype = TREE_TYPE (fndecl);
4215   tree fnargs = DECL_ARGUMENTS (fndecl);
4216   /* This is used for the arg pointer when referring to stack args.  */
4217   rtx internal_arg_pointer;
4218   /* This is a dummy PARM_DECL that we used for the function result if
4219      the function returns a structure.  */
4220   tree function_result_decl = 0;
4221 #ifdef SETUP_INCOMING_VARARGS
4222   int varargs_setup = 0;
4223 #endif
4224   rtx conversion_insns = 0;
4225   struct args_size alignment_pad;
4226
4227   /* Nonzero if the last arg is named `__builtin_va_alist',
4228      which is used on some machines for old-fashioned non-ANSI varargs.h;
4229      this should be stuck onto the stack as if it had arrived there.  */
4230   int hide_last_arg
4231     = (current_function_varargs
4232        && fnargs
4233        && (parm = tree_last (fnargs)) != 0
4234        && DECL_NAME (parm)
4235        && (! strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
4236                      "__builtin_va_alist")));
4237
4238   /* Nonzero if function takes extra anonymous args.
4239      This means the last named arg must be on the stack
4240      right before the anonymous ones.  */
4241   int stdarg
4242     = (TYPE_ARG_TYPES (fntype) != 0
4243        && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4244            != void_type_node));
4245
4246   current_function_stdarg = stdarg;
4247
4248   /* If the reg that the virtual arg pointer will be translated into is
4249      not a fixed reg or is the stack pointer, make a copy of the virtual
4250      arg pointer, and address parms via the copy.  The frame pointer is
4251      considered fixed even though it is not marked as such.
4252
4253      The second time through, simply use ap to avoid generating rtx.  */
4254
4255   if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
4256        || ! (fixed_regs[ARG_POINTER_REGNUM]
4257              || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM)))
4258     internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
4259   else
4260     internal_arg_pointer = virtual_incoming_args_rtx;
4261   current_function_internal_arg_pointer = internal_arg_pointer;
4262
4263   stack_args_size.constant = 0;
4264   stack_args_size.var = 0;
4265
4266   /* If struct value address is treated as the first argument, make it so.  */
4267   if (aggregate_value_p (DECL_RESULT (fndecl))
4268       && ! current_function_returns_pcc_struct
4269       && struct_value_incoming_rtx == 0)
4270     {
4271       tree type = build_pointer_type (TREE_TYPE (fntype));
4272
4273       function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
4274
4275       DECL_ARG_TYPE (function_result_decl) = type;
4276       TREE_CHAIN (function_result_decl) = fnargs;
4277       fnargs = function_result_decl;
4278     }
4279
4280   max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
4281   parm_reg_stack_loc = (rtx *) xcalloc (max_parm_reg, sizeof (rtx));
4282
4283 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
4284   INIT_CUMULATIVE_INCOMING_ARGS (args_so_far, fntype, NULL_RTX);
4285 #else
4286   INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, 0);
4287 #endif
4288
4289   /* We haven't yet found an argument that we must push and pretend the
4290      caller did.  */
4291   current_function_pretend_args_size = 0;
4292
4293   for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
4294     {
4295       struct args_size stack_offset;
4296       struct args_size arg_size;
4297       int passed_pointer = 0;
4298       int did_conversion = 0;
4299       tree passed_type = DECL_ARG_TYPE (parm);
4300       tree nominal_type = TREE_TYPE (parm);
4301       int pretend_named;
4302       int last_named = 0, named_arg;
4303
4304       /* Set LAST_NAMED if this is last named arg before last
4305          anonymous args.  */
4306       if (stdarg || current_function_varargs)
4307         {
4308           tree tem;
4309
4310           for (tem = TREE_CHAIN (parm); tem; tem = TREE_CHAIN (tem))
4311             if (DECL_NAME (tem))
4312               break;
4313
4314           if (tem == 0)
4315             last_named = 1;
4316         }
4317       /* Set NAMED_ARG if this arg should be treated as a named arg.  For
4318          most machines, if this is a varargs/stdarg function, then we treat
4319          the last named arg as if it were anonymous too.  */
4320       named_arg = STRICT_ARGUMENT_NAMING ? 1 : ! last_named;
4321
4322       if (TREE_TYPE (parm) == error_mark_node
4323           /* This can happen after weird syntax errors
4324              or if an enum type is defined among the parms.  */
4325           || TREE_CODE (parm) != PARM_DECL
4326           || passed_type == NULL)
4327         {
4328           SET_DECL_RTL (parm, gen_rtx_MEM (BLKmode, const0_rtx));
4329           DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4330           TREE_USED (parm) = 1;
4331           continue;
4332         }
4333
4334       /* For varargs.h function, save info about regs and stack space
4335          used by the individual args, not including the va_alist arg.  */
4336       if (hide_last_arg && last_named)
4337         current_function_args_info = args_so_far;
4338
4339       /* Find mode of arg as it is passed, and mode of arg
4340          as it should be during execution of this function.  */
4341       passed_mode = TYPE_MODE (passed_type);
4342       nominal_mode = TYPE_MODE (nominal_type);
4343
4344       /* If the parm's mode is VOID, its value doesn't matter,
4345          and avoid the usual things like emit_move_insn that could crash.  */
4346       if (nominal_mode == VOIDmode)
4347         {
4348           SET_DECL_RTL (parm, const0_rtx);
4349           DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4350           continue;
4351         }
4352
4353       /* If the parm is to be passed as a transparent union, use the
4354          type of the first field for the tests below.  We have already
4355          verified that the modes are the same.  */
4356       if (DECL_TRANSPARENT_UNION (parm)
4357           || (TREE_CODE (passed_type) == UNION_TYPE
4358               && TYPE_TRANSPARENT_UNION (passed_type)))
4359         passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
4360
4361       /* See if this arg was passed by invisible reference.  It is if
4362          it is an object whose size depends on the contents of the
4363          object itself or if the machine requires these objects be passed
4364          that way.  */
4365
4366       if ((TREE_CODE (TYPE_SIZE (passed_type)) != INTEGER_CST
4367            && contains_placeholder_p (TYPE_SIZE (passed_type)))
4368           || TREE_ADDRESSABLE (passed_type)
4369 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
4370           || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, passed_mode,
4371                                               passed_type, named_arg)
4372 #endif
4373           )
4374         {
4375           passed_type = nominal_type = build_pointer_type (passed_type);
4376           passed_pointer = 1;
4377           passed_mode = nominal_mode = Pmode;
4378         }
4379
4380       promoted_mode = passed_mode;
4381
4382 #ifdef PROMOTE_FUNCTION_ARGS
4383       /* Compute the mode in which the arg is actually extended to.  */
4384       unsignedp = TREE_UNSIGNED (passed_type);
4385       promoted_mode = promote_mode (passed_type, promoted_mode, &unsignedp, 1);
4386 #endif
4387
4388       /* Let machine desc say which reg (if any) the parm arrives in.
4389          0 means it arrives on the stack.  */
4390 #ifdef FUNCTION_INCOMING_ARG
4391       entry_parm = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4392                                           passed_type, named_arg);
4393 #else
4394       entry_parm = FUNCTION_ARG (args_so_far, promoted_mode,
4395                                  passed_type, named_arg);
4396 #endif
4397
4398       if (entry_parm == 0)
4399         promoted_mode = passed_mode;
4400
4401 #ifdef SETUP_INCOMING_VARARGS
4402       /* If this is the last named parameter, do any required setup for
4403          varargs or stdargs.  We need to know about the case of this being an
4404          addressable type, in which case we skip the registers it
4405          would have arrived in.
4406
4407          For stdargs, LAST_NAMED will be set for two parameters, the one that
4408          is actually the last named, and the dummy parameter.  We only
4409          want to do this action once.
4410
4411          Also, indicate when RTL generation is to be suppressed.  */
4412       if (last_named && !varargs_setup)
4413         {
4414           SETUP_INCOMING_VARARGS (args_so_far, promoted_mode, passed_type,
4415                                   current_function_pretend_args_size, 0);
4416           varargs_setup = 1;
4417         }
4418 #endif
4419
4420       /* Determine parm's home in the stack,
4421          in case it arrives in the stack or we should pretend it did.
4422
4423          Compute the stack position and rtx where the argument arrives
4424          and its size.
4425
4426          There is one complexity here:  If this was a parameter that would
4427          have been passed in registers, but wasn't only because it is
4428          __builtin_va_alist, we want locate_and_pad_parm to treat it as if
4429          it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
4430          In this case, we call FUNCTION_ARG with NAMED set to 1 instead of
4431          0 as it was the previous time.  */
4432
4433       pretend_named = named_arg || PRETEND_OUTGOING_VARARGS_NAMED;
4434       locate_and_pad_parm (promoted_mode, passed_type,
4435 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4436                            1,
4437 #else
4438 #ifdef FUNCTION_INCOMING_ARG
4439                            FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4440                                                   passed_type,
4441                                                   pretend_named) != 0,
4442 #else
4443                            FUNCTION_ARG (args_so_far, promoted_mode,
4444                                          passed_type,
4445                                          pretend_named) != 0,
4446 #endif
4447 #endif
4448                            fndecl, &stack_args_size, &stack_offset, &arg_size,
4449                            &alignment_pad);
4450
4451       {
4452         rtx offset_rtx = ARGS_SIZE_RTX (stack_offset);
4453
4454         if (offset_rtx == const0_rtx)
4455           stack_parm = gen_rtx_MEM (promoted_mode, internal_arg_pointer);
4456         else
4457           stack_parm = gen_rtx_MEM (promoted_mode,
4458                                     gen_rtx_PLUS (Pmode,
4459                                                   internal_arg_pointer,
4460                                                   offset_rtx));
4461
4462         set_mem_attributes (stack_parm, parm, 1);
4463       }
4464
4465       /* If this parameter was passed both in registers and in the stack,
4466          use the copy on the stack.  */
4467       if (MUST_PASS_IN_STACK (promoted_mode, passed_type))
4468         entry_parm = 0;
4469
4470 #ifdef FUNCTION_ARG_PARTIAL_NREGS
4471       /* If this parm was passed part in regs and part in memory,
4472          pretend it arrived entirely in memory
4473          by pushing the register-part onto the stack.
4474
4475          In the special case of a DImode or DFmode that is split,
4476          we could put it together in a pseudoreg directly,
4477          but for now that's not worth bothering with.  */
4478
4479       if (entry_parm)
4480         {
4481           int nregs = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, promoted_mode,
4482                                                   passed_type, named_arg);
4483
4484           if (nregs > 0)
4485             {
4486               current_function_pretend_args_size
4487                 = (((nregs * UNITS_PER_WORD) + (PARM_BOUNDARY / BITS_PER_UNIT) - 1)
4488                    / (PARM_BOUNDARY / BITS_PER_UNIT)
4489                    * (PARM_BOUNDARY / BITS_PER_UNIT));
4490
4491               /* Handle calls that pass values in multiple non-contiguous
4492                  locations.  The Irix 6 ABI has examples of this.  */
4493               if (GET_CODE (entry_parm) == PARALLEL)
4494                 emit_group_store (validize_mem (stack_parm), entry_parm,
4495                                   int_size_in_bytes (TREE_TYPE (parm)));
4496
4497               else
4498                 move_block_from_reg (REGNO (entry_parm),
4499                                      validize_mem (stack_parm), nregs,
4500                                      int_size_in_bytes (TREE_TYPE (parm)));
4501
4502               entry_parm = stack_parm;
4503             }
4504         }
4505 #endif
4506
4507       /* If we didn't decide this parm came in a register,
4508          by default it came on the stack.  */
4509       if (entry_parm == 0)
4510         entry_parm = stack_parm;
4511
4512       /* Record permanently how this parm was passed.  */
4513       DECL_INCOMING_RTL (parm) = entry_parm;
4514
4515       /* If there is actually space on the stack for this parm,
4516          count it in stack_args_size; otherwise set stack_parm to 0
4517          to indicate there is no preallocated stack slot for the parm.  */
4518
4519       if (entry_parm == stack_parm
4520           || (GET_CODE (entry_parm) == PARALLEL
4521               && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
4522 #if defined (REG_PARM_STACK_SPACE) && ! defined (MAYBE_REG_PARM_STACK_SPACE)
4523           /* On some machines, even if a parm value arrives in a register
4524              there is still an (uninitialized) stack slot allocated for it.
4525
4526              ??? When MAYBE_REG_PARM_STACK_SPACE is defined, we can't tell
4527              whether this parameter already has a stack slot allocated,
4528              because an arg block exists only if current_function_args_size
4529              is larger than some threshold, and we haven't calculated that
4530              yet.  So, for now, we just assume that stack slots never exist
4531              in this case.  */
4532           || REG_PARM_STACK_SPACE (fndecl) > 0
4533 #endif
4534           )
4535         {
4536           stack_args_size.constant += arg_size.constant;
4537           if (arg_size.var)
4538             ADD_PARM_SIZE (stack_args_size, arg_size.var);
4539         }
4540       else
4541         /* No stack slot was pushed for this parm.  */
4542         stack_parm = 0;
4543
4544       /* Update info on where next arg arrives in registers.  */
4545
4546       FUNCTION_ARG_ADVANCE (args_so_far, promoted_mode,
4547                             passed_type, named_arg);
4548
4549       /* If we can't trust the parm stack slot to be aligned enough
4550          for its ultimate type, don't use that slot after entry.
4551          We'll make another stack slot, if we need one.  */
4552       {
4553         unsigned int thisparm_boundary
4554           = FUNCTION_ARG_BOUNDARY (promoted_mode, passed_type);
4555
4556         if (GET_MODE_ALIGNMENT (nominal_mode) > thisparm_boundary)
4557           stack_parm = 0;
4558       }
4559
4560       /* If parm was passed in memory, and we need to convert it on entry,
4561          don't store it back in that same slot.  */
4562       if (entry_parm != 0
4563           && nominal_mode != BLKmode && nominal_mode != passed_mode)
4564         stack_parm = 0;
4565
4566       /* When an argument is passed in multiple locations, we can't
4567          make use of this information, but we can save some copying if
4568          the whole argument is passed in a single register.  */
4569       if (GET_CODE (entry_parm) == PARALLEL
4570           && nominal_mode != BLKmode && passed_mode != BLKmode)
4571         {
4572           int i, len = XVECLEN (entry_parm, 0);
4573
4574           for (i = 0; i < len; i++)
4575             if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
4576                 && GET_CODE (XEXP (XVECEXP (entry_parm, 0, i), 0)) == REG
4577                 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
4578                     == passed_mode)
4579                 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
4580               {
4581                 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
4582                 DECL_INCOMING_RTL (parm) = entry_parm;
4583                 break;
4584               }
4585         }
4586
4587       /* ENTRY_PARM is an RTX for the parameter as it arrives,
4588          in the mode in which it arrives.
4589          STACK_PARM is an RTX for a stack slot where the parameter can live
4590          during the function (in case we want to put it there).
4591          STACK_PARM is 0 if no stack slot was pushed for it.
4592
4593          Now output code if necessary to convert ENTRY_PARM to
4594          the type in which this function declares it,
4595          and store that result in an appropriate place,
4596          which may be a pseudo reg, may be STACK_PARM,
4597          or may be a local stack slot if STACK_PARM is 0.
4598
4599          Set DECL_RTL to that place.  */
4600
4601       if (nominal_mode == BLKmode || GET_CODE (entry_parm) == PARALLEL)
4602         {
4603           /* If a BLKmode arrives in registers, copy it to a stack slot.
4604              Handle calls that pass values in multiple non-contiguous
4605              locations.  The Irix 6 ABI has examples of this.  */
4606           if (GET_CODE (entry_parm) == REG
4607               || GET_CODE (entry_parm) == PARALLEL)
4608             {
4609               int size_stored
4610                 = CEIL_ROUND (int_size_in_bytes (TREE_TYPE (parm)),
4611                               UNITS_PER_WORD);
4612
4613               /* Note that we will be storing an integral number of words.
4614                  So we have to be careful to ensure that we allocate an
4615                  integral number of words.  We do this below in the
4616                  assign_stack_local if space was not allocated in the argument
4617                  list.  If it was, this will not work if PARM_BOUNDARY is not
4618                  a multiple of BITS_PER_WORD.  It isn't clear how to fix this
4619                  if it becomes a problem.  */
4620
4621               if (stack_parm == 0)
4622                 {
4623                   stack_parm
4624                     = assign_stack_local (GET_MODE (entry_parm),
4625                                           size_stored, 0);
4626                   set_mem_attributes (stack_parm, parm, 1);
4627                 }
4628
4629               else if (PARM_BOUNDARY % BITS_PER_WORD != 0)
4630                 abort ();
4631
4632               /* Handle calls that pass values in multiple non-contiguous
4633                  locations.  The Irix 6 ABI has examples of this.  */
4634               if (GET_CODE (entry_parm) == PARALLEL)
4635                 emit_group_store (validize_mem (stack_parm), entry_parm,
4636                                   int_size_in_bytes (TREE_TYPE (parm)));
4637               else
4638                 move_block_from_reg (REGNO (entry_parm),
4639                                      validize_mem (stack_parm),
4640                                      size_stored / UNITS_PER_WORD,
4641                                      int_size_in_bytes (TREE_TYPE (parm)));
4642             }
4643           SET_DECL_RTL (parm, stack_parm);
4644         }
4645       else if (! ((! optimize
4646                    && ! DECL_REGISTER (parm)
4647                    && ! DECL_INLINE (fndecl))
4648                   || TREE_SIDE_EFFECTS (parm)
4649                   /* If -ffloat-store specified, don't put explicit
4650                      float variables into registers.  */
4651                   || (flag_float_store
4652                       && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
4653                /* Always assign pseudo to structure return or item passed
4654                   by invisible reference.  */
4655                || passed_pointer || parm == function_result_decl)
4656         {
4657           /* Store the parm in a pseudoregister during the function, but we
4658              may need to do it in a wider mode.  */
4659
4660           rtx parmreg;
4661           unsigned int regno, regnoi = 0, regnor = 0;
4662
4663           unsignedp = TREE_UNSIGNED (TREE_TYPE (parm));
4664
4665           promoted_nominal_mode
4666             = promote_mode (TREE_TYPE (parm), nominal_mode, &unsignedp, 0);
4667
4668           parmreg = gen_reg_rtx (promoted_nominal_mode);
4669           mark_user_reg (parmreg);
4670
4671           /* If this was an item that we received a pointer to, set DECL_RTL
4672              appropriately.  */
4673           if (passed_pointer)
4674             {
4675               rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (passed_type)),
4676                                    parmreg);
4677               set_mem_attributes (x, parm, 1);
4678               SET_DECL_RTL (parm, x);
4679             }
4680           else
4681             {
4682               SET_DECL_RTL (parm, parmreg);
4683               maybe_set_unchanging (DECL_RTL (parm), parm);
4684             }
4685               
4686           /* Copy the value into the register.  */
4687           if (nominal_mode != passed_mode
4688               || promoted_nominal_mode != promoted_mode)
4689             {
4690               int save_tree_used;
4691               /* ENTRY_PARM has been converted to PROMOTED_MODE, its
4692                  mode, by the caller.  We now have to convert it to
4693                  NOMINAL_MODE, if different.  However, PARMREG may be in
4694                  a different mode than NOMINAL_MODE if it is being stored
4695                  promoted.
4696
4697                  If ENTRY_PARM is a hard register, it might be in a register
4698                  not valid for operating in its mode (e.g., an odd-numbered
4699                  register for a DFmode).  In that case, moves are the only
4700                  thing valid, so we can't do a convert from there.  This
4701                  occurs when the calling sequence allow such misaligned
4702                  usages.
4703
4704                  In addition, the conversion may involve a call, which could
4705                  clobber parameters which haven't been copied to pseudo
4706                  registers yet.  Therefore, we must first copy the parm to
4707                  a pseudo reg here, and save the conversion until after all
4708                  parameters have been moved.  */
4709
4710               rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4711
4712               emit_move_insn (tempreg, validize_mem (entry_parm));
4713
4714               push_to_sequence (conversion_insns);
4715               tempreg = convert_to_mode (nominal_mode, tempreg, unsignedp);
4716
4717               if (GET_CODE (tempreg) == SUBREG
4718                   && GET_MODE (tempreg) == nominal_mode
4719                   && GET_CODE (SUBREG_REG (tempreg)) == REG
4720                   && nominal_mode == passed_mode
4721                   && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (entry_parm)
4722                   && GET_MODE_SIZE (GET_MODE (tempreg))
4723                      < GET_MODE_SIZE (GET_MODE (entry_parm)))
4724                 {
4725                   /* The argument is already sign/zero extended, so note it
4726                      into the subreg.  */
4727                   SUBREG_PROMOTED_VAR_P (tempreg) = 1;
4728                   SUBREG_PROMOTED_UNSIGNED_P (tempreg) = unsignedp;
4729                 }
4730
4731               /* TREE_USED gets set erroneously during expand_assignment.  */
4732               save_tree_used = TREE_USED (parm);
4733               expand_assignment (parm,
4734                                  make_tree (nominal_type, tempreg), 0, 0);
4735               TREE_USED (parm) = save_tree_used;
4736               conversion_insns = get_insns ();
4737               did_conversion = 1;
4738               end_sequence ();
4739             }
4740           else
4741             emit_move_insn (parmreg, validize_mem (entry_parm));
4742
4743           /* If we were passed a pointer but the actual value
4744              can safely live in a register, put it in one.  */
4745           if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
4746               && ! ((! optimize
4747                      && ! DECL_REGISTER (parm)
4748                      && ! DECL_INLINE (fndecl))
4749                     || TREE_SIDE_EFFECTS (parm)
4750                     /* If -ffloat-store specified, don't put explicit
4751                        float variables into registers.  */
4752                     || (flag_float_store
4753                         && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE)))
4754             {
4755               /* We can't use nominal_mode, because it will have been set to
4756                  Pmode above.  We must use the actual mode of the parm.  */
4757               parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
4758               mark_user_reg (parmreg);
4759               if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
4760                 {
4761                   rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
4762                   int unsigned_p = TREE_UNSIGNED (TREE_TYPE (parm));
4763                   push_to_sequence (conversion_insns);
4764                   emit_move_insn (tempreg, DECL_RTL (parm));
4765                   SET_DECL_RTL (parm,
4766                                 convert_to_mode (GET_MODE (parmreg), 
4767                                                  tempreg,
4768                                                  unsigned_p));
4769                   emit_move_insn (parmreg, DECL_RTL (parm));
4770                   conversion_insns = get_insns();
4771                   did_conversion = 1;
4772                   end_sequence ();
4773                 }
4774               else
4775                 emit_move_insn (parmreg, DECL_RTL (parm));
4776               SET_DECL_RTL (parm, parmreg);
4777               /* STACK_PARM is the pointer, not the parm, and PARMREG is
4778                  now the parm.  */
4779               stack_parm = 0;
4780             }
4781 #ifdef FUNCTION_ARG_CALLEE_COPIES
4782           /* If we are passed an arg by reference and it is our responsibility
4783              to make a copy, do it now.
4784              PASSED_TYPE and PASSED mode now refer to the pointer, not the
4785              original argument, so we must recreate them in the call to
4786              FUNCTION_ARG_CALLEE_COPIES.  */
4787           /* ??? Later add code to handle the case that if the argument isn't
4788              modified, don't do the copy.  */
4789
4790           else if (passed_pointer
4791                    && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
4792                                                   TYPE_MODE (DECL_ARG_TYPE (parm)),
4793                                                   DECL_ARG_TYPE (parm),
4794                                                   named_arg)
4795                    && ! TREE_ADDRESSABLE (DECL_ARG_TYPE (parm)))
4796             {
4797               rtx copy;
4798               tree type = DECL_ARG_TYPE (parm);
4799
4800               /* This sequence may involve a library call perhaps clobbering
4801                  registers that haven't been copied to pseudos yet.  */
4802
4803               push_to_sequence (conversion_insns);
4804
4805               if (!COMPLETE_TYPE_P (type)
4806                   || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4807                 /* This is a variable sized object.  */
4808                 copy = gen_rtx_MEM (BLKmode,
4809                                     allocate_dynamic_stack_space
4810                                     (expr_size (parm), NULL_RTX,
4811                                      TYPE_ALIGN (type)));
4812               else
4813                 copy = assign_stack_temp (TYPE_MODE (type),
4814                                           int_size_in_bytes (type), 1);
4815               set_mem_attributes (copy, parm, 1);
4816
4817               store_expr (parm, copy, 0);
4818               emit_move_insn (parmreg, XEXP (copy, 0));
4819               conversion_insns = get_insns ();
4820               did_conversion = 1;
4821               end_sequence ();
4822             }
4823 #endif /* FUNCTION_ARG_CALLEE_COPIES */
4824
4825           /* In any case, record the parm's desired stack location
4826              in case we later discover it must live in the stack.
4827
4828              If it is a COMPLEX value, store the stack location for both
4829              halves.  */
4830
4831           if (GET_CODE (parmreg) == CONCAT)
4832             regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
4833           else
4834             regno = REGNO (parmreg);
4835
4836           if (regno >= max_parm_reg)
4837             {
4838               rtx *new;
4839               int old_max_parm_reg = max_parm_reg;
4840
4841               /* It's slow to expand this one register at a time,
4842                  but it's also rare and we need max_parm_reg to be
4843                  precisely correct.  */
4844               max_parm_reg = regno + 1;
4845               new = (rtx *) xrealloc (parm_reg_stack_loc,
4846                                       max_parm_reg * sizeof (rtx));
4847               memset ((char *) (new + old_max_parm_reg), 0,
4848                      (max_parm_reg - old_max_parm_reg) * sizeof (rtx));
4849               parm_reg_stack_loc = new;
4850             }
4851
4852           if (GET_CODE (parmreg) == CONCAT)
4853             {
4854               enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
4855
4856               regnor = REGNO (gen_realpart (submode, parmreg));
4857               regnoi = REGNO (gen_imagpart (submode, parmreg));
4858
4859               if (stack_parm != 0)
4860                 {
4861                   parm_reg_stack_loc[regnor]
4862                     = gen_realpart (submode, stack_parm);
4863                   parm_reg_stack_loc[regnoi]
4864                     = gen_imagpart (submode, stack_parm);
4865                 }
4866               else
4867                 {
4868                   parm_reg_stack_loc[regnor] = 0;
4869                   parm_reg_stack_loc[regnoi] = 0;
4870                 }
4871             }
4872           else
4873             parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
4874
4875           /* Mark the register as eliminable if we did no conversion
4876              and it was copied from memory at a fixed offset,
4877              and the arg pointer was not copied to a pseudo-reg.
4878              If the arg pointer is a pseudo reg or the offset formed
4879              an invalid address, such memory-equivalences
4880              as we make here would screw up life analysis for it.  */
4881           if (nominal_mode == passed_mode
4882               && ! did_conversion
4883               && stack_parm != 0
4884               && GET_CODE (stack_parm) == MEM
4885               && stack_offset.var == 0
4886               && reg_mentioned_p (virtual_incoming_args_rtx,
4887                                   XEXP (stack_parm, 0)))
4888             {
4889               rtx linsn = get_last_insn ();
4890               rtx sinsn, set;
4891
4892               /* Mark complex types separately.  */
4893               if (GET_CODE (parmreg) == CONCAT)
4894                 /* Scan backwards for the set of the real and
4895                    imaginary parts.  */
4896                 for (sinsn = linsn; sinsn != 0;
4897                      sinsn = prev_nonnote_insn (sinsn))
4898                   {
4899                     set = single_set (sinsn);
4900                     if (set != 0
4901                         && SET_DEST (set) == regno_reg_rtx [regnoi])
4902                       REG_NOTES (sinsn)
4903                         = gen_rtx_EXPR_LIST (REG_EQUIV,
4904                                              parm_reg_stack_loc[regnoi],
4905                                              REG_NOTES (sinsn));
4906                     else if (set != 0
4907                              && SET_DEST (set) == regno_reg_rtx [regnor])
4908                       REG_NOTES (sinsn)
4909                         = gen_rtx_EXPR_LIST (REG_EQUIV,
4910                                              parm_reg_stack_loc[regnor],
4911                                              REG_NOTES (sinsn));
4912                   }
4913               else if ((set = single_set (linsn)) != 0
4914                        && SET_DEST (set) == parmreg)
4915                 REG_NOTES (linsn)
4916                   = gen_rtx_EXPR_LIST (REG_EQUIV,
4917                                        stack_parm, REG_NOTES (linsn));
4918             }
4919
4920           /* For pointer data type, suggest pointer register.  */
4921           if (POINTER_TYPE_P (TREE_TYPE (parm)))
4922             mark_reg_pointer (parmreg,
4923                               TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4924
4925           /* If something wants our address, try to use ADDRESSOF.  */
4926           if (TREE_ADDRESSABLE (parm))
4927             {
4928               /* If we end up putting something into the stack,
4929                  fixup_var_refs_insns will need to make a pass over
4930                  all the instructions.  It looks through the pending
4931                  sequences -- but it can't see the ones in the
4932                  CONVERSION_INSNS, if they're not on the sequence
4933                  stack.  So, we go back to that sequence, just so that
4934                  the fixups will happen.  */
4935               push_to_sequence (conversion_insns);
4936               put_var_into_stack (parm);
4937               conversion_insns = get_insns ();
4938               end_sequence ();
4939             }
4940         }
4941       else
4942         {
4943           /* Value must be stored in the stack slot STACK_PARM
4944              during function execution.  */
4945
4946           if (promoted_mode != nominal_mode)
4947             {
4948               /* Conversion is required.  */
4949               rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4950
4951               emit_move_insn (tempreg, validize_mem (entry_parm));
4952
4953               push_to_sequence (conversion_insns);
4954               entry_parm = convert_to_mode (nominal_mode, tempreg,
4955                                             TREE_UNSIGNED (TREE_TYPE (parm)));
4956               if (stack_parm)
4957                 /* ??? This may need a big-endian conversion on sparc64.  */
4958                 stack_parm = adjust_address (stack_parm, nominal_mode, 0);
4959
4960               conversion_insns = get_insns ();
4961               did_conversion = 1;
4962               end_sequence ();
4963             }
4964
4965           if (entry_parm != stack_parm)
4966             {
4967               if (stack_parm == 0)
4968                 {
4969                   stack_parm
4970                     = assign_stack_local (GET_MODE (entry_parm),
4971                                           GET_MODE_SIZE (GET_MODE (entry_parm)), 0);
4972                   set_mem_attributes (stack_parm, parm, 1);
4973                 }
4974
4975               if (promoted_mode != nominal_mode)
4976                 {
4977                   push_to_sequence (conversion_insns);
4978                   emit_move_insn (validize_mem (stack_parm),
4979                                   validize_mem (entry_parm));
4980                   conversion_insns = get_insns ();
4981                   end_sequence ();
4982                 }
4983               else
4984                 emit_move_insn (validize_mem (stack_parm),
4985                                 validize_mem (entry_parm));
4986             }
4987
4988           SET_DECL_RTL (parm, stack_parm);
4989         }
4990
4991       /* If this "parameter" was the place where we are receiving the
4992          function's incoming structure pointer, set up the result.  */
4993       if (parm == function_result_decl)
4994         {
4995           tree result = DECL_RESULT (fndecl);
4996           rtx addr = DECL_RTL (parm);
4997           rtx x;
4998
4999 #ifdef POINTERS_EXTEND_UNSIGNED
5000           if (GET_MODE (addr) != Pmode)
5001             addr = convert_memory_address (Pmode, addr);
5002 #endif
5003
5004           x = gen_rtx_MEM (DECL_MODE (result), addr);
5005           set_mem_attributes (x, result, 1);
5006           SET_DECL_RTL (result, x);
5007         }
5008
5009       if (GET_CODE (DECL_RTL (parm)) == REG)
5010         REGNO_DECL (REGNO (DECL_RTL (parm))) = parm;
5011       else if (GET_CODE (DECL_RTL (parm)) == CONCAT)
5012         {
5013           REGNO_DECL (REGNO (XEXP (DECL_RTL (parm), 0))) = parm;
5014           REGNO_DECL (REGNO (XEXP (DECL_RTL (parm), 1))) = parm;
5015         }
5016
5017     }
5018
5019   /* Output all parameter conversion instructions (possibly including calls)
5020      now that all parameters have been copied out of hard registers.  */
5021   emit_insns (conversion_insns);
5022
5023   last_parm_insn = get_last_insn ();
5024
5025   current_function_args_size = stack_args_size.constant;
5026
5027   /* Adjust function incoming argument size for alignment and
5028      minimum length.  */
5029
5030 #ifdef REG_PARM_STACK_SPACE
5031 #ifndef MAYBE_REG_PARM_STACK_SPACE
5032   current_function_args_size = MAX (current_function_args_size,
5033                                     REG_PARM_STACK_SPACE (fndecl));
5034 #endif
5035 #endif
5036
5037 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
5038
5039   current_function_args_size
5040     = ((current_function_args_size + STACK_BYTES - 1)
5041        / STACK_BYTES) * STACK_BYTES;
5042
5043 #ifdef ARGS_GROW_DOWNWARD
5044   current_function_arg_offset_rtx
5045     = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
5046        : expand_expr (size_diffop (stack_args_size.var,
5047                                    size_int (-stack_args_size.constant)),
5048                       NULL_RTX, VOIDmode, 0));
5049 #else
5050   current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
5051 #endif
5052
5053   /* See how many bytes, if any, of its args a function should try to pop
5054      on return.  */
5055
5056   current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
5057                                                  current_function_args_size);
5058
5059   /* For stdarg.h function, save info about
5060      regs and stack space used by the named args.  */
5061
5062   if (!hide_last_arg)
5063     current_function_args_info = args_so_far;
5064
5065   /* Set the rtx used for the function return value.  Put this in its
5066      own variable so any optimizers that need this information don't have
5067      to include tree.h.  Do this here so it gets done when an inlined
5068      function gets output.  */
5069
5070   current_function_return_rtx
5071     = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
5072        ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
5073 }
5074 \f
5075 /* Indicate whether REGNO is an incoming argument to the current function
5076    that was promoted to a wider mode.  If so, return the RTX for the
5077    register (to get its mode).  PMODE and PUNSIGNEDP are set to the mode
5078    that REGNO is promoted from and whether the promotion was signed or
5079    unsigned.  */
5080
5081 #ifdef PROMOTE_FUNCTION_ARGS
5082
5083 rtx
5084 promoted_input_arg (regno, pmode, punsignedp)
5085      unsigned int regno;
5086      enum machine_mode *pmode;
5087      int *punsignedp;
5088 {
5089   tree arg;
5090
5091   for (arg = DECL_ARGUMENTS (current_function_decl); arg;
5092        arg = TREE_CHAIN (arg))
5093     if (GET_CODE (DECL_INCOMING_RTL (arg)) == REG
5094         && REGNO (DECL_INCOMING_RTL (arg)) == regno
5095         && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
5096       {
5097         enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
5098         int unsignedp = TREE_UNSIGNED (TREE_TYPE (arg));
5099
5100         mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
5101         if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
5102             && mode != DECL_MODE (arg))
5103           {
5104             *pmode = DECL_MODE (arg);
5105             *punsignedp = unsignedp;
5106             return DECL_INCOMING_RTL (arg);
5107           }
5108       }
5109
5110   return 0;
5111 }
5112
5113 #endif
5114 \f
5115 /* Compute the size and offset from the start of the stacked arguments for a
5116    parm passed in mode PASSED_MODE and with type TYPE.
5117
5118    INITIAL_OFFSET_PTR points to the current offset into the stacked
5119    arguments.
5120
5121    The starting offset and size for this parm are returned in *OFFSET_PTR
5122    and *ARG_SIZE_PTR, respectively.
5123
5124    IN_REGS is non-zero if the argument will be passed in registers.  It will
5125    never be set if REG_PARM_STACK_SPACE is not defined.
5126
5127    FNDECL is the function in which the argument was defined.
5128
5129    There are two types of rounding that are done.  The first, controlled by
5130    FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
5131    list to be aligned to the specific boundary (in bits).  This rounding
5132    affects the initial and starting offsets, but not the argument size.
5133
5134    The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
5135    optionally rounds the size of the parm to PARM_BOUNDARY.  The
5136    initial offset is not affected by this rounding, while the size always
5137    is and the starting offset may be.  */
5138
5139 /*  offset_ptr will be negative for ARGS_GROW_DOWNWARD case;
5140     initial_offset_ptr is positive because locate_and_pad_parm's
5141     callers pass in the total size of args so far as
5142     initial_offset_ptr. arg_size_ptr is always positive.*/
5143
5144 void
5145 locate_and_pad_parm (passed_mode, type, in_regs, fndecl,
5146                      initial_offset_ptr, offset_ptr, arg_size_ptr,
5147                      alignment_pad)
5148      enum machine_mode passed_mode;
5149      tree type;
5150      int in_regs ATTRIBUTE_UNUSED;
5151      tree fndecl ATTRIBUTE_UNUSED;
5152      struct args_size *initial_offset_ptr;
5153      struct args_size *offset_ptr;
5154      struct args_size *arg_size_ptr;
5155      struct args_size *alignment_pad;
5156
5157 {
5158   tree sizetree
5159     = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
5160   enum direction where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
5161   int boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
5162
5163 #ifdef REG_PARM_STACK_SPACE
5164   /* If we have found a stack parm before we reach the end of the
5165      area reserved for registers, skip that area.  */
5166   if (! in_regs)
5167     {
5168       int reg_parm_stack_space = 0;
5169
5170 #ifdef MAYBE_REG_PARM_STACK_SPACE
5171       reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
5172 #else
5173       reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
5174 #endif
5175       if (reg_parm_stack_space > 0)
5176         {
5177           if (initial_offset_ptr->var)
5178             {
5179               initial_offset_ptr->var
5180                 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
5181                               ssize_int (reg_parm_stack_space));
5182               initial_offset_ptr->constant = 0;
5183             }
5184           else if (initial_offset_ptr->constant < reg_parm_stack_space)
5185             initial_offset_ptr->constant = reg_parm_stack_space;
5186         }
5187     }
5188 #endif /* REG_PARM_STACK_SPACE */
5189
5190   arg_size_ptr->var = 0;
5191   arg_size_ptr->constant = 0;
5192   alignment_pad->var = 0;
5193   alignment_pad->constant = 0;
5194
5195 #ifdef ARGS_GROW_DOWNWARD
5196   if (initial_offset_ptr->var)
5197     {
5198       offset_ptr->constant = 0;
5199       offset_ptr->var = size_binop (MINUS_EXPR, ssize_int (0),
5200                                     initial_offset_ptr->var);
5201     }
5202   else
5203     {
5204       offset_ptr->constant = -initial_offset_ptr->constant;
5205       offset_ptr->var = 0;
5206     }
5207   if (where_pad != none
5208       && (!host_integerp (sizetree, 1)
5209           || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5210     sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5211   SUB_PARM_SIZE (*offset_ptr, sizetree);
5212   if (where_pad != downward)
5213     pad_to_arg_alignment (offset_ptr, boundary, alignment_pad);
5214   if (initial_offset_ptr->var)
5215     arg_size_ptr->var = size_binop (MINUS_EXPR,
5216                                     size_binop (MINUS_EXPR,
5217                                                 ssize_int (0),
5218                                                 initial_offset_ptr->var),
5219                                     offset_ptr->var);
5220
5221   else
5222     arg_size_ptr->constant = (-initial_offset_ptr->constant
5223                               - offset_ptr->constant);
5224
5225 #else /* !ARGS_GROW_DOWNWARD */
5226   if (!in_regs
5227 #ifdef REG_PARM_STACK_SPACE
5228       || REG_PARM_STACK_SPACE (fndecl) > 0
5229 #endif
5230       )
5231     pad_to_arg_alignment (initial_offset_ptr, boundary, alignment_pad);
5232   *offset_ptr = *initial_offset_ptr;
5233
5234 #ifdef PUSH_ROUNDING
5235   if (passed_mode != BLKmode)
5236     sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
5237 #endif
5238
5239   /* Pad_below needs the pre-rounded size to know how much to pad below
5240      so this must be done before rounding up.  */
5241   if (where_pad == downward
5242     /* However, BLKmode args passed in regs have their padding done elsewhere.
5243        The stack slot must be able to hold the entire register.  */
5244       && !(in_regs && passed_mode == BLKmode))
5245     pad_below (offset_ptr, passed_mode, sizetree);
5246
5247   if (where_pad != none
5248       && (!host_integerp (sizetree, 1)
5249           || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5250     sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5251
5252   ADD_PARM_SIZE (*arg_size_ptr, sizetree);
5253 #endif /* ARGS_GROW_DOWNWARD */
5254 }
5255
5256 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
5257    BOUNDARY is measured in bits, but must be a multiple of a storage unit.  */
5258
5259 static void
5260 pad_to_arg_alignment (offset_ptr, boundary, alignment_pad)
5261      struct args_size *offset_ptr;
5262      int boundary;
5263      struct args_size *alignment_pad;
5264 {
5265   tree save_var = NULL_TREE;
5266   HOST_WIDE_INT save_constant = 0;
5267
5268   int boundary_in_bytes = boundary / BITS_PER_UNIT;
5269
5270   if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5271     {
5272       save_var = offset_ptr->var;
5273       save_constant = offset_ptr->constant;
5274     }
5275
5276   alignment_pad->var = NULL_TREE;
5277   alignment_pad->constant = 0;
5278
5279   if (boundary > BITS_PER_UNIT)
5280     {
5281       if (offset_ptr->var)
5282         {
5283           offset_ptr->var =
5284 #ifdef ARGS_GROW_DOWNWARD
5285             round_down
5286 #else
5287             round_up
5288 #endif
5289               (ARGS_SIZE_TREE (*offset_ptr),
5290                boundary / BITS_PER_UNIT);
5291           offset_ptr->constant = 0; /*?*/
5292           if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5293             alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
5294                                              save_var);
5295         }
5296       else
5297         {
5298           offset_ptr->constant =
5299 #ifdef ARGS_GROW_DOWNWARD
5300             FLOOR_ROUND (offset_ptr->constant, boundary_in_bytes);
5301 #else
5302             CEIL_ROUND (offset_ptr->constant, boundary_in_bytes);
5303 #endif
5304             if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5305               alignment_pad->constant = offset_ptr->constant - save_constant;
5306         }
5307     }
5308 }
5309
5310 #ifndef ARGS_GROW_DOWNWARD
5311 static void
5312 pad_below (offset_ptr, passed_mode, sizetree)
5313      struct args_size *offset_ptr;
5314      enum machine_mode passed_mode;
5315      tree sizetree;
5316 {
5317   if (passed_mode != BLKmode)
5318     {
5319       if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
5320         offset_ptr->constant
5321           += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
5322                / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
5323               - GET_MODE_SIZE (passed_mode));
5324     }
5325   else
5326     {
5327       if (TREE_CODE (sizetree) != INTEGER_CST
5328           || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
5329         {
5330           /* Round the size up to multiple of PARM_BOUNDARY bits.  */
5331           tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5332           /* Add it in.  */
5333           ADD_PARM_SIZE (*offset_ptr, s2);
5334           SUB_PARM_SIZE (*offset_ptr, sizetree);
5335         }
5336     }
5337 }
5338 #endif
5339 \f
5340 /* Walk the tree of blocks describing the binding levels within a function
5341    and warn about uninitialized variables.
5342    This is done after calling flow_analysis and before global_alloc
5343    clobbers the pseudo-regs to hard regs.  */
5344
5345 void
5346 uninitialized_vars_warning (block)
5347      tree block;
5348 {
5349   tree decl, sub;
5350   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5351     {
5352       if (warn_uninitialized
5353           && TREE_CODE (decl) == VAR_DECL
5354           /* These warnings are unreliable for and aggregates
5355              because assigning the fields one by one can fail to convince
5356              flow.c that the entire aggregate was initialized.
5357              Unions are troublesome because members may be shorter.  */
5358           && ! AGGREGATE_TYPE_P (TREE_TYPE (decl))
5359           && DECL_RTL (decl) != 0
5360           && GET_CODE (DECL_RTL (decl)) == REG
5361           /* Global optimizations can make it difficult to determine if a
5362              particular variable has been initialized.  However, a VAR_DECL
5363              with a nonzero DECL_INITIAL had an initializer, so do not
5364              claim it is potentially uninitialized.
5365
5366              We do not care about the actual value in DECL_INITIAL, so we do
5367              not worry that it may be a dangling pointer.  */
5368           && DECL_INITIAL (decl) == NULL_TREE
5369           && regno_uninitialized (REGNO (DECL_RTL (decl))))
5370         warning_with_decl (decl,
5371                            "`%s' might be used uninitialized in this function");
5372       if (extra_warnings
5373           && TREE_CODE (decl) == VAR_DECL
5374           && DECL_RTL (decl) != 0
5375           && GET_CODE (DECL_RTL (decl)) == REG
5376           && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5377         warning_with_decl (decl,
5378                            "variable `%s' might be clobbered by `longjmp' or `vfork'");
5379     }
5380   for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5381     uninitialized_vars_warning (sub);
5382 }
5383
5384 /* Do the appropriate part of uninitialized_vars_warning
5385    but for arguments instead of local variables.  */
5386
5387 void
5388 setjmp_args_warning ()
5389 {
5390   tree decl;
5391   for (decl = DECL_ARGUMENTS (current_function_decl);
5392        decl; decl = TREE_CHAIN (decl))
5393     if (DECL_RTL (decl) != 0
5394         && GET_CODE (DECL_RTL (decl)) == REG
5395         && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5396       warning_with_decl (decl,
5397                          "argument `%s' might be clobbered by `longjmp' or `vfork'");
5398 }
5399
5400 /* If this function call setjmp, put all vars into the stack
5401    unless they were declared `register'.  */
5402
5403 void
5404 setjmp_protect (block)
5405      tree block;
5406 {
5407   tree decl, sub;
5408   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5409     if ((TREE_CODE (decl) == VAR_DECL
5410          || TREE_CODE (decl) == PARM_DECL)
5411         && DECL_RTL (decl) != 0
5412         && (GET_CODE (DECL_RTL (decl)) == REG
5413             || (GET_CODE (DECL_RTL (decl)) == MEM
5414                 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5415         /* If this variable came from an inline function, it must be
5416            that its life doesn't overlap the setjmp.  If there was a
5417            setjmp in the function, it would already be in memory.  We
5418            must exclude such variable because their DECL_RTL might be
5419            set to strange things such as virtual_stack_vars_rtx.  */
5420         && ! DECL_FROM_INLINE (decl)
5421         && (
5422 #ifdef NON_SAVING_SETJMP
5423             /* If longjmp doesn't restore the registers,
5424                don't put anything in them.  */
5425             NON_SAVING_SETJMP
5426             ||
5427 #endif
5428             ! DECL_REGISTER (decl)))
5429       put_var_into_stack (decl);
5430   for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5431     setjmp_protect (sub);
5432 }
5433 \f
5434 /* Like the previous function, but for args instead of local variables.  */
5435
5436 void
5437 setjmp_protect_args ()
5438 {
5439   tree decl;
5440   for (decl = DECL_ARGUMENTS (current_function_decl);
5441        decl; decl = TREE_CHAIN (decl))
5442     if ((TREE_CODE (decl) == VAR_DECL
5443          || TREE_CODE (decl) == PARM_DECL)
5444         && DECL_RTL (decl) != 0
5445         && (GET_CODE (DECL_RTL (decl)) == REG
5446             || (GET_CODE (DECL_RTL (decl)) == MEM
5447                 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5448         && (
5449             /* If longjmp doesn't restore the registers,
5450                don't put anything in them.  */
5451 #ifdef NON_SAVING_SETJMP
5452             NON_SAVING_SETJMP
5453             ||
5454 #endif
5455             ! DECL_REGISTER (decl)))
5456       put_var_into_stack (decl);
5457 }
5458 \f
5459 /* Return the context-pointer register corresponding to DECL,
5460    or 0 if it does not need one.  */
5461
5462 rtx
5463 lookup_static_chain (decl)
5464      tree decl;
5465 {
5466   tree context = decl_function_context (decl);
5467   tree link;
5468
5469   if (context == 0
5470       || (TREE_CODE (decl) == FUNCTION_DECL && DECL_NO_STATIC_CHAIN (decl)))
5471     return 0;
5472
5473   /* We treat inline_function_decl as an alias for the current function
5474      because that is the inline function whose vars, types, etc.
5475      are being merged into the current function.
5476      See expand_inline_function.  */
5477   if (context == current_function_decl || context == inline_function_decl)
5478     return virtual_stack_vars_rtx;
5479
5480   for (link = context_display; link; link = TREE_CHAIN (link))
5481     if (TREE_PURPOSE (link) == context)
5482       return RTL_EXPR_RTL (TREE_VALUE (link));
5483
5484   abort ();
5485 }
5486 \f
5487 /* Convert a stack slot address ADDR for variable VAR
5488    (from a containing function)
5489    into an address valid in this function (using a static chain).  */
5490
5491 rtx
5492 fix_lexical_addr (addr, var)
5493      rtx addr;
5494      tree var;
5495 {
5496   rtx basereg;
5497   HOST_WIDE_INT displacement;
5498   tree context = decl_function_context (var);
5499   struct function *fp;
5500   rtx base = 0;
5501
5502   /* If this is the present function, we need not do anything.  */
5503   if (context == current_function_decl || context == inline_function_decl)
5504     return addr;
5505
5506   fp = find_function_data (context);
5507
5508   if (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == MEM)
5509     addr = XEXP (XEXP (addr, 0), 0);
5510
5511   /* Decode given address as base reg plus displacement.  */
5512   if (GET_CODE (addr) == REG)
5513     basereg = addr, displacement = 0;
5514   else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
5515     basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
5516   else
5517     abort ();
5518
5519   /* We accept vars reached via the containing function's
5520      incoming arg pointer and via its stack variables pointer.  */
5521   if (basereg == fp->internal_arg_pointer)
5522     {
5523       /* If reached via arg pointer, get the arg pointer value
5524          out of that function's stack frame.
5525
5526          There are two cases:  If a separate ap is needed, allocate a
5527          slot in the outer function for it and dereference it that way.
5528          This is correct even if the real ap is actually a pseudo.
5529          Otherwise, just adjust the offset from the frame pointer to
5530          compensate.  */
5531
5532 #ifdef NEED_SEPARATE_AP
5533       rtx addr;
5534
5535       addr = get_arg_pointer_save_area (fp);
5536       addr = fix_lexical_addr (XEXP (addr, 0), var);
5537       addr = memory_address (Pmode, addr);
5538
5539       base = gen_rtx_MEM (Pmode, addr);
5540       set_mem_alias_set (base, get_frame_alias_set ());
5541       base = copy_to_reg (base);
5542 #else
5543       displacement += (FIRST_PARM_OFFSET (context) - STARTING_FRAME_OFFSET);
5544       base = lookup_static_chain (var);
5545 #endif
5546     }
5547
5548   else if (basereg == virtual_stack_vars_rtx)
5549     {
5550       /* This is the same code as lookup_static_chain, duplicated here to
5551          avoid an extra call to decl_function_context.  */
5552       tree link;
5553
5554       for (link = context_display; link; link = TREE_CHAIN (link))
5555         if (TREE_PURPOSE (link) == context)
5556           {
5557             base = RTL_EXPR_RTL (TREE_VALUE (link));
5558             break;
5559           }
5560     }
5561
5562   if (base == 0)
5563     abort ();
5564
5565   /* Use same offset, relative to appropriate static chain or argument
5566      pointer.  */
5567   return plus_constant (base, displacement);
5568 }
5569 \f
5570 /* Return the address of the trampoline for entering nested fn FUNCTION.
5571    If necessary, allocate a trampoline (in the stack frame)
5572    and emit rtl to initialize its contents (at entry to this function).  */
5573
5574 rtx
5575 trampoline_address (function)
5576      tree function;
5577 {
5578   tree link;
5579   tree rtlexp;
5580   rtx tramp;
5581   struct function *fp;
5582   tree fn_context;
5583
5584   /* Find an existing trampoline and return it.  */
5585   for (link = trampoline_list; link; link = TREE_CHAIN (link))
5586     if (TREE_PURPOSE (link) == function)
5587       return
5588         adjust_trampoline_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0));
5589
5590   for (fp = outer_function_chain; fp; fp = fp->outer)
5591     for (link = fp->x_trampoline_list; link; link = TREE_CHAIN (link))
5592       if (TREE_PURPOSE (link) == function)
5593         {
5594           tramp = fix_lexical_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0),
5595                                     function);
5596           return adjust_trampoline_addr (tramp);
5597         }
5598
5599   /* None exists; we must make one.  */
5600
5601   /* Find the `struct function' for the function containing FUNCTION.  */
5602   fp = 0;
5603   fn_context = decl_function_context (function);
5604   if (fn_context != current_function_decl
5605       && fn_context != inline_function_decl)
5606     fp = find_function_data (fn_context);
5607
5608   /* Allocate run-time space for this trampoline
5609      (usually in the defining function's stack frame).  */
5610 #ifdef ALLOCATE_TRAMPOLINE
5611   tramp = ALLOCATE_TRAMPOLINE (fp);
5612 #else
5613   /* If rounding needed, allocate extra space
5614      to ensure we have TRAMPOLINE_SIZE bytes left after rounding up.  */
5615 #ifdef TRAMPOLINE_ALIGNMENT
5616 #define TRAMPOLINE_REAL_SIZE \
5617   (TRAMPOLINE_SIZE + (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT) - 1)
5618 #else
5619 #define TRAMPOLINE_REAL_SIZE (TRAMPOLINE_SIZE)
5620 #endif
5621   tramp = assign_stack_local_1 (BLKmode, TRAMPOLINE_REAL_SIZE, 0,
5622                                 fp ? fp : cfun);
5623 #endif
5624
5625   /* Record the trampoline for reuse and note it for later initialization
5626      by expand_function_end.  */
5627   if (fp != 0)
5628     {
5629       rtlexp = make_node (RTL_EXPR);
5630       RTL_EXPR_RTL (rtlexp) = tramp;
5631       fp->x_trampoline_list = tree_cons (function, rtlexp,
5632                                          fp->x_trampoline_list);
5633     }
5634   else
5635     {
5636       /* Make the RTL_EXPR node temporary, not momentary, so that the
5637          trampoline_list doesn't become garbage.  */
5638       rtlexp = make_node (RTL_EXPR);
5639
5640       RTL_EXPR_RTL (rtlexp) = tramp;
5641       trampoline_list = tree_cons (function, rtlexp, trampoline_list);
5642     }
5643
5644   tramp = fix_lexical_addr (XEXP (tramp, 0), function);
5645   return adjust_trampoline_addr (tramp);
5646 }
5647
5648 /* Given a trampoline address,
5649    round it to multiple of TRAMPOLINE_ALIGNMENT.  */
5650
5651 static rtx
5652 round_trampoline_addr (tramp)
5653      rtx tramp;
5654 {
5655 #ifdef TRAMPOLINE_ALIGNMENT
5656   /* Round address up to desired boundary.  */
5657   rtx temp = gen_reg_rtx (Pmode);
5658   rtx addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5659   rtx mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5660
5661   temp  = expand_simple_binop (Pmode, PLUS, tramp, addend,
5662                                temp, 0, OPTAB_LIB_WIDEN);
5663   tramp = expand_simple_binop (Pmode, AND, temp, mask,
5664                                temp, 0, OPTAB_LIB_WIDEN);
5665 #endif
5666   return tramp;
5667 }
5668
5669 /* Given a trampoline address, round it then apply any
5670    platform-specific adjustments so that the result can be used for a
5671    function call .  */
5672
5673 static rtx
5674 adjust_trampoline_addr (tramp)
5675      rtx tramp;
5676 {
5677   tramp = round_trampoline_addr (tramp);
5678 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5679   TRAMPOLINE_ADJUST_ADDRESS (tramp);
5680 #endif
5681   return tramp;
5682 }
5683 \f
5684 /* Put all this function's BLOCK nodes including those that are chained
5685    onto the first block into a vector, and return it.
5686    Also store in each NOTE for the beginning or end of a block
5687    the index of that block in the vector.
5688    The arguments are BLOCK, the chain of top-level blocks of the function,
5689    and INSNS, the insn chain of the function.  */
5690
5691 void
5692 identify_blocks ()
5693 {
5694   int n_blocks;
5695   tree *block_vector, *last_block_vector;
5696   tree *block_stack;
5697   tree block = DECL_INITIAL (current_function_decl);
5698
5699   if (block == 0)
5700     return;
5701
5702   /* Fill the BLOCK_VECTOR with all of the BLOCKs in this function, in
5703      depth-first order.  */
5704   block_vector = get_block_vector (block, &n_blocks);
5705   block_stack = (tree *) xmalloc (n_blocks * sizeof (tree));
5706
5707   last_block_vector = identify_blocks_1 (get_insns (),
5708                                          block_vector + 1,
5709                                          block_vector + n_blocks,
5710                                          block_stack);
5711
5712   /* If we didn't use all of the subblocks, we've misplaced block notes.  */
5713   /* ??? This appears to happen all the time.  Latent bugs elsewhere?  */
5714   if (0 && last_block_vector != block_vector + n_blocks)
5715     abort ();
5716
5717   free (block_vector);
5718   free (block_stack);
5719 }
5720
5721 /* Subroutine of identify_blocks.  Do the block substitution on the
5722    insn chain beginning with INSNS.  Recurse for CALL_PLACEHOLDER chains.
5723
5724    BLOCK_STACK is pushed and popped for each BLOCK_BEGIN/BLOCK_END pair.
5725    BLOCK_VECTOR is incremented for each block seen.  */
5726
5727 static tree *
5728 identify_blocks_1 (insns, block_vector, end_block_vector, orig_block_stack)
5729      rtx insns;
5730      tree *block_vector;
5731      tree *end_block_vector;
5732      tree *orig_block_stack;
5733 {
5734   rtx insn;
5735   tree *block_stack = orig_block_stack;
5736
5737   for (insn = insns; insn; insn = NEXT_INSN (insn))
5738     {
5739       if (GET_CODE (insn) == NOTE)
5740         {
5741           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5742             {
5743               tree b;
5744
5745               /* If there are more block notes than BLOCKs, something
5746                  is badly wrong.  */
5747               if (block_vector == end_block_vector)
5748                 abort ();
5749
5750               b = *block_vector++;
5751               NOTE_BLOCK (insn) = b;
5752               *block_stack++ = b;
5753             }
5754           else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5755             {
5756               /* If there are more NOTE_INSN_BLOCK_ENDs than
5757                  NOTE_INSN_BLOCK_BEGs, something is badly wrong.  */
5758               if (block_stack == orig_block_stack)
5759                 abort ();
5760
5761               NOTE_BLOCK (insn) = *--block_stack;
5762             }
5763         }
5764       else if (GET_CODE (insn) == CALL_INSN
5765                && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5766         {
5767           rtx cp = PATTERN (insn);
5768
5769           block_vector = identify_blocks_1 (XEXP (cp, 0), block_vector,
5770                                             end_block_vector, block_stack);
5771           if (XEXP (cp, 1))
5772             block_vector = identify_blocks_1 (XEXP (cp, 1), block_vector,
5773                                               end_block_vector, block_stack);
5774           if (XEXP (cp, 2))
5775             block_vector = identify_blocks_1 (XEXP (cp, 2), block_vector,
5776                                               end_block_vector, block_stack);
5777         }
5778     }
5779
5780   /* If there are more NOTE_INSN_BLOCK_BEGINs than NOTE_INSN_BLOCK_ENDs,
5781      something is badly wrong.  */
5782   if (block_stack != orig_block_stack)
5783     abort ();
5784
5785   return block_vector;
5786 }
5787
5788 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
5789    and create duplicate blocks.  */
5790 /* ??? Need an option to either create block fragments or to create
5791    abstract origin duplicates of a source block.  It really depends
5792    on what optimization has been performed.  */
5793
5794 void
5795 reorder_blocks ()
5796 {
5797   tree block = DECL_INITIAL (current_function_decl);
5798   varray_type block_stack;
5799
5800   if (block == NULL_TREE)
5801     return;
5802
5803   VARRAY_TREE_INIT (block_stack, 10, "block_stack");
5804
5805   /* Reset the TREE_ASM_WRITTEN bit for all blocks.  */
5806   reorder_blocks_0 (block);
5807
5808   /* Prune the old trees away, so that they don't get in the way.  */
5809   BLOCK_SUBBLOCKS (block) = NULL_TREE;
5810   BLOCK_CHAIN (block) = NULL_TREE;
5811
5812   /* Recreate the block tree from the note nesting.  */
5813   reorder_blocks_1 (get_insns (), block, &block_stack);
5814   BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
5815
5816   /* Remove deleted blocks from the block fragment chains.  */
5817   reorder_fix_fragments (block);
5818
5819   VARRAY_FREE (block_stack);
5820 }
5821
5822 /* Helper function for reorder_blocks.  Reset TREE_ASM_WRITTEN.  */
5823
5824 static void
5825 reorder_blocks_0 (block)
5826      tree block;
5827 {
5828   while (block)
5829     {
5830       TREE_ASM_WRITTEN (block) = 0;
5831       reorder_blocks_0 (BLOCK_SUBBLOCKS (block));
5832       block = BLOCK_CHAIN (block);
5833     }
5834 }
5835
5836 static void
5837 reorder_blocks_1 (insns, current_block, p_block_stack)
5838      rtx insns;
5839      tree current_block;
5840      varray_type *p_block_stack;
5841 {
5842   rtx insn;
5843
5844   for (insn = insns; insn; insn = NEXT_INSN (insn))
5845     {
5846       if (GET_CODE (insn) == NOTE)
5847         {
5848           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5849             {
5850               tree block = NOTE_BLOCK (insn);
5851
5852               /* If we have seen this block before, that means it now
5853                  spans multiple address regions.  Create a new fragment.  */
5854               if (TREE_ASM_WRITTEN (block))
5855                 {
5856                   tree new_block = copy_node (block);
5857                   tree origin;
5858
5859                   origin = (BLOCK_FRAGMENT_ORIGIN (block)
5860                             ? BLOCK_FRAGMENT_ORIGIN (block)
5861                             : block);
5862                   BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
5863                   BLOCK_FRAGMENT_CHAIN (new_block)
5864                     = BLOCK_FRAGMENT_CHAIN (origin);
5865                   BLOCK_FRAGMENT_CHAIN (origin) = new_block;
5866
5867                   NOTE_BLOCK (insn) = new_block;
5868                   block = new_block;
5869                 }
5870
5871               BLOCK_SUBBLOCKS (block) = 0;
5872               TREE_ASM_WRITTEN (block) = 1;
5873               BLOCK_SUPERCONTEXT (block) = current_block;
5874               BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
5875               BLOCK_SUBBLOCKS (current_block) = block;
5876               current_block = block;
5877               VARRAY_PUSH_TREE (*p_block_stack, block);
5878             }
5879           else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5880             {
5881               NOTE_BLOCK (insn) = VARRAY_TOP_TREE (*p_block_stack);
5882               VARRAY_POP (*p_block_stack);
5883               BLOCK_SUBBLOCKS (current_block)
5884                 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
5885               current_block = BLOCK_SUPERCONTEXT (current_block);
5886             }
5887         }
5888       else if (GET_CODE (insn) == CALL_INSN
5889                && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5890         {
5891           rtx cp = PATTERN (insn);
5892           reorder_blocks_1 (XEXP (cp, 0), current_block, p_block_stack);
5893           if (XEXP (cp, 1))
5894             reorder_blocks_1 (XEXP (cp, 1), current_block, p_block_stack);
5895           if (XEXP (cp, 2))
5896             reorder_blocks_1 (XEXP (cp, 2), current_block, p_block_stack);
5897         }
5898     }
5899 }
5900
5901 /* Rationalize BLOCK_FRAGMENT_ORIGIN.  If an origin block no longer
5902    appears in the block tree, select one of the fragments to become
5903    the new origin block.  */
5904
5905 static void
5906 reorder_fix_fragments (block)
5907     tree block;
5908 {
5909   while (block)
5910     {
5911       tree dup_origin = BLOCK_FRAGMENT_ORIGIN (block);
5912       tree new_origin = NULL_TREE;
5913
5914       if (dup_origin)
5915         {
5916           if (! TREE_ASM_WRITTEN (dup_origin))
5917             {
5918               new_origin = BLOCK_FRAGMENT_CHAIN (dup_origin);
5919               
5920               /* Find the first of the remaining fragments.  There must
5921                  be at least one -- the current block.  */
5922               while (! TREE_ASM_WRITTEN (new_origin))
5923                 new_origin = BLOCK_FRAGMENT_CHAIN (new_origin);
5924               BLOCK_FRAGMENT_ORIGIN (new_origin) = NULL_TREE;
5925             }
5926         }
5927       else if (! dup_origin)
5928         new_origin = block;
5929
5930       /* Re-root the rest of the fragments to the new origin.  In the
5931          case that DUP_ORIGIN was null, that means BLOCK was the origin
5932          of a chain of fragments and we want to remove those fragments
5933          that didn't make it to the output.  */
5934       if (new_origin)
5935         {
5936           tree *pp = &BLOCK_FRAGMENT_CHAIN (new_origin);
5937           tree chain = *pp;
5938
5939           while (chain)
5940             {
5941               if (TREE_ASM_WRITTEN (chain))
5942                 {
5943                   BLOCK_FRAGMENT_ORIGIN (chain) = new_origin;
5944                   *pp = chain;
5945                   pp = &BLOCK_FRAGMENT_CHAIN (chain);
5946                 }
5947               chain = BLOCK_FRAGMENT_CHAIN (chain);
5948             }
5949           *pp = NULL_TREE;
5950         }
5951
5952       reorder_fix_fragments (BLOCK_SUBBLOCKS (block));
5953       block = BLOCK_CHAIN (block);
5954     }
5955 }
5956
5957 /* Reverse the order of elements in the chain T of blocks,
5958    and return the new head of the chain (old last element).  */
5959
5960 static tree
5961 blocks_nreverse (t)
5962      tree t;
5963 {
5964   tree prev = 0, decl, next;
5965   for (decl = t; decl; decl = next)
5966     {
5967       next = BLOCK_CHAIN (decl);
5968       BLOCK_CHAIN (decl) = prev;
5969       prev = decl;
5970     }
5971   return prev;
5972 }
5973
5974 /* Count the subblocks of the list starting with BLOCK.  If VECTOR is
5975    non-NULL, list them all into VECTOR, in a depth-first preorder
5976    traversal of the block tree.  Also clear TREE_ASM_WRITTEN in all
5977    blocks.  */
5978
5979 static int
5980 all_blocks (block, vector)
5981      tree block;
5982      tree *vector;
5983 {
5984   int n_blocks = 0;
5985
5986   while (block)
5987     {
5988       TREE_ASM_WRITTEN (block) = 0;
5989
5990       /* Record this block.  */
5991       if (vector)
5992         vector[n_blocks] = block;
5993
5994       ++n_blocks;
5995
5996       /* Record the subblocks, and their subblocks...  */
5997       n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
5998                               vector ? vector + n_blocks : 0);
5999       block = BLOCK_CHAIN (block);
6000     }
6001
6002   return n_blocks;
6003 }
6004
6005 /* Return a vector containing all the blocks rooted at BLOCK.  The
6006    number of elements in the vector is stored in N_BLOCKS_P.  The
6007    vector is dynamically allocated; it is the caller's responsibility
6008    to call `free' on the pointer returned.  */
6009
6010 static tree *
6011 get_block_vector (block, n_blocks_p)
6012      tree block;
6013      int *n_blocks_p;
6014 {
6015   tree *block_vector;
6016
6017   *n_blocks_p = all_blocks (block, NULL);
6018   block_vector = (tree *) xmalloc (*n_blocks_p * sizeof (tree));
6019   all_blocks (block, block_vector);
6020
6021   return block_vector;
6022 }
6023
6024 static int next_block_index = 2;
6025
6026 /* Set BLOCK_NUMBER for all the blocks in FN.  */
6027
6028 void
6029 number_blocks (fn)
6030      tree fn;
6031 {
6032   int i;
6033   int n_blocks;
6034   tree *block_vector;
6035
6036   /* For SDB and XCOFF debugging output, we start numbering the blocks
6037      from 1 within each function, rather than keeping a running
6038      count.  */
6039 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
6040   if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
6041     next_block_index = 1;
6042 #endif
6043
6044   block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
6045
6046   /* The top-level BLOCK isn't numbered at all.  */
6047   for (i = 1; i < n_blocks; ++i)
6048     /* We number the blocks from two.  */
6049     BLOCK_NUMBER (block_vector[i]) = next_block_index++;
6050
6051   free (block_vector);
6052
6053   return;
6054 }
6055 \f
6056 /* Allocate a function structure and reset its contents to the defaults.  */
6057
6058 static void
6059 prepare_function_start ()
6060 {
6061   cfun = (struct function *) ggc_alloc_cleared (sizeof (struct function));
6062
6063   init_stmt_for_function ();
6064   init_eh_for_function ();
6065
6066   cse_not_expected = ! optimize;
6067
6068   /* Caller save not needed yet.  */
6069   caller_save_needed = 0;
6070
6071   /* No stack slots have been made yet.  */
6072   stack_slot_list = 0;
6073
6074   current_function_has_nonlocal_label = 0;
6075   current_function_has_nonlocal_goto = 0;
6076
6077   /* There is no stack slot for handling nonlocal gotos.  */
6078   nonlocal_goto_handler_slots = 0;
6079   nonlocal_goto_stack_level = 0;
6080
6081   /* No labels have been declared for nonlocal use.  */
6082   nonlocal_labels = 0;
6083   nonlocal_goto_handler_labels = 0;
6084
6085   /* No function calls so far in this function.  */
6086   function_call_count = 0;
6087
6088   /* No parm regs have been allocated.
6089      (This is important for output_inline_function.)  */
6090   max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
6091
6092   /* Initialize the RTL mechanism.  */
6093   init_emit ();
6094
6095   /* Initialize the queue of pending postincrement and postdecrements,
6096      and some other info in expr.c.  */
6097   init_expr ();
6098
6099   /* We haven't done register allocation yet.  */
6100   reg_renumber = 0;
6101
6102   init_varasm_status (cfun);
6103
6104   /* Clear out data used for inlining.  */
6105   cfun->inlinable = 0;
6106   cfun->original_decl_initial = 0;
6107   cfun->original_arg_vector = 0;
6108
6109   cfun->stack_alignment_needed = STACK_BOUNDARY;
6110   cfun->preferred_stack_boundary = STACK_BOUNDARY;
6111
6112   /* Set if a call to setjmp is seen.  */
6113   current_function_calls_setjmp = 0;
6114
6115   /* Set if a call to longjmp is seen.  */
6116   current_function_calls_longjmp = 0;
6117
6118   current_function_calls_alloca = 0;
6119   current_function_contains_functions = 0;
6120   current_function_is_leaf = 0;
6121   current_function_nothrow = 0;
6122   current_function_sp_is_unchanging = 0;
6123   current_function_uses_only_leaf_regs = 0;
6124   current_function_has_computed_jump = 0;
6125   current_function_is_thunk = 0;
6126
6127   current_function_returns_pcc_struct = 0;
6128   current_function_returns_struct = 0;
6129   current_function_epilogue_delay_list = 0;
6130   current_function_uses_const_pool = 0;
6131   current_function_uses_pic_offset_table = 0;
6132   current_function_cannot_inline = 0;
6133
6134   /* We have not yet needed to make a label to jump to for tail-recursion.  */
6135   tail_recursion_label = 0;
6136
6137   /* We haven't had a need to make a save area for ap yet.  */
6138   arg_pointer_save_area = 0;
6139
6140   /* No stack slots allocated yet.  */
6141   frame_offset = 0;
6142
6143   /* No SAVE_EXPRs in this function yet.  */
6144   save_expr_regs = 0;
6145
6146   /* No RTL_EXPRs in this function yet.  */
6147   rtl_expr_chain = 0;
6148
6149   /* Set up to allocate temporaries.  */
6150   init_temp_slots ();
6151
6152   /* Indicate that we need to distinguish between the return value of the
6153      present function and the return value of a function being called.  */
6154   rtx_equal_function_value_matters = 1;
6155
6156   /* Indicate that we have not instantiated virtual registers yet.  */
6157   virtuals_instantiated = 0;
6158
6159   /* Indicate that we want CONCATs now.  */
6160   generating_concat_p = 1;
6161
6162   /* Indicate we have no need of a frame pointer yet.  */
6163   frame_pointer_needed = 0;
6164
6165   /* By default assume not varargs or stdarg.  */
6166   current_function_varargs = 0;
6167   current_function_stdarg = 0;
6168
6169   /* We haven't made any trampolines for this function yet.  */
6170   trampoline_list = 0;
6171
6172   init_pending_stack_adjust ();
6173   inhibit_defer_pop = 0;
6174
6175   current_function_outgoing_args_size = 0;
6176
6177   if (init_lang_status)
6178     (*init_lang_status) (cfun);
6179   if (init_machine_status)
6180     (*init_machine_status) (cfun);
6181 }
6182
6183 /* Initialize the rtl expansion mechanism so that we can do simple things
6184    like generate sequences.  This is used to provide a context during global
6185    initialization of some passes.  */
6186 void
6187 init_dummy_function_start ()
6188 {
6189   prepare_function_start ();
6190 }
6191
6192 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
6193    and initialize static variables for generating RTL for the statements
6194    of the function.  */
6195
6196 void
6197 init_function_start (subr, filename, line)
6198      tree subr;
6199      const char *filename;
6200      int line;
6201 {
6202   prepare_function_start ();
6203
6204   current_function_name = (*decl_printable_name) (subr, 2);
6205   cfun->decl = subr;
6206
6207   /* Nonzero if this is a nested function that uses a static chain.  */
6208
6209   current_function_needs_context
6210     = (decl_function_context (current_function_decl) != 0
6211        && ! DECL_NO_STATIC_CHAIN (current_function_decl));
6212
6213   /* Within function body, compute a type's size as soon it is laid out.  */
6214   immediate_size_expand++;
6215
6216   /* Prevent ever trying to delete the first instruction of a function.
6217      Also tell final how to output a linenum before the function prologue.
6218      Note linenums could be missing, e.g. when compiling a Java .class file.  */
6219   if (line > 0)
6220     emit_line_note (filename, line);
6221
6222   /* Make sure first insn is a note even if we don't want linenums.
6223      This makes sure the first insn will never be deleted.
6224      Also, final expects a note to appear there.  */
6225   emit_note (NULL, NOTE_INSN_DELETED);
6226
6227   /* Set flags used by final.c.  */
6228   if (aggregate_value_p (DECL_RESULT (subr)))
6229     {
6230 #ifdef PCC_STATIC_STRUCT_RETURN
6231       current_function_returns_pcc_struct = 1;
6232 #endif
6233       current_function_returns_struct = 1;
6234     }
6235
6236   /* Warn if this value is an aggregate type,
6237      regardless of which calling convention we are using for it.  */
6238   if (warn_aggregate_return
6239       && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
6240     warning ("function returns an aggregate");
6241
6242   current_function_returns_pointer
6243     = POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
6244 }
6245
6246 /* Make sure all values used by the optimization passes have sane
6247    defaults.  */
6248 void
6249 init_function_for_compilation ()
6250 {
6251   reg_renumber = 0;
6252
6253   /* No prologue/epilogue insns yet.  */
6254   VARRAY_GROW (prologue, 0);
6255   VARRAY_GROW (epilogue, 0);
6256   VARRAY_GROW (sibcall_epilogue, 0);
6257 }
6258
6259 /* Indicate that the current function uses extra args
6260    not explicitly mentioned in the argument list in any fashion.  */
6261
6262 void
6263 mark_varargs ()
6264 {
6265   current_function_varargs = 1;
6266 }
6267
6268 /* Expand a call to __main at the beginning of a possible main function.  */
6269
6270 #if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
6271 #undef HAS_INIT_SECTION
6272 #define HAS_INIT_SECTION
6273 #endif
6274
6275 void
6276 expand_main_function ()
6277 {
6278 #ifdef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
6279   if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN)
6280     {
6281       int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
6282       rtx tmp;
6283
6284       /* Forcibly align the stack.  */
6285 #ifdef STACK_GROWS_DOWNWARD
6286       tmp = expand_simple_binop (Pmode, AND, stack_pointer_rtx, GEN_INT(-align),
6287                                  stack_pointer_rtx, 1, OPTAB_WIDEN);
6288 #else
6289       tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
6290                                  GEN_INT (align - 1), NULL_RTX, 1, OPTAB_WIDEN);
6291       tmp = expand_simple_binop (Pmode, AND, tmp, GEN_INT (-align),
6292                                  stack_pointer_rtx, 1, OPTAB_WIDEN);
6293 #endif
6294       if (tmp != stack_pointer_rtx)
6295         emit_move_insn (stack_pointer_rtx, tmp);
6296       
6297       /* Enlist allocate_dynamic_stack_space to pick up the pieces.  */
6298       tmp = force_reg (Pmode, const0_rtx);
6299       allocate_dynamic_stack_space (tmp, NULL_RTX, BIGGEST_ALIGNMENT);
6300     }
6301 #endif
6302
6303 #ifndef HAS_INIT_SECTION
6304   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, NAME__MAIN), LCT_NORMAL,
6305                      VOIDmode, 0);
6306 #endif
6307 }
6308 \f
6309 extern struct obstack permanent_obstack;
6310
6311 /* The PENDING_SIZES represent the sizes of variable-sized types.
6312    Create RTL for the various sizes now (using temporary variables),
6313    so that we can refer to the sizes from the RTL we are generating
6314    for the current function.  The PENDING_SIZES are a TREE_LIST.  The
6315    TREE_VALUE of each node is a SAVE_EXPR.  */
6316
6317 void
6318 expand_pending_sizes (pending_sizes)
6319      tree pending_sizes;
6320 {
6321   tree tem;
6322
6323   /* Evaluate now the sizes of any types declared among the arguments.  */
6324   for (tem = pending_sizes; tem; tem = TREE_CHAIN (tem))
6325     {
6326       expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode, 0);
6327       /* Flush the queue in case this parameter declaration has
6328          side-effects.  */
6329       emit_queue ();
6330     }
6331 }
6332
6333 /* Start the RTL for a new function, and set variables used for
6334    emitting RTL.
6335    SUBR is the FUNCTION_DECL node.
6336    PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
6337    the function's parameters, which must be run at any return statement.  */
6338
6339 void
6340 expand_function_start (subr, parms_have_cleanups)
6341      tree subr;
6342      int parms_have_cleanups;
6343 {
6344   tree tem;
6345   rtx last_ptr = NULL_RTX;
6346
6347   /* Make sure volatile mem refs aren't considered
6348      valid operands of arithmetic insns.  */
6349   init_recog_no_volatile ();
6350
6351   current_function_instrument_entry_exit
6352     = (flag_instrument_function_entry_exit
6353        && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6354
6355   current_function_limit_stack
6356     = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
6357
6358   /* If function gets a static chain arg, store it in the stack frame.
6359      Do this first, so it gets the first stack slot offset.  */
6360   if (current_function_needs_context)
6361     {
6362       last_ptr = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
6363
6364       /* Delay copying static chain if it is not a register to avoid
6365          conflicts with regs used for parameters.  */
6366       if (! SMALL_REGISTER_CLASSES
6367           || GET_CODE (static_chain_incoming_rtx) == REG)
6368         emit_move_insn (last_ptr, static_chain_incoming_rtx);
6369     }
6370
6371   /* If the parameters of this function need cleaning up, get a label
6372      for the beginning of the code which executes those cleanups.  This must
6373      be done before doing anything with return_label.  */
6374   if (parms_have_cleanups)
6375     cleanup_label = gen_label_rtx ();
6376   else
6377     cleanup_label = 0;
6378
6379   /* Make the label for return statements to jump to.  Do not special
6380      case machines with special return instructions -- they will be
6381      handled later during jump, ifcvt, or epilogue creation.  */
6382   return_label = gen_label_rtx ();
6383
6384   /* Initialize rtx used to return the value.  */
6385   /* Do this before assign_parms so that we copy the struct value address
6386      before any library calls that assign parms might generate.  */
6387
6388   /* Decide whether to return the value in memory or in a register.  */
6389   if (aggregate_value_p (DECL_RESULT (subr)))
6390     {
6391       /* Returning something that won't go in a register.  */
6392       rtx value_address = 0;
6393
6394 #ifdef PCC_STATIC_STRUCT_RETURN
6395       if (current_function_returns_pcc_struct)
6396         {
6397           int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
6398           value_address = assemble_static_space (size);
6399         }
6400       else
6401 #endif
6402         {
6403           /* Expect to be passed the address of a place to store the value.
6404              If it is passed as an argument, assign_parms will take care of
6405              it.  */
6406           if (struct_value_incoming_rtx)
6407             {
6408               value_address = gen_reg_rtx (Pmode);
6409               emit_move_insn (value_address, struct_value_incoming_rtx);
6410             }
6411         }
6412       if (value_address)
6413         {
6414           rtx x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), value_address);
6415           set_mem_attributes (x, DECL_RESULT (subr), 1);
6416           SET_DECL_RTL (DECL_RESULT (subr), x);
6417         }
6418     }
6419   else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
6420     /* If return mode is void, this decl rtl should not be used.  */
6421     SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
6422   else
6423     {
6424       /* Compute the return values into a pseudo reg, which we will copy
6425          into the true return register after the cleanups are done.  */
6426
6427       /* In order to figure out what mode to use for the pseudo, we
6428          figure out what the mode of the eventual return register will
6429          actually be, and use that.  */
6430       rtx hard_reg
6431         = hard_function_value (TREE_TYPE (DECL_RESULT (subr)),
6432                                subr, 1);
6433
6434       /* Structures that are returned in registers are not aggregate_value_p,
6435          so we may see a PARALLEL.  Don't play pseudo games with this.  */
6436       if (! REG_P (hard_reg))
6437         SET_DECL_RTL (DECL_RESULT (subr), hard_reg);
6438       else
6439         {
6440           /* Create the pseudo.  */
6441           SET_DECL_RTL (DECL_RESULT (subr), gen_reg_rtx (GET_MODE (hard_reg)));
6442
6443           /* Needed because we may need to move this to memory
6444              in case it's a named return value whose address is taken.  */
6445           DECL_REGISTER (DECL_RESULT (subr)) = 1;
6446         }
6447     }
6448
6449   /* Initialize rtx for parameters and local variables.
6450      In some cases this requires emitting insns.  */
6451
6452   assign_parms (subr);
6453
6454   /* Copy the static chain now if it wasn't a register.  The delay is to
6455      avoid conflicts with the parameter passing registers.  */
6456
6457   if (SMALL_REGISTER_CLASSES && current_function_needs_context)
6458       if (GET_CODE (static_chain_incoming_rtx) != REG)
6459         emit_move_insn (last_ptr, static_chain_incoming_rtx);
6460
6461   /* The following was moved from init_function_start.
6462      The move is supposed to make sdb output more accurate.  */
6463   /* Indicate the beginning of the function body,
6464      as opposed to parm setup.  */
6465   emit_note (NULL, NOTE_INSN_FUNCTION_BEG);
6466
6467   if (GET_CODE (get_last_insn ()) != NOTE)
6468     emit_note (NULL, NOTE_INSN_DELETED);
6469   parm_birth_insn = get_last_insn ();
6470
6471   context_display = 0;
6472   if (current_function_needs_context)
6473     {
6474       /* Fetch static chain values for containing functions.  */
6475       tem = decl_function_context (current_function_decl);
6476       /* Copy the static chain pointer into a pseudo.  If we have
6477          small register classes, copy the value from memory if
6478          static_chain_incoming_rtx is a REG.  */
6479       if (tem)
6480         {
6481           /* If the static chain originally came in a register, put it back
6482              there, then move it out in the next insn.  The reason for
6483              this peculiar code is to satisfy function integration.  */
6484           if (SMALL_REGISTER_CLASSES
6485               && GET_CODE (static_chain_incoming_rtx) == REG)
6486             emit_move_insn (static_chain_incoming_rtx, last_ptr);
6487           last_ptr = copy_to_reg (static_chain_incoming_rtx);
6488         }
6489
6490       while (tem)
6491         {
6492           tree rtlexp = make_node (RTL_EXPR);
6493
6494           RTL_EXPR_RTL (rtlexp) = last_ptr;
6495           context_display = tree_cons (tem, rtlexp, context_display);
6496           tem = decl_function_context (tem);
6497           if (tem == 0)
6498             break;
6499           /* Chain thru stack frames, assuming pointer to next lexical frame
6500              is found at the place we always store it.  */
6501 #ifdef FRAME_GROWS_DOWNWARD
6502           last_ptr = plus_constant (last_ptr,
6503                                     -(HOST_WIDE_INT) GET_MODE_SIZE (Pmode));
6504 #endif
6505           last_ptr = gen_rtx_MEM (Pmode, memory_address (Pmode, last_ptr));
6506           set_mem_alias_set (last_ptr, get_frame_alias_set ());
6507           last_ptr = copy_to_reg (last_ptr);
6508
6509           /* If we are not optimizing, ensure that we know that this
6510              piece of context is live over the entire function.  */
6511           if (! optimize)
6512             save_expr_regs = gen_rtx_EXPR_LIST (VOIDmode, last_ptr,
6513                                                 save_expr_regs);
6514         }
6515     }
6516
6517   if (current_function_instrument_entry_exit)
6518     {
6519       rtx fun = DECL_RTL (current_function_decl);
6520       if (GET_CODE (fun) == MEM)
6521         fun = XEXP (fun, 0);
6522       else
6523         abort ();
6524       emit_library_call (profile_function_entry_libfunc, LCT_NORMAL, VOIDmode,
6525                          2, fun, Pmode,
6526                          expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6527                                                      0,
6528                                                      hard_frame_pointer_rtx),
6529                          Pmode);
6530     }
6531
6532 #ifdef PROFILE_HOOK
6533   if (profile_flag)
6534     PROFILE_HOOK (profile_label_no);
6535 #endif
6536
6537   /* After the display initializations is where the tail-recursion label
6538      should go, if we end up needing one.   Ensure we have a NOTE here
6539      since some things (like trampolines) get placed before this.  */
6540   tail_recursion_reentry = emit_note (NULL, NOTE_INSN_DELETED);
6541
6542   /* Evaluate now the sizes of any types declared among the arguments.  */
6543   expand_pending_sizes (nreverse (get_pending_sizes ()));
6544
6545   /* Make sure there is a line number after the function entry setup code.  */
6546   force_next_line_note ();
6547 }
6548 \f
6549 /* Undo the effects of init_dummy_function_start.  */
6550 void
6551 expand_dummy_function_end ()
6552 {
6553   /* End any sequences that failed to be closed due to syntax errors.  */
6554   while (in_sequence_p ())
6555     end_sequence ();
6556
6557   /* Outside function body, can't compute type's actual size
6558      until next function's body starts.  */
6559
6560   free_after_parsing (cfun);
6561   free_after_compilation (cfun);
6562   cfun = 0;
6563 }
6564
6565 /* Call DOIT for each hard register used as a return value from
6566    the current function.  */
6567
6568 void
6569 diddle_return_value (doit, arg)
6570      void (*doit) PARAMS ((rtx, void *));
6571      void *arg;
6572 {
6573   rtx outgoing = current_function_return_rtx;
6574
6575   if (! outgoing)
6576     return;
6577
6578   if (GET_CODE (outgoing) == REG)
6579     (*doit) (outgoing, arg);
6580   else if (GET_CODE (outgoing) == PARALLEL)
6581     {
6582       int i;
6583
6584       for (i = 0; i < XVECLEN (outgoing, 0); i++)
6585         {
6586           rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
6587
6588           if (GET_CODE (x) == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
6589             (*doit) (x, arg);
6590         }
6591     }
6592 }
6593
6594 static void
6595 do_clobber_return_reg (reg, arg)
6596      rtx reg;
6597      void *arg ATTRIBUTE_UNUSED;
6598 {
6599   emit_insn (gen_rtx_CLOBBER (VOIDmode, reg));
6600 }
6601
6602 void
6603 clobber_return_register ()
6604 {
6605   diddle_return_value (do_clobber_return_reg, NULL);
6606
6607   /* In case we do use pseudo to return value, clobber it too.  */
6608   if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6609     {
6610       tree decl_result = DECL_RESULT (current_function_decl);
6611       rtx decl_rtl = DECL_RTL (decl_result);
6612       if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
6613         {
6614           do_clobber_return_reg (decl_rtl, NULL);
6615         }
6616     }
6617 }
6618
6619 static void
6620 do_use_return_reg (reg, arg)
6621      rtx reg;
6622      void *arg ATTRIBUTE_UNUSED;
6623 {
6624   emit_insn (gen_rtx_USE (VOIDmode, reg));
6625 }
6626
6627 void
6628 use_return_register ()
6629 {
6630   diddle_return_value (do_use_return_reg, NULL);
6631 }
6632
6633 /* Generate RTL for the end of the current function.
6634    FILENAME and LINE are the current position in the source file.
6635
6636    It is up to language-specific callers to do cleanups for parameters--
6637    or else, supply 1 for END_BINDINGS and we will call expand_end_bindings.  */
6638
6639 void
6640 expand_function_end (filename, line, end_bindings)
6641      const char *filename;
6642      int line;
6643      int end_bindings;
6644 {
6645   tree link;
6646   rtx clobber_after;
6647
6648 #ifdef TRAMPOLINE_TEMPLATE
6649   static rtx initial_trampoline;
6650 #endif
6651
6652   finish_expr_for_function ();
6653
6654   /* If arg_pointer_save_area was referenced only from a nested
6655      function, we will not have initialized it yet.  Do that now.  */
6656   if (arg_pointer_save_area && ! cfun->arg_pointer_save_area_init)
6657     get_arg_pointer_save_area (cfun);
6658
6659 #ifdef NON_SAVING_SETJMP
6660   /* Don't put any variables in registers if we call setjmp
6661      on a machine that fails to restore the registers.  */
6662   if (NON_SAVING_SETJMP && current_function_calls_setjmp)
6663     {
6664       if (DECL_INITIAL (current_function_decl) != error_mark_node)
6665         setjmp_protect (DECL_INITIAL (current_function_decl));
6666
6667       setjmp_protect_args ();
6668     }
6669 #endif
6670
6671   /* Initialize any trampolines required by this function.  */
6672   for (link = trampoline_list; link; link = TREE_CHAIN (link))
6673     {
6674       tree function = TREE_PURPOSE (link);
6675       rtx context ATTRIBUTE_UNUSED = lookup_static_chain (function);
6676       rtx tramp = RTL_EXPR_RTL (TREE_VALUE (link));
6677 #ifdef TRAMPOLINE_TEMPLATE
6678       rtx blktramp;
6679 #endif
6680       rtx seq;
6681
6682 #ifdef TRAMPOLINE_TEMPLATE
6683       /* First make sure this compilation has a template for
6684          initializing trampolines.  */
6685       if (initial_trampoline == 0)
6686         {
6687           initial_trampoline
6688             = gen_rtx_MEM (BLKmode, assemble_trampoline_template ());
6689           set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
6690
6691           ggc_add_rtx_root (&initial_trampoline, 1);
6692         }
6693 #endif
6694
6695       /* Generate insns to initialize the trampoline.  */
6696       start_sequence ();
6697       tramp = round_trampoline_addr (XEXP (tramp, 0));
6698 #ifdef TRAMPOLINE_TEMPLATE
6699       blktramp = replace_equiv_address (initial_trampoline, tramp);
6700       emit_block_move (blktramp, initial_trampoline,
6701                        GEN_INT (TRAMPOLINE_SIZE));
6702 #endif
6703       INITIALIZE_TRAMPOLINE (tramp, XEXP (DECL_RTL (function), 0), context);
6704       seq = get_insns ();
6705       end_sequence ();
6706
6707       /* Put those insns at entry to the containing function (this one).  */
6708       emit_insns_before (seq, tail_recursion_reentry);
6709     }
6710
6711   /* If we are doing stack checking and this function makes calls,
6712      do a stack probe at the start of the function to ensure we have enough
6713      space for another stack frame.  */
6714   if (flag_stack_check && ! STACK_CHECK_BUILTIN)
6715     {
6716       rtx insn, seq;
6717
6718       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6719         if (GET_CODE (insn) == CALL_INSN)
6720           {
6721             start_sequence ();
6722             probe_stack_range (STACK_CHECK_PROTECT,
6723                                GEN_INT (STACK_CHECK_MAX_FRAME_SIZE));
6724             seq = get_insns ();
6725             end_sequence ();
6726             emit_insns_before (seq, tail_recursion_reentry);
6727             break;
6728           }
6729     }
6730
6731   /* Warn about unused parms if extra warnings were specified.  */
6732   /* Either ``-W -Wunused'' or ``-Wunused-parameter'' enables this
6733      warning.  WARN_UNUSED_PARAMETER is negative when set by
6734      -Wunused.  */
6735   if (warn_unused_parameter > 0
6736       || (warn_unused_parameter < 0 && extra_warnings))
6737     {
6738       tree decl;
6739
6740       for (decl = DECL_ARGUMENTS (current_function_decl);
6741            decl; decl = TREE_CHAIN (decl))
6742         if (! TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
6743             && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
6744           warning_with_decl (decl, "unused parameter `%s'");
6745     }
6746
6747   /* Delete handlers for nonlocal gotos if nothing uses them.  */
6748   if (nonlocal_goto_handler_slots != 0
6749       && ! current_function_has_nonlocal_label)
6750     delete_handlers ();
6751
6752   /* End any sequences that failed to be closed due to syntax errors.  */
6753   while (in_sequence_p ())
6754     end_sequence ();
6755
6756   /* Outside function body, can't compute type's actual size
6757      until next function's body starts.  */
6758   immediate_size_expand--;
6759
6760   clear_pending_stack_adjust ();
6761   do_pending_stack_adjust ();
6762
6763   /* Mark the end of the function body.
6764      If control reaches this insn, the function can drop through
6765      without returning a value.  */
6766   emit_note (NULL, NOTE_INSN_FUNCTION_END);
6767
6768   /* Must mark the last line number note in the function, so that the test
6769      coverage code can avoid counting the last line twice.  This just tells
6770      the code to ignore the immediately following line note, since there
6771      already exists a copy of this note somewhere above.  This line number
6772      note is still needed for debugging though, so we can't delete it.  */
6773   if (flag_test_coverage)
6774     emit_note (NULL, NOTE_INSN_REPEATED_LINE_NUMBER);
6775
6776   /* Output a linenumber for the end of the function.
6777      SDB depends on this.  */
6778   emit_line_note_force (filename, line);
6779
6780   /* Before the return label (if any), clobber the return
6781      registers so that they are not propagated live to the rest of
6782      the function.  This can only happen with functions that drop
6783      through; if there had been a return statement, there would
6784      have either been a return rtx, or a jump to the return label.
6785
6786      We delay actual code generation after the current_function_value_rtx
6787      is computed.  */
6788   clobber_after = get_last_insn ();
6789
6790   /* Output the label for the actual return from the function,
6791      if one is expected.  This happens either because a function epilogue
6792      is used instead of a return instruction, or because a return was done
6793      with a goto in order to run local cleanups, or because of pcc-style
6794      structure returning.  */
6795   if (return_label)
6796     emit_label (return_label);
6797
6798   /* C++ uses this.  */
6799   if (end_bindings)
6800     expand_end_bindings (0, 0, 0);
6801
6802   if (current_function_instrument_entry_exit)
6803     {
6804       rtx fun = DECL_RTL (current_function_decl);
6805       if (GET_CODE (fun) == MEM)
6806         fun = XEXP (fun, 0);
6807       else
6808         abort ();
6809       emit_library_call (profile_function_exit_libfunc, LCT_NORMAL, VOIDmode,
6810                          2, fun, Pmode,
6811                          expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6812                                                      0,
6813                                                      hard_frame_pointer_rtx),
6814                          Pmode);
6815     }
6816
6817   /* Let except.c know where it should emit the call to unregister
6818      the function context for sjlj exceptions.  */
6819   if (flag_exceptions && USING_SJLJ_EXCEPTIONS)
6820     sjlj_emit_function_exit_after (get_last_insn ());
6821
6822   /* If we had calls to alloca, and this machine needs
6823      an accurate stack pointer to exit the function,
6824      insert some code to save and restore the stack pointer.  */
6825 #ifdef EXIT_IGNORE_STACK
6826   if (! EXIT_IGNORE_STACK)
6827 #endif
6828     if (current_function_calls_alloca)
6829       {
6830         rtx tem = 0;
6831
6832         emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
6833         emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
6834       }
6835
6836   /* If scalar return value was computed in a pseudo-reg, or was a named
6837      return value that got dumped to the stack, copy that to the hard
6838      return register.  */
6839   if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6840     {
6841       tree decl_result = DECL_RESULT (current_function_decl);
6842       rtx decl_rtl = DECL_RTL (decl_result);
6843
6844       if (REG_P (decl_rtl)
6845           ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
6846           : DECL_REGISTER (decl_result))
6847         {
6848           rtx real_decl_rtl;
6849
6850 #ifdef FUNCTION_OUTGOING_VALUE
6851           real_decl_rtl = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl_result),
6852                                                    current_function_decl);
6853 #else
6854           real_decl_rtl = FUNCTION_VALUE (TREE_TYPE (decl_result),
6855                                           current_function_decl);
6856 #endif
6857           REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
6858
6859           /* If this is a BLKmode structure being returned in registers,
6860              then use the mode computed in expand_return.  Note that if
6861              decl_rtl is memory, then its mode may have been changed, 
6862              but that current_function_return_rtx has not.  */
6863           if (GET_MODE (real_decl_rtl) == BLKmode)
6864             PUT_MODE (real_decl_rtl, GET_MODE (current_function_return_rtx));
6865
6866           /* If a named return value dumped decl_return to memory, then
6867              we may need to re-do the PROMOTE_MODE signed/unsigned 
6868              extension.  */
6869           if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
6870             {
6871               int unsignedp = TREE_UNSIGNED (TREE_TYPE (decl_result));
6872
6873 #ifdef PROMOTE_FUNCTION_RETURN
6874               promote_mode (TREE_TYPE (decl_result), GET_MODE (decl_rtl),
6875                             &unsignedp, 1);
6876 #endif
6877
6878               convert_move (real_decl_rtl, decl_rtl, unsignedp);
6879             }
6880           else if (GET_CODE (real_decl_rtl) == PARALLEL)
6881             emit_group_load (real_decl_rtl, decl_rtl,
6882                              int_size_in_bytes (TREE_TYPE (decl_result)));
6883           else
6884             emit_move_insn (real_decl_rtl, decl_rtl);
6885
6886           /* The delay slot scheduler assumes that current_function_return_rtx
6887              holds the hard register containing the return value, not a
6888              temporary pseudo.  */
6889           current_function_return_rtx = real_decl_rtl;
6890         }
6891     }
6892
6893   /* If returning a structure, arrange to return the address of the value
6894      in a place where debuggers expect to find it.
6895
6896      If returning a structure PCC style,
6897      the caller also depends on this value.
6898      And current_function_returns_pcc_struct is not necessarily set.  */
6899   if (current_function_returns_struct
6900       || current_function_returns_pcc_struct)
6901     {
6902       rtx value_address
6903         = XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6904       tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
6905 #ifdef FUNCTION_OUTGOING_VALUE
6906       rtx outgoing
6907         = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
6908                                    current_function_decl);
6909 #else
6910       rtx outgoing
6911         = FUNCTION_VALUE (build_pointer_type (type), current_function_decl);
6912 #endif
6913
6914       /* Mark this as a function return value so integrate will delete the
6915          assignment and USE below when inlining this function.  */
6916       REG_FUNCTION_VALUE_P (outgoing) = 1;
6917
6918 #ifdef POINTERS_EXTEND_UNSIGNED
6919       /* The address may be ptr_mode and OUTGOING may be Pmode.  */
6920       if (GET_MODE (outgoing) != GET_MODE (value_address))
6921         value_address = convert_memory_address (GET_MODE (outgoing),
6922                                                 value_address);
6923 #endif
6924
6925       emit_move_insn (outgoing, value_address);
6926
6927       /* Show return register used to hold result (in this case the address
6928          of the result.  */
6929       current_function_return_rtx = outgoing;
6930     }
6931
6932   /* If this is an implementation of throw, do what's necessary to
6933      communicate between __builtin_eh_return and the epilogue.  */
6934   expand_eh_return ();
6935
6936   /* Emit the actual code to clobber return register.  */
6937   {
6938     rtx seq, after;
6939     
6940     start_sequence ();
6941     clobber_return_register ();
6942     seq = gen_sequence ();
6943     end_sequence ();
6944
6945     after = emit_insn_after (seq, clobber_after);
6946     
6947     if (clobber_after != after)
6948       cfun->x_clobber_return_insn = after;
6949   }
6950
6951   /* ??? This should no longer be necessary since stupid is no longer with
6952      us, but there are some parts of the compiler (eg reload_combine, and
6953      sh mach_dep_reorg) that still try and compute their own lifetime info
6954      instead of using the general framework.  */
6955   use_return_register ();
6956
6957   /* Fix up any gotos that jumped out to the outermost
6958      binding level of the function.
6959      Must follow emitting RETURN_LABEL.  */
6960
6961   /* If you have any cleanups to do at this point,
6962      and they need to create temporary variables,
6963      then you will lose.  */
6964   expand_fixups (get_insns ());
6965 }
6966
6967 rtx
6968 get_arg_pointer_save_area (f)
6969      struct function *f;
6970 {
6971   rtx ret = f->x_arg_pointer_save_area;
6972
6973   if (! ret)
6974     {
6975       ret = assign_stack_local_1 (Pmode, GET_MODE_SIZE (Pmode), 0, f);
6976       f->x_arg_pointer_save_area = ret;
6977     }
6978
6979   if (f == cfun && ! f->arg_pointer_save_area_init)
6980     {
6981       rtx seq;
6982
6983       /* Save the arg pointer at the beginning of the function.  The 
6984          generated stack slot may not be a valid memory address, so we
6985          have to check it and fix it if necessary.  */
6986       start_sequence ();
6987       emit_move_insn (validize_mem (ret), virtual_incoming_args_rtx);
6988       seq = gen_sequence ();
6989       end_sequence ();
6990
6991       push_topmost_sequence ();
6992       emit_insn_after (seq, get_insns ());
6993       pop_topmost_sequence ();
6994     }
6995
6996   return ret;
6997 }
6998 \f
6999 /* Extend a vector that records the INSN_UIDs of INSNS (either a
7000    sequence or a single insn).  */
7001
7002 static void
7003 record_insns (insns, vecp)
7004      rtx insns;
7005      varray_type *vecp;
7006 {
7007   if (GET_CODE (insns) == SEQUENCE)
7008     {
7009       int len = XVECLEN (insns, 0);
7010       int i = VARRAY_SIZE (*vecp);
7011
7012       VARRAY_GROW (*vecp, i + len);
7013       while (--len >= 0)
7014         {
7015           VARRAY_INT (*vecp, i) = INSN_UID (XVECEXP (insns, 0, len));
7016           ++i;
7017         }
7018     }
7019   else
7020     {
7021       int i = VARRAY_SIZE (*vecp);
7022       VARRAY_GROW (*vecp, i + 1);
7023       VARRAY_INT (*vecp, i) = INSN_UID (insns);
7024     }
7025 }
7026
7027 /* Determine how many INSN_UIDs in VEC are part of INSN.  */
7028
7029 static int
7030 contains (insn, vec)
7031      rtx insn;
7032      varray_type vec;
7033 {
7034   int i, j;
7035
7036   if (GET_CODE (insn) == INSN
7037       && GET_CODE (PATTERN (insn)) == SEQUENCE)
7038     {
7039       int count = 0;
7040       for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
7041         for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7042           if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == VARRAY_INT (vec, j))
7043             count++;
7044       return count;
7045     }
7046   else
7047     {
7048       for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7049         if (INSN_UID (insn) == VARRAY_INT (vec, j))
7050           return 1;
7051     }
7052   return 0;
7053 }
7054
7055 int
7056 prologue_epilogue_contains (insn)
7057      rtx insn;
7058 {
7059   if (contains (insn, prologue))
7060     return 1;
7061   if (contains (insn, epilogue))
7062     return 1;
7063   return 0;
7064 }
7065
7066 int
7067 sibcall_epilogue_contains (insn)
7068      rtx insn;
7069 {
7070   if (sibcall_epilogue)
7071     return contains (insn, sibcall_epilogue);
7072   return 0;
7073 }
7074
7075 #ifdef HAVE_return
7076 /* Insert gen_return at the end of block BB.  This also means updating
7077    block_for_insn appropriately.  */
7078
7079 static void
7080 emit_return_into_block (bb, line_note)
7081      basic_block bb;
7082      rtx line_note;
7083 {
7084   rtx p, end;
7085
7086   p = NEXT_INSN (bb->end);
7087   end = emit_jump_insn_after (gen_return (), bb->end);
7088   if (line_note)
7089     emit_line_note_after (NOTE_SOURCE_FILE (line_note),
7090                           NOTE_LINE_NUMBER (line_note), PREV_INSN (bb->end));
7091 }
7092 #endif /* HAVE_return */
7093
7094 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
7095
7096 /* These functions convert the epilogue into a variant that does not modify the
7097    stack pointer.  This is used in cases where a function returns an object
7098    whose size is not known until it is computed.  The called function leaves the
7099    object on the stack, leaves the stack depressed, and returns a pointer to
7100    the object.
7101
7102    What we need to do is track all modifications and references to the stack
7103    pointer, deleting the modifications and changing the references to point to
7104    the location the stack pointer would have pointed to had the modifications
7105    taken place.
7106
7107    These functions need to be portable so we need to make as few assumptions
7108    about the epilogue as we can.  However, the epilogue basically contains
7109    three things: instructions to reset the stack pointer, instructions to
7110    reload registers, possibly including the frame pointer, and an
7111    instruction to return to the caller.
7112
7113    If we can't be sure of what a relevant epilogue insn is doing, we abort.
7114    We also make no attempt to validate the insns we make since if they are
7115    invalid, we probably can't do anything valid.  The intent is that these
7116    routines get "smarter" as more and more machines start to use them and
7117    they try operating on different epilogues.
7118
7119    We use the following structure to track what the part of the epilogue that
7120    we've already processed has done.  We keep two copies of the SP equivalence,
7121    one for use during the insn we are processing and one for use in the next
7122    insn.  The difference is because one part of a PARALLEL may adjust SP
7123    and the other may use it.  */
7124
7125 struct epi_info
7126 {
7127   rtx sp_equiv_reg;             /* REG that SP is set from, perhaps SP.  */
7128   HOST_WIDE_INT sp_offset;      /* Offset from SP_EQUIV_REG of present SP.  */
7129   rtx new_sp_equiv_reg;         /* REG to be used at end of insn.   */
7130   HOST_WIDE_INT new_sp_offset;  /* Offset to be used at end of insn.  */
7131   rtx equiv_reg_src;            /* If nonzero, the value that SP_EQUIV_REG
7132                                    should be set to once we no longer need
7133                                    its value.  */
7134 };
7135
7136 static void handle_epilogue_set PARAMS ((rtx, struct epi_info *));
7137 static void emit_equiv_load PARAMS ((struct epi_info *));
7138
7139 /* Modify SEQ, a SEQUENCE that is part of the epilogue, to no modifications
7140    to the stack pointer.  Return the new sequence.  */
7141
7142 static rtx
7143 keep_stack_depressed (seq)
7144      rtx seq;
7145 {
7146   int i, j;
7147   struct epi_info info;
7148
7149   /* If the epilogue is just a single instruction, it ust be OK as is.   */
7150
7151   if (GET_CODE (seq) != SEQUENCE)
7152     return seq;
7153
7154   /* Otherwise, start a sequence, initialize the information we have, and
7155      process all the insns we were given.  */
7156   start_sequence ();
7157
7158   info.sp_equiv_reg = stack_pointer_rtx;
7159   info.sp_offset = 0;
7160   info.equiv_reg_src = 0;
7161
7162   for (i = 0; i < XVECLEN (seq, 0); i++)
7163     {
7164       rtx insn = XVECEXP (seq, 0, i);
7165
7166       if (!INSN_P (insn))
7167         {
7168           add_insn (insn);
7169           continue;
7170         }
7171
7172       /* If this insn references the register that SP is equivalent to and
7173          we have a pending load to that register, we must force out the load
7174          first and then indicate we no longer know what SP's equivalent is.  */
7175       if (info.equiv_reg_src != 0
7176           && reg_referenced_p (info.sp_equiv_reg, PATTERN (insn)))
7177         {
7178           emit_equiv_load (&info);
7179           info.sp_equiv_reg = 0;
7180         }
7181
7182       info.new_sp_equiv_reg = info.sp_equiv_reg;
7183       info.new_sp_offset = info.sp_offset;
7184
7185       /* If this is a (RETURN) and the return address is on the stack,
7186          update the address and change to an indirect jump.  */
7187       if (GET_CODE (PATTERN (insn)) == RETURN
7188           || (GET_CODE (PATTERN (insn)) == PARALLEL
7189               && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == RETURN))
7190         {
7191           rtx retaddr = INCOMING_RETURN_ADDR_RTX;
7192           rtx base = 0;
7193           HOST_WIDE_INT offset = 0;
7194           rtx jump_insn, jump_set;
7195
7196           /* If the return address is in a register, we can emit the insn
7197              unchanged.  Otherwise, it must be a MEM and we see what the
7198              base register and offset are.  In any case, we have to emit any
7199              pending load to the equivalent reg of SP, if any.  */
7200           if (GET_CODE (retaddr) == REG)
7201             {
7202               emit_equiv_load (&info);
7203               add_insn (insn);
7204               continue;
7205             }
7206           else if (GET_CODE (retaddr) == MEM
7207                    && GET_CODE (XEXP (retaddr, 0)) == REG)
7208             base = gen_rtx_REG (Pmode, REGNO (XEXP (retaddr, 0))), offset = 0;
7209           else if (GET_CODE (retaddr) == MEM
7210                    && GET_CODE (XEXP (retaddr, 0)) == PLUS
7211                    && GET_CODE (XEXP (XEXP (retaddr, 0), 0)) == REG
7212                    && GET_CODE (XEXP (XEXP (retaddr, 0), 1)) == CONST_INT)
7213             {
7214               base = gen_rtx_REG (Pmode, REGNO (XEXP (XEXP (retaddr, 0), 0)));
7215               offset = INTVAL (XEXP (XEXP (retaddr, 0), 1));
7216             }
7217           else
7218             abort ();
7219
7220           /* If the base of the location containing the return pointer
7221              is SP, we must update it with the replacement address.  Otherwise,
7222              just build the necessary MEM.  */
7223           retaddr = plus_constant (base, offset);
7224           if (base == stack_pointer_rtx)
7225             retaddr = simplify_replace_rtx (retaddr, stack_pointer_rtx,
7226                                             plus_constant (info.sp_equiv_reg,
7227                                                            info.sp_offset));
7228
7229           retaddr = gen_rtx_MEM (Pmode, retaddr);
7230
7231           /* If there is a pending load to the equivalent register for SP
7232              and we reference that register, we must load our address into
7233              a scratch register and then do that load.  */
7234           if (info.equiv_reg_src
7235               && reg_overlap_mentioned_p (info.equiv_reg_src, retaddr))
7236             {
7237               unsigned int regno;
7238               rtx reg;
7239
7240               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
7241                 if (HARD_REGNO_MODE_OK (regno, Pmode)
7242                     && !fixed_regs[regno]
7243                     && TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)
7244                     && !REGNO_REG_SET_P (EXIT_BLOCK_PTR->global_live_at_start,
7245                                          regno)
7246                     && !refers_to_regno_p (regno,
7247                                            regno + HARD_REGNO_NREGS (regno,
7248                                                                      Pmode),
7249                                            info.equiv_reg_src, NULL))
7250                   break;
7251
7252               if (regno == FIRST_PSEUDO_REGISTER)
7253                 abort ();
7254
7255               reg = gen_rtx_REG (Pmode, regno);
7256               emit_move_insn (reg, retaddr);
7257               retaddr = reg;
7258             }
7259
7260           emit_equiv_load (&info);
7261           jump_insn = emit_jump_insn (gen_indirect_jump (retaddr));
7262
7263           /* Show the SET in the above insn is a RETURN.  */
7264           jump_set = single_set (jump_insn);
7265           if (jump_set == 0)
7266             abort ();
7267           else
7268             SET_IS_RETURN_P (jump_set) = 1;
7269         }
7270
7271       /* If SP is not mentioned in the pattern and its equivalent register, if
7272          any, is not modified, just emit it.  Otherwise, if neither is set,
7273          replace the reference to SP and emit the insn.  If none of those are
7274          true, handle each SET individually.  */
7275       else if (!reg_mentioned_p (stack_pointer_rtx, PATTERN (insn))
7276                && (info.sp_equiv_reg == stack_pointer_rtx
7277                    || !reg_set_p (info.sp_equiv_reg, insn)))
7278         add_insn (insn);
7279       else if (! reg_set_p (stack_pointer_rtx, insn)
7280                && (info.sp_equiv_reg == stack_pointer_rtx
7281                    || !reg_set_p (info.sp_equiv_reg, insn)))
7282         {
7283           if (! validate_replace_rtx (stack_pointer_rtx,
7284                                       plus_constant (info.sp_equiv_reg,
7285                                                      info.sp_offset),
7286                                       insn))
7287             abort ();
7288
7289           add_insn (insn);
7290         }
7291       else if (GET_CODE (PATTERN (insn)) == SET)
7292         handle_epilogue_set (PATTERN (insn), &info);
7293       else if (GET_CODE (PATTERN (insn)) == PARALLEL)
7294         {
7295           for (j = 0; j < XVECLEN (PATTERN (insn), 0); j++)
7296             if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET)
7297               handle_epilogue_set (XVECEXP (PATTERN (insn), 0, j), &info);
7298         }
7299       else
7300         add_insn (insn);
7301
7302       info.sp_equiv_reg = info.new_sp_equiv_reg;
7303       info.sp_offset = info.new_sp_offset;
7304     }
7305
7306   seq = gen_sequence ();
7307   end_sequence ();
7308   return seq;
7309 }
7310
7311 /* SET is a SET from an insn in the epilogue.  P is a pointer to the epi_info
7312    structure that contains information about what we've seen so far.  We
7313    process this SET by either updating that data or by emitting one or 
7314    more insns.  */
7315
7316 static void
7317 handle_epilogue_set (set, p)
7318      rtx set;
7319      struct epi_info *p;
7320 {
7321   /* First handle the case where we are setting SP.  Record what it is being
7322      set from.  If unknown, abort.  */
7323   if (reg_set_p (stack_pointer_rtx, set))
7324     {
7325       if (SET_DEST (set) != stack_pointer_rtx)
7326         abort ();
7327
7328       if (GET_CODE (SET_SRC (set)) == PLUS
7329           && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
7330         {
7331           p->new_sp_equiv_reg = XEXP (SET_SRC (set), 0);
7332           p->new_sp_offset = INTVAL (XEXP (SET_SRC (set), 1));
7333         }
7334       else
7335         p->new_sp_equiv_reg = SET_SRC (set), p->new_sp_offset = 0;
7336
7337       /* If we are adjusting SP, we adjust from the old data.  */
7338       if (p->new_sp_equiv_reg == stack_pointer_rtx)
7339         {
7340           p->new_sp_equiv_reg = p->sp_equiv_reg;
7341           p->new_sp_offset += p->sp_offset;
7342         }
7343
7344       if (p->new_sp_equiv_reg == 0 || GET_CODE (p->new_sp_equiv_reg) != REG)
7345         abort ();
7346
7347       return;
7348     }
7349
7350   /* Next handle the case where we are setting SP's equivalent register.
7351      If we already have a value to set it to, abort.  We could update, but
7352      there seems little point in handling that case.  Note that we have
7353      to allow for the case where we are setting the register set in
7354      the previous part of a PARALLEL inside a single insn.  But use the
7355      old offset for any updates within this insn.  */
7356   else if (p->new_sp_equiv_reg != 0 && reg_set_p (p->new_sp_equiv_reg, set))
7357     {
7358       if (!rtx_equal_p (p->new_sp_equiv_reg, SET_DEST (set))
7359           || p->equiv_reg_src != 0)
7360         abort ();
7361       else
7362         p->equiv_reg_src
7363           = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7364                                   plus_constant (p->sp_equiv_reg,
7365                                                  p->sp_offset));
7366     }
7367
7368   /* Otherwise, replace any references to SP in the insn to its new value
7369      and emit the insn.  */
7370   else
7371     {
7372       SET_SRC (set) = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7373                                             plus_constant (p->sp_equiv_reg,
7374                                                            p->sp_offset));
7375       SET_DEST (set) = simplify_replace_rtx (SET_DEST (set), stack_pointer_rtx,
7376                                              plus_constant (p->sp_equiv_reg,
7377                                                             p->sp_offset));
7378       emit_insn (set);
7379     }
7380 }
7381
7382 /* Emit an insn to do the load shown in p->equiv_reg_src, if needed.  */
7383
7384 static void
7385 emit_equiv_load (p)
7386      struct epi_info *p;
7387 {
7388   if (p->equiv_reg_src != 0)
7389     emit_move_insn (p->sp_equiv_reg, p->equiv_reg_src);
7390
7391   p->equiv_reg_src = 0;
7392 }
7393 #endif
7394
7395 /* Generate the prologue and epilogue RTL if the machine supports it.  Thread
7396    this into place with notes indicating where the prologue ends and where
7397    the epilogue begins.  Update the basic block information when possible.  */
7398
7399 void
7400 thread_prologue_and_epilogue_insns (f)
7401      rtx f ATTRIBUTE_UNUSED;
7402 {
7403   int inserted = 0;
7404   edge e;
7405 #if defined (HAVE_sibcall_epilogue) || defined (HAVE_epilogue) || defined (HAVE_return) || defined (HAVE_prologue)
7406   rtx seq;
7407 #endif
7408 #ifdef HAVE_prologue
7409   rtx prologue_end = NULL_RTX;
7410 #endif
7411 #if defined (HAVE_epilogue) || defined(HAVE_return)
7412   rtx epilogue_end = NULL_RTX;
7413 #endif
7414
7415 #ifdef HAVE_prologue
7416   if (HAVE_prologue)
7417     {
7418       start_sequence ();
7419       seq = gen_prologue ();
7420       emit_insn (seq);
7421
7422       /* Retain a map of the prologue insns.  */
7423       if (GET_CODE (seq) != SEQUENCE)
7424         seq = get_insns ();
7425       record_insns (seq, &prologue);
7426       prologue_end = emit_note (NULL, NOTE_INSN_PROLOGUE_END);
7427
7428       seq = gen_sequence ();
7429       end_sequence ();
7430
7431       /* Can't deal with multiple successors of the entry block
7432          at the moment.  Function should always have at least one
7433          entry point.  */
7434       if (!ENTRY_BLOCK_PTR->succ || ENTRY_BLOCK_PTR->succ->succ_next)
7435         abort ();
7436
7437       insert_insn_on_edge (seq, ENTRY_BLOCK_PTR->succ);
7438       inserted = 1;
7439     }
7440 #endif
7441
7442   /* If the exit block has no non-fake predecessors, we don't need
7443      an epilogue.  */
7444   for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7445     if ((e->flags & EDGE_FAKE) == 0)
7446       break;
7447   if (e == NULL)
7448     goto epilogue_done;
7449
7450 #ifdef HAVE_return
7451   if (optimize && HAVE_return)
7452     {
7453       /* If we're allowed to generate a simple return instruction,
7454          then by definition we don't need a full epilogue.  Examine
7455          the block that falls through to EXIT.   If it does not
7456          contain any code, examine its predecessors and try to
7457          emit (conditional) return instructions.  */
7458
7459       basic_block last;
7460       edge e_next;
7461       rtx label;
7462
7463       for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7464         if (e->flags & EDGE_FALLTHRU)
7465           break;
7466       if (e == NULL)
7467         goto epilogue_done;
7468       last = e->src;
7469
7470       /* Verify that there are no active instructions in the last block.  */
7471       label = last->end;
7472       while (label && GET_CODE (label) != CODE_LABEL)
7473         {
7474           if (active_insn_p (label))
7475             break;
7476           label = PREV_INSN (label);
7477         }
7478
7479       if (last->head == label && GET_CODE (label) == CODE_LABEL)
7480         {
7481           rtx epilogue_line_note = NULL_RTX;
7482
7483           /* Locate the line number associated with the closing brace,
7484              if we can find one.  */
7485           for (seq = get_last_insn ();
7486                seq && ! active_insn_p (seq);
7487                seq = PREV_INSN (seq))
7488             if (GET_CODE (seq) == NOTE && NOTE_LINE_NUMBER (seq) > 0)
7489               {
7490                 epilogue_line_note = seq;
7491                 break;
7492               }
7493
7494           for (e = last->pred; e; e = e_next)
7495             {
7496               basic_block bb = e->src;
7497               rtx jump;
7498
7499               e_next = e->pred_next;
7500               if (bb == ENTRY_BLOCK_PTR)
7501                 continue;
7502
7503               jump = bb->end;
7504               if ((GET_CODE (jump) != JUMP_INSN) || JUMP_LABEL (jump) != label)
7505                 continue;
7506
7507               /* If we have an unconditional jump, we can replace that
7508                  with a simple return instruction.  */
7509               if (simplejump_p (jump))
7510                 {
7511                   emit_return_into_block (bb, epilogue_line_note);
7512                   delete_insn (jump);
7513                 }
7514
7515               /* If we have a conditional jump, we can try to replace
7516                  that with a conditional return instruction.  */
7517               else if (condjump_p (jump))
7518                 {
7519                   rtx ret, *loc;
7520
7521                   ret = SET_SRC (PATTERN (jump));
7522                   if (GET_CODE (XEXP (ret, 1)) == LABEL_REF)
7523                     loc = &XEXP (ret, 1);
7524                   else
7525                     loc = &XEXP (ret, 2);
7526                   ret = gen_rtx_RETURN (VOIDmode);
7527
7528                   if (! validate_change (jump, loc, ret, 0))
7529                     continue;
7530                   if (JUMP_LABEL (jump))
7531                     LABEL_NUSES (JUMP_LABEL (jump))--;
7532
7533                   /* If this block has only one successor, it both jumps
7534                      and falls through to the fallthru block, so we can't
7535                      delete the edge.  */
7536                   if (bb->succ->succ_next == NULL)
7537                     continue;
7538                 }
7539               else
7540                 continue;
7541
7542               /* Fix up the CFG for the successful change we just made.  */
7543               redirect_edge_succ (e, EXIT_BLOCK_PTR);
7544             }
7545
7546           /* Emit a return insn for the exit fallthru block.  Whether
7547              this is still reachable will be determined later.  */
7548
7549           emit_barrier_after (last->end);
7550           emit_return_into_block (last, epilogue_line_note);
7551           epilogue_end = last->end;
7552           last->succ->flags &= ~EDGE_FALLTHRU;
7553           goto epilogue_done;
7554         }
7555     }
7556 #endif
7557 #ifdef HAVE_epilogue
7558   if (HAVE_epilogue)
7559     {
7560       /* Find the edge that falls through to EXIT.  Other edges may exist
7561          due to RETURN instructions, but those don't need epilogues.
7562          There really shouldn't be a mixture -- either all should have
7563          been converted or none, however...  */
7564
7565       for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7566         if (e->flags & EDGE_FALLTHRU)
7567           break;
7568       if (e == NULL)
7569         goto epilogue_done;
7570
7571       start_sequence ();
7572       epilogue_end = emit_note (NULL, NOTE_INSN_EPILOGUE_BEG);
7573
7574       seq = gen_epilogue ();
7575
7576 #ifdef INCOMING_RETURN_ADDR_RTX
7577       /* If this function returns with the stack depressed and we can support
7578          it, massage the epilogue to actually do that.  */
7579       if (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
7580           && TYPE_RETURNS_STACK_DEPRESSED (TREE_TYPE (current_function_decl)))
7581         seq = keep_stack_depressed (seq);
7582 #endif
7583
7584       emit_jump_insn (seq);
7585
7586       /* Retain a map of the epilogue insns.  */
7587       if (GET_CODE (seq) != SEQUENCE)
7588         seq = get_insns ();
7589       record_insns (seq, &epilogue);
7590
7591       seq = gen_sequence ();
7592       end_sequence ();
7593
7594       insert_insn_on_edge (seq, e);
7595       inserted = 1;
7596     }
7597 #endif
7598 epilogue_done:
7599
7600   if (inserted)
7601     commit_edge_insertions ();
7602
7603 #ifdef HAVE_sibcall_epilogue
7604   /* Emit sibling epilogues before any sibling call sites.  */
7605   for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7606     {
7607       basic_block bb = e->src;
7608       rtx insn = bb->end;
7609       rtx i;
7610       rtx newinsn;
7611
7612       if (GET_CODE (insn) != CALL_INSN
7613           || ! SIBLING_CALL_P (insn))
7614         continue;
7615
7616       start_sequence ();
7617       seq = gen_sibcall_epilogue ();
7618       end_sequence ();
7619
7620       i = PREV_INSN (insn);
7621       newinsn = emit_insn_before (seq, insn);
7622
7623       /* Retain a map of the epilogue insns.  Used in life analysis to
7624          avoid getting rid of sibcall epilogue insns.  */
7625       record_insns (GET_CODE (seq) == SEQUENCE
7626                     ? seq : newinsn, &sibcall_epilogue);
7627     }
7628 #endif
7629
7630 #ifdef HAVE_prologue
7631   if (prologue_end)
7632     {
7633       rtx insn, prev;
7634
7635       /* GDB handles `break f' by setting a breakpoint on the first
7636          line note after the prologue.  Which means (1) that if
7637          there are line number notes before where we inserted the
7638          prologue we should move them, and (2) we should generate a
7639          note before the end of the first basic block, if there isn't
7640          one already there.
7641
7642          ??? This behaviour is completely broken when dealing with
7643          multiple entry functions.  We simply place the note always
7644          into first basic block and let alternate entry points
7645          to be missed.
7646        */
7647
7648       for (insn = prologue_end; insn; insn = prev)
7649         {
7650           prev = PREV_INSN (insn);
7651           if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7652             {
7653               /* Note that we cannot reorder the first insn in the
7654                  chain, since rest_of_compilation relies on that
7655                  remaining constant.  */
7656               if (prev == NULL)
7657                 break;
7658               reorder_insns (insn, insn, prologue_end);
7659             }
7660         }
7661
7662       /* Find the last line number note in the first block.  */
7663       for (insn = BASIC_BLOCK (0)->end;
7664            insn != prologue_end && insn;
7665            insn = PREV_INSN (insn))
7666         if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7667           break;
7668
7669       /* If we didn't find one, make a copy of the first line number
7670          we run across.  */
7671       if (! insn)
7672         {
7673           for (insn = next_active_insn (prologue_end);
7674                insn;
7675                insn = PREV_INSN (insn))
7676             if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7677               {
7678                 emit_line_note_after (NOTE_SOURCE_FILE (insn),
7679                                       NOTE_LINE_NUMBER (insn),
7680                                       prologue_end);
7681                 break;
7682               }
7683         }
7684     }
7685 #endif
7686 #ifdef HAVE_epilogue
7687   if (epilogue_end)
7688     {
7689       rtx insn, next;
7690
7691       /* Similarly, move any line notes that appear after the epilogue.
7692          There is no need, however, to be quite so anal about the existence
7693          of such a note.  */
7694       for (insn = epilogue_end; insn; insn = next)
7695         {
7696           next = NEXT_INSN (insn);
7697           if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7698             reorder_insns (insn, insn, PREV_INSN (epilogue_end));
7699         }
7700     }
7701 #endif
7702 }
7703
7704 /* Reposition the prologue-end and epilogue-begin notes after instruction
7705    scheduling and delayed branch scheduling.  */
7706
7707 void
7708 reposition_prologue_and_epilogue_notes (f)
7709      rtx f ATTRIBUTE_UNUSED;
7710 {
7711 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
7712   int len;
7713
7714   if ((len = VARRAY_SIZE (prologue)) > 0)
7715     {
7716       rtx insn, note = 0;
7717
7718       /* Scan from the beginning until we reach the last prologue insn.
7719          We apparently can't depend on basic_block_{head,end} after
7720          reorg has run.  */
7721       for (insn = f; len && insn; insn = NEXT_INSN (insn))
7722         {
7723           if (GET_CODE (insn) == NOTE)
7724             {
7725               if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
7726                 note = insn;
7727             }
7728           else if ((len -= contains (insn, prologue)) == 0)
7729             {
7730               rtx next;
7731               /* Find the prologue-end note if we haven't already, and
7732                  move it to just after the last prologue insn.  */
7733               if (note == 0)
7734                 {
7735                   for (note = insn; (note = NEXT_INSN (note));)
7736                     if (GET_CODE (note) == NOTE
7737                         && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
7738                       break;
7739                 }
7740
7741               next = NEXT_INSN (note);
7742
7743               /* Whether or not we can depend on BLOCK_HEAD,
7744                  attempt to keep it up-to-date.  */
7745               if (BLOCK_HEAD (0) == note)
7746                 BLOCK_HEAD (0) = next;
7747
7748               remove_insn (note);
7749               /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note.  */
7750               if (GET_CODE (insn) == CODE_LABEL)
7751                 insn = NEXT_INSN (insn);
7752               add_insn_after (note, insn);
7753             }
7754         }
7755     }
7756
7757   if ((len = VARRAY_SIZE (epilogue)) > 0)
7758     {
7759       rtx insn, note = 0;
7760
7761       /* Scan from the end until we reach the first epilogue insn.
7762          We apparently can't depend on basic_block_{head,end} after
7763          reorg has run.  */
7764       for (insn = get_last_insn (); len && insn; insn = PREV_INSN (insn))
7765         {
7766           if (GET_CODE (insn) == NOTE)
7767             {
7768               if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
7769                 note = insn;
7770             }
7771           else if ((len -= contains (insn, epilogue)) == 0)
7772             {
7773               /* Find the epilogue-begin note if we haven't already, and
7774                  move it to just before the first epilogue insn.  */
7775               if (note == 0)
7776                 {
7777                   for (note = insn; (note = PREV_INSN (note));)
7778                     if (GET_CODE (note) == NOTE
7779                         && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
7780                       break;
7781                 }
7782
7783               /* Whether or not we can depend on BLOCK_HEAD,
7784                  attempt to keep it up-to-date.  */
7785               if (n_basic_blocks
7786                   && BLOCK_HEAD (n_basic_blocks-1) == insn)
7787                 BLOCK_HEAD (n_basic_blocks-1) = note;
7788
7789               remove_insn (note);
7790               add_insn_before (note, insn);
7791             }
7792         }
7793     }
7794 #endif /* HAVE_prologue or HAVE_epilogue */
7795 }
7796
7797 /* Mark P for GC.  */
7798
7799 static void
7800 mark_function_status (p)
7801      struct function *p;
7802 {
7803   struct var_refs_queue *q;
7804   struct temp_slot *t;
7805   int i;
7806   rtx *r;
7807
7808   if (p == 0)
7809     return;
7810
7811   ggc_mark_rtx (p->arg_offset_rtx);
7812
7813   if (p->x_parm_reg_stack_loc)
7814     for (i = p->x_max_parm_reg, r = p->x_parm_reg_stack_loc;
7815          i > 0; --i, ++r)
7816       ggc_mark_rtx (*r);
7817
7818   ggc_mark_rtx (p->return_rtx);
7819   ggc_mark_rtx (p->x_cleanup_label);
7820   ggc_mark_rtx (p->x_return_label);
7821   ggc_mark_rtx (p->x_save_expr_regs);
7822   ggc_mark_rtx (p->x_stack_slot_list);
7823   ggc_mark_rtx (p->x_parm_birth_insn);
7824   ggc_mark_rtx (p->x_tail_recursion_label);
7825   ggc_mark_rtx (p->x_tail_recursion_reentry);
7826   ggc_mark_rtx (p->internal_arg_pointer);
7827   ggc_mark_rtx (p->x_arg_pointer_save_area);
7828   ggc_mark_tree (p->x_rtl_expr_chain);
7829   ggc_mark_rtx (p->x_last_parm_insn);
7830   ggc_mark_tree (p->x_context_display);
7831   ggc_mark_tree (p->x_trampoline_list);
7832   ggc_mark_rtx (p->epilogue_delay_list);
7833   ggc_mark_rtx (p->x_clobber_return_insn);
7834
7835   for (t = p->x_temp_slots; t != 0; t = t->next)
7836     {
7837       ggc_mark (t);
7838       ggc_mark_rtx (t->slot);
7839       ggc_mark_rtx (t->address);
7840       ggc_mark_tree (t->rtl_expr);
7841       ggc_mark_tree (t->type);
7842     }
7843
7844   for (q = p->fixup_var_refs_queue; q != 0; q = q->next)
7845     {
7846       ggc_mark (q);
7847       ggc_mark_rtx (q->modified);
7848       }
7849
7850   ggc_mark_rtx (p->x_nonlocal_goto_handler_slots);
7851   ggc_mark_rtx (p->x_nonlocal_goto_handler_labels);
7852   ggc_mark_rtx (p->x_nonlocal_goto_stack_level);
7853   ggc_mark_tree (p->x_nonlocal_labels);
7854
7855   mark_hard_reg_initial_vals (p);
7856 }
7857
7858 /* Mark the struct function pointed to by *ARG for GC, if it is not
7859    NULL.  This is used to mark the current function and the outer
7860    function chain.  */
7861
7862 static void
7863 maybe_mark_struct_function (arg)
7864      void *arg;
7865 {
7866   struct function *f = *(struct function **) arg;
7867
7868   if (f == 0)
7869     return;
7870
7871   ggc_mark_struct_function (f);
7872 }
7873
7874 /* Mark a struct function * for GC.  This is called from ggc-common.c.  */
7875
7876 void
7877 ggc_mark_struct_function (f)
7878      struct function *f;
7879 {
7880   ggc_mark (f);
7881   ggc_mark_tree (f->decl);
7882
7883   mark_function_status (f);
7884   mark_eh_status (f->eh);
7885   mark_stmt_status (f->stmt);
7886   mark_expr_status (f->expr);
7887   mark_emit_status (f->emit);
7888   mark_varasm_status (f->varasm);
7889
7890   if (mark_machine_status)
7891     (*mark_machine_status) (f);
7892   if (mark_lang_status)
7893     (*mark_lang_status) (f);
7894
7895   if (f->original_arg_vector)
7896     ggc_mark_rtvec ((rtvec) f->original_arg_vector);
7897   if (f->original_decl_initial)
7898     ggc_mark_tree (f->original_decl_initial);
7899   if (f->outer)
7900     ggc_mark_struct_function (f->outer);
7901 }
7902
7903 /* Called once, at initialization, to initialize function.c.  */
7904
7905 void
7906 init_function_once ()
7907 {
7908   ggc_add_root (&cfun, 1, sizeof cfun, maybe_mark_struct_function);
7909   ggc_add_root (&outer_function_chain, 1, sizeof outer_function_chain,
7910                 maybe_mark_struct_function);
7911
7912   VARRAY_INT_INIT (prologue, 0, "prologue");
7913   VARRAY_INT_INIT (epilogue, 0, "epilogue");
7914   VARRAY_INT_INIT (sibcall_epilogue, 0, "sibcall_epilogue");
7915 }