OSDN Git Service

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