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