OSDN Git Service

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