OSDN Git Service

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