OSDN Git Service

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