OSDN Git Service

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