OSDN Git Service

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