* target.h (struct gcc_target): Add new field to struct cxx: import_export_class.
[official-gcc.git] / gcc / function.c
blob7cfb1aaae87a9efa1aee7e373e3c8683e4d424ce
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.
5 This file is part of GCC.
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.
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.
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. */
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.
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.
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.
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. */
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"
68 #ifndef LOCAL_ALIGNMENT
69 #define LOCAL_ALIGNMENT(TYPE, ALIGNMENT) ALIGNMENT
70 #endif
72 #ifndef STACK_ALIGNMENT_NEEDED
73 #define STACK_ALIGNMENT_NEEDED 1
74 #endif
76 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
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
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))
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))
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. */
101 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
102 #define NEED_SEPARATE_AP
103 #endif
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;
111 /* Nonzero if function being compiled doesn't contain any instructions
112 that can throw an exception. This is set prior to final. */
114 int current_function_nothrow;
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;
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;
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;
132 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
133 static GTY(()) int funcdef_no;
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);
139 /* The currently compiled function. */
140 struct function *cfun = 0;
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;
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;
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.
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.
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. */
168 struct temp_slot GTY(())
170 /* Points to next temporary slot. */
171 struct temp_slot *next;
172 /* Points to previous temporary slot. */
173 struct temp_slot *prev;
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;
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. */
212 struct fixup_replacement GTY(())
214 rtx old;
215 rtx new;
216 struct fixup_replacement *next;
219 struct insns_for_mem_entry
221 /* A MEM. */
222 rtx key;
223 /* These are the INSNs which reference the MEM. */
224 rtx insns;
227 /* Forward declarations. */
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 void reorder_blocks_1 (rtx, tree, varray_type *);
256 static void reorder_fix_fragments (tree);
257 static int all_blocks (tree, tree *);
258 static tree *get_block_vector (tree, int *);
259 extern tree debug_find_var_in_block_tree (tree, tree);
260 /* We always define `record_insns' even if it's not used so that we
261 can always export `prologue_epilogue_contains'. */
262 static void record_insns (rtx, varray_type *) ATTRIBUTE_UNUSED;
263 static int contains (rtx, varray_type);
264 #ifdef HAVE_return
265 static void emit_return_into_block (basic_block, rtx);
266 #endif
267 static void put_addressof_into_stack (rtx, htab_t);
268 static bool purge_addressof_1 (rtx *, rtx, int, int, int, htab_t);
269 static void purge_single_hard_subreg_set (rtx);
270 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
271 static rtx keep_stack_depressed (rtx);
272 #endif
273 static int is_addressof (rtx *, void *);
274 static hashval_t insns_for_mem_hash (const void *);
275 static int insns_for_mem_comp (const void *, const void *);
276 static int insns_for_mem_walk (rtx *, void *);
277 static void compute_insns_for_mem (rtx, rtx, htab_t);
278 static void prepare_function_start (tree);
279 static void do_clobber_return_reg (rtx, void *);
280 static void do_use_return_reg (rtx, void *);
281 static void instantiate_virtual_regs_lossage (rtx);
282 static tree split_complex_args (tree);
283 static void set_insn_locators (rtx, int) ATTRIBUTE_UNUSED;
285 /* Pointer to chain of `struct function' for containing functions. */
286 struct function *outer_function_chain;
288 /* List of insns that were postponed by purge_addressof_1. */
289 static rtx postponed_insns;
291 /* Given a function decl for a containing function,
292 return the `struct function' for it. */
294 struct function *
295 find_function_data (tree decl)
297 struct function *p;
299 for (p = outer_function_chain; p; p = p->outer)
300 if (p->decl == decl)
301 return p;
303 abort ();
306 /* Save the current context for compilation of a nested function.
307 This is called from language-specific code. The caller should use
308 the enter_nested langhook to save any language-specific state,
309 since this function knows only about language-independent
310 variables. */
312 void
313 push_function_context_to (tree context)
315 struct function *p;
317 if (context)
319 if (context == current_function_decl)
320 cfun->contains_functions = 1;
321 else
323 struct function *containing = find_function_data (context);
324 containing->contains_functions = 1;
328 if (cfun == 0)
329 init_dummy_function_start ();
330 p = cfun;
332 p->outer = outer_function_chain;
333 outer_function_chain = p;
334 p->fixup_var_refs_queue = 0;
336 lang_hooks.function.enter_nested (p);
338 cfun = 0;
341 void
342 push_function_context (void)
344 push_function_context_to (current_function_decl);
347 /* Restore the last saved context, at the end of a nested function.
348 This function is called from language-specific code. */
350 void
351 pop_function_context_from (tree context ATTRIBUTE_UNUSED)
353 struct function *p = outer_function_chain;
354 struct var_refs_queue *queue;
356 cfun = p;
357 outer_function_chain = p->outer;
359 current_function_decl = p->decl;
360 reg_renumber = 0;
362 restore_emit_status (p);
364 lang_hooks.function.leave_nested (p);
366 /* Finish doing put_var_into_stack for any of our variables which became
367 addressable during the nested function. If only one entry has to be
368 fixed up, just do that one. Otherwise, first make a list of MEMs that
369 are not to be unshared. */
370 if (p->fixup_var_refs_queue == 0)
372 else if (p->fixup_var_refs_queue->next == 0)
373 fixup_var_refs (p->fixup_var_refs_queue->modified,
374 p->fixup_var_refs_queue->promoted_mode,
375 p->fixup_var_refs_queue->unsignedp,
376 p->fixup_var_refs_queue->modified, 0);
377 else
379 rtx list = 0;
381 for (queue = p->fixup_var_refs_queue; queue; queue = queue->next)
382 list = gen_rtx_EXPR_LIST (VOIDmode, queue->modified, list);
384 for (queue = p->fixup_var_refs_queue; queue; queue = queue->next)
385 fixup_var_refs (queue->modified, queue->promoted_mode,
386 queue->unsignedp, list, 0);
390 p->fixup_var_refs_queue = 0;
392 /* Reset variables that have known state during rtx generation. */
393 rtx_equal_function_value_matters = 1;
394 virtuals_instantiated = 0;
395 generating_concat_p = 1;
398 void
399 pop_function_context (void)
401 pop_function_context_from (current_function_decl);
404 /* Clear out all parts of the state in F that can safely be discarded
405 after the function has been parsed, but not compiled, to let
406 garbage collection reclaim the memory. */
408 void
409 free_after_parsing (struct function *f)
411 /* f->expr->forced_labels is used by code generation. */
412 /* f->emit->regno_reg_rtx is used by code generation. */
413 /* f->varasm is used by code generation. */
414 /* f->eh->eh_return_stub_label is used by code generation. */
416 lang_hooks.function.final (f);
417 f->stmt = NULL;
420 /* Clear out all parts of the state in F that can safely be discarded
421 after the function has been compiled, to let garbage collection
422 reclaim the memory. */
424 void
425 free_after_compilation (struct function *f)
427 f->eh = NULL;
428 f->expr = NULL;
429 f->emit = NULL;
430 f->varasm = NULL;
431 f->machine = NULL;
433 f->x_avail_temp_slots = NULL;
434 f->x_used_temp_slots = NULL;
435 f->arg_offset_rtx = NULL;
436 f->return_rtx = NULL;
437 f->internal_arg_pointer = NULL;
438 f->x_nonlocal_goto_handler_labels = NULL;
439 f->x_cleanup_label = NULL;
440 f->x_return_label = NULL;
441 f->x_naked_return_label = NULL;
442 f->x_save_expr_regs = NULL;
443 f->x_stack_slot_list = NULL;
444 f->x_rtl_expr_chain = NULL;
445 f->x_tail_recursion_reentry = NULL;
446 f->x_arg_pointer_save_area = NULL;
447 f->x_parm_birth_insn = NULL;
448 f->x_parm_reg_stack_loc = NULL;
449 f->fixup_var_refs_queue = NULL;
450 f->original_arg_vector = NULL;
451 f->original_decl_initial = NULL;
452 f->epilogue_delay_list = NULL;
455 /* Allocate fixed slots in the stack frame of the current function. */
457 /* Return size needed for stack frame based on slots so far allocated in
458 function F.
459 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
460 the caller may have to do that. */
462 HOST_WIDE_INT
463 get_func_frame_size (struct function *f)
465 #ifdef FRAME_GROWS_DOWNWARD
466 return -f->x_frame_offset;
467 #else
468 return f->x_frame_offset;
469 #endif
472 /* Return size needed for stack frame based on slots so far allocated.
473 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
474 the caller may have to do that. */
475 HOST_WIDE_INT
476 get_frame_size (void)
478 return get_func_frame_size (cfun);
481 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
482 with machine mode MODE.
484 ALIGN controls the amount of alignment for the address of the slot:
485 0 means according to MODE,
486 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
487 -2 means use BITS_PER_UNIT,
488 positive specifies alignment boundary in bits.
490 We do not round to stack_boundary here.
492 FUNCTION specifies the function to allocate in. */
494 static rtx
495 assign_stack_local_1 (enum machine_mode mode, HOST_WIDE_INT size, int align,
496 struct function *function)
498 rtx x, addr;
499 int bigend_correction = 0;
500 int alignment;
501 int frame_off, frame_alignment, frame_phase;
503 if (align == 0)
505 tree type;
507 if (mode == BLKmode)
508 alignment = BIGGEST_ALIGNMENT;
509 else
510 alignment = GET_MODE_ALIGNMENT (mode);
512 /* Allow the target to (possibly) increase the alignment of this
513 stack slot. */
514 type = lang_hooks.types.type_for_mode (mode, 0);
515 if (type)
516 alignment = LOCAL_ALIGNMENT (type, alignment);
518 alignment /= BITS_PER_UNIT;
520 else if (align == -1)
522 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
523 size = CEIL_ROUND (size, alignment);
525 else if (align == -2)
526 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
527 else
528 alignment = align / BITS_PER_UNIT;
530 #ifdef FRAME_GROWS_DOWNWARD
531 function->x_frame_offset -= size;
532 #endif
534 /* Ignore alignment we can't do with expected alignment of the boundary. */
535 if (alignment * BITS_PER_UNIT > PREFERRED_STACK_BOUNDARY)
536 alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
538 if (function->stack_alignment_needed < alignment * BITS_PER_UNIT)
539 function->stack_alignment_needed = alignment * BITS_PER_UNIT;
541 /* Calculate how many bytes the start of local variables is off from
542 stack alignment. */
543 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
544 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
545 frame_phase = frame_off ? frame_alignment - frame_off : 0;
547 /* Round the frame offset to the specified alignment. The default is
548 to always honor requests to align the stack but a port may choose to
549 do its own stack alignment by defining STACK_ALIGNMENT_NEEDED. */
550 if (STACK_ALIGNMENT_NEEDED
551 || mode != BLKmode
552 || size != 0)
554 /* We must be careful here, since FRAME_OFFSET might be negative and
555 division with a negative dividend isn't as well defined as we might
556 like. So we instead assume that ALIGNMENT is a power of two and
557 use logical operations which are unambiguous. */
558 #ifdef FRAME_GROWS_DOWNWARD
559 function->x_frame_offset
560 = (FLOOR_ROUND (function->x_frame_offset - frame_phase, alignment)
561 + frame_phase);
562 #else
563 function->x_frame_offset
564 = (CEIL_ROUND (function->x_frame_offset - frame_phase, alignment)
565 + frame_phase);
566 #endif
569 /* On a big-endian machine, if we are allocating more space than we will use,
570 use the least significant bytes of those that are allocated. */
571 if (BYTES_BIG_ENDIAN && mode != BLKmode)
572 bigend_correction = size - GET_MODE_SIZE (mode);
574 /* If we have already instantiated virtual registers, return the actual
575 address relative to the frame pointer. */
576 if (function == cfun && virtuals_instantiated)
577 addr = plus_constant (frame_pointer_rtx,
578 trunc_int_for_mode
579 (frame_offset + bigend_correction
580 + STARTING_FRAME_OFFSET, Pmode));
581 else
582 addr = plus_constant (virtual_stack_vars_rtx,
583 trunc_int_for_mode
584 (function->x_frame_offset + bigend_correction,
585 Pmode));
587 #ifndef FRAME_GROWS_DOWNWARD
588 function->x_frame_offset += size;
589 #endif
591 x = gen_rtx_MEM (mode, addr);
593 function->x_stack_slot_list
594 = gen_rtx_EXPR_LIST (VOIDmode, x, function->x_stack_slot_list);
596 return x;
599 /* Wrapper around assign_stack_local_1; assign a local stack slot for the
600 current function. */
603 assign_stack_local (enum machine_mode mode, HOST_WIDE_INT size, int align)
605 return assign_stack_local_1 (mode, size, align, cfun);
609 /* Removes temporary slot TEMP from LIST. */
611 static void
612 cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
614 if (temp->next)
615 temp->next->prev = temp->prev;
616 if (temp->prev)
617 temp->prev->next = temp->next;
618 else
619 *list = temp->next;
621 temp->prev = temp->next = NULL;
624 /* Inserts temporary slot TEMP to LIST. */
626 static void
627 insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
629 temp->next = *list;
630 if (*list)
631 (*list)->prev = temp;
632 temp->prev = NULL;
633 *list = temp;
636 /* Returns the list of used temp slots at LEVEL. */
638 static struct temp_slot **
639 temp_slots_at_level (int level)
641 level++;
643 if (!used_temp_slots)
644 VARRAY_GENERIC_PTR_INIT (used_temp_slots, 3, "used_temp_slots");
646 while (level >= (int) VARRAY_ACTIVE_SIZE (used_temp_slots))
647 VARRAY_PUSH_GENERIC_PTR (used_temp_slots, NULL);
649 return (struct temp_slot **) &VARRAY_GENERIC_PTR (used_temp_slots, level);
652 /* Returns the maximal temporary slot level. */
654 static int
655 max_slot_level (void)
657 if (!used_temp_slots)
658 return -1;
660 return VARRAY_ACTIVE_SIZE (used_temp_slots) - 1;
663 /* Moves temporary slot TEMP to LEVEL. */
665 static void
666 move_slot_to_level (struct temp_slot *temp, int level)
668 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
669 insert_slot_to_list (temp, temp_slots_at_level (level));
670 temp->level = level;
673 /* Make temporary slot TEMP available. */
675 static void
676 make_slot_available (struct temp_slot *temp)
678 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
679 insert_slot_to_list (temp, &avail_temp_slots);
680 temp->in_use = 0;
681 temp->level = -1;
684 /* Allocate a temporary stack slot and record it for possible later
685 reuse.
687 MODE is the machine mode to be given to the returned rtx.
689 SIZE is the size in units of the space required. We do no rounding here
690 since assign_stack_local will do any required rounding.
692 KEEP is 1 if this slot is to be retained after a call to
693 free_temp_slots. Automatic variables for a block are allocated
694 with this flag. KEEP is 2 if we allocate a longer term temporary,
695 whose lifetime is controlled by CLEANUP_POINT_EXPRs. KEEP is 3
696 if we are to allocate something at an inner level to be treated as
697 a variable in the block (e.g., a SAVE_EXPR).
699 TYPE is the type that will be used for the stack slot. */
702 assign_stack_temp_for_type (enum machine_mode mode, HOST_WIDE_INT size, int keep,
703 tree type)
705 unsigned int align;
706 struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
707 rtx slot;
709 /* If SIZE is -1 it means that somebody tried to allocate a temporary
710 of a variable size. */
711 if (size == -1)
712 abort ();
714 if (mode == BLKmode)
715 align = BIGGEST_ALIGNMENT;
716 else
717 align = GET_MODE_ALIGNMENT (mode);
719 if (! type)
720 type = lang_hooks.types.type_for_mode (mode, 0);
722 if (type)
723 align = LOCAL_ALIGNMENT (type, align);
725 /* Try to find an available, already-allocated temporary of the proper
726 mode which meets the size and alignment requirements. Choose the
727 smallest one with the closest alignment. */
728 for (p = avail_temp_slots; p; p = p->next)
730 if (p->align >= align && p->size >= size && GET_MODE (p->slot) == mode
731 && objects_must_conflict_p (p->type, type)
732 && (best_p == 0 || best_p->size > p->size
733 || (best_p->size == p->size && best_p->align > p->align)))
735 if (p->align == align && p->size == size)
737 selected = p;
738 cut_slot_from_list (selected, &avail_temp_slots);
739 best_p = 0;
740 break;
742 best_p = p;
746 /* Make our best, if any, the one to use. */
747 if (best_p)
749 selected = best_p;
750 cut_slot_from_list (selected, &avail_temp_slots);
752 /* If there are enough aligned bytes left over, make them into a new
753 temp_slot so that the extra bytes don't get wasted. Do this only
754 for BLKmode slots, so that we can be sure of the alignment. */
755 if (GET_MODE (best_p->slot) == BLKmode)
757 int alignment = best_p->align / BITS_PER_UNIT;
758 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
760 if (best_p->size - rounded_size >= alignment)
762 p = ggc_alloc (sizeof (struct temp_slot));
763 p->in_use = p->addr_taken = 0;
764 p->size = best_p->size - rounded_size;
765 p->base_offset = best_p->base_offset + rounded_size;
766 p->full_size = best_p->full_size - rounded_size;
767 p->slot = gen_rtx_MEM (BLKmode,
768 plus_constant (XEXP (best_p->slot, 0),
769 rounded_size));
770 p->align = best_p->align;
771 p->address = 0;
772 p->rtl_expr = 0;
773 p->type = best_p->type;
774 insert_slot_to_list (p, &avail_temp_slots);
776 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
777 stack_slot_list);
779 best_p->size = rounded_size;
780 best_p->full_size = rounded_size;
785 /* If we still didn't find one, make a new temporary. */
786 if (selected == 0)
788 HOST_WIDE_INT frame_offset_old = frame_offset;
790 p = ggc_alloc (sizeof (struct temp_slot));
792 /* We are passing an explicit alignment request to assign_stack_local.
793 One side effect of that is assign_stack_local will not round SIZE
794 to ensure the frame offset remains suitably aligned.
796 So for requests which depended on the rounding of SIZE, we go ahead
797 and round it now. We also make sure ALIGNMENT is at least
798 BIGGEST_ALIGNMENT. */
799 if (mode == BLKmode && align < BIGGEST_ALIGNMENT)
800 abort ();
801 p->slot = assign_stack_local (mode,
802 (mode == BLKmode
803 ? CEIL_ROUND (size, (int) align / BITS_PER_UNIT)
804 : size),
805 align);
807 p->align = align;
809 /* The following slot size computation is necessary because we don't
810 know the actual size of the temporary slot until assign_stack_local
811 has performed all the frame alignment and size rounding for the
812 requested temporary. Note that extra space added for alignment
813 can be either above or below this stack slot depending on which
814 way the frame grows. We include the extra space if and only if it
815 is above this slot. */
816 #ifdef FRAME_GROWS_DOWNWARD
817 p->size = frame_offset_old - frame_offset;
818 #else
819 p->size = size;
820 #endif
822 /* Now define the fields used by combine_temp_slots. */
823 #ifdef FRAME_GROWS_DOWNWARD
824 p->base_offset = frame_offset;
825 p->full_size = frame_offset_old - frame_offset;
826 #else
827 p->base_offset = frame_offset_old;
828 p->full_size = frame_offset - frame_offset_old;
829 #endif
830 p->address = 0;
832 selected = p;
835 p = selected;
836 p->in_use = 1;
837 p->addr_taken = 0;
838 p->rtl_expr = seq_rtl_expr;
839 p->type = type;
841 if (keep == 2)
843 p->level = target_temp_slot_level;
844 p->keep = 1;
846 else if (keep == 3)
848 p->level = var_temp_slot_level;
849 p->keep = 0;
851 else
853 p->level = temp_slot_level;
854 p->keep = keep;
857 pp = temp_slots_at_level (p->level);
858 insert_slot_to_list (p, pp);
860 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
861 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
862 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
864 /* If we know the alias set for the memory that will be used, use
865 it. If there's no TYPE, then we don't know anything about the
866 alias set for the memory. */
867 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
868 set_mem_align (slot, align);
870 /* If a type is specified, set the relevant flags. */
871 if (type != 0)
873 RTX_UNCHANGING_P (slot) = (lang_hooks.honor_readonly
874 && TYPE_READONLY (type));
875 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
876 MEM_SET_IN_STRUCT_P (slot, AGGREGATE_TYPE_P (type));
879 return slot;
882 /* Allocate a temporary stack slot and record it for possible later
883 reuse. First three arguments are same as in preceding function. */
886 assign_stack_temp (enum machine_mode mode, HOST_WIDE_INT size, int keep)
888 return assign_stack_temp_for_type (mode, size, keep, NULL_TREE);
891 /* Assign a temporary.
892 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
893 and so that should be used in error messages. In either case, we
894 allocate of the given type.
895 KEEP is as for assign_stack_temp.
896 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
897 it is 0 if a register is OK.
898 DONT_PROMOTE is 1 if we should not promote values in register
899 to wider modes. */
902 assign_temp (tree type_or_decl, int keep, int memory_required,
903 int dont_promote ATTRIBUTE_UNUSED)
905 tree type, decl;
906 enum machine_mode mode;
907 #ifdef PROMOTE_MODE
908 int unsignedp;
909 #endif
911 if (DECL_P (type_or_decl))
912 decl = type_or_decl, type = TREE_TYPE (decl);
913 else
914 decl = NULL, type = type_or_decl;
916 mode = TYPE_MODE (type);
917 #ifdef PROMOTE_MODE
918 unsignedp = TYPE_UNSIGNED (type);
919 #endif
921 if (mode == BLKmode || memory_required)
923 HOST_WIDE_INT size = int_size_in_bytes (type);
924 rtx tmp;
926 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
927 problems with allocating the stack space. */
928 if (size == 0)
929 size = 1;
931 /* Unfortunately, we don't yet know how to allocate variable-sized
932 temporaries. However, sometimes we have a fixed upper limit on
933 the size (which is stored in TYPE_ARRAY_MAX_SIZE) and can use that
934 instead. This is the case for Chill variable-sized strings. */
935 if (size == -1 && TREE_CODE (type) == ARRAY_TYPE
936 && TYPE_ARRAY_MAX_SIZE (type) != NULL_TREE
937 && host_integerp (TYPE_ARRAY_MAX_SIZE (type), 1))
938 size = tree_low_cst (TYPE_ARRAY_MAX_SIZE (type), 1);
940 /* The size of the temporary may be too large to fit into an integer. */
941 /* ??? Not sure this should happen except for user silliness, so limit
942 this to things that aren't compiler-generated temporaries. The
943 rest of the time we'll abort in assign_stack_temp_for_type. */
944 if (decl && size == -1
945 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
947 error ("%Jsize of variable '%D' is too large", decl, decl);
948 size = 1;
951 tmp = assign_stack_temp_for_type (mode, size, keep, type);
952 return tmp;
955 #ifdef PROMOTE_MODE
956 if (! dont_promote)
957 mode = promote_mode (type, mode, &unsignedp, 0);
958 #endif
960 return gen_reg_rtx (mode);
963 /* Combine temporary stack slots which are adjacent on the stack.
965 This allows for better use of already allocated stack space. This is only
966 done for BLKmode slots because we can be sure that we won't have alignment
967 problems in this case. */
969 void
970 combine_temp_slots (void)
972 struct temp_slot *p, *q, *next, *next_q;
973 int num_slots;
975 /* We can't combine slots, because the information about which slot
976 is in which alias set will be lost. */
977 if (flag_strict_aliasing)
978 return;
980 /* If there are a lot of temp slots, don't do anything unless
981 high levels of optimization. */
982 if (! flag_expensive_optimizations)
983 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
984 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
985 return;
987 for (p = avail_temp_slots; p; p = next)
989 int delete_p = 0;
991 next = p->next;
993 if (GET_MODE (p->slot) != BLKmode)
994 continue;
996 for (q = p->next; q; q = next_q)
998 int delete_q = 0;
1000 next_q = q->next;
1002 if (GET_MODE (q->slot) != BLKmode)
1003 continue;
1005 if (p->base_offset + p->full_size == q->base_offset)
1007 /* Q comes after P; combine Q into P. */
1008 p->size += q->size;
1009 p->full_size += q->full_size;
1010 delete_q = 1;
1012 else if (q->base_offset + q->full_size == p->base_offset)
1014 /* P comes after Q; combine P into Q. */
1015 q->size += p->size;
1016 q->full_size += p->full_size;
1017 delete_p = 1;
1018 break;
1020 if (delete_q)
1021 cut_slot_from_list (q, &avail_temp_slots);
1024 /* Either delete P or advance past it. */
1025 if (delete_p)
1026 cut_slot_from_list (p, &avail_temp_slots);
1030 /* Find the temp slot corresponding to the object at address X. */
1032 static struct temp_slot *
1033 find_temp_slot_from_address (rtx x)
1035 struct temp_slot *p;
1036 rtx next;
1037 int i;
1039 for (i = max_slot_level (); i >= 0; i--)
1040 for (p = *temp_slots_at_level (i); p; p = p->next)
1042 if (XEXP (p->slot, 0) == x
1043 || p->address == x
1044 || (GET_CODE (x) == PLUS
1045 && XEXP (x, 0) == virtual_stack_vars_rtx
1046 && GET_CODE (XEXP (x, 1)) == CONST_INT
1047 && INTVAL (XEXP (x, 1)) >= p->base_offset
1048 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size))
1049 return p;
1051 else if (p->address != 0 && GET_CODE (p->address) == EXPR_LIST)
1052 for (next = p->address; next; next = XEXP (next, 1))
1053 if (XEXP (next, 0) == x)
1054 return p;
1057 /* If we have a sum involving a register, see if it points to a temp
1058 slot. */
1059 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
1060 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
1061 return p;
1062 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
1063 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
1064 return p;
1066 return 0;
1069 /* Indicate that NEW is an alternate way of referring to the temp slot
1070 that previously was known by OLD. */
1072 void
1073 update_temp_slot_address (rtx old, rtx new)
1075 struct temp_slot *p;
1077 if (rtx_equal_p (old, new))
1078 return;
1080 p = find_temp_slot_from_address (old);
1082 /* If we didn't find one, see if both OLD is a PLUS. If so, and NEW
1083 is a register, see if one operand of the PLUS is a temporary
1084 location. If so, NEW points into it. Otherwise, if both OLD and
1085 NEW are a PLUS and if there is a register in common between them.
1086 If so, try a recursive call on those values. */
1087 if (p == 0)
1089 if (GET_CODE (old) != PLUS)
1090 return;
1092 if (REG_P (new))
1094 update_temp_slot_address (XEXP (old, 0), new);
1095 update_temp_slot_address (XEXP (old, 1), new);
1096 return;
1098 else if (GET_CODE (new) != PLUS)
1099 return;
1101 if (rtx_equal_p (XEXP (old, 0), XEXP (new, 0)))
1102 update_temp_slot_address (XEXP (old, 1), XEXP (new, 1));
1103 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 0)))
1104 update_temp_slot_address (XEXP (old, 0), XEXP (new, 1));
1105 else if (rtx_equal_p (XEXP (old, 0), XEXP (new, 1)))
1106 update_temp_slot_address (XEXP (old, 1), XEXP (new, 0));
1107 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 1)))
1108 update_temp_slot_address (XEXP (old, 0), XEXP (new, 0));
1110 return;
1113 /* Otherwise add an alias for the temp's address. */
1114 else if (p->address == 0)
1115 p->address = new;
1116 else
1118 if (GET_CODE (p->address) != EXPR_LIST)
1119 p->address = gen_rtx_EXPR_LIST (VOIDmode, p->address, NULL_RTX);
1121 p->address = gen_rtx_EXPR_LIST (VOIDmode, new, p->address);
1125 /* If X could be a reference to a temporary slot, mark the fact that its
1126 address was taken. */
1128 void
1129 mark_temp_addr_taken (rtx x)
1131 struct temp_slot *p;
1133 if (x == 0)
1134 return;
1136 /* If X is not in memory or is at a constant address, it cannot be in
1137 a temporary slot. */
1138 if (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1139 return;
1141 p = find_temp_slot_from_address (XEXP (x, 0));
1142 if (p != 0)
1143 p->addr_taken = 1;
1146 /* If X could be a reference to a temporary slot, mark that slot as
1147 belonging to the to one level higher than the current level. If X
1148 matched one of our slots, just mark that one. Otherwise, we can't
1149 easily predict which it is, so upgrade all of them. Kept slots
1150 need not be touched.
1152 This is called when an ({...}) construct occurs and a statement
1153 returns a value in memory. */
1155 void
1156 preserve_temp_slots (rtx x)
1158 struct temp_slot *p = 0, *next;
1160 /* If there is no result, we still might have some objects whose address
1161 were taken, so we need to make sure they stay around. */
1162 if (x == 0)
1164 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1166 next = p->next;
1168 if (p->addr_taken)
1169 move_slot_to_level (p, temp_slot_level - 1);
1172 return;
1175 /* If X is a register that is being used as a pointer, see if we have
1176 a temporary slot we know it points to. To be consistent with
1177 the code below, we really should preserve all non-kept slots
1178 if we can't find a match, but that seems to be much too costly. */
1179 if (REG_P (x) && REG_POINTER (x))
1180 p = find_temp_slot_from_address (x);
1182 /* If X is not in memory or is at a constant address, it cannot be in
1183 a temporary slot, but it can contain something whose address was
1184 taken. */
1185 if (p == 0 && (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0))))
1187 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1189 next = p->next;
1191 if (p->addr_taken)
1192 move_slot_to_level (p, temp_slot_level - 1);
1195 return;
1198 /* First see if we can find a match. */
1199 if (p == 0)
1200 p = find_temp_slot_from_address (XEXP (x, 0));
1202 if (p != 0)
1204 /* Move everything at our level whose address was taken to our new
1205 level in case we used its address. */
1206 struct temp_slot *q;
1208 if (p->level == temp_slot_level)
1210 for (q = *temp_slots_at_level (temp_slot_level); q; q = next)
1212 next = q->next;
1214 if (p != q && q->addr_taken)
1215 move_slot_to_level (q, temp_slot_level - 1);
1218 move_slot_to_level (p, temp_slot_level - 1);
1219 p->addr_taken = 0;
1221 return;
1224 /* Otherwise, preserve all non-kept slots at this level. */
1225 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1227 next = p->next;
1229 if (!p->keep)
1230 move_slot_to_level (p, temp_slot_level - 1);
1234 /* X is the result of an RTL_EXPR. If it is a temporary slot associated
1235 with that RTL_EXPR, promote it into a temporary slot at the present
1236 level so it will not be freed when we free slots made in the
1237 RTL_EXPR. */
1239 void
1240 preserve_rtl_expr_result (rtx x)
1242 struct temp_slot *p;
1244 /* If X is not in memory or is at a constant address, it cannot be in
1245 a temporary slot. */
1246 if (x == 0 || GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1247 return;
1249 /* If we can find a match, move it to our level unless it is already at
1250 an upper level. */
1251 p = find_temp_slot_from_address (XEXP (x, 0));
1252 if (p != 0)
1254 move_slot_to_level (p, MIN (p->level, temp_slot_level));
1255 p->rtl_expr = 0;
1258 return;
1261 /* Free all temporaries used so far. This is normally called at the end
1262 of generating code for a statement. Don't free any temporaries
1263 currently in use for an RTL_EXPR that hasn't yet been emitted.
1264 We could eventually do better than this since it can be reused while
1265 generating the same RTL_EXPR, but this is complex and probably not
1266 worthwhile. */
1268 void
1269 free_temp_slots (void)
1271 struct temp_slot *p, *next;
1273 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1275 next = p->next;
1277 if (!p->keep && p->rtl_expr == 0)
1278 make_slot_available (p);
1281 combine_temp_slots ();
1284 /* Free all temporary slots used in T, an RTL_EXPR node. */
1286 void
1287 free_temps_for_rtl_expr (tree t)
1289 struct temp_slot *p, *next;
1291 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1293 next = p->next;
1295 if (p->rtl_expr == t)
1297 /* If this slot is below the current TEMP_SLOT_LEVEL, then it
1298 needs to be preserved. This can happen if a temporary in
1299 the RTL_EXPR was addressed; preserve_temp_slots will move
1300 the temporary into a higher level. */
1301 if (temp_slot_level <= p->level)
1302 make_slot_available (p);
1303 else
1304 p->rtl_expr = NULL_TREE;
1308 combine_temp_slots ();
1311 /* Push deeper into the nesting level for stack temporaries. */
1313 void
1314 push_temp_slots (void)
1316 temp_slot_level++;
1319 /* Pop a temporary nesting level. All slots in use in the current level
1320 are freed. */
1322 void
1323 pop_temp_slots (void)
1325 struct temp_slot *p, *next;
1327 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1329 next = p->next;
1331 if (p->rtl_expr == 0)
1332 make_slot_available (p);
1335 combine_temp_slots ();
1337 temp_slot_level--;
1340 /* Initialize temporary slots. */
1342 void
1343 init_temp_slots (void)
1345 /* We have not allocated any temporaries yet. */
1346 avail_temp_slots = 0;
1347 used_temp_slots = 0;
1348 temp_slot_level = 0;
1349 var_temp_slot_level = 0;
1350 target_temp_slot_level = 0;
1353 /* Retroactively move an auto variable from a register to a stack
1354 slot. This is done when an address-reference to the variable is
1355 seen. If RESCAN is true, all previously emitted instructions are
1356 examined and modified to handle the fact that DECL is now
1357 addressable. */
1359 void
1360 put_var_into_stack (tree decl, int rescan)
1362 rtx orig_reg, reg;
1363 enum machine_mode promoted_mode, decl_mode;
1364 struct function *function = 0;
1365 tree context;
1366 bool can_use_addressof_p;
1367 bool volatile_p = TREE_CODE (decl) != SAVE_EXPR && TREE_THIS_VOLATILE (decl);
1368 bool used_p = (TREE_USED (decl)
1369 || (TREE_CODE (decl) != SAVE_EXPR && DECL_INITIAL (decl) != 0));
1371 context = decl_function_context (decl);
1373 /* Get the current rtl used for this object and its original mode. */
1374 orig_reg = reg = (TREE_CODE (decl) == SAVE_EXPR
1375 ? SAVE_EXPR_RTL (decl)
1376 : DECL_RTL_IF_SET (decl));
1378 /* No need to do anything if decl has no rtx yet
1379 since in that case caller is setting TREE_ADDRESSABLE
1380 and a stack slot will be assigned when the rtl is made. */
1381 if (reg == 0)
1382 return;
1384 /* Get the declared mode for this object. */
1385 decl_mode = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
1386 : DECL_MODE (decl));
1387 /* Get the mode it's actually stored in. */
1388 promoted_mode = GET_MODE (reg);
1390 /* If this variable comes from an outer function, find that
1391 function's saved context. Don't use find_function_data here,
1392 because it might not be in any active function.
1393 FIXME: Is that really supposed to happen?
1394 It does in ObjC at least. */
1395 if (context != current_function_decl)
1396 for (function = outer_function_chain; function; function = function->outer)
1397 if (function->decl == context)
1398 break;
1400 /* If this is a variable-sized object or a structure passed by invisible
1401 reference, with a pseudo to address it, put that pseudo into the stack
1402 if the var is non-local. */
1403 if (TREE_CODE (decl) != SAVE_EXPR && DECL_NONLOCAL (decl)
1404 && GET_CODE (reg) == MEM
1405 && REG_P (XEXP (reg, 0))
1406 && REGNO (XEXP (reg, 0)) > LAST_VIRTUAL_REGISTER)
1408 orig_reg = reg = XEXP (reg, 0);
1409 decl_mode = promoted_mode = GET_MODE (reg);
1412 /* If this variable lives in the current function and we don't need to put it
1413 in the stack for the sake of setjmp or the non-locality, try to keep it in
1414 a register until we know we actually need the address. */
1415 can_use_addressof_p
1416 = (function == 0
1417 && ! (TREE_CODE (decl) != SAVE_EXPR && DECL_NONLOCAL (decl))
1418 && optimize > 0
1419 /* FIXME make it work for promoted modes too */
1420 && decl_mode == promoted_mode
1421 #ifdef NON_SAVING_SETJMP
1422 && ! (NON_SAVING_SETJMP && current_function_calls_setjmp)
1423 #endif
1426 /* If we can't use ADDRESSOF, make sure we see through one we already
1427 generated. */
1428 if (! can_use_addressof_p
1429 && GET_CODE (reg) == MEM
1430 && GET_CODE (XEXP (reg, 0)) == ADDRESSOF)
1431 reg = XEXP (XEXP (reg, 0), 0);
1433 /* Now we should have a value that resides in one or more pseudo regs. */
1435 if (REG_P (reg))
1437 if (can_use_addressof_p)
1438 gen_mem_addressof (reg, decl, rescan);
1439 else
1440 put_reg_into_stack (function, reg, TREE_TYPE (decl), decl_mode,
1441 0, volatile_p, used_p, false, 0);
1443 /* If this was previously a MEM but we've removed the ADDRESSOF,
1444 set this address into that MEM so we always use the same
1445 rtx for this variable. */
1446 if (orig_reg != reg && GET_CODE (orig_reg) == MEM)
1447 XEXP (orig_reg, 0) = XEXP (reg, 0);
1449 else if (GET_CODE (reg) == CONCAT)
1451 /* A CONCAT contains two pseudos; put them both in the stack.
1452 We do it so they end up consecutive.
1453 We fixup references to the parts only after we fixup references
1454 to the whole CONCAT, lest we do double fixups for the latter
1455 references. */
1456 enum machine_mode part_mode = GET_MODE (XEXP (reg, 0));
1457 tree part_type = lang_hooks.types.type_for_mode (part_mode, 0);
1458 rtx lopart = XEXP (reg, 0);
1459 rtx hipart = XEXP (reg, 1);
1460 #ifdef FRAME_GROWS_DOWNWARD
1461 /* Since part 0 should have a lower address, do it second. */
1462 put_reg_into_stack (function, hipart, part_type, part_mode,
1463 0, volatile_p, false, false, 0);
1464 put_reg_into_stack (function, lopart, part_type, part_mode,
1465 0, volatile_p, false, true, 0);
1466 #else
1467 put_reg_into_stack (function, lopart, part_type, part_mode,
1468 0, volatile_p, false, false, 0);
1469 put_reg_into_stack (function, hipart, part_type, part_mode,
1470 0, volatile_p, false, true, 0);
1471 #endif
1473 /* Change the CONCAT into a combined MEM for both parts. */
1474 PUT_CODE (reg, MEM);
1475 MEM_ATTRS (reg) = 0;
1477 /* set_mem_attributes uses DECL_RTL to avoid re-generating of
1478 already computed alias sets. Here we want to re-generate. */
1479 if (DECL_P (decl))
1480 SET_DECL_RTL (decl, NULL);
1481 set_mem_attributes (reg, decl, 1);
1482 if (DECL_P (decl))
1483 SET_DECL_RTL (decl, reg);
1485 /* The two parts are in memory order already.
1486 Use the lower parts address as ours. */
1487 XEXP (reg, 0) = XEXP (XEXP (reg, 0), 0);
1488 /* Prevent sharing of rtl that might lose. */
1489 if (GET_CODE (XEXP (reg, 0)) == PLUS)
1490 XEXP (reg, 0) = copy_rtx (XEXP (reg, 0));
1491 if (used_p && rescan)
1493 schedule_fixup_var_refs (function, reg, TREE_TYPE (decl),
1494 promoted_mode, 0);
1495 schedule_fixup_var_refs (function, lopart, part_type, part_mode, 0);
1496 schedule_fixup_var_refs (function, hipart, part_type, part_mode, 0);
1499 else
1500 return;
1503 /* Subroutine of put_var_into_stack. This puts a single pseudo reg REG
1504 into the stack frame of FUNCTION (0 means the current function).
1505 TYPE is the user-level data type of the value hold in the register.
1506 DECL_MODE is the machine mode of the user-level data type.
1507 ORIGINAL_REGNO must be set if the real regno is not visible in REG.
1508 VOLATILE_P is true if this is for a "volatile" decl.
1509 USED_P is true if this reg might have already been used in an insn.
1510 CONSECUTIVE_P is true if the stack slot assigned to reg must be
1511 consecutive with the previous stack slot. */
1513 static void
1514 put_reg_into_stack (struct function *function, rtx reg, tree type,
1515 enum machine_mode decl_mode, unsigned int original_regno,
1516 bool volatile_p, bool used_p, bool consecutive_p,
1517 htab_t ht)
1519 struct function *func = function ? function : cfun;
1520 enum machine_mode mode = GET_MODE (reg);
1521 unsigned int regno = original_regno;
1522 rtx new = 0;
1524 if (regno == 0)
1525 regno = REGNO (reg);
1527 if (regno < func->x_max_parm_reg)
1529 if (!func->x_parm_reg_stack_loc)
1530 abort ();
1531 new = func->x_parm_reg_stack_loc[regno];
1534 if (new == 0)
1535 new = assign_stack_local_1 (decl_mode, GET_MODE_SIZE (decl_mode),
1536 consecutive_p ? -2 : 0, func);
1538 PUT_CODE (reg, MEM);
1539 PUT_MODE (reg, decl_mode);
1540 XEXP (reg, 0) = XEXP (new, 0);
1541 MEM_ATTRS (reg) = 0;
1542 /* `volatil' bit means one thing for MEMs, another entirely for REGs. */
1543 MEM_VOLATILE_P (reg) = volatile_p;
1545 /* If this is a memory ref that contains aggregate components,
1546 mark it as such for cse and loop optimize. If we are reusing a
1547 previously generated stack slot, then we need to copy the bit in
1548 case it was set for other reasons. For instance, it is set for
1549 __builtin_va_alist. */
1550 if (type)
1552 MEM_SET_IN_STRUCT_P (reg,
1553 AGGREGATE_TYPE_P (type) || MEM_IN_STRUCT_P (new));
1554 set_mem_alias_set (reg, get_alias_set (type));
1557 if (used_p)
1558 schedule_fixup_var_refs (function, reg, type, mode, ht);
1561 /* Make sure that all refs to the variable, previously made
1562 when it was a register, are fixed up to be valid again.
1563 See function above for meaning of arguments. */
1565 static void
1566 schedule_fixup_var_refs (struct function *function, rtx reg, tree type,
1567 enum machine_mode promoted_mode, htab_t ht)
1569 int unsigned_p = type ? TYPE_UNSIGNED (type) : 0;
1571 if (function != 0)
1573 struct var_refs_queue *temp;
1575 temp = ggc_alloc (sizeof (struct var_refs_queue));
1576 temp->modified = reg;
1577 temp->promoted_mode = promoted_mode;
1578 temp->unsignedp = unsigned_p;
1579 temp->next = function->fixup_var_refs_queue;
1580 function->fixup_var_refs_queue = temp;
1582 else
1583 /* Variable is local; fix it up now. */
1584 fixup_var_refs (reg, promoted_mode, unsigned_p, reg, ht);
1587 static void
1588 fixup_var_refs (rtx var, enum machine_mode promoted_mode, int unsignedp,
1589 rtx may_share, htab_t ht)
1591 tree pending;
1592 rtx first_insn = get_insns ();
1593 struct sequence_stack *stack = seq_stack;
1594 tree rtl_exps = rtl_expr_chain;
1595 int save_volatile_ok = volatile_ok;
1597 /* If there's a hash table, it must record all uses of VAR. */
1598 if (ht)
1600 if (stack != 0)
1601 abort ();
1602 fixup_var_refs_insns_with_hash (ht, var, promoted_mode, unsignedp,
1603 may_share);
1604 return;
1607 /* Volatile is valid in MEMs because all we're doing in changing the
1608 address inside. */
1609 volatile_ok = 1;
1610 fixup_var_refs_insns (first_insn, var, promoted_mode, unsignedp,
1611 stack == 0, may_share);
1613 /* Scan all pending sequences too. */
1614 for (; stack; stack = stack->next)
1616 push_to_full_sequence (stack->first, stack->last);
1617 fixup_var_refs_insns (stack->first, var, promoted_mode, unsignedp,
1618 stack->next != 0, may_share);
1619 /* Update bounds of sequence in case we added insns. */
1620 stack->first = get_insns ();
1621 stack->last = get_last_insn ();
1622 end_sequence ();
1625 /* Scan all waiting RTL_EXPRs too. */
1626 for (pending = rtl_exps; pending; pending = TREE_CHAIN (pending))
1628 rtx seq = RTL_EXPR_SEQUENCE (TREE_VALUE (pending));
1629 if (seq != const0_rtx && seq != 0)
1631 push_to_sequence (seq);
1632 fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0,
1633 may_share);
1634 end_sequence ();
1638 volatile_ok = save_volatile_ok;
1641 /* REPLACEMENTS is a pointer to a list of the struct fixup_replacement and X is
1642 some part of an insn. Return a struct fixup_replacement whose OLD
1643 value is equal to X. Allocate a new structure if no such entry exists. */
1645 static struct fixup_replacement *
1646 find_fixup_replacement (struct fixup_replacement **replacements, rtx x)
1648 struct fixup_replacement *p;
1650 /* See if we have already replaced this. */
1651 for (p = *replacements; p != 0 && ! rtx_equal_p (p->old, x); p = p->next)
1654 if (p == 0)
1656 p = xmalloc (sizeof (struct fixup_replacement));
1657 p->old = x;
1658 p->new = 0;
1659 p->next = *replacements;
1660 *replacements = p;
1663 return p;
1666 /* Scan the insn-chain starting with INSN for refs to VAR and fix them
1667 up. TOPLEVEL is nonzero if this chain is the main chain of insns
1668 for the current function. MAY_SHARE is either a MEM that is not
1669 to be unshared or a list of them. */
1671 static void
1672 fixup_var_refs_insns (rtx insn, rtx var, enum machine_mode promoted_mode,
1673 int unsignedp, int toplevel, rtx may_share)
1675 while (insn)
1677 /* fixup_var_refs_insn might modify insn, so save its next
1678 pointer now. */
1679 rtx next = NEXT_INSN (insn);
1681 if (INSN_P (insn))
1682 fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, toplevel,
1683 may_share);
1685 insn = next;
1689 /* Look up the insns which reference VAR in HT and fix them up. Other
1690 arguments are the same as fixup_var_refs_insns. */
1692 static void
1693 fixup_var_refs_insns_with_hash (htab_t ht, rtx var, enum machine_mode promoted_mode,
1694 int unsignedp, rtx may_share)
1696 struct insns_for_mem_entry tmp;
1697 struct insns_for_mem_entry *ime;
1698 rtx insn_list;
1700 tmp.key = var;
1701 ime = htab_find (ht, &tmp);
1702 for (insn_list = ime->insns; insn_list != 0; insn_list = XEXP (insn_list, 1))
1703 if (INSN_P (XEXP (insn_list, 0)))
1704 fixup_var_refs_insn (XEXP (insn_list, 0), var, promoted_mode,
1705 unsignedp, 1, may_share);
1709 /* Per-insn processing by fixup_var_refs_insns(_with_hash). INSN is
1710 the insn under examination, VAR is the variable to fix up
1711 references to, PROMOTED_MODE and UNSIGNEDP describe VAR, and
1712 TOPLEVEL is nonzero if this is the main insn chain for this
1713 function. */
1715 static void
1716 fixup_var_refs_insn (rtx insn, rtx var, enum machine_mode promoted_mode,
1717 int unsignedp, int toplevel, rtx no_share)
1719 rtx call_dest = 0;
1720 rtx set, prev, prev_set;
1721 rtx note;
1723 /* Remember the notes in case we delete the insn. */
1724 note = REG_NOTES (insn);
1726 /* If this is a CLOBBER of VAR, delete it.
1728 If it has a REG_LIBCALL note, delete the REG_LIBCALL
1729 and REG_RETVAL notes too. */
1730 if (GET_CODE (PATTERN (insn)) == CLOBBER
1731 && (XEXP (PATTERN (insn), 0) == var
1732 || (GET_CODE (XEXP (PATTERN (insn), 0)) == CONCAT
1733 && (XEXP (XEXP (PATTERN (insn), 0), 0) == var
1734 || XEXP (XEXP (PATTERN (insn), 0), 1) == var))))
1736 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0)
1737 /* The REG_LIBCALL note will go away since we are going to
1738 turn INSN into a NOTE, so just delete the
1739 corresponding REG_RETVAL note. */
1740 remove_note (XEXP (note, 0),
1741 find_reg_note (XEXP (note, 0), REG_RETVAL,
1742 NULL_RTX));
1744 delete_insn (insn);
1747 /* The insn to load VAR from a home in the arglist
1748 is now a no-op. When we see it, just delete it.
1749 Similarly if this is storing VAR from a register from which
1750 it was loaded in the previous insn. This will occur
1751 when an ADDRESSOF was made for an arglist slot. */
1752 else if (toplevel
1753 && (set = single_set (insn)) != 0
1754 && SET_DEST (set) == var
1755 /* If this represents the result of an insn group,
1756 don't delete the insn. */
1757 && find_reg_note (insn, REG_RETVAL, NULL_RTX) == 0
1758 && (rtx_equal_p (SET_SRC (set), var)
1759 || (REG_P (SET_SRC (set))
1760 && (prev = prev_nonnote_insn (insn)) != 0
1761 && (prev_set = single_set (prev)) != 0
1762 && SET_DEST (prev_set) == SET_SRC (set)
1763 && rtx_equal_p (SET_SRC (prev_set), var))))
1765 delete_insn (insn);
1767 else
1769 struct fixup_replacement *replacements = 0;
1771 if (SMALL_REGISTER_CLASSES)
1773 /* If the insn that copies the results of a CALL_INSN
1774 into a pseudo now references VAR, we have to use an
1775 intermediate pseudo since we want the life of the
1776 return value register to be only a single insn.
1778 If we don't use an intermediate pseudo, such things as
1779 address computations to make the address of VAR valid
1780 if it is not can be placed between the CALL_INSN and INSN.
1782 To make sure this doesn't happen, we record the destination
1783 of the CALL_INSN and see if the next insn uses both that
1784 and VAR. */
1786 if (call_dest != 0 && GET_CODE (insn) == INSN
1787 && reg_mentioned_p (var, PATTERN (insn))
1788 && reg_mentioned_p (call_dest, PATTERN (insn)))
1790 rtx temp = gen_reg_rtx (GET_MODE (call_dest));
1792 emit_insn_before (gen_move_insn (temp, call_dest), insn);
1794 PATTERN (insn) = replace_rtx (PATTERN (insn),
1795 call_dest, temp);
1798 if (GET_CODE (insn) == CALL_INSN
1799 && GET_CODE (PATTERN (insn)) == SET)
1800 call_dest = SET_DEST (PATTERN (insn));
1801 else if (GET_CODE (insn) == CALL_INSN
1802 && GET_CODE (PATTERN (insn)) == PARALLEL
1803 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1804 call_dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1805 else
1806 call_dest = 0;
1809 /* See if we have to do anything to INSN now that VAR is in
1810 memory. If it needs to be loaded into a pseudo, use a single
1811 pseudo for the entire insn in case there is a MATCH_DUP
1812 between two operands. We pass a pointer to the head of
1813 a list of struct fixup_replacements. If fixup_var_refs_1
1814 needs to allocate pseudos or replacement MEMs (for SUBREGs),
1815 it will record them in this list.
1817 If it allocated a pseudo for any replacement, we copy into
1818 it here. */
1820 fixup_var_refs_1 (var, promoted_mode, &PATTERN (insn), insn,
1821 &replacements, no_share);
1823 while (replacements)
1825 struct fixup_replacement *next;
1827 if (REG_P (replacements->new))
1829 rtx insert_before;
1830 rtx seq;
1832 /* OLD might be a (subreg (mem)). */
1833 if (GET_CODE (replacements->old) == SUBREG)
1834 replacements->old
1835 = fixup_memory_subreg (replacements->old, insn,
1836 promoted_mode, 0);
1837 else
1838 replacements->old
1839 = fixup_stack_1 (replacements->old, insn);
1841 insert_before = insn;
1843 /* If we are changing the mode, do a conversion.
1844 This might be wasteful, but combine.c will
1845 eliminate much of the waste. */
1847 if (GET_MODE (replacements->new)
1848 != GET_MODE (replacements->old))
1850 start_sequence ();
1851 convert_move (replacements->new,
1852 replacements->old, unsignedp);
1853 seq = get_insns ();
1854 end_sequence ();
1856 else
1857 seq = gen_move_insn (replacements->new,
1858 replacements->old);
1860 emit_insn_before (seq, insert_before);
1863 next = replacements->next;
1864 free (replacements);
1865 replacements = next;
1869 /* Also fix up any invalid exprs in the REG_NOTES of this insn.
1870 But don't touch other insns referred to by reg-notes;
1871 we will get them elsewhere. */
1872 while (note)
1874 if (GET_CODE (note) != INSN_LIST)
1875 XEXP (note, 0)
1876 = walk_fixup_memory_subreg (XEXP (note, 0), insn, var,
1877 promoted_mode, 1);
1878 note = XEXP (note, 1);
1882 /* VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
1883 See if the rtx expression at *LOC in INSN needs to be changed.
1885 REPLACEMENTS is a pointer to a list head that starts out zero, but may
1886 contain a list of original rtx's and replacements. If we find that we need
1887 to modify this insn by replacing a memory reference with a pseudo or by
1888 making a new MEM to implement a SUBREG, we consult that list to see if
1889 we have already chosen a replacement. If none has already been allocated,
1890 we allocate it and update the list. fixup_var_refs_insn will copy VAR
1891 or the SUBREG, as appropriate, to the pseudo. */
1893 static void
1894 fixup_var_refs_1 (rtx var, enum machine_mode promoted_mode, rtx *loc, rtx insn,
1895 struct fixup_replacement **replacements, rtx no_share)
1897 int i;
1898 rtx x = *loc;
1899 RTX_CODE code = GET_CODE (x);
1900 const char *fmt;
1901 rtx tem, tem1;
1902 struct fixup_replacement *replacement;
1904 switch (code)
1906 case ADDRESSOF:
1907 if (XEXP (x, 0) == var)
1909 /* Prevent sharing of rtl that might lose. */
1910 rtx sub = copy_rtx (XEXP (var, 0));
1912 if (! validate_change (insn, loc, sub, 0))
1914 rtx y = gen_reg_rtx (GET_MODE (sub));
1915 rtx seq, new_insn;
1917 /* We should be able to replace with a register or all is lost.
1918 Note that we can't use validate_change to verify this, since
1919 we're not caring for replacing all dups simultaneously. */
1920 if (! validate_replace_rtx (*loc, y, insn))
1921 abort ();
1923 /* Careful! First try to recognize a direct move of the
1924 value, mimicking how things are done in gen_reload wrt
1925 PLUS. Consider what happens when insn is a conditional
1926 move instruction and addsi3 clobbers flags. */
1928 start_sequence ();
1929 new_insn = emit_insn (gen_rtx_SET (VOIDmode, y, sub));
1930 seq = get_insns ();
1931 end_sequence ();
1933 if (recog_memoized (new_insn) < 0)
1935 /* That failed. Fall back on force_operand and hope. */
1937 start_sequence ();
1938 sub = force_operand (sub, y);
1939 if (sub != y)
1940 emit_insn (gen_move_insn (y, sub));
1941 seq = get_insns ();
1942 end_sequence ();
1945 #ifdef HAVE_cc0
1946 /* Don't separate setter from user. */
1947 if (PREV_INSN (insn) && sets_cc0_p (PREV_INSN (insn)))
1948 insn = PREV_INSN (insn);
1949 #endif
1951 emit_insn_before (seq, insn);
1954 return;
1956 case MEM:
1957 if (var == x)
1959 /* If we already have a replacement, use it. Otherwise,
1960 try to fix up this address in case it is invalid. */
1962 replacement = find_fixup_replacement (replacements, var);
1963 if (replacement->new)
1965 *loc = replacement->new;
1966 return;
1969 *loc = replacement->new = x = fixup_stack_1 (x, insn);
1971 /* Unless we are forcing memory to register or we changed the mode,
1972 we can leave things the way they are if the insn is valid. */
1974 INSN_CODE (insn) = -1;
1975 if (! flag_force_mem && GET_MODE (x) == promoted_mode
1976 && recog_memoized (insn) >= 0)
1977 return;
1979 *loc = replacement->new = gen_reg_rtx (promoted_mode);
1980 return;
1983 /* If X contains VAR, we need to unshare it here so that we update
1984 each occurrence separately. But all identical MEMs in one insn
1985 must be replaced with the same rtx because of the possibility of
1986 MATCH_DUPs. */
1988 if (reg_mentioned_p (var, x))
1990 replacement = find_fixup_replacement (replacements, x);
1991 if (replacement->new == 0)
1992 replacement->new = copy_most_rtx (x, no_share);
1994 *loc = x = replacement->new;
1995 code = GET_CODE (x);
1997 break;
1999 case REG:
2000 case CC0:
2001 case PC:
2002 case CONST_INT:
2003 case CONST:
2004 case SYMBOL_REF:
2005 case LABEL_REF:
2006 case CONST_DOUBLE:
2007 case CONST_VECTOR:
2008 return;
2010 case SIGN_EXTRACT:
2011 case ZERO_EXTRACT:
2012 /* Note that in some cases those types of expressions are altered
2013 by optimize_bit_field, and do not survive to get here. */
2014 if (XEXP (x, 0) == var
2015 || (GET_CODE (XEXP (x, 0)) == SUBREG
2016 && SUBREG_REG (XEXP (x, 0)) == var))
2018 /* Get TEM as a valid MEM in the mode presently in the insn.
2020 We don't worry about the possibility of MATCH_DUP here; it
2021 is highly unlikely and would be tricky to handle. */
2023 tem = XEXP (x, 0);
2024 if (GET_CODE (tem) == SUBREG)
2026 if (GET_MODE_BITSIZE (GET_MODE (tem))
2027 > GET_MODE_BITSIZE (GET_MODE (var)))
2029 replacement = find_fixup_replacement (replacements, var);
2030 if (replacement->new == 0)
2031 replacement->new = gen_reg_rtx (GET_MODE (var));
2032 SUBREG_REG (tem) = replacement->new;
2034 /* The following code works only if we have a MEM, so we
2035 need to handle the subreg here. We directly substitute
2036 it assuming that a subreg must be OK here. We already
2037 scheduled a replacement to copy the mem into the
2038 subreg. */
2039 XEXP (x, 0) = tem;
2040 return;
2042 else
2043 tem = fixup_memory_subreg (tem, insn, promoted_mode, 0);
2045 else
2046 tem = fixup_stack_1 (tem, insn);
2048 /* Unless we want to load from memory, get TEM into the proper mode
2049 for an extract from memory. This can only be done if the
2050 extract is at a constant position and length. */
2052 if (! flag_force_mem && GET_CODE (XEXP (x, 1)) == CONST_INT
2053 && GET_CODE (XEXP (x, 2)) == CONST_INT
2054 && ! mode_dependent_address_p (XEXP (tem, 0))
2055 && ! MEM_VOLATILE_P (tem))
2057 enum machine_mode wanted_mode = VOIDmode;
2058 enum machine_mode is_mode = GET_MODE (tem);
2059 HOST_WIDE_INT pos = INTVAL (XEXP (x, 2));
2061 if (GET_CODE (x) == ZERO_EXTRACT)
2063 enum machine_mode new_mode
2064 = mode_for_extraction (EP_extzv, 1);
2065 if (new_mode != MAX_MACHINE_MODE)
2066 wanted_mode = new_mode;
2068 else if (GET_CODE (x) == SIGN_EXTRACT)
2070 enum machine_mode new_mode
2071 = mode_for_extraction (EP_extv, 1);
2072 if (new_mode != MAX_MACHINE_MODE)
2073 wanted_mode = new_mode;
2076 /* If we have a narrower mode, we can do something. */
2077 if (wanted_mode != VOIDmode
2078 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2080 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2081 rtx old_pos = XEXP (x, 2);
2082 rtx newmem;
2084 /* If the bytes and bits are counted differently, we
2085 must adjust the offset. */
2086 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2087 offset = (GET_MODE_SIZE (is_mode)
2088 - GET_MODE_SIZE (wanted_mode) - offset);
2090 pos %= GET_MODE_BITSIZE (wanted_mode);
2092 newmem = adjust_address_nv (tem, wanted_mode, offset);
2094 /* Make the change and see if the insn remains valid. */
2095 INSN_CODE (insn) = -1;
2096 XEXP (x, 0) = newmem;
2097 XEXP (x, 2) = GEN_INT (pos);
2099 if (recog_memoized (insn) >= 0)
2100 return;
2102 /* Otherwise, restore old position. XEXP (x, 0) will be
2103 restored later. */
2104 XEXP (x, 2) = old_pos;
2108 /* If we get here, the bitfield extract insn can't accept a memory
2109 reference. Copy the input into a register. */
2111 tem1 = gen_reg_rtx (GET_MODE (tem));
2112 emit_insn_before (gen_move_insn (tem1, tem), insn);
2113 XEXP (x, 0) = tem1;
2114 return;
2116 break;
2118 case SUBREG:
2119 if (SUBREG_REG (x) == var)
2121 /* If this is a special SUBREG made because VAR was promoted
2122 from a wider mode, replace it with VAR and call ourself
2123 recursively, this time saying that the object previously
2124 had its current mode (by virtue of the SUBREG). */
2126 if (SUBREG_PROMOTED_VAR_P (x))
2128 *loc = var;
2129 fixup_var_refs_1 (var, GET_MODE (var), loc, insn, replacements,
2130 no_share);
2131 return;
2134 /* If this SUBREG makes VAR wider, it has become a paradoxical
2135 SUBREG with VAR in memory, but these aren't allowed at this
2136 stage of the compilation. So load VAR into a pseudo and take
2137 a SUBREG of that pseudo. */
2138 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (var)))
2140 replacement = find_fixup_replacement (replacements, var);
2141 if (replacement->new == 0)
2142 replacement->new = gen_reg_rtx (promoted_mode);
2143 SUBREG_REG (x) = replacement->new;
2144 return;
2147 /* See if we have already found a replacement for this SUBREG.
2148 If so, use it. Otherwise, make a MEM and see if the insn
2149 is recognized. If not, or if we should force MEM into a register,
2150 make a pseudo for this SUBREG. */
2151 replacement = find_fixup_replacement (replacements, x);
2152 if (replacement->new)
2154 enum machine_mode mode = GET_MODE (x);
2155 *loc = replacement->new;
2157 /* Careful! We may have just replaced a SUBREG by a MEM, which
2158 means that the insn may have become invalid again. We can't
2159 in this case make a new replacement since we already have one
2160 and we must deal with MATCH_DUPs. */
2161 if (GET_CODE (replacement->new) == MEM)
2163 INSN_CODE (insn) = -1;
2164 if (recog_memoized (insn) >= 0)
2165 return;
2167 fixup_var_refs_1 (replacement->new, mode, &PATTERN (insn),
2168 insn, replacements, no_share);
2171 return;
2174 replacement->new = *loc = fixup_memory_subreg (x, insn,
2175 promoted_mode, 0);
2177 INSN_CODE (insn) = -1;
2178 if (! flag_force_mem && recog_memoized (insn) >= 0)
2179 return;
2181 *loc = replacement->new = gen_reg_rtx (GET_MODE (x));
2182 return;
2184 break;
2186 case SET:
2187 /* First do special simplification of bit-field references. */
2188 if (GET_CODE (SET_DEST (x)) == SIGN_EXTRACT
2189 || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2190 optimize_bit_field (x, insn, 0);
2191 if (GET_CODE (SET_SRC (x)) == SIGN_EXTRACT
2192 || GET_CODE (SET_SRC (x)) == ZERO_EXTRACT)
2193 optimize_bit_field (x, insn, 0);
2195 /* For a paradoxical SUBREG inside a ZERO_EXTRACT, load the object
2196 into a register and then store it back out. */
2197 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2198 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG
2199 && SUBREG_REG (XEXP (SET_DEST (x), 0)) == var
2200 && (GET_MODE_SIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2201 > GET_MODE_SIZE (GET_MODE (var))))
2203 replacement = find_fixup_replacement (replacements, var);
2204 if (replacement->new == 0)
2205 replacement->new = gen_reg_rtx (GET_MODE (var));
2207 SUBREG_REG (XEXP (SET_DEST (x), 0)) = replacement->new;
2208 emit_insn_after (gen_move_insn (var, replacement->new), insn);
2211 /* If SET_DEST is now a paradoxical SUBREG, put the result of this
2212 insn into a pseudo and store the low part of the pseudo into VAR. */
2213 if (GET_CODE (SET_DEST (x)) == SUBREG
2214 && SUBREG_REG (SET_DEST (x)) == var
2215 && (GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
2216 > GET_MODE_SIZE (GET_MODE (var))))
2218 SET_DEST (x) = tem = gen_reg_rtx (GET_MODE (SET_DEST (x)));
2219 emit_insn_after (gen_move_insn (var, gen_lowpart (GET_MODE (var),
2220 tem)),
2221 insn);
2222 break;
2226 rtx dest = SET_DEST (x);
2227 rtx src = SET_SRC (x);
2228 rtx outerdest = dest;
2230 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2231 || GET_CODE (dest) == SIGN_EXTRACT
2232 || GET_CODE (dest) == ZERO_EXTRACT)
2233 dest = XEXP (dest, 0);
2235 if (GET_CODE (src) == SUBREG)
2236 src = SUBREG_REG (src);
2238 /* If VAR does not appear at the top level of the SET
2239 just scan the lower levels of the tree. */
2241 if (src != var && dest != var)
2242 break;
2244 /* We will need to rerecognize this insn. */
2245 INSN_CODE (insn) = -1;
2247 if (GET_CODE (outerdest) == ZERO_EXTRACT && dest == var
2248 && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
2250 /* Since this case will return, ensure we fixup all the
2251 operands here. */
2252 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 1),
2253 insn, replacements, no_share);
2254 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 2),
2255 insn, replacements, no_share);
2256 fixup_var_refs_1 (var, promoted_mode, &SET_SRC (x),
2257 insn, replacements, no_share);
2259 tem = XEXP (outerdest, 0);
2261 /* Clean up (SUBREG:SI (MEM:mode ...) 0)
2262 that may appear inside a ZERO_EXTRACT.
2263 This was legitimate when the MEM was a REG. */
2264 if (GET_CODE (tem) == SUBREG
2265 && SUBREG_REG (tem) == var)
2266 tem = fixup_memory_subreg (tem, insn, promoted_mode, 0);
2267 else
2268 tem = fixup_stack_1 (tem, insn);
2270 if (GET_CODE (XEXP (outerdest, 1)) == CONST_INT
2271 && GET_CODE (XEXP (outerdest, 2)) == CONST_INT
2272 && ! mode_dependent_address_p (XEXP (tem, 0))
2273 && ! MEM_VOLATILE_P (tem))
2275 enum machine_mode wanted_mode;
2276 enum machine_mode is_mode = GET_MODE (tem);
2277 HOST_WIDE_INT pos = INTVAL (XEXP (outerdest, 2));
2279 wanted_mode = mode_for_extraction (EP_insv, 0);
2281 /* If we have a narrower mode, we can do something. */
2282 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2284 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2285 rtx old_pos = XEXP (outerdest, 2);
2286 rtx newmem;
2288 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2289 offset = (GET_MODE_SIZE (is_mode)
2290 - GET_MODE_SIZE (wanted_mode) - offset);
2292 pos %= GET_MODE_BITSIZE (wanted_mode);
2294 newmem = adjust_address_nv (tem, wanted_mode, offset);
2296 /* Make the change and see if the insn remains valid. */
2297 INSN_CODE (insn) = -1;
2298 XEXP (outerdest, 0) = newmem;
2299 XEXP (outerdest, 2) = GEN_INT (pos);
2301 if (recog_memoized (insn) >= 0)
2302 return;
2304 /* Otherwise, restore old position. XEXP (x, 0) will be
2305 restored later. */
2306 XEXP (outerdest, 2) = old_pos;
2310 /* If we get here, the bit-field store doesn't allow memory
2311 or isn't located at a constant position. Load the value into
2312 a register, do the store, and put it back into memory. */
2314 tem1 = gen_reg_rtx (GET_MODE (tem));
2315 emit_insn_before (gen_move_insn (tem1, tem), insn);
2316 emit_insn_after (gen_move_insn (tem, tem1), insn);
2317 XEXP (outerdest, 0) = tem1;
2318 return;
2321 /* STRICT_LOW_PART is a no-op on memory references
2322 and it can cause combinations to be unrecognizable,
2323 so eliminate it. */
2325 if (dest == var && GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2326 SET_DEST (x) = XEXP (SET_DEST (x), 0);
2328 /* A valid insn to copy VAR into or out of a register
2329 must be left alone, to avoid an infinite loop here.
2330 If the reference to VAR is by a subreg, fix that up,
2331 since SUBREG is not valid for a memref.
2332 Also fix up the address of the stack slot.
2334 Note that we must not try to recognize the insn until
2335 after we know that we have valid addresses and no
2336 (subreg (mem ...) ...) constructs, since these interfere
2337 with determining the validity of the insn. */
2339 if ((SET_SRC (x) == var
2340 || (GET_CODE (SET_SRC (x)) == SUBREG
2341 && SUBREG_REG (SET_SRC (x)) == var))
2342 && (REG_P (SET_DEST (x))
2343 || (GET_CODE (SET_DEST (x)) == SUBREG
2344 && REG_P (SUBREG_REG (SET_DEST (x)))))
2345 && GET_MODE (var) == promoted_mode
2346 && x == single_set (insn))
2348 rtx pat, last;
2350 if (GET_CODE (SET_SRC (x)) == SUBREG
2351 && (GET_MODE_SIZE (GET_MODE (SET_SRC (x)))
2352 > GET_MODE_SIZE (GET_MODE (var))))
2354 /* This (subreg VAR) is now a paradoxical subreg. We need
2355 to replace VAR instead of the subreg. */
2356 replacement = find_fixup_replacement (replacements, var);
2357 if (replacement->new == NULL_RTX)
2358 replacement->new = gen_reg_rtx (GET_MODE (var));
2359 SUBREG_REG (SET_SRC (x)) = replacement->new;
2361 else
2363 replacement = find_fixup_replacement (replacements, SET_SRC (x));
2364 if (replacement->new)
2365 SET_SRC (x) = replacement->new;
2366 else if (GET_CODE (SET_SRC (x)) == SUBREG)
2367 SET_SRC (x) = replacement->new
2368 = fixup_memory_subreg (SET_SRC (x), insn, promoted_mode,
2370 else
2371 SET_SRC (x) = replacement->new
2372 = fixup_stack_1 (SET_SRC (x), insn);
2375 if (recog_memoized (insn) >= 0)
2376 return;
2378 /* INSN is not valid, but we know that we want to
2379 copy SET_SRC (x) to SET_DEST (x) in some way. So
2380 we generate the move and see whether it requires more
2381 than one insn. If it does, we emit those insns and
2382 delete INSN. Otherwise, we can just replace the pattern
2383 of INSN; we have already verified above that INSN has
2384 no other function that to do X. */
2386 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2387 if (NEXT_INSN (pat) != NULL_RTX)
2389 last = emit_insn_before (pat, insn);
2391 /* INSN might have REG_RETVAL or other important notes, so
2392 we need to store the pattern of the last insn in the
2393 sequence into INSN similarly to the normal case. LAST
2394 should not have REG_NOTES, but we allow them if INSN has
2395 no REG_NOTES. */
2396 if (REG_NOTES (last) && REG_NOTES (insn))
2397 abort ();
2398 if (REG_NOTES (last))
2399 REG_NOTES (insn) = REG_NOTES (last);
2400 PATTERN (insn) = PATTERN (last);
2402 delete_insn (last);
2404 else
2405 PATTERN (insn) = PATTERN (pat);
2407 return;
2410 if ((SET_DEST (x) == var
2411 || (GET_CODE (SET_DEST (x)) == SUBREG
2412 && SUBREG_REG (SET_DEST (x)) == var))
2413 && (REG_P (SET_SRC (x))
2414 || (GET_CODE (SET_SRC (x)) == SUBREG
2415 && REG_P (SUBREG_REG (SET_SRC (x)))))
2416 && GET_MODE (var) == promoted_mode
2417 && x == single_set (insn))
2419 rtx pat, last;
2421 if (GET_CODE (SET_DEST (x)) == SUBREG)
2422 SET_DEST (x) = fixup_memory_subreg (SET_DEST (x), insn,
2423 promoted_mode, 0);
2424 else
2425 SET_DEST (x) = fixup_stack_1 (SET_DEST (x), insn);
2427 if (recog_memoized (insn) >= 0)
2428 return;
2430 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2431 if (NEXT_INSN (pat) != NULL_RTX)
2433 last = emit_insn_before (pat, insn);
2435 /* INSN might have REG_RETVAL or other important notes, so
2436 we need to store the pattern of the last insn in the
2437 sequence into INSN similarly to the normal case. LAST
2438 should not have REG_NOTES, but we allow them if INSN has
2439 no REG_NOTES. */
2440 if (REG_NOTES (last) && REG_NOTES (insn))
2441 abort ();
2442 if (REG_NOTES (last))
2443 REG_NOTES (insn) = REG_NOTES (last);
2444 PATTERN (insn) = PATTERN (last);
2446 delete_insn (last);
2448 else
2449 PATTERN (insn) = PATTERN (pat);
2451 return;
2454 /* Otherwise, storing into VAR must be handled specially
2455 by storing into a temporary and copying that into VAR
2456 with a new insn after this one. Note that this case
2457 will be used when storing into a promoted scalar since
2458 the insn will now have different modes on the input
2459 and output and hence will be invalid (except for the case
2460 of setting it to a constant, which does not need any
2461 change if it is valid). We generate extra code in that case,
2462 but combine.c will eliminate it. */
2464 if (dest == var)
2466 rtx temp;
2467 rtx fixeddest = SET_DEST (x);
2468 enum machine_mode temp_mode;
2470 /* STRICT_LOW_PART can be discarded, around a MEM. */
2471 if (GET_CODE (fixeddest) == STRICT_LOW_PART)
2472 fixeddest = XEXP (fixeddest, 0);
2473 /* Convert (SUBREG (MEM)) to a MEM in a changed mode. */
2474 if (GET_CODE (fixeddest) == SUBREG)
2476 fixeddest = fixup_memory_subreg (fixeddest, insn,
2477 promoted_mode, 0);
2478 temp_mode = GET_MODE (fixeddest);
2480 else
2482 fixeddest = fixup_stack_1 (fixeddest, insn);
2483 temp_mode = promoted_mode;
2486 temp = gen_reg_rtx (temp_mode);
2488 emit_insn_after (gen_move_insn (fixeddest,
2489 gen_lowpart (GET_MODE (fixeddest),
2490 temp)),
2491 insn);
2493 SET_DEST (x) = temp;
2497 default:
2498 break;
2501 /* Nothing special about this RTX; fix its operands. */
2503 fmt = GET_RTX_FORMAT (code);
2504 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2506 if (fmt[i] == 'e')
2507 fixup_var_refs_1 (var, promoted_mode, &XEXP (x, i), insn, replacements,
2508 no_share);
2509 else if (fmt[i] == 'E')
2511 int j;
2512 for (j = 0; j < XVECLEN (x, i); j++)
2513 fixup_var_refs_1 (var, promoted_mode, &XVECEXP (x, i, j),
2514 insn, replacements, no_share);
2519 /* Previously, X had the form (SUBREG:m1 (REG:PROMOTED_MODE ...)).
2520 The REG was placed on the stack, so X now has the form (SUBREG:m1
2521 (MEM:m2 ...)).
2523 Return an rtx (MEM:m1 newaddr) which is equivalent. If any insns
2524 must be emitted to compute NEWADDR, put them before INSN.
2526 UNCRITICAL nonzero means accept paradoxical subregs.
2527 This is used for subregs found inside REG_NOTES. */
2529 static rtx
2530 fixup_memory_subreg (rtx x, rtx insn, enum machine_mode promoted_mode, int uncritical)
2532 int offset;
2533 rtx mem = SUBREG_REG (x);
2534 rtx addr = XEXP (mem, 0);
2535 enum machine_mode mode = GET_MODE (x);
2536 rtx result, seq;
2538 /* Paradoxical SUBREGs are usually invalid during RTL generation. */
2539 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (mem)) && ! uncritical)
2540 abort ();
2542 offset = SUBREG_BYTE (x);
2543 if (BYTES_BIG_ENDIAN)
2544 /* If the PROMOTED_MODE is wider than the mode of the MEM, adjust
2545 the offset so that it points to the right location within the
2546 MEM. */
2547 offset -= (GET_MODE_SIZE (promoted_mode) - GET_MODE_SIZE (GET_MODE (mem)));
2549 if (!flag_force_addr
2550 && memory_address_p (mode, plus_constant (addr, offset)))
2551 /* Shortcut if no insns need be emitted. */
2552 return adjust_address (mem, mode, offset);
2554 start_sequence ();
2555 result = adjust_address (mem, mode, offset);
2556 seq = get_insns ();
2557 end_sequence ();
2559 emit_insn_before (seq, insn);
2560 return result;
2563 /* Do fixup_memory_subreg on all (SUBREG (VAR) ...) contained in X.
2564 VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
2565 Replace subexpressions of X in place.
2566 If X itself is a (SUBREG (VAR) ...), return the replacement expression.
2567 Otherwise return X, with its contents possibly altered.
2569 INSN and UNCRITICAL are as for fixup_memory_subreg. */
2571 static rtx
2572 walk_fixup_memory_subreg (rtx x, rtx insn, rtx var,
2573 enum machine_mode promoted_mode, int uncritical)
2575 enum rtx_code code;
2576 const char *fmt;
2577 int i;
2579 if (x == 0)
2580 return 0;
2582 code = GET_CODE (x);
2584 if (code == SUBREG && SUBREG_REG (x) == var)
2585 return fixup_memory_subreg (x, insn, promoted_mode, uncritical);
2587 /* Nothing special about this RTX; fix its operands. */
2589 fmt = GET_RTX_FORMAT (code);
2590 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2592 if (fmt[i] == 'e')
2593 XEXP (x, i) = walk_fixup_memory_subreg (XEXP (x, i), insn, var,
2594 promoted_mode, uncritical);
2595 else if (fmt[i] == 'E')
2597 int j;
2598 for (j = 0; j < XVECLEN (x, i); j++)
2599 XVECEXP (x, i, j)
2600 = walk_fixup_memory_subreg (XVECEXP (x, i, j), insn, var,
2601 promoted_mode, uncritical);
2604 return x;
2607 /* For each memory ref within X, if it refers to a stack slot
2608 with an out of range displacement, put the address in a temp register
2609 (emitting new insns before INSN to load these registers)
2610 and alter the memory ref to use that register.
2611 Replace each such MEM rtx with a copy, to avoid clobberage. */
2613 static rtx
2614 fixup_stack_1 (rtx x, rtx insn)
2616 int i;
2617 RTX_CODE code = GET_CODE (x);
2618 const char *fmt;
2620 if (code == MEM)
2622 rtx ad = XEXP (x, 0);
2623 /* If we have address of a stack slot but it's not valid
2624 (displacement is too large), compute the sum in a register. */
2625 if (GET_CODE (ad) == PLUS
2626 && REG_P (XEXP (ad, 0))
2627 && ((REGNO (XEXP (ad, 0)) >= FIRST_VIRTUAL_REGISTER
2628 && REGNO (XEXP (ad, 0)) <= LAST_VIRTUAL_REGISTER)
2629 || REGNO (XEXP (ad, 0)) == FRAME_POINTER_REGNUM
2630 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2631 || REGNO (XEXP (ad, 0)) == HARD_FRAME_POINTER_REGNUM
2632 #endif
2633 || REGNO (XEXP (ad, 0)) == STACK_POINTER_REGNUM
2634 || REGNO (XEXP (ad, 0)) == ARG_POINTER_REGNUM
2635 || XEXP (ad, 0) == current_function_internal_arg_pointer)
2636 && GET_CODE (XEXP (ad, 1)) == CONST_INT)
2638 rtx temp, seq;
2639 if (memory_address_p (GET_MODE (x), ad))
2640 return x;
2642 start_sequence ();
2643 temp = copy_to_reg (ad);
2644 seq = get_insns ();
2645 end_sequence ();
2646 emit_insn_before (seq, insn);
2647 return replace_equiv_address (x, temp);
2649 return x;
2652 fmt = GET_RTX_FORMAT (code);
2653 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2655 if (fmt[i] == 'e')
2656 XEXP (x, i) = fixup_stack_1 (XEXP (x, i), insn);
2657 else if (fmt[i] == 'E')
2659 int j;
2660 for (j = 0; j < XVECLEN (x, i); j++)
2661 XVECEXP (x, i, j) = fixup_stack_1 (XVECEXP (x, i, j), insn);
2664 return x;
2667 /* Optimization: a bit-field instruction whose field
2668 happens to be a byte or halfword in memory
2669 can be changed to a move instruction.
2671 We call here when INSN is an insn to examine or store into a bit-field.
2672 BODY is the SET-rtx to be altered.
2674 EQUIV_MEM is the table `reg_equiv_mem' if that is available; else 0.
2675 (Currently this is called only from function.c, and EQUIV_MEM
2676 is always 0.) */
2678 static void
2679 optimize_bit_field (rtx body, rtx insn, rtx *equiv_mem)
2681 rtx bitfield;
2682 int destflag;
2683 rtx seq = 0;
2684 enum machine_mode mode;
2686 if (GET_CODE (SET_DEST (body)) == SIGN_EXTRACT
2687 || GET_CODE (SET_DEST (body)) == ZERO_EXTRACT)
2688 bitfield = SET_DEST (body), destflag = 1;
2689 else
2690 bitfield = SET_SRC (body), destflag = 0;
2692 /* First check that the field being stored has constant size and position
2693 and is in fact a byte or halfword suitably aligned. */
2695 if (GET_CODE (XEXP (bitfield, 1)) == CONST_INT
2696 && GET_CODE (XEXP (bitfield, 2)) == CONST_INT
2697 && ((mode = mode_for_size (INTVAL (XEXP (bitfield, 1)), MODE_INT, 1))
2698 != BLKmode)
2699 && INTVAL (XEXP (bitfield, 2)) % INTVAL (XEXP (bitfield, 1)) == 0)
2701 rtx memref = 0;
2703 /* Now check that the containing word is memory, not a register,
2704 and that it is safe to change the machine mode. */
2706 if (GET_CODE (XEXP (bitfield, 0)) == MEM)
2707 memref = XEXP (bitfield, 0);
2708 else if (REG_P (XEXP (bitfield, 0))
2709 && equiv_mem != 0)
2710 memref = equiv_mem[REGNO (XEXP (bitfield, 0))];
2711 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2712 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == MEM)
2713 memref = SUBREG_REG (XEXP (bitfield, 0));
2714 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2715 && equiv_mem != 0
2716 && REG_P (SUBREG_REG (XEXP (bitfield, 0))))
2717 memref = equiv_mem[REGNO (SUBREG_REG (XEXP (bitfield, 0)))];
2719 if (memref
2720 && ! mode_dependent_address_p (XEXP (memref, 0))
2721 && ! MEM_VOLATILE_P (memref))
2723 /* Now adjust the address, first for any subreg'ing
2724 that we are now getting rid of,
2725 and then for which byte of the word is wanted. */
2727 HOST_WIDE_INT offset = INTVAL (XEXP (bitfield, 2));
2728 rtx insns;
2730 /* Adjust OFFSET to count bits from low-address byte. */
2731 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
2732 offset = (GET_MODE_BITSIZE (GET_MODE (XEXP (bitfield, 0)))
2733 - offset - INTVAL (XEXP (bitfield, 1)));
2735 /* Adjust OFFSET to count bytes from low-address byte. */
2736 offset /= BITS_PER_UNIT;
2737 if (GET_CODE (XEXP (bitfield, 0)) == SUBREG)
2739 offset += (SUBREG_BYTE (XEXP (bitfield, 0))
2740 / UNITS_PER_WORD) * UNITS_PER_WORD;
2741 if (BYTES_BIG_ENDIAN)
2742 offset -= (MIN (UNITS_PER_WORD,
2743 GET_MODE_SIZE (GET_MODE (XEXP (bitfield, 0))))
2744 - MIN (UNITS_PER_WORD,
2745 GET_MODE_SIZE (GET_MODE (memref))));
2748 start_sequence ();
2749 memref = adjust_address (memref, mode, offset);
2750 insns = get_insns ();
2751 end_sequence ();
2752 emit_insn_before (insns, insn);
2754 /* Store this memory reference where
2755 we found the bit field reference. */
2757 if (destflag)
2759 validate_change (insn, &SET_DEST (body), memref, 1);
2760 if (! CONSTANT_ADDRESS_P (SET_SRC (body)))
2762 rtx src = SET_SRC (body);
2763 while (GET_CODE (src) == SUBREG
2764 && SUBREG_BYTE (src) == 0)
2765 src = SUBREG_REG (src);
2766 if (GET_MODE (src) != GET_MODE (memref))
2767 src = gen_lowpart (GET_MODE (memref), SET_SRC (body));
2768 validate_change (insn, &SET_SRC (body), src, 1);
2770 else if (GET_MODE (SET_SRC (body)) != VOIDmode
2771 && GET_MODE (SET_SRC (body)) != GET_MODE (memref))
2772 /* This shouldn't happen because anything that didn't have
2773 one of these modes should have got converted explicitly
2774 and then referenced through a subreg.
2775 This is so because the original bit-field was
2776 handled by agg_mode and so its tree structure had
2777 the same mode that memref now has. */
2778 abort ();
2780 else
2782 rtx dest = SET_DEST (body);
2784 while (GET_CODE (dest) == SUBREG
2785 && SUBREG_BYTE (dest) == 0
2786 && (GET_MODE_CLASS (GET_MODE (dest))
2787 == GET_MODE_CLASS (GET_MODE (SUBREG_REG (dest))))
2788 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
2789 <= UNITS_PER_WORD))
2790 dest = SUBREG_REG (dest);
2792 validate_change (insn, &SET_DEST (body), dest, 1);
2794 if (GET_MODE (dest) == GET_MODE (memref))
2795 validate_change (insn, &SET_SRC (body), memref, 1);
2796 else
2798 /* Convert the mem ref to the destination mode. */
2799 rtx newreg = gen_reg_rtx (GET_MODE (dest));
2801 start_sequence ();
2802 convert_move (newreg, memref,
2803 GET_CODE (SET_SRC (body)) == ZERO_EXTRACT);
2804 seq = get_insns ();
2805 end_sequence ();
2807 validate_change (insn, &SET_SRC (body), newreg, 1);
2811 /* See if we can convert this extraction or insertion into
2812 a simple move insn. We might not be able to do so if this
2813 was, for example, part of a PARALLEL.
2815 If we succeed, write out any needed conversions. If we fail,
2816 it is hard to guess why we failed, so don't do anything
2817 special; just let the optimization be suppressed. */
2819 if (apply_change_group () && seq)
2820 emit_insn_before (seq, insn);
2825 /* These routines are responsible for converting virtual register references
2826 to the actual hard register references once RTL generation is complete.
2828 The following four variables are used for communication between the
2829 routines. They contain the offsets of the virtual registers from their
2830 respective hard registers. */
2832 static int in_arg_offset;
2833 static int var_offset;
2834 static int dynamic_offset;
2835 static int out_arg_offset;
2836 static int cfa_offset;
2838 /* In most machines, the stack pointer register is equivalent to the bottom
2839 of the stack. */
2841 #ifndef STACK_POINTER_OFFSET
2842 #define STACK_POINTER_OFFSET 0
2843 #endif
2845 /* If not defined, pick an appropriate default for the offset of dynamically
2846 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
2847 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
2849 #ifndef STACK_DYNAMIC_OFFSET
2851 /* The bottom of the stack points to the actual arguments. If
2852 REG_PARM_STACK_SPACE is defined, this includes the space for the register
2853 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
2854 stack space for register parameters is not pushed by the caller, but
2855 rather part of the fixed stack areas and hence not included in
2856 `current_function_outgoing_args_size'. Nevertheless, we must allow
2857 for it when allocating stack dynamic objects. */
2859 #if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
2860 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2861 ((ACCUMULATE_OUTGOING_ARGS \
2862 ? (current_function_outgoing_args_size + REG_PARM_STACK_SPACE (FNDECL)) : 0)\
2863 + (STACK_POINTER_OFFSET)) \
2865 #else
2866 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2867 ((ACCUMULATE_OUTGOING_ARGS ? current_function_outgoing_args_size : 0) \
2868 + (STACK_POINTER_OFFSET))
2869 #endif
2870 #endif
2872 /* On most machines, the CFA coincides with the first incoming parm. */
2874 #ifndef ARG_POINTER_CFA_OFFSET
2875 #define ARG_POINTER_CFA_OFFSET(FNDECL) FIRST_PARM_OFFSET (FNDECL)
2876 #endif
2878 /* Build up a (MEM (ADDRESSOF (REG))) rtx for a register REG that just
2879 had its address taken. DECL is the decl or SAVE_EXPR for the
2880 object stored in the register, for later use if we do need to force
2881 REG into the stack. REG is overwritten by the MEM like in
2882 put_reg_into_stack. RESCAN is true if previously emitted
2883 instructions must be rescanned and modified now that the REG has
2884 been transformed. */
2887 gen_mem_addressof (rtx reg, tree decl, int rescan)
2889 rtx r = gen_rtx_ADDRESSOF (Pmode, gen_reg_rtx (GET_MODE (reg)),
2890 REGNO (reg), decl);
2892 /* Calculate this before we start messing with decl's RTL. */
2893 HOST_WIDE_INT set = decl ? get_alias_set (decl) : 0;
2895 /* If the original REG was a user-variable, then so is the REG whose
2896 address is being taken. Likewise for unchanging. */
2897 REG_USERVAR_P (XEXP (r, 0)) = REG_USERVAR_P (reg);
2898 RTX_UNCHANGING_P (XEXP (r, 0)) = RTX_UNCHANGING_P (reg);
2900 PUT_CODE (reg, MEM);
2901 MEM_VOLATILE_P (reg) = 0;
2902 MEM_ATTRS (reg) = 0;
2903 XEXP (reg, 0) = r;
2905 if (decl)
2907 tree type = TREE_TYPE (decl);
2908 enum machine_mode decl_mode
2909 = (DECL_P (decl) ? DECL_MODE (decl) : TYPE_MODE (TREE_TYPE (decl)));
2910 rtx decl_rtl = (TREE_CODE (decl) == SAVE_EXPR ? SAVE_EXPR_RTL (decl)
2911 : DECL_RTL_IF_SET (decl));
2913 PUT_MODE (reg, decl_mode);
2915 /* Clear DECL_RTL momentarily so functions below will work
2916 properly, then set it again. */
2917 if (DECL_P (decl) && decl_rtl == reg)
2918 SET_DECL_RTL (decl, 0);
2920 set_mem_attributes (reg, decl, 1);
2921 set_mem_alias_set (reg, set);
2923 if (DECL_P (decl) && decl_rtl == reg)
2924 SET_DECL_RTL (decl, reg);
2926 if (rescan
2927 && (TREE_USED (decl) || (DECL_P (decl) && DECL_INITIAL (decl) != 0)))
2928 fixup_var_refs (reg, GET_MODE (reg), TYPE_UNSIGNED (type), reg, 0);
2930 else if (rescan)
2932 /* This can only happen during reload. Clear the same flag bits as
2933 reload. */
2934 RTX_UNCHANGING_P (reg) = 0;
2935 MEM_IN_STRUCT_P (reg) = 0;
2936 MEM_SCALAR_P (reg) = 0;
2938 fixup_var_refs (reg, GET_MODE (reg), 0, reg, 0);
2941 return reg;
2944 /* If DECL has an RTL that is an ADDRESSOF rtx, put it into the stack. */
2946 void
2947 flush_addressof (tree decl)
2949 if ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL)
2950 && DECL_RTL (decl) != 0
2951 && GET_CODE (DECL_RTL (decl)) == MEM
2952 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF
2953 && REG_P (XEXP (XEXP (DECL_RTL (decl), 0), 0)))
2954 put_addressof_into_stack (XEXP (DECL_RTL (decl), 0), 0);
2957 /* Force the register pointed to by R, an ADDRESSOF rtx, into the stack. */
2959 static void
2960 put_addressof_into_stack (rtx r, htab_t ht)
2962 tree decl, type;
2963 bool volatile_p, used_p;
2965 rtx reg = XEXP (r, 0);
2967 if (!REG_P (reg))
2968 abort ();
2970 decl = ADDRESSOF_DECL (r);
2971 if (decl)
2973 type = TREE_TYPE (decl);
2974 volatile_p = (TREE_CODE (decl) != SAVE_EXPR
2975 && TREE_THIS_VOLATILE (decl));
2976 used_p = (TREE_USED (decl)
2977 || (DECL_P (decl) && DECL_INITIAL (decl) != 0));
2979 else
2981 type = NULL_TREE;
2982 volatile_p = false;
2983 used_p = true;
2986 put_reg_into_stack (0, reg, type, GET_MODE (reg), ADDRESSOF_REGNO (r),
2987 volatile_p, used_p, false, ht);
2990 /* List of replacements made below in purge_addressof_1 when creating
2991 bitfield insertions. */
2992 static rtx purge_bitfield_addressof_replacements;
2994 /* List of replacements made below in purge_addressof_1 for patterns
2995 (MEM (ADDRESSOF (REG ...))). The key of the list entry is the
2996 corresponding (ADDRESSOF (REG ...)) and value is a substitution for
2997 the all pattern. List PURGE_BITFIELD_ADDRESSOF_REPLACEMENTS is not
2998 enough in complex cases, e.g. when some field values can be
2999 extracted by usage MEM with narrower mode. */
3000 static rtx purge_addressof_replacements;
3002 /* Helper function for purge_addressof. See if the rtx expression at *LOC
3003 in INSN needs to be changed. If FORCE, always put any ADDRESSOFs into
3004 the stack. If the function returns FALSE then the replacement could not
3005 be made. If MAY_POSTPONE is true and we would not put the addressof
3006 to stack, postpone processing of the insn. */
3008 static bool
3009 purge_addressof_1 (rtx *loc, rtx insn, int force, int store, int may_postpone,
3010 htab_t ht)
3012 rtx x;
3013 RTX_CODE code;
3014 int i, j;
3015 const char *fmt;
3016 bool result = true;
3017 bool libcall = false;
3019 /* Re-start here to avoid recursion in common cases. */
3020 restart:
3022 x = *loc;
3023 if (x == 0)
3024 return true;
3026 /* Is this a libcall? */
3027 if (!insn)
3028 libcall = REG_NOTE_KIND (*loc) == REG_RETVAL;
3030 code = GET_CODE (x);
3032 /* If we don't return in any of the cases below, we will recurse inside
3033 the RTX, which will normally result in any ADDRESSOF being forced into
3034 memory. */
3035 if (code == SET)
3037 result = purge_addressof_1 (&SET_DEST (x), insn, force, 1,
3038 may_postpone, ht);
3039 result &= purge_addressof_1 (&SET_SRC (x), insn, force, 0,
3040 may_postpone, ht);
3041 return result;
3043 else if (code == ADDRESSOF)
3045 rtx sub, insns;
3047 if (GET_CODE (XEXP (x, 0)) != MEM)
3048 put_addressof_into_stack (x, ht);
3050 /* We must create a copy of the rtx because it was created by
3051 overwriting a REG rtx which is always shared. */
3052 sub = copy_rtx (XEXP (XEXP (x, 0), 0));
3053 if (validate_change (insn, loc, sub, 0)
3054 || validate_replace_rtx (x, sub, insn))
3055 return true;
3057 start_sequence ();
3059 /* If SUB is a hard or virtual register, try it as a pseudo-register.
3060 Otherwise, perhaps SUB is an expression, so generate code to compute
3061 it. */
3062 if (REG_P (sub) && REGNO (sub) <= LAST_VIRTUAL_REGISTER)
3063 sub = copy_to_reg (sub);
3064 else
3065 sub = force_operand (sub, NULL_RTX);
3067 if (! validate_change (insn, loc, sub, 0)
3068 && ! validate_replace_rtx (x, sub, insn))
3069 abort ();
3071 insns = get_insns ();
3072 end_sequence ();
3073 emit_insn_before (insns, insn);
3074 return true;
3077 else if (code == MEM && GET_CODE (XEXP (x, 0)) == ADDRESSOF && ! force)
3079 rtx sub = XEXP (XEXP (x, 0), 0);
3081 if (GET_CODE (sub) == MEM)
3082 sub = adjust_address_nv (sub, GET_MODE (x), 0);
3083 else if (REG_P (sub)
3084 && (MEM_VOLATILE_P (x) || GET_MODE (x) == BLKmode))
3086 else if (REG_P (sub) && GET_MODE (x) != GET_MODE (sub))
3088 int size_x, size_sub;
3090 if (may_postpone)
3092 /* Postpone for now, so that we do not emit bitfield arithmetics
3093 unless there is some benefit from it. */
3094 if (!postponed_insns || XEXP (postponed_insns, 0) != insn)
3095 postponed_insns = alloc_INSN_LIST (insn, postponed_insns);
3096 return true;
3099 if (!insn)
3101 /* When processing REG_NOTES look at the list of
3102 replacements done on the insn to find the register that X
3103 was replaced by. */
3104 rtx tem;
3106 for (tem = purge_bitfield_addressof_replacements;
3107 tem != NULL_RTX;
3108 tem = XEXP (XEXP (tem, 1), 1))
3109 if (rtx_equal_p (x, XEXP (tem, 0)))
3111 *loc = XEXP (XEXP (tem, 1), 0);
3112 return true;
3115 /* See comment for purge_addressof_replacements. */
3116 for (tem = purge_addressof_replacements;
3117 tem != NULL_RTX;
3118 tem = XEXP (XEXP (tem, 1), 1))
3119 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3121 rtx z = XEXP (XEXP (tem, 1), 0);
3123 if (GET_MODE (x) == GET_MODE (z)
3124 || (!REG_P (XEXP (XEXP (tem, 1), 0))
3125 && GET_CODE (XEXP (XEXP (tem, 1), 0)) != SUBREG))
3126 abort ();
3128 /* It can happen that the note may speak of things
3129 in a wider (or just different) mode than the
3130 code did. This is especially true of
3131 REG_RETVAL. */
3133 if (GET_CODE (z) == SUBREG && SUBREG_BYTE (z) == 0)
3134 z = SUBREG_REG (z);
3136 if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3137 && (GET_MODE_SIZE (GET_MODE (x))
3138 > GET_MODE_SIZE (GET_MODE (z))))
3140 /* This can occur as a result in invalid
3141 pointer casts, e.g. float f; ...
3142 *(long long int *)&f.
3143 ??? We could emit a warning here, but
3144 without a line number that wouldn't be
3145 very helpful. */
3146 z = gen_rtx_SUBREG (GET_MODE (x), z, 0);
3148 else
3149 z = gen_lowpart (GET_MODE (x), z);
3151 *loc = z;
3152 return true;
3155 /* When we are processing the REG_NOTES of the last instruction
3156 of a libcall, there will be typically no replacements
3157 for that insn; the replacements happened before, piecemeal
3158 fashion. OTOH we are not interested in the details of
3159 this for the REG_EQUAL note, we want to know the big picture,
3160 which can be succinctly described with a simple SUBREG.
3161 Note that removing the REG_EQUAL note is not an option
3162 on the last insn of a libcall, so we must do a replacement. */
3164 /* In compile/990107-1.c:7 compiled at -O1 -m1 for sh-elf,
3165 we got
3166 (mem:DI (addressof:SI (reg/v:DF 160) 159 0x401c8510)
3167 [0 S8 A32]), which can be expressed with a simple
3168 same-size subreg */
3169 if ((GET_MODE_SIZE (GET_MODE (x))
3170 <= GET_MODE_SIZE (GET_MODE (sub)))
3171 /* Again, invalid pointer casts (as in
3172 compile/990203-1.c) can require paradoxical
3173 subregs. */
3174 || (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3175 && (GET_MODE_SIZE (GET_MODE (x))
3176 > GET_MODE_SIZE (GET_MODE (sub)))
3177 && libcall))
3179 *loc = gen_rtx_SUBREG (GET_MODE (x), sub, 0);
3180 return true;
3182 /* ??? Are there other cases we should handle? */
3184 /* Sometimes we may not be able to find the replacement. For
3185 example when the original insn was a MEM in a wider mode,
3186 and the note is part of a sign extension of a narrowed
3187 version of that MEM. Gcc testcase compile/990829-1.c can
3188 generate an example of this situation. Rather than complain
3189 we return false, which will prompt our caller to remove the
3190 offending note. */
3191 return false;
3194 size_x = GET_MODE_BITSIZE (GET_MODE (x));
3195 size_sub = GET_MODE_BITSIZE (GET_MODE (sub));
3197 /* Do not frob unchanging MEMs. If a later reference forces the
3198 pseudo to the stack, we can wind up with multiple writes to
3199 an unchanging memory, which is invalid. */
3200 if (RTX_UNCHANGING_P (x) && size_x != size_sub)
3203 /* Don't even consider working with paradoxical subregs,
3204 or the moral equivalent seen here. */
3205 else if (size_x <= size_sub
3206 && int_mode_for_mode (GET_MODE (sub)) != BLKmode)
3208 /* Do a bitfield insertion to mirror what would happen
3209 in memory. */
3211 rtx val, seq;
3213 if (store)
3215 rtx p = PREV_INSN (insn);
3217 start_sequence ();
3218 val = gen_reg_rtx (GET_MODE (x));
3219 if (! validate_change (insn, loc, val, 0))
3221 /* Discard the current sequence and put the
3222 ADDRESSOF on stack. */
3223 end_sequence ();
3224 goto give_up;
3226 seq = get_insns ();
3227 end_sequence ();
3228 emit_insn_before (seq, insn);
3229 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3230 insn, ht);
3232 start_sequence ();
3233 store_bit_field (sub, size_x, 0, GET_MODE (x),
3234 val, GET_MODE_SIZE (GET_MODE (sub)));
3236 /* Make sure to unshare any shared rtl that store_bit_field
3237 might have created. */
3238 unshare_all_rtl_again (get_insns ());
3240 seq = get_insns ();
3241 end_sequence ();
3242 p = emit_insn_after (seq, insn);
3243 if (NEXT_INSN (insn))
3244 compute_insns_for_mem (NEXT_INSN (insn),
3245 p ? NEXT_INSN (p) : NULL_RTX,
3246 ht);
3248 else
3250 rtx p = PREV_INSN (insn);
3252 start_sequence ();
3253 val = extract_bit_field (sub, size_x, 0, 1, NULL_RTX,
3254 GET_MODE (x), GET_MODE (x),
3255 GET_MODE_SIZE (GET_MODE (sub)));
3257 if (! validate_change (insn, loc, val, 0))
3259 /* Discard the current sequence and put the
3260 ADDRESSOF on stack. */
3261 end_sequence ();
3262 goto give_up;
3265 seq = get_insns ();
3266 end_sequence ();
3267 emit_insn_before (seq, insn);
3268 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3269 insn, ht);
3272 /* Remember the replacement so that the same one can be done
3273 on the REG_NOTES. */
3274 purge_bitfield_addressof_replacements
3275 = gen_rtx_EXPR_LIST (VOIDmode, x,
3276 gen_rtx_EXPR_LIST
3277 (VOIDmode, val,
3278 purge_bitfield_addressof_replacements));
3280 /* We replaced with a reg -- all done. */
3281 return true;
3285 else if (validate_change (insn, loc, sub, 0))
3287 /* Remember the replacement so that the same one can be done
3288 on the REG_NOTES. */
3289 if (REG_P (sub) || GET_CODE (sub) == SUBREG)
3291 rtx tem;
3293 for (tem = purge_addressof_replacements;
3294 tem != NULL_RTX;
3295 tem = XEXP (XEXP (tem, 1), 1))
3296 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3298 XEXP (XEXP (tem, 1), 0) = sub;
3299 return true;
3301 purge_addressof_replacements
3302 = gen_rtx_EXPR_LIST (VOIDmode, XEXP (x, 0),
3303 gen_rtx_EXPR_LIST (VOIDmode, sub,
3304 purge_addressof_replacements));
3305 return true;
3307 goto restart;
3311 give_up:
3312 /* Scan all subexpressions. */
3313 fmt = GET_RTX_FORMAT (code);
3314 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3316 if (*fmt == 'e')
3317 result &= purge_addressof_1 (&XEXP (x, i), insn, force, 0,
3318 may_postpone, ht);
3319 else if (*fmt == 'E')
3320 for (j = 0; j < XVECLEN (x, i); j++)
3321 result &= purge_addressof_1 (&XVECEXP (x, i, j), insn, force, 0,
3322 may_postpone, ht);
3325 return result;
3328 /* Return a hash value for K, a REG. */
3330 static hashval_t
3331 insns_for_mem_hash (const void *k)
3333 /* Use the address of the key for the hash value. */
3334 struct insns_for_mem_entry *m = (struct insns_for_mem_entry *) k;
3335 return htab_hash_pointer (m->key);
3338 /* Return nonzero if K1 and K2 (two REGs) are the same. */
3340 static int
3341 insns_for_mem_comp (const void *k1, const void *k2)
3343 struct insns_for_mem_entry *m1 = (struct insns_for_mem_entry *) k1;
3344 struct insns_for_mem_entry *m2 = (struct insns_for_mem_entry *) k2;
3345 return m1->key == m2->key;
3348 struct insns_for_mem_walk_info
3350 /* The hash table that we are using to record which INSNs use which
3351 MEMs. */
3352 htab_t ht;
3354 /* The INSN we are currently processing. */
3355 rtx insn;
3357 /* Zero if we are walking to find ADDRESSOFs, one if we are walking
3358 to find the insns that use the REGs in the ADDRESSOFs. */
3359 int pass;
3362 /* Called from compute_insns_for_mem via for_each_rtx. If R is a REG
3363 that might be used in an ADDRESSOF expression, record this INSN in
3364 the hash table given by DATA (which is really a pointer to an
3365 insns_for_mem_walk_info structure). */
3367 static int
3368 insns_for_mem_walk (rtx *r, void *data)
3370 struct insns_for_mem_walk_info *ifmwi
3371 = (struct insns_for_mem_walk_info *) data;
3372 struct insns_for_mem_entry tmp;
3373 tmp.insns = NULL_RTX;
3375 if (ifmwi->pass == 0 && *r && GET_CODE (*r) == ADDRESSOF
3376 && REG_P (XEXP (*r, 0)))
3378 void **e;
3379 tmp.key = XEXP (*r, 0);
3380 e = htab_find_slot (ifmwi->ht, &tmp, INSERT);
3381 if (*e == NULL)
3383 *e = ggc_alloc (sizeof (tmp));
3384 memcpy (*e, &tmp, sizeof (tmp));
3387 else if (ifmwi->pass == 1 && *r && REG_P (*r))
3389 struct insns_for_mem_entry *ifme;
3390 tmp.key = *r;
3391 ifme = htab_find (ifmwi->ht, &tmp);
3393 /* If we have not already recorded this INSN, do so now. Since
3394 we process the INSNs in order, we know that if we have
3395 recorded it it must be at the front of the list. */
3396 if (ifme && (!ifme->insns || XEXP (ifme->insns, 0) != ifmwi->insn))
3397 ifme->insns = gen_rtx_EXPR_LIST (VOIDmode, ifmwi->insn,
3398 ifme->insns);
3401 return 0;
3404 /* Walk the INSNS, until we reach LAST_INSN, recording which INSNs use
3405 which REGs in HT. */
3407 static void
3408 compute_insns_for_mem (rtx insns, rtx last_insn, htab_t ht)
3410 rtx insn;
3411 struct insns_for_mem_walk_info ifmwi;
3412 ifmwi.ht = ht;
3414 for (ifmwi.pass = 0; ifmwi.pass < 2; ++ifmwi.pass)
3415 for (insn = insns; insn != last_insn; insn = NEXT_INSN (insn))
3416 if (INSN_P (insn))
3418 ifmwi.insn = insn;
3419 for_each_rtx (&insn, insns_for_mem_walk, &ifmwi);
3423 /* Helper function for purge_addressof called through for_each_rtx.
3424 Returns true iff the rtl is an ADDRESSOF. */
3426 static int
3427 is_addressof (rtx *rtl, void *data ATTRIBUTE_UNUSED)
3429 return GET_CODE (*rtl) == ADDRESSOF;
3432 /* Eliminate all occurrences of ADDRESSOF from INSNS. Elide any remaining
3433 (MEM (ADDRESSOF)) patterns, and force any needed registers into the
3434 stack. */
3436 void
3437 purge_addressof (rtx insns)
3439 rtx insn, tmp;
3440 htab_t ht;
3442 /* When we actually purge ADDRESSOFs, we turn REGs into MEMs. That
3443 requires a fixup pass over the instruction stream to correct
3444 INSNs that depended on the REG being a REG, and not a MEM. But,
3445 these fixup passes are slow. Furthermore, most MEMs are not
3446 mentioned in very many instructions. So, we speed up the process
3447 by pre-calculating which REGs occur in which INSNs; that allows
3448 us to perform the fixup passes much more quickly. */
3449 ht = htab_create_ggc (1000, insns_for_mem_hash, insns_for_mem_comp, NULL);
3450 compute_insns_for_mem (insns, NULL_RTX, ht);
3452 postponed_insns = NULL;
3454 for (insn = insns; insn; insn = NEXT_INSN (insn))
3455 if (INSN_P (insn))
3457 if (! purge_addressof_1 (&PATTERN (insn), insn,
3458 asm_noperands (PATTERN (insn)) > 0, 0, 1, ht))
3459 /* If we could not replace the ADDRESSOFs in the insn,
3460 something is wrong. */
3461 abort ();
3463 if (! purge_addressof_1 (&REG_NOTES (insn), NULL_RTX, 0, 0, 0, ht))
3465 /* If we could not replace the ADDRESSOFs in the insn's notes,
3466 we can just remove the offending notes instead. */
3467 rtx note;
3469 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3471 /* If we find a REG_RETVAL note then the insn is a libcall.
3472 Such insns must have REG_EQUAL notes as well, in order
3473 for later passes of the compiler to work. So it is not
3474 safe to delete the notes here, and instead we abort. */
3475 if (REG_NOTE_KIND (note) == REG_RETVAL)
3476 abort ();
3477 if (for_each_rtx (&note, is_addressof, NULL))
3478 remove_note (insn, note);
3483 /* Process the postponed insns. */
3484 while (postponed_insns)
3486 insn = XEXP (postponed_insns, 0);
3487 tmp = postponed_insns;
3488 postponed_insns = XEXP (postponed_insns, 1);
3489 free_INSN_LIST_node (tmp);
3491 if (! purge_addressof_1 (&PATTERN (insn), insn,
3492 asm_noperands (PATTERN (insn)) > 0, 0, 0, ht))
3493 abort ();
3496 /* Clean up. */
3497 purge_bitfield_addressof_replacements = 0;
3498 purge_addressof_replacements = 0;
3500 /* REGs are shared. purge_addressof will destructively replace a REG
3501 with a MEM, which creates shared MEMs.
3503 Unfortunately, the children of put_reg_into_stack assume that MEMs
3504 referring to the same stack slot are shared (fixup_var_refs and
3505 the associated hash table code).
3507 So, we have to do another unsharing pass after we have flushed any
3508 REGs that had their address taken into the stack.
3510 It may be worth tracking whether or not we converted any REGs into
3511 MEMs to avoid this overhead when it is not needed. */
3512 unshare_all_rtl_again (get_insns ());
3515 /* Convert a SET of a hard subreg to a set of the appropriate hard
3516 register. A subroutine of purge_hard_subreg_sets. */
3518 static void
3519 purge_single_hard_subreg_set (rtx pattern)
3521 rtx reg = SET_DEST (pattern);
3522 enum machine_mode mode = GET_MODE (SET_DEST (pattern));
3523 int offset = 0;
3525 if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg))
3526 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
3528 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
3529 GET_MODE (SUBREG_REG (reg)),
3530 SUBREG_BYTE (reg),
3531 GET_MODE (reg));
3532 reg = SUBREG_REG (reg);
3536 if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
3538 reg = gen_rtx_REG (mode, REGNO (reg) + offset);
3539 SET_DEST (pattern) = reg;
3543 /* Eliminate all occurrences of SETs of hard subregs from INSNS. The
3544 only such SETs that we expect to see are those left in because
3545 integrate can't handle sets of parts of a return value register.
3547 We don't use alter_subreg because we only want to eliminate subregs
3548 of hard registers. */
3550 void
3551 purge_hard_subreg_sets (rtx insn)
3553 for (; insn; insn = NEXT_INSN (insn))
3555 if (INSN_P (insn))
3557 rtx pattern = PATTERN (insn);
3558 switch (GET_CODE (pattern))
3560 case SET:
3561 if (GET_CODE (SET_DEST (pattern)) == SUBREG)
3562 purge_single_hard_subreg_set (pattern);
3563 break;
3564 case PARALLEL:
3566 int j;
3567 for (j = XVECLEN (pattern, 0) - 1; j >= 0; j--)
3569 rtx inner_pattern = XVECEXP (pattern, 0, j);
3570 if (GET_CODE (inner_pattern) == SET
3571 && GET_CODE (SET_DEST (inner_pattern)) == SUBREG)
3572 purge_single_hard_subreg_set (inner_pattern);
3575 break;
3576 default:
3577 break;
3583 /* Pass through the INSNS of function FNDECL and convert virtual register
3584 references to hard register references. */
3586 void
3587 instantiate_virtual_regs (void)
3589 rtx insn;
3590 unsigned int i;
3592 /* Compute the offsets to use for this function. */
3593 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
3594 var_offset = STARTING_FRAME_OFFSET;
3595 dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
3596 out_arg_offset = STACK_POINTER_OFFSET;
3597 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
3599 /* Scan all variables and parameters of this function. For each that is
3600 in memory, instantiate all virtual registers if the result is a valid
3601 address. If not, we do it later. That will handle most uses of virtual
3602 regs on many machines. */
3603 instantiate_decls (current_function_decl, 1);
3605 /* Initialize recognition, indicating that volatile is OK. */
3606 init_recog ();
3608 /* Scan through all the insns, instantiating every virtual register still
3609 present. */
3610 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3611 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3612 || GET_CODE (insn) == CALL_INSN)
3614 instantiate_virtual_regs_1 (&PATTERN (insn), insn, 1);
3615 if (INSN_DELETED_P (insn))
3616 continue;
3617 instantiate_virtual_regs_1 (&REG_NOTES (insn), NULL_RTX, 0);
3618 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
3619 if (GET_CODE (insn) == CALL_INSN)
3620 instantiate_virtual_regs_1 (&CALL_INSN_FUNCTION_USAGE (insn),
3621 NULL_RTX, 0);
3623 /* Past this point all ASM statements should match. Verify that
3624 to avoid failures later in the compilation process. */
3625 if (asm_noperands (PATTERN (insn)) >= 0
3626 && ! check_asm_operands (PATTERN (insn)))
3627 instantiate_virtual_regs_lossage (insn);
3630 /* Instantiate the stack slots for the parm registers, for later use in
3631 addressof elimination. */
3632 for (i = 0; i < max_parm_reg; ++i)
3633 if (parm_reg_stack_loc[i])
3634 instantiate_virtual_regs_1 (&parm_reg_stack_loc[i], NULL_RTX, 0);
3636 /* Now instantiate the remaining register equivalences for debugging info.
3637 These will not be valid addresses. */
3638 instantiate_decls (current_function_decl, 0);
3640 /* Indicate that, from now on, assign_stack_local should use
3641 frame_pointer_rtx. */
3642 virtuals_instantiated = 1;
3645 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
3646 all virtual registers in their DECL_RTL's.
3648 If VALID_ONLY, do this only if the resulting address is still valid.
3649 Otherwise, always do it. */
3651 static void
3652 instantiate_decls (tree fndecl, int valid_only)
3654 tree decl;
3656 /* Process all parameters of the function. */
3657 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
3659 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
3660 HOST_WIDE_INT size_rtl;
3662 instantiate_decl (DECL_RTL (decl), size, valid_only);
3664 /* If the parameter was promoted, then the incoming RTL mode may be
3665 larger than the declared type size. We must use the larger of
3666 the two sizes. */
3667 size_rtl = GET_MODE_SIZE (GET_MODE (DECL_INCOMING_RTL (decl)));
3668 size = MAX (size_rtl, size);
3669 instantiate_decl (DECL_INCOMING_RTL (decl), size, valid_only);
3672 /* Now process all variables defined in the function or its subblocks. */
3673 instantiate_decls_1 (DECL_INITIAL (fndecl), valid_only);
3676 /* Subroutine of instantiate_decls: Process all decls in the given
3677 BLOCK node and all its subblocks. */
3679 static void
3680 instantiate_decls_1 (tree let, int valid_only)
3682 tree t;
3684 for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
3685 if (DECL_RTL_SET_P (t))
3686 instantiate_decl (DECL_RTL (t),
3687 int_size_in_bytes (TREE_TYPE (t)),
3688 valid_only);
3690 /* Process all subblocks. */
3691 for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
3692 instantiate_decls_1 (t, valid_only);
3695 /* Subroutine of the preceding procedures: Given RTL representing a
3696 decl and the size of the object, do any instantiation required.
3698 If VALID_ONLY is nonzero, it means that the RTL should only be
3699 changed if the new address is valid. */
3701 static void
3702 instantiate_decl (rtx x, HOST_WIDE_INT size, int valid_only)
3704 enum machine_mode mode;
3705 rtx addr;
3707 /* If this is not a MEM, no need to do anything. Similarly if the
3708 address is a constant or a register that is not a virtual register. */
3710 if (x == 0 || GET_CODE (x) != MEM)
3711 return;
3713 addr = XEXP (x, 0);
3714 if (CONSTANT_P (addr)
3715 || (GET_CODE (addr) == ADDRESSOF && REG_P (XEXP (addr, 0)))
3716 || (REG_P (addr)
3717 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
3718 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
3719 return;
3721 /* If we should only do this if the address is valid, copy the address.
3722 We need to do this so we can undo any changes that might make the
3723 address invalid. This copy is unfortunate, but probably can't be
3724 avoided. */
3726 if (valid_only)
3727 addr = copy_rtx (addr);
3729 instantiate_virtual_regs_1 (&addr, NULL_RTX, 0);
3731 if (valid_only && size >= 0)
3733 unsigned HOST_WIDE_INT decl_size = size;
3735 /* Now verify that the resulting address is valid for every integer or
3736 floating-point mode up to and including SIZE bytes long. We do this
3737 since the object might be accessed in any mode and frame addresses
3738 are shared. */
3740 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3741 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3742 mode = GET_MODE_WIDER_MODE (mode))
3743 if (! memory_address_p (mode, addr))
3744 return;
3746 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
3747 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3748 mode = GET_MODE_WIDER_MODE (mode))
3749 if (! memory_address_p (mode, addr))
3750 return;
3753 /* Put back the address now that we have updated it and we either know
3754 it is valid or we don't care whether it is valid. */
3756 XEXP (x, 0) = addr;
3759 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
3760 is a virtual register, return the equivalent hard register and set the
3761 offset indirectly through the pointer. Otherwise, return 0. */
3763 static rtx
3764 instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
3766 rtx new;
3767 HOST_WIDE_INT offset;
3769 if (x == virtual_incoming_args_rtx)
3770 new = arg_pointer_rtx, offset = in_arg_offset;
3771 else if (x == virtual_stack_vars_rtx)
3772 new = frame_pointer_rtx, offset = var_offset;
3773 else if (x == virtual_stack_dynamic_rtx)
3774 new = stack_pointer_rtx, offset = dynamic_offset;
3775 else if (x == virtual_outgoing_args_rtx)
3776 new = stack_pointer_rtx, offset = out_arg_offset;
3777 else if (x == virtual_cfa_rtx)
3778 new = arg_pointer_rtx, offset = cfa_offset;
3779 else
3780 return 0;
3782 *poffset = offset;
3783 return new;
3787 /* Called when instantiate_virtual_regs has failed to update the instruction.
3788 Usually this means that non-matching instruction has been emit, however for
3789 asm statements it may be the problem in the constraints. */
3790 static void
3791 instantiate_virtual_regs_lossage (rtx insn)
3793 if (asm_noperands (PATTERN (insn)) >= 0)
3795 error_for_asm (insn, "impossible constraint in `asm'");
3796 delete_insn (insn);
3798 else
3799 abort ();
3801 /* Given a pointer to a piece of rtx and an optional pointer to the
3802 containing object, instantiate any virtual registers present in it.
3804 If EXTRA_INSNS, we always do the replacement and generate
3805 any extra insns before OBJECT. If it zero, we do nothing if replacement
3806 is not valid.
3808 Return 1 if we either had nothing to do or if we were able to do the
3809 needed replacement. Return 0 otherwise; we only return zero if
3810 EXTRA_INSNS is zero.
3812 We first try some simple transformations to avoid the creation of extra
3813 pseudos. */
3815 static int
3816 instantiate_virtual_regs_1 (rtx *loc, rtx object, int extra_insns)
3818 rtx x;
3819 RTX_CODE code;
3820 rtx new = 0;
3821 HOST_WIDE_INT offset = 0;
3822 rtx temp;
3823 rtx seq;
3824 int i, j;
3825 const char *fmt;
3827 /* Re-start here to avoid recursion in common cases. */
3828 restart:
3830 x = *loc;
3831 if (x == 0)
3832 return 1;
3834 /* We may have detected and deleted invalid asm statements. */
3835 if (object && INSN_P (object) && INSN_DELETED_P (object))
3836 return 1;
3838 code = GET_CODE (x);
3840 /* Check for some special cases. */
3841 switch (code)
3843 case CONST_INT:
3844 case CONST_DOUBLE:
3845 case CONST_VECTOR:
3846 case CONST:
3847 case SYMBOL_REF:
3848 case CODE_LABEL:
3849 case PC:
3850 case CC0:
3851 case ASM_INPUT:
3852 case ADDR_VEC:
3853 case ADDR_DIFF_VEC:
3854 case RETURN:
3855 return 1;
3857 case SET:
3858 /* We are allowed to set the virtual registers. This means that
3859 the actual register should receive the source minus the
3860 appropriate offset. This is used, for example, in the handling
3861 of non-local gotos. */
3862 if ((new = instantiate_new_reg (SET_DEST (x), &offset)) != 0)
3864 rtx src = SET_SRC (x);
3866 /* We are setting the register, not using it, so the relevant
3867 offset is the negative of the offset to use were we using
3868 the register. */
3869 offset = - offset;
3870 instantiate_virtual_regs_1 (&src, NULL_RTX, 0);
3872 /* The only valid sources here are PLUS or REG. Just do
3873 the simplest possible thing to handle them. */
3874 if (!REG_P (src) && GET_CODE (src) != PLUS)
3876 instantiate_virtual_regs_lossage (object);
3877 return 1;
3880 start_sequence ();
3881 if (!REG_P (src))
3882 temp = force_operand (src, NULL_RTX);
3883 else
3884 temp = src;
3885 temp = force_operand (plus_constant (temp, offset), NULL_RTX);
3886 seq = get_insns ();
3887 end_sequence ();
3889 emit_insn_before (seq, object);
3890 SET_DEST (x) = new;
3892 if (! validate_change (object, &SET_SRC (x), temp, 0)
3893 || ! extra_insns)
3894 instantiate_virtual_regs_lossage (object);
3896 return 1;
3899 instantiate_virtual_regs_1 (&SET_DEST (x), object, extra_insns);
3900 loc = &SET_SRC (x);
3901 goto restart;
3903 case PLUS:
3904 /* Handle special case of virtual register plus constant. */
3905 if (CONSTANT_P (XEXP (x, 1)))
3907 rtx old, new_offset;
3909 /* Check for (plus (plus VIRT foo) (const_int)) first. */
3910 if (GET_CODE (XEXP (x, 0)) == PLUS)
3912 if ((new = instantiate_new_reg (XEXP (XEXP (x, 0), 0), &offset)))
3914 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 1), object,
3915 extra_insns);
3916 new = gen_rtx_PLUS (Pmode, new, XEXP (XEXP (x, 0), 1));
3918 else
3920 loc = &XEXP (x, 0);
3921 goto restart;
3925 #ifdef POINTERS_EXTEND_UNSIGNED
3926 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
3927 we can commute the PLUS and SUBREG because pointers into the
3928 frame are well-behaved. */
3929 else if (GET_CODE (XEXP (x, 0)) == SUBREG && GET_MODE (x) == ptr_mode
3930 && GET_CODE (XEXP (x, 1)) == CONST_INT
3931 && 0 != (new
3932 = instantiate_new_reg (SUBREG_REG (XEXP (x, 0)),
3933 &offset))
3934 && validate_change (object, loc,
3935 plus_constant (gen_lowpart (ptr_mode,
3936 new),
3937 offset
3938 + INTVAL (XEXP (x, 1))),
3940 return 1;
3941 #endif
3942 else if ((new = instantiate_new_reg (XEXP (x, 0), &offset)) == 0)
3944 /* We know the second operand is a constant. Unless the
3945 first operand is a REG (which has been already checked),
3946 it needs to be checked. */
3947 if (!REG_P (XEXP (x, 0)))
3949 loc = &XEXP (x, 0);
3950 goto restart;
3952 return 1;
3955 new_offset = plus_constant (XEXP (x, 1), offset);
3957 /* If the new constant is zero, try to replace the sum with just
3958 the register. */
3959 if (new_offset == const0_rtx
3960 && validate_change (object, loc, new, 0))
3961 return 1;
3963 /* Next try to replace the register and new offset.
3964 There are two changes to validate here and we can't assume that
3965 in the case of old offset equals new just changing the register
3966 will yield a valid insn. In the interests of a little efficiency,
3967 however, we only call validate change once (we don't queue up the
3968 changes and then call apply_change_group). */
3970 old = XEXP (x, 0);
3971 if (offset == 0
3972 ? ! validate_change (object, &XEXP (x, 0), new, 0)
3973 : (XEXP (x, 0) = new,
3974 ! validate_change (object, &XEXP (x, 1), new_offset, 0)))
3976 if (! extra_insns)
3978 XEXP (x, 0) = old;
3979 return 0;
3982 /* Otherwise copy the new constant into a register and replace
3983 constant with that register. */
3984 temp = gen_reg_rtx (Pmode);
3985 XEXP (x, 0) = new;
3986 if (validate_change (object, &XEXP (x, 1), temp, 0))
3987 emit_insn_before (gen_move_insn (temp, new_offset), object);
3988 else
3990 /* If that didn't work, replace this expression with a
3991 register containing the sum. */
3993 XEXP (x, 0) = old;
3994 new = gen_rtx_PLUS (Pmode, new, new_offset);
3996 start_sequence ();
3997 temp = force_operand (new, NULL_RTX);
3998 seq = get_insns ();
3999 end_sequence ();
4001 emit_insn_before (seq, object);
4002 if (! validate_change (object, loc, temp, 0)
4003 && ! validate_replace_rtx (x, temp, object))
4005 instantiate_virtual_regs_lossage (object);
4006 return 1;
4011 return 1;
4014 /* Fall through to generic two-operand expression case. */
4015 case EXPR_LIST:
4016 case CALL:
4017 case COMPARE:
4018 case MINUS:
4019 case MULT:
4020 case DIV: case UDIV:
4021 case MOD: case UMOD:
4022 case AND: case IOR: case XOR:
4023 case ROTATERT: case ROTATE:
4024 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
4025 case NE: case EQ:
4026 case GE: case GT: case GEU: case GTU:
4027 case LE: case LT: case LEU: case LTU:
4028 if (XEXP (x, 1) && ! CONSTANT_P (XEXP (x, 1)))
4029 instantiate_virtual_regs_1 (&XEXP (x, 1), object, extra_insns);
4030 loc = &XEXP (x, 0);
4031 goto restart;
4033 case MEM:
4034 /* Most cases of MEM that convert to valid addresses have already been
4035 handled by our scan of decls. The only special handling we
4036 need here is to make a copy of the rtx to ensure it isn't being
4037 shared if we have to change it to a pseudo.
4039 If the rtx is a simple reference to an address via a virtual register,
4040 it can potentially be shared. In such cases, first try to make it
4041 a valid address, which can also be shared. Otherwise, copy it and
4042 proceed normally.
4044 First check for common cases that need no processing. These are
4045 usually due to instantiation already being done on a previous instance
4046 of a shared rtx. */
4048 temp = XEXP (x, 0);
4049 if (CONSTANT_ADDRESS_P (temp)
4050 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4051 || temp == arg_pointer_rtx
4052 #endif
4053 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
4054 || temp == hard_frame_pointer_rtx
4055 #endif
4056 || temp == frame_pointer_rtx)
4057 return 1;
4059 if (GET_CODE (temp) == PLUS
4060 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
4061 && (XEXP (temp, 0) == frame_pointer_rtx
4062 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
4063 || XEXP (temp, 0) == hard_frame_pointer_rtx
4064 #endif
4065 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4066 || XEXP (temp, 0) == arg_pointer_rtx
4067 #endif
4069 return 1;
4071 if (temp == virtual_stack_vars_rtx
4072 || temp == virtual_incoming_args_rtx
4073 || (GET_CODE (temp) == PLUS
4074 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
4075 && (XEXP (temp, 0) == virtual_stack_vars_rtx
4076 || XEXP (temp, 0) == virtual_incoming_args_rtx)))
4078 /* This MEM may be shared. If the substitution can be done without
4079 the need to generate new pseudos, we want to do it in place
4080 so all copies of the shared rtx benefit. The call below will
4081 only make substitutions if the resulting address is still
4082 valid.
4084 Note that we cannot pass X as the object in the recursive call
4085 since the insn being processed may not allow all valid
4086 addresses. However, if we were not passed on object, we can
4087 only modify X without copying it if X will have a valid
4088 address.
4090 ??? Also note that this can still lose if OBJECT is an insn that
4091 has less restrictions on an address that some other insn.
4092 In that case, we will modify the shared address. This case
4093 doesn't seem very likely, though. One case where this could
4094 happen is in the case of a USE or CLOBBER reference, but we
4095 take care of that below. */
4097 if (instantiate_virtual_regs_1 (&XEXP (x, 0),
4098 object ? object : x, 0))
4099 return 1;
4101 /* Otherwise make a copy and process that copy. We copy the entire
4102 RTL expression since it might be a PLUS which could also be
4103 shared. */
4104 *loc = x = copy_rtx (x);
4107 /* Fall through to generic unary operation case. */
4108 case PREFETCH:
4109 case SUBREG:
4110 case STRICT_LOW_PART:
4111 case NEG: case NOT:
4112 case PRE_DEC: case PRE_INC: case POST_DEC: case POST_INC:
4113 case SIGN_EXTEND: case ZERO_EXTEND:
4114 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
4115 case FLOAT: case FIX:
4116 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
4117 case ABS:
4118 case SQRT:
4119 case FFS:
4120 case CLZ: case CTZ:
4121 case POPCOUNT: case PARITY:
4122 /* These case either have just one operand or we know that we need not
4123 check the rest of the operands. */
4124 loc = &XEXP (x, 0);
4125 goto restart;
4127 case USE:
4128 case CLOBBER:
4129 /* If the operand is a MEM, see if the change is a valid MEM. If not,
4130 go ahead and make the invalid one, but do it to a copy. For a REG,
4131 just make the recursive call, since there's no chance of a problem. */
4133 if ((GET_CODE (XEXP (x, 0)) == MEM
4134 && instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), XEXP (x, 0),
4136 || (REG_P (XEXP (x, 0))
4137 && instantiate_virtual_regs_1 (&XEXP (x, 0), object, 0)))
4138 return 1;
4140 XEXP (x, 0) = copy_rtx (XEXP (x, 0));
4141 loc = &XEXP (x, 0);
4142 goto restart;
4144 case REG:
4145 /* Try to replace with a PLUS. If that doesn't work, compute the sum
4146 in front of this insn and substitute the temporary. */
4147 if ((new = instantiate_new_reg (x, &offset)) != 0)
4149 temp = plus_constant (new, offset);
4150 if (!validate_change (object, loc, temp, 0))
4152 if (! extra_insns)
4153 return 0;
4155 start_sequence ();
4156 temp = force_operand (temp, NULL_RTX);
4157 seq = get_insns ();
4158 end_sequence ();
4160 emit_insn_before (seq, object);
4161 if (! validate_change (object, loc, temp, 0)
4162 && ! validate_replace_rtx (x, temp, object))
4163 instantiate_virtual_regs_lossage (object);
4167 return 1;
4169 case ADDRESSOF:
4170 if (REG_P (XEXP (x, 0)))
4171 return 1;
4173 else if (GET_CODE (XEXP (x, 0)) == MEM)
4175 /* If we have a (addressof (mem ..)), do any instantiation inside
4176 since we know we'll be making the inside valid when we finally
4177 remove the ADDRESSOF. */
4178 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), NULL_RTX, 0);
4179 return 1;
4181 break;
4183 default:
4184 break;
4187 /* Scan all subexpressions. */
4188 fmt = GET_RTX_FORMAT (code);
4189 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
4190 if (*fmt == 'e')
4192 if (!instantiate_virtual_regs_1 (&XEXP (x, i), object, extra_insns))
4193 return 0;
4195 else if (*fmt == 'E')
4196 for (j = 0; j < XVECLEN (x, i); j++)
4197 if (! instantiate_virtual_regs_1 (&XVECEXP (x, i, j), object,
4198 extra_insns))
4199 return 0;
4201 return 1;
4204 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
4205 This means a type for which function calls must pass an address to the
4206 function or get an address back from the function.
4207 EXP may be a type node or an expression (whose type is tested). */
4210 aggregate_value_p (tree exp, tree fntype)
4212 int i, regno, nregs;
4213 rtx reg;
4215 tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
4217 if (fntype)
4218 switch (TREE_CODE (fntype))
4220 case CALL_EXPR:
4221 fntype = get_callee_fndecl (fntype);
4222 fntype = fntype ? TREE_TYPE (fntype) : 0;
4223 break;
4224 case FUNCTION_DECL:
4225 fntype = TREE_TYPE (fntype);
4226 break;
4227 case FUNCTION_TYPE:
4228 case METHOD_TYPE:
4229 break;
4230 case IDENTIFIER_NODE:
4231 fntype = 0;
4232 break;
4233 default:
4234 /* We don't expect other rtl types here. */
4235 abort();
4238 if (TREE_CODE (type) == VOID_TYPE)
4239 return 0;
4240 if (targetm.calls.return_in_memory (type, fntype))
4241 return 1;
4242 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
4243 and thus can't be returned in registers. */
4244 if (TREE_ADDRESSABLE (type))
4245 return 1;
4246 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
4247 return 1;
4248 /* Make sure we have suitable call-clobbered regs to return
4249 the value in; if not, we must return it in memory. */
4250 reg = hard_function_value (type, 0, 0);
4252 /* If we have something other than a REG (e.g. a PARALLEL), then assume
4253 it is OK. */
4254 if (!REG_P (reg))
4255 return 0;
4257 regno = REGNO (reg);
4258 nregs = hard_regno_nregs[regno][TYPE_MODE (type)];
4259 for (i = 0; i < nregs; i++)
4260 if (! call_used_regs[regno + i])
4261 return 1;
4262 return 0;
4265 /* Assign RTL expressions to the function's parameters.
4266 This may involve copying them into registers and using
4267 those registers as the RTL for them. */
4269 void
4270 assign_parms (tree fndecl)
4272 tree parm;
4273 CUMULATIVE_ARGS args_so_far;
4274 /* Total space needed so far for args on the stack,
4275 given as a constant and a tree-expression. */
4276 struct args_size stack_args_size;
4277 HOST_WIDE_INT extra_pretend_bytes = 0;
4278 tree fntype = TREE_TYPE (fndecl);
4279 tree fnargs = DECL_ARGUMENTS (fndecl), orig_fnargs;
4280 /* This is used for the arg pointer when referring to stack args. */
4281 rtx internal_arg_pointer;
4282 /* This is a dummy PARM_DECL that we used for the function result if
4283 the function returns a structure. */
4284 tree function_result_decl = 0;
4285 int varargs_setup = 0;
4286 int reg_parm_stack_space ATTRIBUTE_UNUSED = 0;
4287 rtx conversion_insns = 0;
4289 /* Nonzero if function takes extra anonymous args.
4290 This means the last named arg must be on the stack
4291 right before the anonymous ones. */
4292 int stdarg = current_function_stdarg;
4294 /* If the reg that the virtual arg pointer will be translated into is
4295 not a fixed reg or is the stack pointer, make a copy of the virtual
4296 arg pointer, and address parms via the copy. The frame pointer is
4297 considered fixed even though it is not marked as such.
4299 The second time through, simply use ap to avoid generating rtx. */
4301 if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
4302 || ! (fixed_regs[ARG_POINTER_REGNUM]
4303 || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM)))
4304 internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
4305 else
4306 internal_arg_pointer = virtual_incoming_args_rtx;
4307 current_function_internal_arg_pointer = internal_arg_pointer;
4309 stack_args_size.constant = 0;
4310 stack_args_size.var = 0;
4312 /* If struct value address is treated as the first argument, make it so. */
4313 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
4314 && ! current_function_returns_pcc_struct
4315 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
4317 tree type = build_pointer_type (TREE_TYPE (fntype));
4319 function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
4321 DECL_ARG_TYPE (function_result_decl) = type;
4322 TREE_CHAIN (function_result_decl) = fnargs;
4323 fnargs = function_result_decl;
4326 orig_fnargs = fnargs;
4328 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
4329 parm_reg_stack_loc = ggc_alloc_cleared (max_parm_reg * sizeof (rtx));
4331 /* If the target wants to split complex arguments into scalars, do so. */
4332 if (targetm.calls.split_complex_arg)
4333 fnargs = split_complex_args (fnargs);
4335 #ifdef REG_PARM_STACK_SPACE
4336 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
4337 #endif
4339 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
4340 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far, fntype, NULL_RTX);
4341 #else
4342 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, fndecl, -1);
4343 #endif
4345 /* We haven't yet found an argument that we must push and pretend the
4346 caller did. */
4347 current_function_pretend_args_size = 0;
4349 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
4351 rtx entry_parm;
4352 rtx stack_parm;
4353 enum machine_mode promoted_mode, passed_mode;
4354 enum machine_mode nominal_mode, promoted_nominal_mode;
4355 int unsignedp;
4356 struct locate_and_pad_arg_data locate;
4357 int passed_pointer = 0;
4358 int did_conversion = 0;
4359 tree passed_type = DECL_ARG_TYPE (parm);
4360 tree nominal_type = TREE_TYPE (parm);
4361 int last_named = 0, named_arg;
4362 int in_regs;
4363 int partial = 0;
4364 int pretend_bytes = 0;
4365 int loaded_in_reg = 0;
4367 /* Set LAST_NAMED if this is last named arg before last
4368 anonymous args. */
4369 if (stdarg)
4371 tree tem;
4373 for (tem = TREE_CHAIN (parm); tem; tem = TREE_CHAIN (tem))
4374 if (DECL_NAME (tem))
4375 break;
4377 if (tem == 0)
4378 last_named = 1;
4380 /* Set NAMED_ARG if this arg should be treated as a named arg. For
4381 most machines, if this is a varargs/stdarg function, then we treat
4382 the last named arg as if it were anonymous too. */
4383 named_arg = (targetm.calls.strict_argument_naming (&args_so_far)
4384 ? 1 : !last_named);
4386 if (TREE_TYPE (parm) == error_mark_node
4387 /* This can happen after weird syntax errors
4388 or if an enum type is defined among the parms. */
4389 || TREE_CODE (parm) != PARM_DECL
4390 || passed_type == NULL)
4392 SET_DECL_RTL (parm, gen_rtx_MEM (BLKmode, const0_rtx));
4393 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4394 TREE_USED (parm) = 1;
4395 continue;
4398 /* Find mode of arg as it is passed, and mode of arg
4399 as it should be during execution of this function. */
4400 passed_mode = TYPE_MODE (passed_type);
4401 nominal_mode = TYPE_MODE (nominal_type);
4403 /* If the parm's mode is VOID, its value doesn't matter,
4404 and avoid the usual things like emit_move_insn that could crash. */
4405 if (nominal_mode == VOIDmode)
4407 SET_DECL_RTL (parm, const0_rtx);
4408 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4409 continue;
4412 /* If the parm is to be passed as a transparent union, use the
4413 type of the first field for the tests below. We have already
4414 verified that the modes are the same. */
4415 if (DECL_TRANSPARENT_UNION (parm)
4416 || (TREE_CODE (passed_type) == UNION_TYPE
4417 && TYPE_TRANSPARENT_UNION (passed_type)))
4418 passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
4420 /* See if this arg was passed by invisible reference. It is if
4421 it is an object whose size depends on the contents of the
4422 object itself or if the machine requires these objects be passed
4423 that way. */
4425 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (passed_type))
4426 || TREE_ADDRESSABLE (passed_type)
4427 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
4428 || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, passed_mode,
4429 passed_type, named_arg)
4430 #endif
4433 passed_type = nominal_type = build_pointer_type (passed_type);
4434 passed_pointer = 1;
4435 passed_mode = nominal_mode = Pmode;
4437 /* See if the frontend wants to pass this by invisible reference. */
4438 else if (passed_type != nominal_type
4439 && POINTER_TYPE_P (passed_type)
4440 && TREE_TYPE (passed_type) == nominal_type)
4442 nominal_type = passed_type;
4443 passed_pointer = 1;
4444 passed_mode = nominal_mode = Pmode;
4447 promoted_mode = passed_mode;
4449 if (targetm.calls.promote_function_args (TREE_TYPE (fndecl)))
4451 /* Compute the mode in which the arg is actually extended to. */
4452 unsignedp = TYPE_UNSIGNED (passed_type);
4453 promoted_mode = promote_mode (passed_type, promoted_mode,
4454 &unsignedp, 1);
4457 /* Let machine desc say which reg (if any) the parm arrives in.
4458 0 means it arrives on the stack. */
4459 #ifdef FUNCTION_INCOMING_ARG
4460 entry_parm = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4461 passed_type, named_arg);
4462 #else
4463 entry_parm = FUNCTION_ARG (args_so_far, promoted_mode,
4464 passed_type, named_arg);
4465 #endif
4467 if (entry_parm == 0)
4468 promoted_mode = passed_mode;
4470 /* If this is the last named parameter, do any required setup for
4471 varargs or stdargs. We need to know about the case of this being an
4472 addressable type, in which case we skip the registers it
4473 would have arrived in.
4475 For stdargs, LAST_NAMED will be set for two parameters, the one that
4476 is actually the last named, and the dummy parameter. We only
4477 want to do this action once.
4479 Also, indicate when RTL generation is to be suppressed. */
4480 if (last_named && !varargs_setup)
4482 int varargs_pretend_bytes = 0;
4483 targetm.calls.setup_incoming_varargs (&args_so_far, promoted_mode,
4484 passed_type,
4485 &varargs_pretend_bytes, 0);
4486 varargs_setup = 1;
4488 /* If the back-end has requested extra stack space, record how
4489 much is needed. Do not change pretend_args_size otherwise
4490 since it may be nonzero from an earlier partial argument. */
4491 if (varargs_pretend_bytes > 0)
4492 current_function_pretend_args_size = varargs_pretend_bytes;
4495 /* Determine parm's home in the stack,
4496 in case it arrives in the stack or we should pretend it did.
4498 Compute the stack position and rtx where the argument arrives
4499 and its size.
4501 There is one complexity here: If this was a parameter that would
4502 have been passed in registers, but wasn't only because it is
4503 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
4504 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
4505 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of
4506 0 as it was the previous time. */
4507 in_regs = entry_parm != 0;
4508 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4509 in_regs = 1;
4510 #endif
4511 if (!in_regs && !named_arg)
4513 int pretend_named =
4514 targetm.calls.pretend_outgoing_varargs_named (&args_so_far);
4515 if (pretend_named)
4517 #ifdef FUNCTION_INCOMING_ARG
4518 in_regs = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4519 passed_type,
4520 pretend_named) != 0;
4521 #else
4522 in_regs = FUNCTION_ARG (args_so_far, promoted_mode,
4523 passed_type,
4524 pretend_named) != 0;
4525 #endif
4529 /* If this parameter was passed both in registers and in the stack,
4530 use the copy on the stack. */
4531 if (MUST_PASS_IN_STACK (promoted_mode, passed_type))
4532 entry_parm = 0;
4534 #ifdef FUNCTION_ARG_PARTIAL_NREGS
4535 if (entry_parm)
4537 partial = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, promoted_mode,
4538 passed_type, named_arg);
4539 if (partial
4540 /* The caller might already have allocated stack space
4541 for the register parameters. */
4542 && reg_parm_stack_space == 0)
4544 /* Part of this argument is passed in registers and part
4545 is passed on the stack. Ask the prologue code to extend
4546 the stack part so that we can recreate the full value.
4548 PRETEND_BYTES is the size of the registers we need to store.
4549 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
4550 stack space that the prologue should allocate.
4552 Internally, gcc assumes that the argument pointer is
4553 aligned to STACK_BOUNDARY bits. This is used both for
4554 alignment optimizations (see init_emit) and to locate
4555 arguments that are aligned to more than PARM_BOUNDARY
4556 bits. We must preserve this invariant by rounding
4557 CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to a stack
4558 boundary. */
4560 /* We assume at most one partial arg, and it must be the first
4561 argument on the stack. */
4562 if (extra_pretend_bytes || current_function_pretend_args_size)
4563 abort ();
4565 pretend_bytes = partial * UNITS_PER_WORD;
4566 current_function_pretend_args_size
4567 = CEIL_ROUND (pretend_bytes, STACK_BYTES);
4569 /* We want to align relative to the actual stack pointer, so
4570 don't include this in the stack size until later. */
4571 extra_pretend_bytes = current_function_pretend_args_size;
4574 #endif
4576 memset (&locate, 0, sizeof (locate));
4577 locate_and_pad_parm (promoted_mode, passed_type, in_regs,
4578 entry_parm ? partial : 0, fndecl,
4579 &stack_args_size, &locate);
4580 /* Adjust offsets to include the pretend args. */
4581 locate.slot_offset.constant += extra_pretend_bytes - pretend_bytes;
4582 locate.offset.constant += extra_pretend_bytes - pretend_bytes;
4585 rtx offset_rtx;
4586 unsigned int align, boundary;
4588 /* If we're passing this arg using a reg, make its stack home
4589 the aligned stack slot. */
4590 if (entry_parm)
4591 offset_rtx = ARGS_SIZE_RTX (locate.slot_offset);
4592 else
4593 offset_rtx = ARGS_SIZE_RTX (locate.offset);
4595 if (offset_rtx == const0_rtx)
4596 stack_parm = gen_rtx_MEM (promoted_mode, internal_arg_pointer);
4597 else
4598 stack_parm = gen_rtx_MEM (promoted_mode,
4599 gen_rtx_PLUS (Pmode,
4600 internal_arg_pointer,
4601 offset_rtx));
4603 set_mem_attributes (stack_parm, parm, 1);
4605 boundary = FUNCTION_ARG_BOUNDARY (promoted_mode, passed_type);
4606 align = 0;
4608 /* If we're padding upward, we know that the alignment of the slot
4609 is FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
4610 intentionally forcing upward padding. Otherwise we have to come
4611 up with a guess at the alignment based on OFFSET_RTX. */
4612 if (locate.where_pad == upward || entry_parm)
4613 align = boundary;
4614 else if (GET_CODE (offset_rtx) == CONST_INT)
4616 align = INTVAL (offset_rtx) * BITS_PER_UNIT | boundary;
4617 align = align & -align;
4619 if (align > 0)
4620 set_mem_align (stack_parm, align);
4622 if (entry_parm)
4623 set_reg_attrs_for_parm (entry_parm, stack_parm);
4626 /* If this parm was passed part in regs and part in memory,
4627 pretend it arrived entirely in memory
4628 by pushing the register-part onto the stack.
4630 In the special case of a DImode or DFmode that is split,
4631 we could put it together in a pseudoreg directly,
4632 but for now that's not worth bothering with. */
4634 if (partial)
4636 /* Handle calls that pass values in multiple non-contiguous
4637 locations. The Irix 6 ABI has examples of this. */
4638 if (GET_CODE (entry_parm) == PARALLEL)
4639 emit_group_store (validize_mem (stack_parm), entry_parm,
4640 TREE_TYPE (parm),
4641 int_size_in_bytes (TREE_TYPE (parm)));
4643 else
4644 move_block_from_reg (REGNO (entry_parm), validize_mem (stack_parm),
4645 partial);
4647 entry_parm = stack_parm;
4650 /* If we didn't decide this parm came in a register,
4651 by default it came on the stack. */
4652 if (entry_parm == 0)
4653 entry_parm = stack_parm;
4655 /* Record permanently how this parm was passed. */
4656 set_decl_incoming_rtl (parm, entry_parm);
4658 /* If there is actually space on the stack for this parm,
4659 count it in stack_args_size; otherwise set stack_parm to 0
4660 to indicate there is no preallocated stack slot for the parm. */
4662 if (entry_parm == stack_parm
4663 || (GET_CODE (entry_parm) == PARALLEL
4664 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
4665 #if defined (REG_PARM_STACK_SPACE)
4666 /* On some machines, even if a parm value arrives in a register
4667 there is still an (uninitialized) stack slot allocated
4668 for it. */
4669 || REG_PARM_STACK_SPACE (fndecl) > 0
4670 #endif
4673 stack_args_size.constant += locate.size.constant;
4674 if (locate.size.var)
4675 ADD_PARM_SIZE (stack_args_size, locate.size.var);
4677 else
4678 /* No stack slot was pushed for this parm. */
4679 stack_parm = 0;
4681 /* Update info on where next arg arrives in registers. */
4683 FUNCTION_ARG_ADVANCE (args_so_far, promoted_mode,
4684 passed_type, named_arg);
4686 /* If we can't trust the parm stack slot to be aligned enough
4687 for its ultimate type, don't use that slot after entry.
4688 We'll make another stack slot, if we need one. */
4689 if (STRICT_ALIGNMENT && stack_parm
4690 && GET_MODE_ALIGNMENT (nominal_mode) > MEM_ALIGN (stack_parm))
4691 stack_parm = 0;
4693 /* If parm was passed in memory, and we need to convert it on entry,
4694 don't store it back in that same slot. */
4695 if (entry_parm == stack_parm
4696 && nominal_mode != BLKmode && nominal_mode != passed_mode)
4697 stack_parm = 0;
4699 /* When an argument is passed in multiple locations, we can't
4700 make use of this information, but we can save some copying if
4701 the whole argument is passed in a single register. */
4702 if (GET_CODE (entry_parm) == PARALLEL
4703 && nominal_mode != BLKmode && passed_mode != BLKmode)
4705 int i, len = XVECLEN (entry_parm, 0);
4707 for (i = 0; i < len; i++)
4708 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
4709 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
4710 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
4711 == passed_mode)
4712 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
4714 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
4715 set_decl_incoming_rtl (parm, entry_parm);
4716 break;
4720 /* ENTRY_PARM is an RTX for the parameter as it arrives,
4721 in the mode in which it arrives.
4722 STACK_PARM is an RTX for a stack slot where the parameter can live
4723 during the function (in case we want to put it there).
4724 STACK_PARM is 0 if no stack slot was pushed for it.
4726 Now output code if necessary to convert ENTRY_PARM to
4727 the type in which this function declares it,
4728 and store that result in an appropriate place,
4729 which may be a pseudo reg, may be STACK_PARM,
4730 or may be a local stack slot if STACK_PARM is 0.
4732 Set DECL_RTL to that place. */
4734 if (GET_CODE (entry_parm) == PARALLEL && nominal_mode != BLKmode
4735 && XVECLEN (entry_parm, 0) > 1)
4737 /* Reconstitute objects the size of a register or larger using
4738 register operations instead of the stack. */
4739 rtx parmreg = gen_reg_rtx (nominal_mode);
4741 if (REG_P (parmreg))
4743 unsigned int regno = REGNO (parmreg);
4745 emit_group_store (parmreg, entry_parm, TREE_TYPE (parm),
4746 int_size_in_bytes (TREE_TYPE (parm)));
4747 SET_DECL_RTL (parm, parmreg);
4748 loaded_in_reg = 1;
4750 if (regno >= max_parm_reg)
4752 rtx *new;
4753 int old_max_parm_reg = max_parm_reg;
4755 /* It's slow to expand this one register at a time,
4756 but it's also rare and we need max_parm_reg to be
4757 precisely correct. */
4758 max_parm_reg = regno + 1;
4759 new = ggc_realloc (parm_reg_stack_loc,
4760 max_parm_reg * sizeof (rtx));
4761 memset (new + old_max_parm_reg, 0,
4762 (max_parm_reg - old_max_parm_reg) * sizeof (rtx));
4763 parm_reg_stack_loc = new;
4764 parm_reg_stack_loc[regno] = stack_parm;
4769 if (nominal_mode == BLKmode
4770 #ifdef BLOCK_REG_PADDING
4771 || (locate.where_pad == (BYTES_BIG_ENDIAN ? upward : downward)
4772 && GET_MODE_SIZE (promoted_mode) < UNITS_PER_WORD)
4773 #endif
4774 || GET_CODE (entry_parm) == PARALLEL)
4776 /* If a BLKmode arrives in registers, copy it to a stack slot.
4777 Handle calls that pass values in multiple non-contiguous
4778 locations. The Irix 6 ABI has examples of this. */
4779 if (REG_P (entry_parm)
4780 || (GET_CODE (entry_parm) == PARALLEL
4781 && (!loaded_in_reg || !optimize)))
4783 int size = int_size_in_bytes (TREE_TYPE (parm));
4784 int size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
4785 rtx mem;
4787 /* Note that we will be storing an integral number of words.
4788 So we have to be careful to ensure that we allocate an
4789 integral number of words. We do this below in the
4790 assign_stack_local if space was not allocated in the argument
4791 list. If it was, this will not work if PARM_BOUNDARY is not
4792 a multiple of BITS_PER_WORD. It isn't clear how to fix this
4793 if it becomes a problem. Exception is when BLKmode arrives
4794 with arguments not conforming to word_mode. */
4796 if (stack_parm == 0)
4798 stack_parm = assign_stack_local (BLKmode, size_stored, 0);
4799 PUT_MODE (stack_parm, GET_MODE (entry_parm));
4800 set_mem_attributes (stack_parm, parm, 1);
4802 else if (GET_CODE (entry_parm) == PARALLEL)
4804 else if (size != 0 && PARM_BOUNDARY % BITS_PER_WORD != 0)
4805 abort ();
4807 mem = validize_mem (stack_parm);
4809 /* Handle calls that pass values in multiple non-contiguous
4810 locations. The Irix 6 ABI has examples of this. */
4811 if (GET_CODE (entry_parm) == PARALLEL)
4812 emit_group_store (mem, entry_parm, TREE_TYPE (parm), size);
4814 else if (size == 0)
4817 /* If SIZE is that of a mode no bigger than a word, just use
4818 that mode's store operation. */
4819 else if (size <= UNITS_PER_WORD)
4821 enum machine_mode mode
4822 = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
4824 if (mode != BLKmode
4825 #ifdef BLOCK_REG_PADDING
4826 && (size == UNITS_PER_WORD
4827 || (BLOCK_REG_PADDING (mode, TREE_TYPE (parm), 1)
4828 != (BYTES_BIG_ENDIAN ? upward : downward)))
4829 #endif
4832 rtx reg = gen_rtx_REG (mode, REGNO (entry_parm));
4833 emit_move_insn (change_address (mem, mode, 0), reg);
4836 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
4837 machine must be aligned to the left before storing
4838 to memory. Note that the previous test doesn't
4839 handle all cases (e.g. SIZE == 3). */
4840 else if (size != UNITS_PER_WORD
4841 #ifdef BLOCK_REG_PADDING
4842 && (BLOCK_REG_PADDING (mode, TREE_TYPE (parm), 1)
4843 == downward)
4844 #else
4845 && BYTES_BIG_ENDIAN
4846 #endif
4849 rtx tem, x;
4850 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
4851 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
4853 x = expand_binop (word_mode, ashl_optab, reg,
4854 GEN_INT (by), 0, 1, OPTAB_WIDEN);
4855 tem = change_address (mem, word_mode, 0);
4856 emit_move_insn (tem, x);
4858 else
4859 move_block_from_reg (REGNO (entry_parm), mem,
4860 size_stored / UNITS_PER_WORD);
4862 else
4863 move_block_from_reg (REGNO (entry_parm), mem,
4864 size_stored / UNITS_PER_WORD);
4866 /* If parm is already bound to register pair, don't change
4867 this binding. */
4868 if (! DECL_RTL_SET_P (parm))
4869 SET_DECL_RTL (parm, stack_parm);
4871 else if (! ((! optimize
4872 && ! DECL_REGISTER (parm))
4873 || TREE_SIDE_EFFECTS (parm)
4874 /* If -ffloat-store specified, don't put explicit
4875 float variables into registers. */
4876 || (flag_float_store
4877 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
4878 /* Always assign pseudo to structure return or item passed
4879 by invisible reference. */
4880 || passed_pointer || parm == function_result_decl)
4882 /* Store the parm in a pseudoregister during the function, but we
4883 may need to do it in a wider mode. */
4885 rtx parmreg;
4886 unsigned int regno, regnoi = 0, regnor = 0;
4888 unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
4890 promoted_nominal_mode
4891 = promote_mode (TREE_TYPE (parm), nominal_mode, &unsignedp, 0);
4893 parmreg = gen_reg_rtx (promoted_nominal_mode);
4894 mark_user_reg (parmreg);
4896 /* If this was an item that we received a pointer to, set DECL_RTL
4897 appropriately. */
4898 if (passed_pointer)
4900 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (passed_type)),
4901 parmreg);
4902 set_mem_attributes (x, parm, 1);
4903 SET_DECL_RTL (parm, x);
4905 else
4907 SET_DECL_RTL (parm, parmreg);
4908 maybe_set_unchanging (DECL_RTL (parm), parm);
4911 /* Copy the value into the register. */
4912 if (nominal_mode != passed_mode
4913 || promoted_nominal_mode != promoted_mode)
4915 int save_tree_used;
4916 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
4917 mode, by the caller. We now have to convert it to
4918 NOMINAL_MODE, if different. However, PARMREG may be in
4919 a different mode than NOMINAL_MODE if it is being stored
4920 promoted.
4922 If ENTRY_PARM is a hard register, it might be in a register
4923 not valid for operating in its mode (e.g., an odd-numbered
4924 register for a DFmode). In that case, moves are the only
4925 thing valid, so we can't do a convert from there. This
4926 occurs when the calling sequence allow such misaligned
4927 usages.
4929 In addition, the conversion may involve a call, which could
4930 clobber parameters which haven't been copied to pseudo
4931 registers yet. Therefore, we must first copy the parm to
4932 a pseudo reg here, and save the conversion until after all
4933 parameters have been moved. */
4935 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4937 emit_move_insn (tempreg, validize_mem (entry_parm));
4939 push_to_sequence (conversion_insns);
4940 tempreg = convert_to_mode (nominal_mode, tempreg, unsignedp);
4942 if (GET_CODE (tempreg) == SUBREG
4943 && GET_MODE (tempreg) == nominal_mode
4944 && REG_P (SUBREG_REG (tempreg))
4945 && nominal_mode == passed_mode
4946 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (entry_parm)
4947 && GET_MODE_SIZE (GET_MODE (tempreg))
4948 < GET_MODE_SIZE (GET_MODE (entry_parm)))
4950 /* The argument is already sign/zero extended, so note it
4951 into the subreg. */
4952 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
4953 SUBREG_PROMOTED_UNSIGNED_SET (tempreg, unsignedp);
4956 /* TREE_USED gets set erroneously during expand_assignment. */
4957 save_tree_used = TREE_USED (parm);
4958 expand_assignment (parm,
4959 make_tree (nominal_type, tempreg), 0);
4960 TREE_USED (parm) = save_tree_used;
4961 conversion_insns = get_insns ();
4962 did_conversion = 1;
4963 end_sequence ();
4965 else
4966 emit_move_insn (parmreg, validize_mem (entry_parm));
4968 /* If we were passed a pointer but the actual value
4969 can safely live in a register, put it in one. */
4970 if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
4971 /* If by-reference argument was promoted, demote it. */
4972 && (TYPE_MODE (TREE_TYPE (parm)) != GET_MODE (DECL_RTL (parm))
4973 || ! ((! optimize
4974 && ! DECL_REGISTER (parm))
4975 || TREE_SIDE_EFFECTS (parm)
4976 /* If -ffloat-store specified, don't put explicit
4977 float variables into registers. */
4978 || (flag_float_store
4979 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))))
4981 /* We can't use nominal_mode, because it will have been set to
4982 Pmode above. We must use the actual mode of the parm. */
4983 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
4984 mark_user_reg (parmreg);
4985 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
4987 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
4988 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
4989 push_to_sequence (conversion_insns);
4990 emit_move_insn (tempreg, DECL_RTL (parm));
4991 SET_DECL_RTL (parm,
4992 convert_to_mode (GET_MODE (parmreg),
4993 tempreg,
4994 unsigned_p));
4995 emit_move_insn (parmreg, DECL_RTL (parm));
4996 conversion_insns = get_insns();
4997 did_conversion = 1;
4998 end_sequence ();
5000 else
5001 emit_move_insn (parmreg, DECL_RTL (parm));
5002 SET_DECL_RTL (parm, parmreg);
5003 /* STACK_PARM is the pointer, not the parm, and PARMREG is
5004 now the parm. */
5005 stack_parm = 0;
5007 #ifdef FUNCTION_ARG_CALLEE_COPIES
5008 /* If we are passed an arg by reference and it is our responsibility
5009 to make a copy, do it now.
5010 PASSED_TYPE and PASSED mode now refer to the pointer, not the
5011 original argument, so we must recreate them in the call to
5012 FUNCTION_ARG_CALLEE_COPIES. */
5013 /* ??? Later add code to handle the case that if the argument isn't
5014 modified, don't do the copy. */
5016 else if (passed_pointer
5017 && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
5018 TYPE_MODE (TREE_TYPE (passed_type)),
5019 TREE_TYPE (passed_type),
5020 named_arg)
5021 && ! TREE_ADDRESSABLE (TREE_TYPE (passed_type)))
5023 rtx copy;
5024 tree type = TREE_TYPE (passed_type);
5026 /* This sequence may involve a library call perhaps clobbering
5027 registers that haven't been copied to pseudos yet. */
5029 push_to_sequence (conversion_insns);
5031 if (!COMPLETE_TYPE_P (type)
5032 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5033 /* This is a variable sized object. */
5034 copy = gen_rtx_MEM (BLKmode,
5035 allocate_dynamic_stack_space
5036 (expr_size (parm), NULL_RTX,
5037 TYPE_ALIGN (type)));
5038 else
5039 copy = assign_stack_temp (TYPE_MODE (type),
5040 int_size_in_bytes (type), 1);
5041 set_mem_attributes (copy, parm, 1);
5043 store_expr (parm, copy, 0);
5044 emit_move_insn (parmreg, XEXP (copy, 0));
5045 conversion_insns = get_insns ();
5046 did_conversion = 1;
5047 end_sequence ();
5049 #endif /* FUNCTION_ARG_CALLEE_COPIES */
5051 /* In any case, record the parm's desired stack location
5052 in case we later discover it must live in the stack.
5054 If it is a COMPLEX value, store the stack location for both
5055 halves. */
5057 if (GET_CODE (parmreg) == CONCAT)
5058 regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
5059 else
5060 regno = REGNO (parmreg);
5062 if (regno >= max_parm_reg)
5064 rtx *new;
5065 int old_max_parm_reg = max_parm_reg;
5067 /* It's slow to expand this one register at a time,
5068 but it's also rare and we need max_parm_reg to be
5069 precisely correct. */
5070 max_parm_reg = regno + 1;
5071 new = ggc_realloc (parm_reg_stack_loc,
5072 max_parm_reg * sizeof (rtx));
5073 memset (new + old_max_parm_reg, 0,
5074 (max_parm_reg - old_max_parm_reg) * sizeof (rtx));
5075 parm_reg_stack_loc = new;
5078 if (GET_CODE (parmreg) == CONCAT)
5080 enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
5082 regnor = REGNO (gen_realpart (submode, parmreg));
5083 regnoi = REGNO (gen_imagpart (submode, parmreg));
5085 if (stack_parm != 0)
5087 parm_reg_stack_loc[regnor]
5088 = gen_realpart (submode, stack_parm);
5089 parm_reg_stack_loc[regnoi]
5090 = gen_imagpart (submode, stack_parm);
5092 else
5094 parm_reg_stack_loc[regnor] = 0;
5095 parm_reg_stack_loc[regnoi] = 0;
5098 else
5099 parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
5101 /* Mark the register as eliminable if we did no conversion
5102 and it was copied from memory at a fixed offset,
5103 and the arg pointer was not copied to a pseudo-reg.
5104 If the arg pointer is a pseudo reg or the offset formed
5105 an invalid address, such memory-equivalences
5106 as we make here would screw up life analysis for it. */
5107 if (nominal_mode == passed_mode
5108 && ! did_conversion
5109 && stack_parm != 0
5110 && GET_CODE (stack_parm) == MEM
5111 && locate.offset.var == 0
5112 && reg_mentioned_p (virtual_incoming_args_rtx,
5113 XEXP (stack_parm, 0)))
5115 rtx linsn = get_last_insn ();
5116 rtx sinsn, set;
5118 /* Mark complex types separately. */
5119 if (GET_CODE (parmreg) == CONCAT)
5120 /* Scan backwards for the set of the real and
5121 imaginary parts. */
5122 for (sinsn = linsn; sinsn != 0;
5123 sinsn = prev_nonnote_insn (sinsn))
5125 set = single_set (sinsn);
5126 if (set != 0
5127 && SET_DEST (set) == regno_reg_rtx [regnoi])
5128 REG_NOTES (sinsn)
5129 = gen_rtx_EXPR_LIST (REG_EQUIV,
5130 parm_reg_stack_loc[regnoi],
5131 REG_NOTES (sinsn));
5132 else if (set != 0
5133 && SET_DEST (set) == regno_reg_rtx [regnor])
5134 REG_NOTES (sinsn)
5135 = gen_rtx_EXPR_LIST (REG_EQUIV,
5136 parm_reg_stack_loc[regnor],
5137 REG_NOTES (sinsn));
5139 else if ((set = single_set (linsn)) != 0
5140 && SET_DEST (set) == parmreg)
5141 REG_NOTES (linsn)
5142 = gen_rtx_EXPR_LIST (REG_EQUIV,
5143 stack_parm, REG_NOTES (linsn));
5146 /* For pointer data type, suggest pointer register. */
5147 if (POINTER_TYPE_P (TREE_TYPE (parm)))
5148 mark_reg_pointer (parmreg,
5149 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
5151 /* If something wants our address, try to use ADDRESSOF. */
5152 if (TREE_ADDRESSABLE (parm))
5154 /* If we end up putting something into the stack,
5155 fixup_var_refs_insns will need to make a pass over
5156 all the instructions. It looks through the pending
5157 sequences -- but it can't see the ones in the
5158 CONVERSION_INSNS, if they're not on the sequence
5159 stack. So, we go back to that sequence, just so that
5160 the fixups will happen. */
5161 push_to_sequence (conversion_insns);
5162 put_var_into_stack (parm, /*rescan=*/true);
5163 conversion_insns = get_insns ();
5164 end_sequence ();
5167 else
5169 /* Value must be stored in the stack slot STACK_PARM
5170 during function execution. */
5172 if (promoted_mode != nominal_mode)
5174 /* Conversion is required. */
5175 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
5177 emit_move_insn (tempreg, validize_mem (entry_parm));
5179 push_to_sequence (conversion_insns);
5180 entry_parm = convert_to_mode (nominal_mode, tempreg,
5181 TYPE_UNSIGNED (TREE_TYPE (parm)));
5182 if (stack_parm)
5183 /* ??? This may need a big-endian conversion on sparc64. */
5184 stack_parm = adjust_address (stack_parm, nominal_mode, 0);
5186 conversion_insns = get_insns ();
5187 did_conversion = 1;
5188 end_sequence ();
5191 if (entry_parm != stack_parm)
5193 if (stack_parm == 0)
5195 stack_parm
5196 = assign_stack_local (GET_MODE (entry_parm),
5197 GET_MODE_SIZE (GET_MODE (entry_parm)),
5199 set_mem_attributes (stack_parm, parm, 1);
5202 if (promoted_mode != nominal_mode)
5204 push_to_sequence (conversion_insns);
5205 emit_move_insn (validize_mem (stack_parm),
5206 validize_mem (entry_parm));
5207 conversion_insns = get_insns ();
5208 end_sequence ();
5210 else
5211 emit_move_insn (validize_mem (stack_parm),
5212 validize_mem (entry_parm));
5215 SET_DECL_RTL (parm, stack_parm);
5219 if (targetm.calls.split_complex_arg && fnargs != orig_fnargs)
5221 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm))
5223 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
5224 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
5226 rtx tmp, real, imag;
5227 enum machine_mode inner = GET_MODE_INNER (DECL_MODE (parm));
5229 real = DECL_RTL (fnargs);
5230 imag = DECL_RTL (TREE_CHAIN (fnargs));
5231 if (inner != GET_MODE (real))
5233 real = gen_lowpart_SUBREG (inner, real);
5234 imag = gen_lowpart_SUBREG (inner, imag);
5236 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
5237 SET_DECL_RTL (parm, tmp);
5239 real = DECL_INCOMING_RTL (fnargs);
5240 imag = DECL_INCOMING_RTL (TREE_CHAIN (fnargs));
5241 if (inner != GET_MODE (real))
5243 real = gen_lowpart_SUBREG (inner, real);
5244 imag = gen_lowpart_SUBREG (inner, imag);
5246 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
5247 set_decl_incoming_rtl (parm, tmp);
5248 fnargs = TREE_CHAIN (fnargs);
5250 else
5252 SET_DECL_RTL (parm, DECL_RTL (fnargs));
5253 set_decl_incoming_rtl (parm, DECL_INCOMING_RTL (fnargs));
5255 /* Set MEM_EXPR to the original decl, i.e. to PARM,
5256 instead of the copy of decl, i.e. FNARGS. */
5257 if (DECL_INCOMING_RTL (parm)
5258 && GET_CODE (DECL_INCOMING_RTL (parm)) == MEM)
5259 set_mem_expr (DECL_INCOMING_RTL (parm), parm);
5261 fnargs = TREE_CHAIN (fnargs);
5265 /* Output all parameter conversion instructions (possibly including calls)
5266 now that all parameters have been copied out of hard registers. */
5267 emit_insn (conversion_insns);
5269 /* If we are receiving a struct value address as the first argument, set up
5270 the RTL for the function result. As this might require code to convert
5271 the transmitted address to Pmode, we do this here to ensure that possible
5272 preliminary conversions of the address have been emitted already. */
5273 if (function_result_decl)
5275 tree result = DECL_RESULT (fndecl);
5276 rtx addr = DECL_RTL (function_result_decl);
5277 rtx x;
5279 addr = convert_memory_address (Pmode, addr);
5280 x = gen_rtx_MEM (DECL_MODE (result), addr);
5281 set_mem_attributes (x, result, 1);
5282 SET_DECL_RTL (result, x);
5285 /* We have aligned all the args, so add space for the pretend args. */
5286 stack_args_size.constant += extra_pretend_bytes;
5287 current_function_args_size = stack_args_size.constant;
5289 /* Adjust function incoming argument size for alignment and
5290 minimum length. */
5292 #ifdef REG_PARM_STACK_SPACE
5293 current_function_args_size = MAX (current_function_args_size,
5294 REG_PARM_STACK_SPACE (fndecl));
5295 #endif
5297 current_function_args_size
5298 = ((current_function_args_size + STACK_BYTES - 1)
5299 / STACK_BYTES) * STACK_BYTES;
5301 #ifdef ARGS_GROW_DOWNWARD
5302 current_function_arg_offset_rtx
5303 = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
5304 : expand_expr (size_diffop (stack_args_size.var,
5305 size_int (-stack_args_size.constant)),
5306 NULL_RTX, VOIDmode, 0));
5307 #else
5308 current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
5309 #endif
5311 /* See how many bytes, if any, of its args a function should try to pop
5312 on return. */
5314 current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
5315 current_function_args_size);
5317 /* For stdarg.h function, save info about
5318 regs and stack space used by the named args. */
5320 current_function_args_info = args_so_far;
5322 /* Set the rtx used for the function return value. Put this in its
5323 own variable so any optimizers that need this information don't have
5324 to include tree.h. Do this here so it gets done when an inlined
5325 function gets output. */
5327 current_function_return_rtx
5328 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
5329 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
5331 /* If scalar return value was computed in a pseudo-reg, or was a named
5332 return value that got dumped to the stack, copy that to the hard
5333 return register. */
5334 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
5336 tree decl_result = DECL_RESULT (fndecl);
5337 rtx decl_rtl = DECL_RTL (decl_result);
5339 if (REG_P (decl_rtl)
5340 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5341 : DECL_REGISTER (decl_result))
5343 rtx real_decl_rtl;
5345 #ifdef FUNCTION_OUTGOING_VALUE
5346 real_decl_rtl = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl_result),
5347 fndecl);
5348 #else
5349 real_decl_rtl = FUNCTION_VALUE (TREE_TYPE (decl_result),
5350 fndecl);
5351 #endif
5352 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
5353 /* The delay slot scheduler assumes that current_function_return_rtx
5354 holds the hard register containing the return value, not a
5355 temporary pseudo. */
5356 current_function_return_rtx = real_decl_rtl;
5361 /* If ARGS contains entries with complex types, split the entry into two
5362 entries of the component type. Return a new list of substitutions are
5363 needed, else the old list. */
5365 static tree
5366 split_complex_args (tree args)
5368 tree p;
5370 /* Before allocating memory, check for the common case of no complex. */
5371 for (p = args; p; p = TREE_CHAIN (p))
5373 tree type = TREE_TYPE (p);
5374 if (TREE_CODE (type) == COMPLEX_TYPE
5375 && targetm.calls.split_complex_arg (type))
5376 goto found;
5378 return args;
5380 found:
5381 args = copy_list (args);
5383 for (p = args; p; p = TREE_CHAIN (p))
5385 tree type = TREE_TYPE (p);
5386 if (TREE_CODE (type) == COMPLEX_TYPE
5387 && targetm.calls.split_complex_arg (type))
5389 tree decl;
5390 tree subtype = TREE_TYPE (type);
5392 /* Rewrite the PARM_DECL's type with its component. */
5393 TREE_TYPE (p) = subtype;
5394 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
5395 DECL_MODE (p) = VOIDmode;
5396 DECL_SIZE (p) = NULL;
5397 DECL_SIZE_UNIT (p) = NULL;
5398 layout_decl (p, 0);
5400 /* Build a second synthetic decl. */
5401 decl = build_decl (PARM_DECL, NULL_TREE, subtype);
5402 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
5403 layout_decl (decl, 0);
5405 /* Splice it in; skip the new decl. */
5406 TREE_CHAIN (decl) = TREE_CHAIN (p);
5407 TREE_CHAIN (p) = decl;
5408 p = decl;
5412 return args;
5415 /* Indicate whether REGNO is an incoming argument to the current function
5416 that was promoted to a wider mode. If so, return the RTX for the
5417 register (to get its mode). PMODE and PUNSIGNEDP are set to the mode
5418 that REGNO is promoted from and whether the promotion was signed or
5419 unsigned. */
5422 promoted_input_arg (unsigned int regno, enum machine_mode *pmode, int *punsignedp)
5424 tree arg;
5426 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
5427 arg = TREE_CHAIN (arg))
5428 if (REG_P (DECL_INCOMING_RTL (arg))
5429 && REGNO (DECL_INCOMING_RTL (arg)) == regno
5430 && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
5432 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
5433 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (arg));
5435 mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
5436 if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
5437 && mode != DECL_MODE (arg))
5439 *pmode = DECL_MODE (arg);
5440 *punsignedp = unsignedp;
5441 return DECL_INCOMING_RTL (arg);
5445 return 0;
5449 /* Compute the size and offset from the start of the stacked arguments for a
5450 parm passed in mode PASSED_MODE and with type TYPE.
5452 INITIAL_OFFSET_PTR points to the current offset into the stacked
5453 arguments.
5455 The starting offset and size for this parm are returned in
5456 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
5457 nonzero, the offset is that of stack slot, which is returned in
5458 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
5459 padding required from the initial offset ptr to the stack slot.
5461 IN_REGS is nonzero if the argument will be passed in registers. It will
5462 never be set if REG_PARM_STACK_SPACE is not defined.
5464 FNDECL is the function in which the argument was defined.
5466 There are two types of rounding that are done. The first, controlled by
5467 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
5468 list to be aligned to the specific boundary (in bits). This rounding
5469 affects the initial and starting offsets, but not the argument size.
5471 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
5472 optionally rounds the size of the parm to PARM_BOUNDARY. The
5473 initial offset is not affected by this rounding, while the size always
5474 is and the starting offset may be. */
5476 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
5477 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
5478 callers pass in the total size of args so far as
5479 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
5481 void
5482 locate_and_pad_parm (enum machine_mode passed_mode, tree type, int in_regs,
5483 int partial, tree fndecl ATTRIBUTE_UNUSED,
5484 struct args_size *initial_offset_ptr,
5485 struct locate_and_pad_arg_data *locate)
5487 tree sizetree;
5488 enum direction where_pad;
5489 int boundary;
5490 int reg_parm_stack_space = 0;
5491 int part_size_in_regs;
5493 #ifdef REG_PARM_STACK_SPACE
5494 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
5496 /* If we have found a stack parm before we reach the end of the
5497 area reserved for registers, skip that area. */
5498 if (! in_regs)
5500 if (reg_parm_stack_space > 0)
5502 if (initial_offset_ptr->var)
5504 initial_offset_ptr->var
5505 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
5506 ssize_int (reg_parm_stack_space));
5507 initial_offset_ptr->constant = 0;
5509 else if (initial_offset_ptr->constant < reg_parm_stack_space)
5510 initial_offset_ptr->constant = reg_parm_stack_space;
5513 #endif /* REG_PARM_STACK_SPACE */
5515 part_size_in_regs = 0;
5516 if (reg_parm_stack_space == 0)
5517 part_size_in_regs = ((partial * UNITS_PER_WORD)
5518 / (PARM_BOUNDARY / BITS_PER_UNIT)
5519 * (PARM_BOUNDARY / BITS_PER_UNIT));
5521 sizetree
5522 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
5523 where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
5524 boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
5525 locate->where_pad = where_pad;
5527 #ifdef ARGS_GROW_DOWNWARD
5528 locate->slot_offset.constant = -initial_offset_ptr->constant;
5529 if (initial_offset_ptr->var)
5530 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
5531 initial_offset_ptr->var);
5534 tree s2 = sizetree;
5535 if (where_pad != none
5536 && (!host_integerp (sizetree, 1)
5537 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5538 s2 = round_up (s2, PARM_BOUNDARY / BITS_PER_UNIT);
5539 SUB_PARM_SIZE (locate->slot_offset, s2);
5542 locate->slot_offset.constant += part_size_in_regs;
5544 if (!in_regs
5545 #ifdef REG_PARM_STACK_SPACE
5546 || REG_PARM_STACK_SPACE (fndecl) > 0
5547 #endif
5549 pad_to_arg_alignment (&locate->slot_offset, boundary,
5550 &locate->alignment_pad);
5552 locate->size.constant = (-initial_offset_ptr->constant
5553 - locate->slot_offset.constant);
5554 if (initial_offset_ptr->var)
5555 locate->size.var = size_binop (MINUS_EXPR,
5556 size_binop (MINUS_EXPR,
5557 ssize_int (0),
5558 initial_offset_ptr->var),
5559 locate->slot_offset.var);
5561 /* Pad_below needs the pre-rounded size to know how much to pad
5562 below. */
5563 locate->offset = locate->slot_offset;
5564 if (where_pad == downward)
5565 pad_below (&locate->offset, passed_mode, sizetree);
5567 #else /* !ARGS_GROW_DOWNWARD */
5568 if (!in_regs
5569 #ifdef REG_PARM_STACK_SPACE
5570 || REG_PARM_STACK_SPACE (fndecl) > 0
5571 #endif
5573 pad_to_arg_alignment (initial_offset_ptr, boundary,
5574 &locate->alignment_pad);
5575 locate->slot_offset = *initial_offset_ptr;
5577 #ifdef PUSH_ROUNDING
5578 if (passed_mode != BLKmode)
5579 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
5580 #endif
5582 /* Pad_below needs the pre-rounded size to know how much to pad below
5583 so this must be done before rounding up. */
5584 locate->offset = locate->slot_offset;
5585 if (where_pad == downward)
5586 pad_below (&locate->offset, passed_mode, sizetree);
5588 if (where_pad != none
5589 && (!host_integerp (sizetree, 1)
5590 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5591 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5593 ADD_PARM_SIZE (locate->size, sizetree);
5595 locate->size.constant -= part_size_in_regs;
5596 #endif /* ARGS_GROW_DOWNWARD */
5599 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
5600 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
5602 static void
5603 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
5604 struct args_size *alignment_pad)
5606 tree save_var = NULL_TREE;
5607 HOST_WIDE_INT save_constant = 0;
5608 int boundary_in_bytes = boundary / BITS_PER_UNIT;
5609 HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
5611 #ifdef SPARC_STACK_BOUNDARY_HACK
5612 /* The sparc port has a bug. It sometimes claims a STACK_BOUNDARY
5613 higher than the real alignment of %sp. However, when it does this,
5614 the alignment of %sp+STACK_POINTER_OFFSET will be STACK_BOUNDARY.
5615 This is a temporary hack while the sparc port is fixed. */
5616 if (SPARC_STACK_BOUNDARY_HACK)
5617 sp_offset = 0;
5618 #endif
5620 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5622 save_var = offset_ptr->var;
5623 save_constant = offset_ptr->constant;
5626 alignment_pad->var = NULL_TREE;
5627 alignment_pad->constant = 0;
5629 if (boundary > BITS_PER_UNIT)
5631 if (offset_ptr->var)
5633 tree sp_offset_tree = ssize_int (sp_offset);
5634 tree offset = size_binop (PLUS_EXPR,
5635 ARGS_SIZE_TREE (*offset_ptr),
5636 sp_offset_tree);
5637 #ifdef ARGS_GROW_DOWNWARD
5638 tree rounded = round_down (offset, boundary / BITS_PER_UNIT);
5639 #else
5640 tree rounded = round_up (offset, boundary / BITS_PER_UNIT);
5641 #endif
5643 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
5644 /* ARGS_SIZE_TREE includes constant term. */
5645 offset_ptr->constant = 0;
5646 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5647 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
5648 save_var);
5650 else
5652 offset_ptr->constant = -sp_offset +
5653 #ifdef ARGS_GROW_DOWNWARD
5654 FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
5655 #else
5656 CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
5657 #endif
5658 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5659 alignment_pad->constant = offset_ptr->constant - save_constant;
5664 static void
5665 pad_below (struct args_size *offset_ptr, enum machine_mode passed_mode, tree sizetree)
5667 if (passed_mode != BLKmode)
5669 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
5670 offset_ptr->constant
5671 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
5672 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
5673 - GET_MODE_SIZE (passed_mode));
5675 else
5677 if (TREE_CODE (sizetree) != INTEGER_CST
5678 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
5680 /* Round the size up to multiple of PARM_BOUNDARY bits. */
5681 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5682 /* Add it in. */
5683 ADD_PARM_SIZE (*offset_ptr, s2);
5684 SUB_PARM_SIZE (*offset_ptr, sizetree);
5689 /* Walk the tree of blocks describing the binding levels within a function
5690 and warn about variables the might be killed by setjmp or vfork.
5691 This is done after calling flow_analysis and before global_alloc
5692 clobbers the pseudo-regs to hard regs. */
5694 void
5695 setjmp_vars_warning (tree block)
5697 tree decl, sub;
5699 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5701 if (TREE_CODE (decl) == VAR_DECL
5702 && DECL_RTL_SET_P (decl)
5703 && REG_P (DECL_RTL (decl))
5704 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5705 warning ("%Jvariable '%D' might be clobbered by `longjmp' or `vfork'",
5706 decl, decl);
5709 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5710 setjmp_vars_warning (sub);
5713 /* Do the appropriate part of setjmp_vars_warning
5714 but for arguments instead of local variables. */
5716 void
5717 setjmp_args_warning (void)
5719 tree decl;
5720 for (decl = DECL_ARGUMENTS (current_function_decl);
5721 decl; decl = TREE_CHAIN (decl))
5722 if (DECL_RTL (decl) != 0
5723 && REG_P (DECL_RTL (decl))
5724 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5725 warning ("%Jargument '%D' might be clobbered by `longjmp' or `vfork'",
5726 decl, decl);
5729 /* If this function call setjmp, put all vars into the stack
5730 unless they were declared `register'. */
5732 void
5733 setjmp_protect (tree block)
5735 tree decl, sub;
5736 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5737 if ((TREE_CODE (decl) == VAR_DECL
5738 || TREE_CODE (decl) == PARM_DECL)
5739 && DECL_RTL (decl) != 0
5740 && (REG_P (DECL_RTL (decl))
5741 || (GET_CODE (DECL_RTL (decl)) == MEM
5742 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5743 /* If this variable came from an inline function, it must be
5744 that its life doesn't overlap the setjmp. If there was a
5745 setjmp in the function, it would already be in memory. We
5746 must exclude such variable because their DECL_RTL might be
5747 set to strange things such as virtual_stack_vars_rtx. */
5748 && ! DECL_FROM_INLINE (decl)
5749 && (
5750 #ifdef NON_SAVING_SETJMP
5751 /* If longjmp doesn't restore the registers,
5752 don't put anything in them. */
5753 NON_SAVING_SETJMP
5755 #endif
5756 ! DECL_REGISTER (decl)))
5757 put_var_into_stack (decl, /*rescan=*/true);
5758 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5759 setjmp_protect (sub);
5762 /* Like the previous function, but for args instead of local variables. */
5764 void
5765 setjmp_protect_args (void)
5767 tree decl;
5768 for (decl = DECL_ARGUMENTS (current_function_decl);
5769 decl; decl = TREE_CHAIN (decl))
5770 if ((TREE_CODE (decl) == VAR_DECL
5771 || TREE_CODE (decl) == PARM_DECL)
5772 && DECL_RTL (decl) != 0
5773 && (REG_P (DECL_RTL (decl))
5774 || (GET_CODE (DECL_RTL (decl)) == MEM
5775 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5776 && (
5777 /* If longjmp doesn't restore the registers,
5778 don't put anything in them. */
5779 #ifdef NON_SAVING_SETJMP
5780 NON_SAVING_SETJMP
5782 #endif
5783 ! DECL_REGISTER (decl)))
5784 put_var_into_stack (decl, /*rescan=*/true);
5787 /* Convert a stack slot address ADDR for variable VAR
5788 (from a containing function)
5789 into an address valid in this function (using a static chain). */
5792 fix_lexical_addr (rtx addr, tree var)
5794 rtx basereg;
5795 HOST_WIDE_INT displacement;
5796 tree context = decl_function_context (var);
5797 struct function *fp;
5798 rtx base = 0;
5800 /* If this is the present function, we need not do anything. */
5801 if (context == current_function_decl)
5802 return addr;
5804 fp = find_function_data (context);
5806 if (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == MEM)
5807 addr = XEXP (XEXP (addr, 0), 0);
5809 /* Decode given address as base reg plus displacement. */
5810 if (REG_P (addr))
5811 basereg = addr, displacement = 0;
5812 else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
5813 basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
5814 else
5815 abort ();
5817 if (base == 0)
5818 abort ();
5820 /* Use same offset, relative to appropriate static chain or argument
5821 pointer. */
5822 return plus_constant (base, displacement);
5825 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
5826 and create duplicate blocks. */
5827 /* ??? Need an option to either create block fragments or to create
5828 abstract origin duplicates of a source block. It really depends
5829 on what optimization has been performed. */
5831 void
5832 reorder_blocks (void)
5834 tree block = DECL_INITIAL (current_function_decl);
5835 varray_type block_stack;
5837 if (block == NULL_TREE)
5838 return;
5840 VARRAY_TREE_INIT (block_stack, 10, "block_stack");
5842 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
5843 clear_block_marks (block);
5845 /* Prune the old trees away, so that they don't get in the way. */
5846 BLOCK_SUBBLOCKS (block) = NULL_TREE;
5847 BLOCK_CHAIN (block) = NULL_TREE;
5849 /* Recreate the block tree from the note nesting. */
5850 reorder_blocks_1 (get_insns (), block, &block_stack);
5851 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
5853 /* Remove deleted blocks from the block fragment chains. */
5854 reorder_fix_fragments (block);
5857 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
5859 void
5860 clear_block_marks (tree block)
5862 while (block)
5864 TREE_ASM_WRITTEN (block) = 0;
5865 clear_block_marks (BLOCK_SUBBLOCKS (block));
5866 block = BLOCK_CHAIN (block);
5870 static void
5871 reorder_blocks_1 (rtx insns, tree current_block, varray_type *p_block_stack)
5873 rtx insn;
5875 for (insn = insns; insn; insn = NEXT_INSN (insn))
5877 if (GET_CODE (insn) == NOTE)
5879 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5881 tree block = NOTE_BLOCK (insn);
5883 /* If we have seen this block before, that means it now
5884 spans multiple address regions. Create a new fragment. */
5885 if (TREE_ASM_WRITTEN (block))
5887 tree new_block = copy_node (block);
5888 tree origin;
5890 origin = (BLOCK_FRAGMENT_ORIGIN (block)
5891 ? BLOCK_FRAGMENT_ORIGIN (block)
5892 : block);
5893 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
5894 BLOCK_FRAGMENT_CHAIN (new_block)
5895 = BLOCK_FRAGMENT_CHAIN (origin);
5896 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
5898 NOTE_BLOCK (insn) = new_block;
5899 block = new_block;
5902 BLOCK_SUBBLOCKS (block) = 0;
5903 TREE_ASM_WRITTEN (block) = 1;
5904 /* When there's only one block for the entire function,
5905 current_block == block and we mustn't do this, it
5906 will cause infinite recursion. */
5907 if (block != current_block)
5909 BLOCK_SUPERCONTEXT (block) = current_block;
5910 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
5911 BLOCK_SUBBLOCKS (current_block) = block;
5912 current_block = block;
5914 VARRAY_PUSH_TREE (*p_block_stack, block);
5916 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5918 NOTE_BLOCK (insn) = VARRAY_TOP_TREE (*p_block_stack);
5919 VARRAY_POP (*p_block_stack);
5920 BLOCK_SUBBLOCKS (current_block)
5921 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
5922 current_block = BLOCK_SUPERCONTEXT (current_block);
5928 /* Rationalize BLOCK_FRAGMENT_ORIGIN. If an origin block no longer
5929 appears in the block tree, select one of the fragments to become
5930 the new origin block. */
5932 static void
5933 reorder_fix_fragments (tree block)
5935 while (block)
5937 tree dup_origin = BLOCK_FRAGMENT_ORIGIN (block);
5938 tree new_origin = NULL_TREE;
5940 if (dup_origin)
5942 if (! TREE_ASM_WRITTEN (dup_origin))
5944 new_origin = BLOCK_FRAGMENT_CHAIN (dup_origin);
5946 /* Find the first of the remaining fragments. There must
5947 be at least one -- the current block. */
5948 while (! TREE_ASM_WRITTEN (new_origin))
5949 new_origin = BLOCK_FRAGMENT_CHAIN (new_origin);
5950 BLOCK_FRAGMENT_ORIGIN (new_origin) = NULL_TREE;
5953 else if (! dup_origin)
5954 new_origin = block;
5956 /* Re-root the rest of the fragments to the new origin. In the
5957 case that DUP_ORIGIN was null, that means BLOCK was the origin
5958 of a chain of fragments and we want to remove those fragments
5959 that didn't make it to the output. */
5960 if (new_origin)
5962 tree *pp = &BLOCK_FRAGMENT_CHAIN (new_origin);
5963 tree chain = *pp;
5965 while (chain)
5967 if (TREE_ASM_WRITTEN (chain))
5969 BLOCK_FRAGMENT_ORIGIN (chain) = new_origin;
5970 *pp = chain;
5971 pp = &BLOCK_FRAGMENT_CHAIN (chain);
5973 chain = BLOCK_FRAGMENT_CHAIN (chain);
5975 *pp = NULL_TREE;
5978 reorder_fix_fragments (BLOCK_SUBBLOCKS (block));
5979 block = BLOCK_CHAIN (block);
5983 /* Reverse the order of elements in the chain T of blocks,
5984 and return the new head of the chain (old last element). */
5986 tree
5987 blocks_nreverse (tree t)
5989 tree prev = 0, decl, next;
5990 for (decl = t; decl; decl = next)
5992 next = BLOCK_CHAIN (decl);
5993 BLOCK_CHAIN (decl) = prev;
5994 prev = decl;
5996 return prev;
5999 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
6000 non-NULL, list them all into VECTOR, in a depth-first preorder
6001 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
6002 blocks. */
6004 static int
6005 all_blocks (tree block, tree *vector)
6007 int n_blocks = 0;
6009 while (block)
6011 TREE_ASM_WRITTEN (block) = 0;
6013 /* Record this block. */
6014 if (vector)
6015 vector[n_blocks] = block;
6017 ++n_blocks;
6019 /* Record the subblocks, and their subblocks... */
6020 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
6021 vector ? vector + n_blocks : 0);
6022 block = BLOCK_CHAIN (block);
6025 return n_blocks;
6028 /* Return a vector containing all the blocks rooted at BLOCK. The
6029 number of elements in the vector is stored in N_BLOCKS_P. The
6030 vector is dynamically allocated; it is the caller's responsibility
6031 to call `free' on the pointer returned. */
6033 static tree *
6034 get_block_vector (tree block, int *n_blocks_p)
6036 tree *block_vector;
6038 *n_blocks_p = all_blocks (block, NULL);
6039 block_vector = xmalloc (*n_blocks_p * sizeof (tree));
6040 all_blocks (block, block_vector);
6042 return block_vector;
6045 static GTY(()) int next_block_index = 2;
6047 /* Set BLOCK_NUMBER for all the blocks in FN. */
6049 void
6050 number_blocks (tree fn)
6052 int i;
6053 int n_blocks;
6054 tree *block_vector;
6056 /* For SDB and XCOFF debugging output, we start numbering the blocks
6057 from 1 within each function, rather than keeping a running
6058 count. */
6059 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
6060 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
6061 next_block_index = 1;
6062 #endif
6064 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
6066 /* The top-level BLOCK isn't numbered at all. */
6067 for (i = 1; i < n_blocks; ++i)
6068 /* We number the blocks from two. */
6069 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
6071 free (block_vector);
6073 return;
6076 /* If VAR is present in a subblock of BLOCK, return the subblock. */
6078 tree
6079 debug_find_var_in_block_tree (tree var, tree block)
6081 tree t;
6083 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
6084 if (t == var)
6085 return block;
6087 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
6089 tree ret = debug_find_var_in_block_tree (var, t);
6090 if (ret)
6091 return ret;
6094 return NULL_TREE;
6097 /* Allocate a function structure for FNDECL and set its contents
6098 to the defaults. */
6100 void
6101 allocate_struct_function (tree fndecl)
6103 tree result;
6104 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
6106 cfun = ggc_alloc_cleared (sizeof (struct function));
6108 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
6110 cfun->stack_alignment_needed = STACK_BOUNDARY;
6111 cfun->preferred_stack_boundary = STACK_BOUNDARY;
6113 current_function_funcdef_no = funcdef_no++;
6115 cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
6117 init_stmt_for_function ();
6118 init_eh_for_function ();
6120 lang_hooks.function.init (cfun);
6121 if (init_machine_status)
6122 cfun->machine = (*init_machine_status) ();
6124 if (fndecl == NULL)
6125 return;
6127 DECL_STRUCT_FUNCTION (fndecl) = cfun;
6128 cfun->decl = fndecl;
6130 result = DECL_RESULT (fndecl);
6131 if (aggregate_value_p (result, fndecl))
6133 #ifdef PCC_STATIC_STRUCT_RETURN
6134 current_function_returns_pcc_struct = 1;
6135 #endif
6136 current_function_returns_struct = 1;
6139 current_function_returns_pointer = POINTER_TYPE_P (TREE_TYPE (result));
6141 current_function_stdarg
6142 = (fntype
6143 && TYPE_ARG_TYPES (fntype) != 0
6144 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
6145 != void_type_node));
6148 /* Reset cfun, and other non-struct-function variables to defaults as
6149 appropriate for emitting rtl at the start of a function. */
6151 static void
6152 prepare_function_start (tree fndecl)
6154 if (fndecl && DECL_STRUCT_FUNCTION (fndecl))
6155 cfun = DECL_STRUCT_FUNCTION (fndecl);
6156 else
6157 allocate_struct_function (fndecl);
6158 init_emit ();
6159 init_varasm_status (cfun);
6160 init_expr ();
6162 cse_not_expected = ! optimize;
6164 /* Caller save not needed yet. */
6165 caller_save_needed = 0;
6167 /* We haven't done register allocation yet. */
6168 reg_renumber = 0;
6170 /* Indicate that we need to distinguish between the return value of the
6171 present function and the return value of a function being called. */
6172 rtx_equal_function_value_matters = 1;
6174 /* Indicate that we have not instantiated virtual registers yet. */
6175 virtuals_instantiated = 0;
6177 /* Indicate that we want CONCATs now. */
6178 generating_concat_p = 1;
6180 /* Indicate we have no need of a frame pointer yet. */
6181 frame_pointer_needed = 0;
6184 /* Initialize the rtl expansion mechanism so that we can do simple things
6185 like generate sequences. This is used to provide a context during global
6186 initialization of some passes. */
6187 void
6188 init_dummy_function_start (void)
6190 prepare_function_start (NULL);
6193 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
6194 and initialize static variables for generating RTL for the statements
6195 of the function. */
6197 void
6198 init_function_start (tree subr)
6200 prepare_function_start (subr);
6202 /* Prevent ever trying to delete the first instruction of a
6203 function. Also tell final how to output a linenum before the
6204 function prologue. Note linenums could be missing, e.g. when
6205 compiling a Java .class file. */
6206 if (! DECL_IS_BUILTIN (subr))
6207 emit_line_note (DECL_SOURCE_LOCATION (subr));
6209 /* Make sure first insn is a note even if we don't want linenums.
6210 This makes sure the first insn will never be deleted.
6211 Also, final expects a note to appear there. */
6212 emit_note (NOTE_INSN_DELETED);
6214 /* Warn if this value is an aggregate type,
6215 regardless of which calling convention we are using for it. */
6216 if (warn_aggregate_return
6217 && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
6218 warning ("function returns an aggregate");
6221 /* Make sure all values used by the optimization passes have sane
6222 defaults. */
6223 void
6224 init_function_for_compilation (void)
6226 reg_renumber = 0;
6228 /* No prologue/epilogue insns yet. */
6229 VARRAY_GROW (prologue, 0);
6230 VARRAY_GROW (epilogue, 0);
6231 VARRAY_GROW (sibcall_epilogue, 0);
6234 /* Expand a call to __main at the beginning of a possible main function. */
6236 #if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
6237 #undef HAS_INIT_SECTION
6238 #define HAS_INIT_SECTION
6239 #endif
6241 void
6242 expand_main_function (void)
6244 #ifdef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
6245 if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN)
6247 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
6248 rtx tmp, seq;
6250 start_sequence ();
6251 /* Forcibly align the stack. */
6252 #ifdef STACK_GROWS_DOWNWARD
6253 tmp = expand_simple_binop (Pmode, AND, stack_pointer_rtx, GEN_INT(-align),
6254 stack_pointer_rtx, 1, OPTAB_WIDEN);
6255 #else
6256 tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
6257 GEN_INT (align - 1), NULL_RTX, 1, OPTAB_WIDEN);
6258 tmp = expand_simple_binop (Pmode, AND, tmp, GEN_INT (-align),
6259 stack_pointer_rtx, 1, OPTAB_WIDEN);
6260 #endif
6261 if (tmp != stack_pointer_rtx)
6262 emit_move_insn (stack_pointer_rtx, tmp);
6264 /* Enlist allocate_dynamic_stack_space to pick up the pieces. */
6265 tmp = force_reg (Pmode, const0_rtx);
6266 allocate_dynamic_stack_space (tmp, NULL_RTX, BIGGEST_ALIGNMENT);
6267 seq = get_insns ();
6268 end_sequence ();
6270 for (tmp = get_last_insn (); tmp; tmp = PREV_INSN (tmp))
6271 if (NOTE_P (tmp) && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_FUNCTION_BEG)
6272 break;
6273 if (tmp)
6274 emit_insn_before (seq, tmp);
6275 else
6276 emit_insn (seq);
6278 #endif
6280 #ifndef HAS_INIT_SECTION
6281 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
6282 #endif
6285 /* The PENDING_SIZES represent the sizes of variable-sized types.
6286 Create RTL for the various sizes now (using temporary variables),
6287 so that we can refer to the sizes from the RTL we are generating
6288 for the current function. The PENDING_SIZES are a TREE_LIST. The
6289 TREE_VALUE of each node is a SAVE_EXPR. */
6291 void
6292 expand_pending_sizes (tree pending_sizes)
6294 tree tem;
6296 /* Evaluate now the sizes of any types declared among the arguments. */
6297 for (tem = pending_sizes; tem; tem = TREE_CHAIN (tem))
6299 expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode, 0);
6300 /* Flush the queue in case this parameter declaration has
6301 side-effects. */
6302 emit_queue ();
6306 /* Start the RTL for a new function, and set variables used for
6307 emitting RTL.
6308 SUBR is the FUNCTION_DECL node.
6309 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
6310 the function's parameters, which must be run at any return statement. */
6312 void
6313 expand_function_start (tree subr, int parms_have_cleanups)
6315 /* Make sure volatile mem refs aren't considered
6316 valid operands of arithmetic insns. */
6317 init_recog_no_volatile ();
6319 current_function_profile
6320 = (profile_flag
6321 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6323 current_function_limit_stack
6324 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
6326 /* If the parameters of this function need cleaning up, get a label
6327 for the beginning of the code which executes those cleanups. This must
6328 be done before doing anything with return_label. */
6329 if (parms_have_cleanups)
6330 cleanup_label = gen_label_rtx ();
6331 else
6332 cleanup_label = 0;
6334 /* Make the label for return statements to jump to. Do not special
6335 case machines with special return instructions -- they will be
6336 handled later during jump, ifcvt, or epilogue creation. */
6337 return_label = gen_label_rtx ();
6339 /* Initialize rtx used to return the value. */
6340 /* Do this before assign_parms so that we copy the struct value address
6341 before any library calls that assign parms might generate. */
6343 /* Decide whether to return the value in memory or in a register. */
6344 if (aggregate_value_p (DECL_RESULT (subr), subr))
6346 /* Returning something that won't go in a register. */
6347 rtx value_address = 0;
6349 #ifdef PCC_STATIC_STRUCT_RETURN
6350 if (current_function_returns_pcc_struct)
6352 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
6353 value_address = assemble_static_space (size);
6355 else
6356 #endif
6358 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 1);
6359 /* Expect to be passed the address of a place to store the value.
6360 If it is passed as an argument, assign_parms will take care of
6361 it. */
6362 if (sv)
6364 value_address = gen_reg_rtx (Pmode);
6365 emit_move_insn (value_address, sv);
6368 if (value_address)
6370 rtx x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), value_address);
6371 set_mem_attributes (x, DECL_RESULT (subr), 1);
6372 SET_DECL_RTL (DECL_RESULT (subr), x);
6375 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
6376 /* If return mode is void, this decl rtl should not be used. */
6377 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
6378 else
6380 /* Compute the return values into a pseudo reg, which we will copy
6381 into the true return register after the cleanups are done. */
6383 /* In order to figure out what mode to use for the pseudo, we
6384 figure out what the mode of the eventual return register will
6385 actually be, and use that. */
6386 rtx hard_reg
6387 = hard_function_value (TREE_TYPE (DECL_RESULT (subr)),
6388 subr, 1);
6390 /* Structures that are returned in registers are not aggregate_value_p,
6391 so we may see a PARALLEL or a REG. */
6392 if (REG_P (hard_reg))
6393 SET_DECL_RTL (DECL_RESULT (subr), gen_reg_rtx (GET_MODE (hard_reg)));
6394 else if (GET_CODE (hard_reg) == PARALLEL)
6395 SET_DECL_RTL (DECL_RESULT (subr), gen_group_rtx (hard_reg));
6396 else
6397 abort ();
6399 /* Set DECL_REGISTER flag so that expand_function_end will copy the
6400 result to the real return register(s). */
6401 DECL_REGISTER (DECL_RESULT (subr)) = 1;
6404 /* Initialize rtx for parameters and local variables.
6405 In some cases this requires emitting insns. */
6406 assign_parms (subr);
6408 /* If function gets a static chain arg, store it. */
6409 if (cfun->static_chain_decl)
6411 tree parm = cfun->static_chain_decl;
6412 rtx local = gen_reg_rtx (Pmode);
6414 set_decl_incoming_rtl (parm, static_chain_incoming_rtx);
6415 SET_DECL_RTL (parm, local);
6416 maybe_set_unchanging (local, parm);
6417 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
6419 emit_move_insn (local, static_chain_incoming_rtx);
6422 /* If the function receives a non-local goto, then store the
6423 bits we need to restore the frame pointer. */
6424 if (cfun->nonlocal_goto_save_area)
6426 tree t_save;
6427 rtx r_save;
6429 /* ??? We need to do this save early. Unfortunately here is
6430 before the frame variable gets declared. Help out... */
6431 expand_var (TREE_OPERAND (cfun->nonlocal_goto_save_area, 0));
6433 t_save = build (ARRAY_REF, ptr_type_node, cfun->nonlocal_goto_save_area,
6434 integer_zero_node, NULL_TREE, NULL_TREE);
6435 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
6437 emit_move_insn (r_save, virtual_stack_vars_rtx);
6438 update_nonlocal_goto_save_area ();
6441 /* The following was moved from init_function_start.
6442 The move is supposed to make sdb output more accurate. */
6443 /* Indicate the beginning of the function body,
6444 as opposed to parm setup. */
6445 emit_note (NOTE_INSN_FUNCTION_BEG);
6447 if (GET_CODE (get_last_insn ()) != NOTE)
6448 emit_note (NOTE_INSN_DELETED);
6449 parm_birth_insn = get_last_insn ();
6451 if (current_function_profile)
6453 #ifdef PROFILE_HOOK
6454 PROFILE_HOOK (current_function_funcdef_no);
6455 #endif
6458 /* After the display initializations is where the tail-recursion label
6459 should go, if we end up needing one. Ensure we have a NOTE here
6460 since some things (like trampolines) get placed before this. */
6461 tail_recursion_reentry = emit_note (NOTE_INSN_DELETED);
6463 /* Evaluate now the sizes of any types declared among the arguments. */
6464 expand_pending_sizes (nreverse (get_pending_sizes ()));
6466 /* Make sure there is a line number after the function entry setup code. */
6467 force_next_line_note ();
6470 /* Undo the effects of init_dummy_function_start. */
6471 void
6472 expand_dummy_function_end (void)
6474 /* End any sequences that failed to be closed due to syntax errors. */
6475 while (in_sequence_p ())
6476 end_sequence ();
6478 /* Outside function body, can't compute type's actual size
6479 until next function's body starts. */
6481 free_after_parsing (cfun);
6482 free_after_compilation (cfun);
6483 cfun = 0;
6486 /* Call DOIT for each hard register used as a return value from
6487 the current function. */
6489 void
6490 diddle_return_value (void (*doit) (rtx, void *), void *arg)
6492 rtx outgoing = current_function_return_rtx;
6494 if (! outgoing)
6495 return;
6497 if (REG_P (outgoing))
6498 (*doit) (outgoing, arg);
6499 else if (GET_CODE (outgoing) == PARALLEL)
6501 int i;
6503 for (i = 0; i < XVECLEN (outgoing, 0); i++)
6505 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
6507 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
6508 (*doit) (x, arg);
6513 static void
6514 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
6516 emit_insn (gen_rtx_CLOBBER (VOIDmode, reg));
6519 void
6520 clobber_return_register (void)
6522 diddle_return_value (do_clobber_return_reg, NULL);
6524 /* In case we do use pseudo to return value, clobber it too. */
6525 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6527 tree decl_result = DECL_RESULT (current_function_decl);
6528 rtx decl_rtl = DECL_RTL (decl_result);
6529 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
6531 do_clobber_return_reg (decl_rtl, NULL);
6536 static void
6537 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
6539 emit_insn (gen_rtx_USE (VOIDmode, reg));
6542 void
6543 use_return_register (void)
6545 diddle_return_value (do_use_return_reg, NULL);
6548 /* Possibly warn about unused parameters. */
6549 void
6550 do_warn_unused_parameter (tree fn)
6552 tree decl;
6554 for (decl = DECL_ARGUMENTS (fn);
6555 decl; decl = TREE_CHAIN (decl))
6556 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
6557 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl))
6558 warning ("%Junused parameter '%D'", decl, decl);
6561 static GTY(()) rtx initial_trampoline;
6563 /* Generate RTL for the end of the current function. */
6565 void
6566 expand_function_end (void)
6568 rtx clobber_after;
6570 finish_expr_for_function ();
6572 /* If arg_pointer_save_area was referenced only from a nested
6573 function, we will not have initialized it yet. Do that now. */
6574 if (arg_pointer_save_area && ! cfun->arg_pointer_save_area_init)
6575 get_arg_pointer_save_area (cfun);
6577 #ifdef NON_SAVING_SETJMP
6578 /* Don't put any variables in registers if we call setjmp
6579 on a machine that fails to restore the registers. */
6580 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
6582 if (DECL_INITIAL (current_function_decl) != error_mark_node)
6583 setjmp_protect (DECL_INITIAL (current_function_decl));
6585 setjmp_protect_args ();
6587 #endif
6589 /* If we are doing stack checking and this function makes calls,
6590 do a stack probe at the start of the function to ensure we have enough
6591 space for another stack frame. */
6592 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
6594 rtx insn, seq;
6596 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6597 if (GET_CODE (insn) == CALL_INSN)
6599 start_sequence ();
6600 probe_stack_range (STACK_CHECK_PROTECT,
6601 GEN_INT (STACK_CHECK_MAX_FRAME_SIZE));
6602 seq = get_insns ();
6603 end_sequence ();
6604 emit_insn_before (seq, tail_recursion_reentry);
6605 break;
6609 /* Possibly warn about unused parameters.
6610 When frontend does unit-at-a-time, the warning is already
6611 issued at finalization time. */
6612 if (warn_unused_parameter
6613 && !lang_hooks.callgraph.expand_function)
6614 do_warn_unused_parameter (current_function_decl);
6616 /* End any sequences that failed to be closed due to syntax errors. */
6617 while (in_sequence_p ())
6618 end_sequence ();
6620 clear_pending_stack_adjust ();
6621 do_pending_stack_adjust ();
6623 /* @@@ This is a kludge. We want to ensure that instructions that
6624 may trap are not moved into the epilogue by scheduling, because
6625 we don't always emit unwind information for the epilogue.
6626 However, not all machine descriptions define a blockage insn, so
6627 emit an ASM_INPUT to act as one. */
6628 if (flag_non_call_exceptions)
6629 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
6631 /* Mark the end of the function body.
6632 If control reaches this insn, the function can drop through
6633 without returning a value. */
6634 emit_note (NOTE_INSN_FUNCTION_END);
6636 /* Must mark the last line number note in the function, so that the test
6637 coverage code can avoid counting the last line twice. This just tells
6638 the code to ignore the immediately following line note, since there
6639 already exists a copy of this note somewhere above. This line number
6640 note is still needed for debugging though, so we can't delete it. */
6641 if (flag_test_coverage)
6642 emit_note (NOTE_INSN_REPEATED_LINE_NUMBER);
6644 /* Output a linenumber for the end of the function.
6645 SDB depends on this. */
6646 force_next_line_note ();
6647 emit_line_note (input_location);
6649 /* Before the return label (if any), clobber the return
6650 registers so that they are not propagated live to the rest of
6651 the function. This can only happen with functions that drop
6652 through; if there had been a return statement, there would
6653 have either been a return rtx, or a jump to the return label.
6655 We delay actual code generation after the current_function_value_rtx
6656 is computed. */
6657 clobber_after = get_last_insn ();
6659 /* Output the label for the actual return from the function,
6660 if one is expected. This happens either because a function epilogue
6661 is used instead of a return instruction, or because a return was done
6662 with a goto in order to run local cleanups, or because of pcc-style
6663 structure returning. */
6664 if (return_label)
6665 emit_label (return_label);
6667 /* Let except.c know where it should emit the call to unregister
6668 the function context for sjlj exceptions. */
6669 if (flag_exceptions && USING_SJLJ_EXCEPTIONS)
6670 sjlj_emit_function_exit_after (get_last_insn ());
6672 /* If we had calls to alloca, and this machine needs
6673 an accurate stack pointer to exit the function,
6674 insert some code to save and restore the stack pointer. */
6675 if (! EXIT_IGNORE_STACK
6676 && current_function_calls_alloca)
6678 rtx tem = 0;
6680 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
6681 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
6684 /* If scalar return value was computed in a pseudo-reg, or was a named
6685 return value that got dumped to the stack, copy that to the hard
6686 return register. */
6687 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6689 tree decl_result = DECL_RESULT (current_function_decl);
6690 rtx decl_rtl = DECL_RTL (decl_result);
6692 if (REG_P (decl_rtl)
6693 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
6694 : DECL_REGISTER (decl_result))
6696 rtx real_decl_rtl = current_function_return_rtx;
6698 /* This should be set in assign_parms. */
6699 if (! REG_FUNCTION_VALUE_P (real_decl_rtl))
6700 abort ();
6702 /* If this is a BLKmode structure being returned in registers,
6703 then use the mode computed in expand_return. Note that if
6704 decl_rtl is memory, then its mode may have been changed,
6705 but that current_function_return_rtx has not. */
6706 if (GET_MODE (real_decl_rtl) == BLKmode)
6707 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
6709 /* If a named return value dumped decl_return to memory, then
6710 we may need to re-do the PROMOTE_MODE signed/unsigned
6711 extension. */
6712 if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
6714 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
6716 if (targetm.calls.promote_function_return (TREE_TYPE (current_function_decl)))
6717 promote_mode (TREE_TYPE (decl_result), GET_MODE (decl_rtl),
6718 &unsignedp, 1);
6720 convert_move (real_decl_rtl, decl_rtl, unsignedp);
6722 else if (GET_CODE (real_decl_rtl) == PARALLEL)
6724 /* If expand_function_start has created a PARALLEL for decl_rtl,
6725 move the result to the real return registers. Otherwise, do
6726 a group load from decl_rtl for a named return. */
6727 if (GET_CODE (decl_rtl) == PARALLEL)
6728 emit_group_move (real_decl_rtl, decl_rtl);
6729 else
6730 emit_group_load (real_decl_rtl, decl_rtl,
6731 TREE_TYPE (decl_result),
6732 int_size_in_bytes (TREE_TYPE (decl_result)));
6734 else
6735 emit_move_insn (real_decl_rtl, decl_rtl);
6739 /* If returning a structure, arrange to return the address of the value
6740 in a place where debuggers expect to find it.
6742 If returning a structure PCC style,
6743 the caller also depends on this value.
6744 And current_function_returns_pcc_struct is not necessarily set. */
6745 if (current_function_returns_struct
6746 || current_function_returns_pcc_struct)
6748 rtx value_address
6749 = XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6750 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
6751 #ifdef FUNCTION_OUTGOING_VALUE
6752 rtx outgoing
6753 = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
6754 current_function_decl);
6755 #else
6756 rtx outgoing
6757 = FUNCTION_VALUE (build_pointer_type (type), current_function_decl);
6758 #endif
6760 /* Mark this as a function return value so integrate will delete the
6761 assignment and USE below when inlining this function. */
6762 REG_FUNCTION_VALUE_P (outgoing) = 1;
6764 /* The address may be ptr_mode and OUTGOING may be Pmode. */
6765 value_address = convert_memory_address (GET_MODE (outgoing),
6766 value_address);
6768 emit_move_insn (outgoing, value_address);
6770 /* Show return register used to hold result (in this case the address
6771 of the result. */
6772 current_function_return_rtx = outgoing;
6775 /* If this is an implementation of throw, do what's necessary to
6776 communicate between __builtin_eh_return and the epilogue. */
6777 expand_eh_return ();
6779 /* Emit the actual code to clobber return register. */
6781 rtx seq, after;
6783 start_sequence ();
6784 clobber_return_register ();
6785 seq = get_insns ();
6786 end_sequence ();
6788 after = emit_insn_after (seq, clobber_after);
6791 /* Output the label for the naked return from the function, if one is
6792 expected. This is currently used only by __builtin_return. */
6793 if (naked_return_label)
6794 emit_label (naked_return_label);
6796 /* ??? This should no longer be necessary since stupid is no longer with
6797 us, but there are some parts of the compiler (eg reload_combine, and
6798 sh mach_dep_reorg) that still try and compute their own lifetime info
6799 instead of using the general framework. */
6800 use_return_register ();
6802 /* Fix up any gotos that jumped out to the outermost
6803 binding level of the function.
6804 Must follow emitting RETURN_LABEL. */
6806 /* If you have any cleanups to do at this point,
6807 and they need to create temporary variables,
6808 then you will lose. */
6809 expand_fixups (get_insns ());
6813 get_arg_pointer_save_area (struct function *f)
6815 rtx ret = f->x_arg_pointer_save_area;
6817 if (! ret)
6819 ret = assign_stack_local_1 (Pmode, GET_MODE_SIZE (Pmode), 0, f);
6820 f->x_arg_pointer_save_area = ret;
6823 if (f == cfun && ! f->arg_pointer_save_area_init)
6825 rtx seq;
6827 /* Save the arg pointer at the beginning of the function. The
6828 generated stack slot may not be a valid memory address, so we
6829 have to check it and fix it if necessary. */
6830 start_sequence ();
6831 emit_move_insn (validize_mem (ret), virtual_incoming_args_rtx);
6832 seq = get_insns ();
6833 end_sequence ();
6835 push_topmost_sequence ();
6836 emit_insn_after (seq, get_insns ());
6837 pop_topmost_sequence ();
6840 return ret;
6843 /* Extend a vector that records the INSN_UIDs of INSNS
6844 (a list of one or more insns). */
6846 static void
6847 record_insns (rtx insns, varray_type *vecp)
6849 int i, len;
6850 rtx tmp;
6852 tmp = insns;
6853 len = 0;
6854 while (tmp != NULL_RTX)
6856 len++;
6857 tmp = NEXT_INSN (tmp);
6860 i = VARRAY_SIZE (*vecp);
6861 VARRAY_GROW (*vecp, i + len);
6862 tmp = insns;
6863 while (tmp != NULL_RTX)
6865 VARRAY_INT (*vecp, i) = INSN_UID (tmp);
6866 i++;
6867 tmp = NEXT_INSN (tmp);
6871 /* Set the locator of the insn chain starting at INSN to LOC. */
6872 static void
6873 set_insn_locators (rtx insn, int loc)
6875 while (insn != NULL_RTX)
6877 if (INSN_P (insn))
6878 INSN_LOCATOR (insn) = loc;
6879 insn = NEXT_INSN (insn);
6883 /* Determine how many INSN_UIDs in VEC are part of INSN. Because we can
6884 be running after reorg, SEQUENCE rtl is possible. */
6886 static int
6887 contains (rtx insn, varray_type vec)
6889 int i, j;
6891 if (GET_CODE (insn) == INSN
6892 && GET_CODE (PATTERN (insn)) == SEQUENCE)
6894 int count = 0;
6895 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
6896 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
6897 if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == VARRAY_INT (vec, j))
6898 count++;
6899 return count;
6901 else
6903 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
6904 if (INSN_UID (insn) == VARRAY_INT (vec, j))
6905 return 1;
6907 return 0;
6911 prologue_epilogue_contains (rtx insn)
6913 if (contains (insn, prologue))
6914 return 1;
6915 if (contains (insn, epilogue))
6916 return 1;
6917 return 0;
6921 sibcall_epilogue_contains (rtx insn)
6923 if (sibcall_epilogue)
6924 return contains (insn, sibcall_epilogue);
6925 return 0;
6928 #ifdef HAVE_return
6929 /* Insert gen_return at the end of block BB. This also means updating
6930 block_for_insn appropriately. */
6932 static void
6933 emit_return_into_block (basic_block bb, rtx line_note)
6935 emit_jump_insn_after (gen_return (), BB_END (bb));
6936 if (line_note)
6937 emit_note_copy_after (line_note, PREV_INSN (BB_END (bb)));
6939 #endif /* HAVE_return */
6941 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
6943 /* These functions convert the epilogue into a variant that does not modify the
6944 stack pointer. This is used in cases where a function returns an object
6945 whose size is not known until it is computed. The called function leaves the
6946 object on the stack, leaves the stack depressed, and returns a pointer to
6947 the object.
6949 What we need to do is track all modifications and references to the stack
6950 pointer, deleting the modifications and changing the references to point to
6951 the location the stack pointer would have pointed to had the modifications
6952 taken place.
6954 These functions need to be portable so we need to make as few assumptions
6955 about the epilogue as we can. However, the epilogue basically contains
6956 three things: instructions to reset the stack pointer, instructions to
6957 reload registers, possibly including the frame pointer, and an
6958 instruction to return to the caller.
6960 If we can't be sure of what a relevant epilogue insn is doing, we abort.
6961 We also make no attempt to validate the insns we make since if they are
6962 invalid, we probably can't do anything valid. The intent is that these
6963 routines get "smarter" as more and more machines start to use them and
6964 they try operating on different epilogues.
6966 We use the following structure to track what the part of the epilogue that
6967 we've already processed has done. We keep two copies of the SP equivalence,
6968 one for use during the insn we are processing and one for use in the next
6969 insn. The difference is because one part of a PARALLEL may adjust SP
6970 and the other may use it. */
6972 struct epi_info
6974 rtx sp_equiv_reg; /* REG that SP is set from, perhaps SP. */
6975 HOST_WIDE_INT sp_offset; /* Offset from SP_EQUIV_REG of present SP. */
6976 rtx new_sp_equiv_reg; /* REG to be used at end of insn. */
6977 HOST_WIDE_INT new_sp_offset; /* Offset to be used at end of insn. */
6978 rtx equiv_reg_src; /* If nonzero, the value that SP_EQUIV_REG
6979 should be set to once we no longer need
6980 its value. */
6981 rtx const_equiv[FIRST_PSEUDO_REGISTER]; /* Any known constant equivalences
6982 for registers. */
6985 static void handle_epilogue_set (rtx, struct epi_info *);
6986 static void update_epilogue_consts (rtx, rtx, void *);
6987 static void emit_equiv_load (struct epi_info *);
6989 /* Modify INSN, a list of one or more insns that is part of the epilogue, to
6990 no modifications to the stack pointer. Return the new list of insns. */
6992 static rtx
6993 keep_stack_depressed (rtx insns)
6995 int j;
6996 struct epi_info info;
6997 rtx insn, next;
6999 /* If the epilogue is just a single instruction, it must be OK as is. */
7000 if (NEXT_INSN (insns) == NULL_RTX)
7001 return insns;
7003 /* Otherwise, start a sequence, initialize the information we have, and
7004 process all the insns we were given. */
7005 start_sequence ();
7007 info.sp_equiv_reg = stack_pointer_rtx;
7008 info.sp_offset = 0;
7009 info.equiv_reg_src = 0;
7011 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
7012 info.const_equiv[j] = 0;
7014 insn = insns;
7015 next = NULL_RTX;
7016 while (insn != NULL_RTX)
7018 next = NEXT_INSN (insn);
7020 if (!INSN_P (insn))
7022 add_insn (insn);
7023 insn = next;
7024 continue;
7027 /* If this insn references the register that SP is equivalent to and
7028 we have a pending load to that register, we must force out the load
7029 first and then indicate we no longer know what SP's equivalent is. */
7030 if (info.equiv_reg_src != 0
7031 && reg_referenced_p (info.sp_equiv_reg, PATTERN (insn)))
7033 emit_equiv_load (&info);
7034 info.sp_equiv_reg = 0;
7037 info.new_sp_equiv_reg = info.sp_equiv_reg;
7038 info.new_sp_offset = info.sp_offset;
7040 /* If this is a (RETURN) and the return address is on the stack,
7041 update the address and change to an indirect jump. */
7042 if (GET_CODE (PATTERN (insn)) == RETURN
7043 || (GET_CODE (PATTERN (insn)) == PARALLEL
7044 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == RETURN))
7046 rtx retaddr = INCOMING_RETURN_ADDR_RTX;
7047 rtx base = 0;
7048 HOST_WIDE_INT offset = 0;
7049 rtx jump_insn, jump_set;
7051 /* If the return address is in a register, we can emit the insn
7052 unchanged. Otherwise, it must be a MEM and we see what the
7053 base register and offset are. In any case, we have to emit any
7054 pending load to the equivalent reg of SP, if any. */
7055 if (REG_P (retaddr))
7057 emit_equiv_load (&info);
7058 add_insn (insn);
7059 insn = next;
7060 continue;
7062 else if (GET_CODE (retaddr) == MEM
7063 && REG_P (XEXP (retaddr, 0)))
7064 base = gen_rtx_REG (Pmode, REGNO (XEXP (retaddr, 0))), offset = 0;
7065 else if (GET_CODE (retaddr) == MEM
7066 && GET_CODE (XEXP (retaddr, 0)) == PLUS
7067 && REG_P (XEXP (XEXP (retaddr, 0), 0))
7068 && GET_CODE (XEXP (XEXP (retaddr, 0), 1)) == CONST_INT)
7070 base = gen_rtx_REG (Pmode, REGNO (XEXP (XEXP (retaddr, 0), 0)));
7071 offset = INTVAL (XEXP (XEXP (retaddr, 0), 1));
7073 else
7074 abort ();
7076 /* If the base of the location containing the return pointer
7077 is SP, we must update it with the replacement address. Otherwise,
7078 just build the necessary MEM. */
7079 retaddr = plus_constant (base, offset);
7080 if (base == stack_pointer_rtx)
7081 retaddr = simplify_replace_rtx (retaddr, stack_pointer_rtx,
7082 plus_constant (info.sp_equiv_reg,
7083 info.sp_offset));
7085 retaddr = gen_rtx_MEM (Pmode, retaddr);
7087 /* If there is a pending load to the equivalent register for SP
7088 and we reference that register, we must load our address into
7089 a scratch register and then do that load. */
7090 if (info.equiv_reg_src
7091 && reg_overlap_mentioned_p (info.equiv_reg_src, retaddr))
7093 unsigned int regno;
7094 rtx reg;
7096 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
7097 if (HARD_REGNO_MODE_OK (regno, Pmode)
7098 && !fixed_regs[regno]
7099 && TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)
7100 && !REGNO_REG_SET_P (EXIT_BLOCK_PTR->global_live_at_start,
7101 regno)
7102 && !refers_to_regno_p (regno,
7103 regno + hard_regno_nregs[regno]
7104 [Pmode],
7105 info.equiv_reg_src, NULL)
7106 && info.const_equiv[regno] == 0)
7107 break;
7109 if (regno == FIRST_PSEUDO_REGISTER)
7110 abort ();
7112 reg = gen_rtx_REG (Pmode, regno);
7113 emit_move_insn (reg, retaddr);
7114 retaddr = reg;
7117 emit_equiv_load (&info);
7118 jump_insn = emit_jump_insn (gen_indirect_jump (retaddr));
7120 /* Show the SET in the above insn is a RETURN. */
7121 jump_set = single_set (jump_insn);
7122 if (jump_set == 0)
7123 abort ();
7124 else
7125 SET_IS_RETURN_P (jump_set) = 1;
7128 /* If SP is not mentioned in the pattern and its equivalent register, if
7129 any, is not modified, just emit it. Otherwise, if neither is set,
7130 replace the reference to SP and emit the insn. If none of those are
7131 true, handle each SET individually. */
7132 else if (!reg_mentioned_p (stack_pointer_rtx, PATTERN (insn))
7133 && (info.sp_equiv_reg == stack_pointer_rtx
7134 || !reg_set_p (info.sp_equiv_reg, insn)))
7135 add_insn (insn);
7136 else if (! reg_set_p (stack_pointer_rtx, insn)
7137 && (info.sp_equiv_reg == stack_pointer_rtx
7138 || !reg_set_p (info.sp_equiv_reg, insn)))
7140 if (! validate_replace_rtx (stack_pointer_rtx,
7141 plus_constant (info.sp_equiv_reg,
7142 info.sp_offset),
7143 insn))
7144 abort ();
7146 add_insn (insn);
7148 else if (GET_CODE (PATTERN (insn)) == SET)
7149 handle_epilogue_set (PATTERN (insn), &info);
7150 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
7152 for (j = 0; j < XVECLEN (PATTERN (insn), 0); j++)
7153 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET)
7154 handle_epilogue_set (XVECEXP (PATTERN (insn), 0, j), &info);
7156 else
7157 add_insn (insn);
7159 info.sp_equiv_reg = info.new_sp_equiv_reg;
7160 info.sp_offset = info.new_sp_offset;
7162 /* Now update any constants this insn sets. */
7163 note_stores (PATTERN (insn), update_epilogue_consts, &info);
7164 insn = next;
7167 insns = get_insns ();
7168 end_sequence ();
7169 return insns;
7172 /* SET is a SET from an insn in the epilogue. P is a pointer to the epi_info
7173 structure that contains information about what we've seen so far. We
7174 process this SET by either updating that data or by emitting one or
7175 more insns. */
7177 static void
7178 handle_epilogue_set (rtx set, struct epi_info *p)
7180 /* First handle the case where we are setting SP. Record what it is being
7181 set from. If unknown, abort. */
7182 if (reg_set_p (stack_pointer_rtx, set))
7184 if (SET_DEST (set) != stack_pointer_rtx)
7185 abort ();
7187 if (GET_CODE (SET_SRC (set)) == PLUS)
7189 p->new_sp_equiv_reg = XEXP (SET_SRC (set), 0);
7190 if (GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
7191 p->new_sp_offset = INTVAL (XEXP (SET_SRC (set), 1));
7192 else if (REG_P (XEXP (SET_SRC (set), 1))
7193 && REGNO (XEXP (SET_SRC (set), 1)) < FIRST_PSEUDO_REGISTER
7194 && p->const_equiv[REGNO (XEXP (SET_SRC (set), 1))] != 0)
7195 p->new_sp_offset
7196 = INTVAL (p->const_equiv[REGNO (XEXP (SET_SRC (set), 1))]);
7197 else
7198 abort ();
7200 else
7201 p->new_sp_equiv_reg = SET_SRC (set), p->new_sp_offset = 0;
7203 /* If we are adjusting SP, we adjust from the old data. */
7204 if (p->new_sp_equiv_reg == stack_pointer_rtx)
7206 p->new_sp_equiv_reg = p->sp_equiv_reg;
7207 p->new_sp_offset += p->sp_offset;
7210 if (p->new_sp_equiv_reg == 0 || !REG_P (p->new_sp_equiv_reg))
7211 abort ();
7213 return;
7216 /* Next handle the case where we are setting SP's equivalent register.
7217 If we already have a value to set it to, abort. We could update, but
7218 there seems little point in handling that case. Note that we have
7219 to allow for the case where we are setting the register set in
7220 the previous part of a PARALLEL inside a single insn. But use the
7221 old offset for any updates within this insn. We must allow for the case
7222 where the register is being set in a different (usually wider) mode than
7223 Pmode). */
7224 else if (p->new_sp_equiv_reg != 0 && reg_set_p (p->new_sp_equiv_reg, set))
7226 if (p->equiv_reg_src != 0
7227 || !REG_P (p->new_sp_equiv_reg)
7228 || !REG_P (SET_DEST (set))
7229 || GET_MODE_BITSIZE (GET_MODE (SET_DEST (set))) > BITS_PER_WORD
7230 || REGNO (p->new_sp_equiv_reg) != REGNO (SET_DEST (set)))
7231 abort ();
7232 else
7233 p->equiv_reg_src
7234 = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7235 plus_constant (p->sp_equiv_reg,
7236 p->sp_offset));
7239 /* Otherwise, replace any references to SP in the insn to its new value
7240 and emit the insn. */
7241 else
7243 SET_SRC (set) = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7244 plus_constant (p->sp_equiv_reg,
7245 p->sp_offset));
7246 SET_DEST (set) = simplify_replace_rtx (SET_DEST (set), stack_pointer_rtx,
7247 plus_constant (p->sp_equiv_reg,
7248 p->sp_offset));
7249 emit_insn (set);
7253 /* Update the tracking information for registers set to constants. */
7255 static void
7256 update_epilogue_consts (rtx dest, rtx x, void *data)
7258 struct epi_info *p = (struct epi_info *) data;
7259 rtx new;
7261 if (!REG_P (dest) || REGNO (dest) >= FIRST_PSEUDO_REGISTER)
7262 return;
7264 /* If we are either clobbering a register or doing a partial set,
7265 show we don't know the value. */
7266 else if (GET_CODE (x) == CLOBBER || ! rtx_equal_p (dest, SET_DEST (x)))
7267 p->const_equiv[REGNO (dest)] = 0;
7269 /* If we are setting it to a constant, record that constant. */
7270 else if (GET_CODE (SET_SRC (x)) == CONST_INT)
7271 p->const_equiv[REGNO (dest)] = SET_SRC (x);
7273 /* If this is a binary operation between a register we have been tracking
7274 and a constant, see if we can compute a new constant value. */
7275 else if (ARITHMETIC_P (SET_SRC (x))
7276 && REG_P (XEXP (SET_SRC (x), 0))
7277 && REGNO (XEXP (SET_SRC (x), 0)) < FIRST_PSEUDO_REGISTER
7278 && p->const_equiv[REGNO (XEXP (SET_SRC (x), 0))] != 0
7279 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
7280 && 0 != (new = simplify_binary_operation
7281 (GET_CODE (SET_SRC (x)), GET_MODE (dest),
7282 p->const_equiv[REGNO (XEXP (SET_SRC (x), 0))],
7283 XEXP (SET_SRC (x), 1)))
7284 && GET_CODE (new) == CONST_INT)
7285 p->const_equiv[REGNO (dest)] = new;
7287 /* Otherwise, we can't do anything with this value. */
7288 else
7289 p->const_equiv[REGNO (dest)] = 0;
7292 /* Emit an insn to do the load shown in p->equiv_reg_src, if needed. */
7294 static void
7295 emit_equiv_load (struct epi_info *p)
7297 if (p->equiv_reg_src != 0)
7299 rtx dest = p->sp_equiv_reg;
7301 if (GET_MODE (p->equiv_reg_src) != GET_MODE (dest))
7302 dest = gen_rtx_REG (GET_MODE (p->equiv_reg_src),
7303 REGNO (p->sp_equiv_reg));
7305 emit_move_insn (dest, p->equiv_reg_src);
7306 p->equiv_reg_src = 0;
7309 #endif
7311 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
7312 this into place with notes indicating where the prologue ends and where
7313 the epilogue begins. Update the basic block information when possible. */
7315 void
7316 thread_prologue_and_epilogue_insns (rtx f ATTRIBUTE_UNUSED)
7318 int inserted = 0;
7319 edge e;
7320 #if defined (HAVE_sibcall_epilogue) || defined (HAVE_epilogue) || defined (HAVE_return) || defined (HAVE_prologue)
7321 rtx seq;
7322 #endif
7323 #ifdef HAVE_prologue
7324 rtx prologue_end = NULL_RTX;
7325 #endif
7326 #if defined (HAVE_epilogue) || defined(HAVE_return)
7327 rtx epilogue_end = NULL_RTX;
7328 #endif
7330 #ifdef HAVE_prologue
7331 if (HAVE_prologue)
7333 start_sequence ();
7334 seq = gen_prologue ();
7335 emit_insn (seq);
7337 /* Retain a map of the prologue insns. */
7338 record_insns (seq, &prologue);
7339 prologue_end = emit_note (NOTE_INSN_PROLOGUE_END);
7341 seq = get_insns ();
7342 end_sequence ();
7343 set_insn_locators (seq, prologue_locator);
7345 /* Can't deal with multiple successors of the entry block
7346 at the moment. Function should always have at least one
7347 entry point. */
7348 if (!ENTRY_BLOCK_PTR->succ || ENTRY_BLOCK_PTR->succ->succ_next)
7349 abort ();
7351 insert_insn_on_edge (seq, ENTRY_BLOCK_PTR->succ);
7352 inserted = 1;
7354 #endif
7356 /* If the exit block has no non-fake predecessors, we don't need
7357 an epilogue. */
7358 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7359 if ((e->flags & EDGE_FAKE) == 0)
7360 break;
7361 if (e == NULL)
7362 goto epilogue_done;
7364 #ifdef HAVE_return
7365 if (optimize && HAVE_return)
7367 /* If we're allowed to generate a simple return instruction,
7368 then by definition we don't need a full epilogue. Examine
7369 the block that falls through to EXIT. If it does not
7370 contain any code, examine its predecessors and try to
7371 emit (conditional) return instructions. */
7373 basic_block last;
7374 edge e_next;
7375 rtx label;
7377 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7378 if (e->flags & EDGE_FALLTHRU)
7379 break;
7380 if (e == NULL)
7381 goto epilogue_done;
7382 last = e->src;
7384 /* Verify that there are no active instructions in the last block. */
7385 label = BB_END (last);
7386 while (label && GET_CODE (label) != CODE_LABEL)
7388 if (active_insn_p (label))
7389 break;
7390 label = PREV_INSN (label);
7393 if (BB_HEAD (last) == label && GET_CODE (label) == CODE_LABEL)
7395 rtx epilogue_line_note = NULL_RTX;
7397 /* Locate the line number associated with the closing brace,
7398 if we can find one. */
7399 for (seq = get_last_insn ();
7400 seq && ! active_insn_p (seq);
7401 seq = PREV_INSN (seq))
7402 if (GET_CODE (seq) == NOTE && NOTE_LINE_NUMBER (seq) > 0)
7404 epilogue_line_note = seq;
7405 break;
7408 for (e = last->pred; e; e = e_next)
7410 basic_block bb = e->src;
7411 rtx jump;
7413 e_next = e->pred_next;
7414 if (bb == ENTRY_BLOCK_PTR)
7415 continue;
7417 jump = BB_END (bb);
7418 if ((GET_CODE (jump) != JUMP_INSN) || JUMP_LABEL (jump) != label)
7419 continue;
7421 /* If we have an unconditional jump, we can replace that
7422 with a simple return instruction. */
7423 if (simplejump_p (jump))
7425 emit_return_into_block (bb, epilogue_line_note);
7426 delete_insn (jump);
7429 /* If we have a conditional jump, we can try to replace
7430 that with a conditional return instruction. */
7431 else if (condjump_p (jump))
7433 if (! redirect_jump (jump, 0, 0))
7434 continue;
7436 /* If this block has only one successor, it both jumps
7437 and falls through to the fallthru block, so we can't
7438 delete the edge. */
7439 if (bb->succ->succ_next == NULL)
7440 continue;
7442 else
7443 continue;
7445 /* Fix up the CFG for the successful change we just made. */
7446 redirect_edge_succ (e, EXIT_BLOCK_PTR);
7449 /* Emit a return insn for the exit fallthru block. Whether
7450 this is still reachable will be determined later. */
7452 emit_barrier_after (BB_END (last));
7453 emit_return_into_block (last, epilogue_line_note);
7454 epilogue_end = BB_END (last);
7455 last->succ->flags &= ~EDGE_FALLTHRU;
7456 goto epilogue_done;
7459 #endif
7460 /* Find the edge that falls through to EXIT. Other edges may exist
7461 due to RETURN instructions, but those don't need epilogues.
7462 There really shouldn't be a mixture -- either all should have
7463 been converted or none, however... */
7465 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7466 if (e->flags & EDGE_FALLTHRU)
7467 break;
7468 if (e == NULL)
7469 goto epilogue_done;
7471 #ifdef HAVE_epilogue
7472 if (HAVE_epilogue)
7474 start_sequence ();
7475 epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
7477 seq = gen_epilogue ();
7479 #ifdef INCOMING_RETURN_ADDR_RTX
7480 /* If this function returns with the stack depressed and we can support
7481 it, massage the epilogue to actually do that. */
7482 if (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
7483 && TYPE_RETURNS_STACK_DEPRESSED (TREE_TYPE (current_function_decl)))
7484 seq = keep_stack_depressed (seq);
7485 #endif
7487 emit_jump_insn (seq);
7489 /* Retain a map of the epilogue insns. */
7490 record_insns (seq, &epilogue);
7491 set_insn_locators (seq, epilogue_locator);
7493 seq = get_insns ();
7494 end_sequence ();
7496 insert_insn_on_edge (seq, e);
7497 inserted = 1;
7499 else
7500 #endif
7502 basic_block cur_bb;
7504 if (! next_active_insn (BB_END (e->src)))
7505 goto epilogue_done;
7506 /* We have a fall-through edge to the exit block, the source is not
7507 at the end of the function, and there will be an assembler epilogue
7508 at the end of the function.
7509 We can't use force_nonfallthru here, because that would try to
7510 use return. Inserting a jump 'by hand' is extremely messy, so
7511 we take advantage of cfg_layout_finalize using
7512 fixup_fallthru_exit_predecessor. */
7513 cfg_layout_initialize ();
7514 FOR_EACH_BB (cur_bb)
7515 if (cur_bb->index >= 0 && cur_bb->next_bb->index >= 0)
7516 cur_bb->rbi->next = cur_bb->next_bb;
7517 cfg_layout_finalize ();
7519 epilogue_done:
7521 if (inserted)
7522 commit_edge_insertions ();
7524 #ifdef HAVE_sibcall_epilogue
7525 /* Emit sibling epilogues before any sibling call sites. */
7526 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7528 basic_block bb = e->src;
7529 rtx insn = BB_END (bb);
7530 rtx i;
7531 rtx newinsn;
7533 if (GET_CODE (insn) != CALL_INSN
7534 || ! SIBLING_CALL_P (insn))
7535 continue;
7537 start_sequence ();
7538 emit_insn (gen_sibcall_epilogue ());
7539 seq = get_insns ();
7540 end_sequence ();
7542 /* Retain a map of the epilogue insns. Used in life analysis to
7543 avoid getting rid of sibcall epilogue insns. Do this before we
7544 actually emit the sequence. */
7545 record_insns (seq, &sibcall_epilogue);
7546 set_insn_locators (seq, epilogue_locator);
7548 i = PREV_INSN (insn);
7549 newinsn = emit_insn_before (seq, insn);
7551 #endif
7553 #ifdef HAVE_prologue
7554 /* This is probably all useless now that we use locators. */
7555 if (prologue_end)
7557 rtx insn, prev;
7559 /* GDB handles `break f' by setting a breakpoint on the first
7560 line note after the prologue. Which means (1) that if
7561 there are line number notes before where we inserted the
7562 prologue we should move them, and (2) we should generate a
7563 note before the end of the first basic block, if there isn't
7564 one already there.
7566 ??? This behavior is completely broken when dealing with
7567 multiple entry functions. We simply place the note always
7568 into first basic block and let alternate entry points
7569 to be missed.
7572 for (insn = prologue_end; insn; insn = prev)
7574 prev = PREV_INSN (insn);
7575 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7577 /* Note that we cannot reorder the first insn in the
7578 chain, since rest_of_compilation relies on that
7579 remaining constant. */
7580 if (prev == NULL)
7581 break;
7582 reorder_insns (insn, insn, prologue_end);
7586 /* Find the last line number note in the first block. */
7587 for (insn = BB_END (ENTRY_BLOCK_PTR->next_bb);
7588 insn != prologue_end && insn;
7589 insn = PREV_INSN (insn))
7590 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7591 break;
7593 /* If we didn't find one, make a copy of the first line number
7594 we run across. */
7595 if (! insn)
7597 for (insn = next_active_insn (prologue_end);
7598 insn;
7599 insn = PREV_INSN (insn))
7600 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7602 emit_note_copy_after (insn, prologue_end);
7603 break;
7607 #endif
7608 #ifdef HAVE_epilogue
7609 if (epilogue_end)
7611 rtx insn, next;
7613 /* Similarly, move any line notes that appear after the epilogue.
7614 There is no need, however, to be quite so anal about the existence
7615 of such a note. Also move the NOTE_INSN_FUNCTION_END and (possibly)
7616 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
7617 info generation. */
7618 for (insn = epilogue_end; insn; insn = next)
7620 next = NEXT_INSN (insn);
7621 if (GET_CODE (insn) == NOTE
7622 && (NOTE_LINE_NUMBER (insn) > 0
7623 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG
7624 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END))
7625 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
7628 #endif
7631 /* Reposition the prologue-end and epilogue-begin notes after instruction
7632 scheduling and delayed branch scheduling. */
7634 void
7635 reposition_prologue_and_epilogue_notes (rtx f ATTRIBUTE_UNUSED)
7637 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
7638 rtx insn, last, note;
7639 int len;
7641 if ((len = VARRAY_SIZE (prologue)) > 0)
7643 last = 0, note = 0;
7645 /* Scan from the beginning until we reach the last prologue insn.
7646 We apparently can't depend on basic_block_{head,end} after
7647 reorg has run. */
7648 for (insn = f; insn; insn = NEXT_INSN (insn))
7650 if (GET_CODE (insn) == NOTE)
7652 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
7653 note = insn;
7655 else if (contains (insn, prologue))
7657 last = insn;
7658 if (--len == 0)
7659 break;
7663 if (last)
7665 /* Find the prologue-end note if we haven't already, and
7666 move it to just after the last prologue insn. */
7667 if (note == 0)
7669 for (note = last; (note = NEXT_INSN (note));)
7670 if (GET_CODE (note) == NOTE
7671 && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
7672 break;
7675 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
7676 if (GET_CODE (last) == CODE_LABEL)
7677 last = NEXT_INSN (last);
7678 reorder_insns (note, note, last);
7682 if ((len = VARRAY_SIZE (epilogue)) > 0)
7684 last = 0, note = 0;
7686 /* Scan from the end until we reach the first epilogue insn.
7687 We apparently can't depend on basic_block_{head,end} after
7688 reorg has run. */
7689 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
7691 if (GET_CODE (insn) == NOTE)
7693 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
7694 note = insn;
7696 else if (contains (insn, epilogue))
7698 last = insn;
7699 if (--len == 0)
7700 break;
7704 if (last)
7706 /* Find the epilogue-begin note if we haven't already, and
7707 move it to just before the first epilogue insn. */
7708 if (note == 0)
7710 for (note = insn; (note = PREV_INSN (note));)
7711 if (GET_CODE (note) == NOTE
7712 && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
7713 break;
7716 if (PREV_INSN (last) != note)
7717 reorder_insns (note, note, PREV_INSN (last));
7720 #endif /* HAVE_prologue or HAVE_epilogue */
7723 /* Called once, at initialization, to initialize function.c. */
7725 void
7726 init_function_once (void)
7728 VARRAY_INT_INIT (prologue, 0, "prologue");
7729 VARRAY_INT_INIT (epilogue, 0, "epilogue");
7730 VARRAY_INT_INIT (sibcall_epilogue, 0, "sibcall_epilogue");
7733 /* Resets insn_block_boundaries array. */
7735 void
7736 reset_block_changes (void)
7738 VARRAY_TREE_INIT (cfun->ib_boundaries_block, 100, "ib_boundaries_block");
7739 VARRAY_PUSH_TREE (cfun->ib_boundaries_block, NULL_TREE);
7742 /* Record the boundary for BLOCK. */
7743 void
7744 record_block_change (tree block)
7746 int i, n;
7747 tree last_block;
7749 if (!block)
7750 return;
7752 last_block = VARRAY_TOP_TREE (cfun->ib_boundaries_block);
7753 VARRAY_POP (cfun->ib_boundaries_block);
7754 n = get_max_uid ();
7755 for (i = VARRAY_ACTIVE_SIZE (cfun->ib_boundaries_block); i < n; i++)
7756 VARRAY_PUSH_TREE (cfun->ib_boundaries_block, last_block);
7758 VARRAY_PUSH_TREE (cfun->ib_boundaries_block, block);
7761 /* Finishes record of boundaries. */
7762 void finalize_block_changes (void)
7764 record_block_change (DECL_INITIAL (current_function_decl));
7767 /* For INSN return the BLOCK it belongs to. */
7768 void
7769 check_block_change (rtx insn, tree *block)
7771 unsigned uid = INSN_UID (insn);
7773 if (uid >= VARRAY_ACTIVE_SIZE (cfun->ib_boundaries_block))
7774 return;
7776 *block = VARRAY_TREE (cfun->ib_boundaries_block, uid);
7779 /* Releases the ib_boundaries_block records. */
7780 void
7781 free_block_changes (void)
7783 cfun->ib_boundaries_block = NULL;
7786 /* Returns the name of the current function. */
7787 const char *
7788 current_function_name (void)
7790 return lang_hooks.decl_printable_name (cfun->decl, 2);
7793 #include "gt-function.h"