OSDN Git Service

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