OSDN Git Service

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