OSDN Git Service

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