OSDN Git Service

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