* loop.c (check_dbra_loop): Fix last change: examine both
[official-gcc.git] / gcc / function.c
blob097aea44684ca8f830b160f4dbe98d2349948f0e
1 /* Expands front end tree to back end RTL for GNU C-Compiler
2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001 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 "rtl.h"
44 #include "tree.h"
45 #include "flags.h"
46 #include "except.h"
47 #include "function.h"
48 #include "expr.h"
49 #include "libfuncs.h"
50 #include "regs.h"
51 #include "hard-reg-set.h"
52 #include "insn-config.h"
53 #include "recog.h"
54 #include "output.h"
55 #include "basic-block.h"
56 #include "obstack.h"
57 #include "toplev.h"
58 #include "hash.h"
59 #include "ggc.h"
60 #include "tm_p.h"
61 #include "integrate.h"
63 #ifndef TRAMPOLINE_ALIGNMENT
64 #define TRAMPOLINE_ALIGNMENT FUNCTION_BOUNDARY
65 #endif
67 #ifndef LOCAL_ALIGNMENT
68 #define LOCAL_ALIGNMENT(TYPE, ALIGNMENT) ALIGNMENT
69 #endif
71 /* Some systems use __main in a way incompatible with its use in gcc, in these
72 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
73 give the same symbol without quotes for an alternative entry point. You
74 must define both, or neither. */
75 #ifndef NAME__MAIN
76 #define NAME__MAIN "__main"
77 #define SYMBOL__MAIN __main
78 #endif
80 /* Round a value to the lowest integer less than it that is a multiple of
81 the required alignment. Avoid using division in case the value is
82 negative. Assume the alignment is a power of two. */
83 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
85 /* Similar, but round to the next highest integer that meets the
86 alignment. */
87 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
89 /* NEED_SEPARATE_AP means that we cannot derive ap from the value of fp
90 during rtl generation. If they are different register numbers, this is
91 always true. It may also be true if
92 FIRST_PARM_OFFSET - STARTING_FRAME_OFFSET is not a constant during rtl
93 generation. See fix_lexical_addr for details. */
95 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
96 #define NEED_SEPARATE_AP
97 #endif
99 /* Nonzero if function being compiled doesn't contain any calls
100 (ignoring the prologue and epilogue). This is set prior to
101 local register allocation and is valid for the remaining
102 compiler passes. */
103 int current_function_is_leaf;
105 /* Nonzero if function being compiled doesn't contain any instructions
106 that can throw an exception. This is set prior to final. */
108 int current_function_nothrow;
110 /* Nonzero if function being compiled doesn't modify the stack pointer
111 (ignoring the prologue and epilogue). This is only valid after
112 life_analysis has run. */
113 int current_function_sp_is_unchanging;
115 /* Nonzero if the function being compiled is a leaf function which only
116 uses leaf registers. This is valid after reload (specifically after
117 sched2) and is useful only if the port defines LEAF_REGISTERS. */
118 int current_function_uses_only_leaf_regs;
120 /* Nonzero once virtual register instantiation has been done.
121 assign_stack_local uses frame_pointer_rtx when this is nonzero.
122 calls.c:emit_library_call_value_1 uses it to set up
123 post-instantiation libcalls. */
124 int virtuals_instantiated;
126 /* These variables hold pointers to functions to create and destroy
127 target specific, per-function data structures. */
128 void (*init_machine_status) PARAMS ((struct function *));
129 void (*free_machine_status) PARAMS ((struct function *));
130 /* This variable holds a pointer to a function to register any
131 data items in the target specific, per-function data structure
132 that will need garbage collection. */
133 void (*mark_machine_status) PARAMS ((struct function *));
135 /* Likewise, but for language-specific data. */
136 void (*init_lang_status) PARAMS ((struct function *));
137 void (*save_lang_status) PARAMS ((struct function *));
138 void (*restore_lang_status) PARAMS ((struct function *));
139 void (*mark_lang_status) PARAMS ((struct function *));
140 void (*free_lang_status) PARAMS ((struct function *));
142 /* The FUNCTION_DECL for an inline function currently being expanded. */
143 tree inline_function_decl;
145 /* The currently compiled function. */
146 struct function *cfun = 0;
148 /* These arrays record the INSN_UIDs of the prologue and epilogue insns. */
149 static varray_type prologue;
150 static varray_type epilogue;
152 /* Array of INSN_UIDs to hold the INSN_UIDs for each sibcall epilogue
153 in this function. */
154 static varray_type sibcall_epilogue;
156 /* In order to evaluate some expressions, such as function calls returning
157 structures in memory, we need to temporarily allocate stack locations.
158 We record each allocated temporary in the following structure.
160 Associated with each temporary slot is a nesting level. When we pop up
161 one level, all temporaries associated with the previous level are freed.
162 Normally, all temporaries are freed after the execution of the statement
163 in which they were created. However, if we are inside a ({...}) grouping,
164 the result may be in a temporary and hence must be preserved. If the
165 result could be in a temporary, we preserve it if we can determine which
166 one it is in. If we cannot determine which temporary may contain the
167 result, all temporaries are preserved. A temporary is preserved by
168 pretending it was allocated at the previous nesting level.
170 Automatic variables are also assigned temporary slots, at the nesting
171 level where they are defined. They are marked a "kept" so that
172 free_temp_slots will not free them. */
174 struct temp_slot
176 /* Points to next temporary slot. */
177 struct temp_slot *next;
178 /* The rtx to used to reference the slot. */
179 rtx slot;
180 /* The rtx used to represent the address if not the address of the
181 slot above. May be an EXPR_LIST if multiple addresses exist. */
182 rtx address;
183 /* The alignment (in bits) of the slot. */
184 unsigned int align;
185 /* The size, in units, of the slot. */
186 HOST_WIDE_INT size;
187 /* The type of the object in the slot, or zero if it doesn't correspond
188 to a type. We use this to determine whether a slot can be reused.
189 It can be reused if objects of the type of the new slot will always
190 conflict with objects of the type of the old slot. */
191 tree type;
192 /* The value of `sequence_rtl_expr' when this temporary is allocated. */
193 tree rtl_expr;
194 /* Non-zero if this temporary is currently in use. */
195 char in_use;
196 /* Non-zero if this temporary has its address taken. */
197 char addr_taken;
198 /* Nesting level at which this slot is being used. */
199 int level;
200 /* Non-zero if this should survive a call to free_temp_slots. */
201 int keep;
202 /* The offset of the slot from the frame_pointer, including extra space
203 for alignment. This info is for combine_temp_slots. */
204 HOST_WIDE_INT base_offset;
205 /* The size of the slot, including extra space for alignment. This
206 info is for combine_temp_slots. */
207 HOST_WIDE_INT full_size;
210 /* This structure is used to record MEMs or pseudos used to replace VAR, any
211 SUBREGs of VAR, and any MEMs containing VAR as an address. We need to
212 maintain this list in case two operands of an insn were required to match;
213 in that case we must ensure we use the same replacement. */
215 struct fixup_replacement
217 rtx old;
218 rtx new;
219 struct fixup_replacement *next;
222 struct insns_for_mem_entry
224 /* The KEY in HE will be a MEM. */
225 struct hash_entry he;
226 /* These are the INSNS which reference the MEM. */
227 rtx insns;
230 /* Forward declarations. */
232 static rtx assign_stack_local_1 PARAMS ((enum machine_mode, HOST_WIDE_INT,
233 int, struct function *));
234 static struct temp_slot *find_temp_slot_from_address PARAMS ((rtx));
235 static void put_reg_into_stack PARAMS ((struct function *, rtx, tree,
236 enum machine_mode, enum machine_mode,
237 int, unsigned int, int,
238 struct hash_table *));
239 static void schedule_fixup_var_refs PARAMS ((struct function *, rtx, tree,
240 enum machine_mode,
241 struct hash_table *));
242 static void fixup_var_refs PARAMS ((rtx, enum machine_mode, int,
243 struct hash_table *));
244 static struct fixup_replacement
245 *find_fixup_replacement PARAMS ((struct fixup_replacement **, rtx));
246 static void fixup_var_refs_insns PARAMS ((rtx, rtx, enum machine_mode,
247 int, int));
248 static void fixup_var_refs_insns_with_hash
249 PARAMS ((struct hash_table *, rtx,
250 enum machine_mode, int));
251 static void fixup_var_refs_insn PARAMS ((rtx, rtx, enum machine_mode,
252 int, int));
253 static void fixup_var_refs_1 PARAMS ((rtx, enum machine_mode, rtx *, rtx,
254 struct fixup_replacement **));
255 static rtx fixup_memory_subreg PARAMS ((rtx, rtx, int));
256 static rtx walk_fixup_memory_subreg PARAMS ((rtx, rtx, int));
257 static rtx fixup_stack_1 PARAMS ((rtx, rtx));
258 static void optimize_bit_field PARAMS ((rtx, rtx, rtx *));
259 static void instantiate_decls PARAMS ((tree, int));
260 static void instantiate_decls_1 PARAMS ((tree, int));
261 static void instantiate_decl PARAMS ((rtx, HOST_WIDE_INT, int));
262 static rtx instantiate_new_reg PARAMS ((rtx, HOST_WIDE_INT *));
263 static int instantiate_virtual_regs_1 PARAMS ((rtx *, rtx, int));
264 static void delete_handlers PARAMS ((void));
265 static void pad_to_arg_alignment PARAMS ((struct args_size *, int,
266 struct args_size *));
267 #ifndef ARGS_GROW_DOWNWARD
268 static void pad_below PARAMS ((struct args_size *, enum machine_mode,
269 tree));
270 #endif
271 static rtx round_trampoline_addr PARAMS ((rtx));
272 static rtx adjust_trampoline_addr PARAMS ((rtx));
273 static tree *identify_blocks_1 PARAMS ((rtx, tree *, tree *, tree *));
274 static void reorder_blocks_0 PARAMS ((tree));
275 static void reorder_blocks_1 PARAMS ((rtx, tree, varray_type *));
276 static void reorder_fix_fragments PARAMS ((tree));
277 static tree blocks_nreverse PARAMS ((tree));
278 static int all_blocks PARAMS ((tree, tree *));
279 static tree *get_block_vector PARAMS ((tree, int *));
280 extern tree debug_find_var_in_block_tree PARAMS ((tree, tree));
281 /* We always define `record_insns' even if its not used so that we
282 can always export `prologue_epilogue_contains'. */
283 static void record_insns PARAMS ((rtx, varray_type *)) ATTRIBUTE_UNUSED;
284 static int contains PARAMS ((rtx, varray_type));
285 #ifdef HAVE_return
286 static void emit_return_into_block PARAMS ((basic_block, rtx));
287 #endif
288 static void put_addressof_into_stack PARAMS ((rtx, struct hash_table *));
289 static bool purge_addressof_1 PARAMS ((rtx *, rtx, int, int,
290 struct hash_table *));
291 static void purge_single_hard_subreg_set PARAMS ((rtx));
292 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
293 static rtx keep_stack_depressed PARAMS ((rtx));
294 #endif
295 static int is_addressof PARAMS ((rtx *, void *));
296 static struct hash_entry *insns_for_mem_newfunc PARAMS ((struct hash_entry *,
297 struct hash_table *,
298 hash_table_key));
299 static unsigned long insns_for_mem_hash PARAMS ((hash_table_key));
300 static bool insns_for_mem_comp PARAMS ((hash_table_key, hash_table_key));
301 static int insns_for_mem_walk PARAMS ((rtx *, void *));
302 static void compute_insns_for_mem PARAMS ((rtx, rtx, struct hash_table *));
303 static void mark_function_status PARAMS ((struct function *));
304 static void maybe_mark_struct_function PARAMS ((void *));
305 static void prepare_function_start PARAMS ((void));
306 static void do_clobber_return_reg PARAMS ((rtx, void *));
307 static void do_use_return_reg PARAMS ((rtx, void *));
309 /* Pointer to chain of `struct function' for containing functions. */
310 static struct function *outer_function_chain;
312 /* Given a function decl for a containing function,
313 return the `struct function' for it. */
315 struct function *
316 find_function_data (decl)
317 tree decl;
319 struct function *p;
321 for (p = outer_function_chain; p; p = p->outer)
322 if (p->decl == decl)
323 return p;
325 abort ();
328 /* Save the current context for compilation of a nested function.
329 This is called from language-specific code. The caller should use
330 the save_lang_status callback to save any language-specific state,
331 since this function knows only about language-independent
332 variables. */
334 void
335 push_function_context_to (context)
336 tree context;
338 struct function *p;
340 if (context)
342 if (context == current_function_decl)
343 cfun->contains_functions = 1;
344 else
346 struct function *containing = find_function_data (context);
347 containing->contains_functions = 1;
351 if (cfun == 0)
352 init_dummy_function_start ();
353 p = cfun;
355 p->outer = outer_function_chain;
356 outer_function_chain = p;
357 p->fixup_var_refs_queue = 0;
359 if (save_lang_status)
360 (*save_lang_status) (p);
362 cfun = 0;
365 void
366 push_function_context ()
368 push_function_context_to (current_function_decl);
371 /* Restore the last saved context, at the end of a nested function.
372 This function is called from language-specific code. */
374 void
375 pop_function_context_from (context)
376 tree context ATTRIBUTE_UNUSED;
378 struct function *p = outer_function_chain;
379 struct var_refs_queue *queue;
381 cfun = p;
382 outer_function_chain = p->outer;
384 current_function_decl = p->decl;
385 reg_renumber = 0;
387 restore_emit_status (p);
389 if (restore_lang_status)
390 (*restore_lang_status) (p);
392 /* Finish doing put_var_into_stack for any of our variables
393 which became addressable during the nested function. */
394 for (queue = p->fixup_var_refs_queue; queue; queue = queue->next)
395 fixup_var_refs (queue->modified, queue->promoted_mode,
396 queue->unsignedp, 0);
398 p->fixup_var_refs_queue = 0;
400 /* Reset variables that have known state during rtx generation. */
401 rtx_equal_function_value_matters = 1;
402 virtuals_instantiated = 0;
403 generating_concat_p = 1;
406 void
407 pop_function_context ()
409 pop_function_context_from (current_function_decl);
412 /* Clear out all parts of the state in F that can safely be discarded
413 after the function has been parsed, but not compiled, to let
414 garbage collection reclaim the memory. */
416 void
417 free_after_parsing (f)
418 struct function *f;
420 /* f->expr->forced_labels is used by code generation. */
421 /* f->emit->regno_reg_rtx is used by code generation. */
422 /* f->varasm is used by code generation. */
423 /* f->eh->eh_return_stub_label is used by code generation. */
425 if (free_lang_status)
426 (*free_lang_status) (f);
427 free_stmt_status (f);
430 /* Clear out all parts of the state in F that can safely be discarded
431 after the function has been compiled, to let garbage collection
432 reclaim the memory. */
434 void
435 free_after_compilation (f)
436 struct function *f;
438 free_eh_status (f);
439 free_expr_status (f);
440 free_emit_status (f);
441 free_varasm_status (f);
443 if (free_machine_status)
444 (*free_machine_status) (f);
446 if (f->x_parm_reg_stack_loc)
447 free (f->x_parm_reg_stack_loc);
449 f->x_temp_slots = NULL;
450 f->arg_offset_rtx = NULL;
451 f->return_rtx = NULL;
452 f->internal_arg_pointer = NULL;
453 f->x_nonlocal_labels = NULL;
454 f->x_nonlocal_goto_handler_slots = NULL;
455 f->x_nonlocal_goto_handler_labels = NULL;
456 f->x_nonlocal_goto_stack_level = NULL;
457 f->x_cleanup_label = NULL;
458 f->x_return_label = NULL;
459 f->x_save_expr_regs = NULL;
460 f->x_stack_slot_list = NULL;
461 f->x_rtl_expr_chain = NULL;
462 f->x_tail_recursion_label = NULL;
463 f->x_tail_recursion_reentry = NULL;
464 f->x_arg_pointer_save_area = NULL;
465 f->x_clobber_return_insn = NULL;
466 f->x_context_display = NULL;
467 f->x_trampoline_list = NULL;
468 f->x_parm_birth_insn = NULL;
469 f->x_last_parm_insn = NULL;
470 f->x_parm_reg_stack_loc = NULL;
471 f->fixup_var_refs_queue = NULL;
472 f->original_arg_vector = NULL;
473 f->original_decl_initial = NULL;
474 f->inl_last_parm_insn = NULL;
475 f->epilogue_delay_list = NULL;
478 /* Allocate fixed slots in the stack frame of the current function. */
480 /* Return size needed for stack frame based on slots so far allocated in
481 function F.
482 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
483 the caller may have to do that. */
485 HOST_WIDE_INT
486 get_func_frame_size (f)
487 struct function *f;
489 #ifdef FRAME_GROWS_DOWNWARD
490 return -f->x_frame_offset;
491 #else
492 return f->x_frame_offset;
493 #endif
496 /* Return size needed for stack frame based on slots so far allocated.
497 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
498 the caller may have to do that. */
499 HOST_WIDE_INT
500 get_frame_size ()
502 return get_func_frame_size (cfun);
505 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
506 with machine mode MODE.
508 ALIGN controls the amount of alignment for the address of the slot:
509 0 means according to MODE,
510 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
511 positive specifies alignment boundary in bits.
513 We do not round to stack_boundary here.
515 FUNCTION specifies the function to allocate in. */
517 static rtx
518 assign_stack_local_1 (mode, size, align, function)
519 enum machine_mode mode;
520 HOST_WIDE_INT size;
521 int align;
522 struct function *function;
524 rtx x, addr;
525 int bigend_correction = 0;
526 int alignment;
528 if (align == 0)
530 tree type;
532 if (mode == BLKmode)
533 alignment = BIGGEST_ALIGNMENT;
534 else
535 alignment = GET_MODE_ALIGNMENT (mode);
537 /* Allow the target to (possibly) increase the alignment of this
538 stack slot. */
539 type = type_for_mode (mode, 0);
540 if (type)
541 alignment = LOCAL_ALIGNMENT (type, alignment);
543 alignment /= BITS_PER_UNIT;
545 else if (align == -1)
547 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
548 size = CEIL_ROUND (size, alignment);
550 else
551 alignment = align / BITS_PER_UNIT;
553 #ifdef FRAME_GROWS_DOWNWARD
554 function->x_frame_offset -= size;
555 #endif
557 /* Ignore alignment we can't do with expected alignment of the boundary. */
558 if (alignment * BITS_PER_UNIT > PREFERRED_STACK_BOUNDARY)
559 alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
561 if (function->stack_alignment_needed < alignment * BITS_PER_UNIT)
562 function->stack_alignment_needed = alignment * BITS_PER_UNIT;
564 /* Round frame offset to that alignment.
565 We must be careful here, since FRAME_OFFSET might be negative and
566 division with a negative dividend isn't as well defined as we might
567 like. So we instead assume that ALIGNMENT is a power of two and
568 use logical operations which are unambiguous. */
569 #ifdef FRAME_GROWS_DOWNWARD
570 function->x_frame_offset = FLOOR_ROUND (function->x_frame_offset, alignment);
571 #else
572 function->x_frame_offset = CEIL_ROUND (function->x_frame_offset, alignment);
573 #endif
575 /* On a big-endian machine, if we are allocating more space than we will use,
576 use the least significant bytes of those that are allocated. */
577 if (BYTES_BIG_ENDIAN && mode != BLKmode)
578 bigend_correction = size - GET_MODE_SIZE (mode);
580 /* If we have already instantiated virtual registers, return the actual
581 address relative to the frame pointer. */
582 if (function == cfun && virtuals_instantiated)
583 addr = plus_constant (frame_pointer_rtx,
584 (frame_offset + bigend_correction
585 + STARTING_FRAME_OFFSET));
586 else
587 addr = plus_constant (virtual_stack_vars_rtx,
588 function->x_frame_offset + bigend_correction);
590 #ifndef FRAME_GROWS_DOWNWARD
591 function->x_frame_offset += size;
592 #endif
594 x = gen_rtx_MEM (mode, addr);
596 function->x_stack_slot_list
597 = gen_rtx_EXPR_LIST (VOIDmode, x, function->x_stack_slot_list);
599 return x;
602 /* Wrapper around assign_stack_local_1; assign a local stack slot for the
603 current function. */
606 assign_stack_local (mode, size, align)
607 enum machine_mode mode;
608 HOST_WIDE_INT size;
609 int align;
611 return assign_stack_local_1 (mode, size, align, cfun);
614 /* Allocate a temporary stack slot and record it for possible later
615 reuse.
617 MODE is the machine mode to be given to the returned rtx.
619 SIZE is the size in units of the space required. We do no rounding here
620 since assign_stack_local will do any required rounding.
622 KEEP is 1 if this slot is to be retained after a call to
623 free_temp_slots. Automatic variables for a block are allocated
624 with this flag. KEEP is 2 if we allocate a longer term temporary,
625 whose lifetime is controlled by CLEANUP_POINT_EXPRs. KEEP is 3
626 if we are to allocate something at an inner level to be treated as
627 a variable in the block (e.g., a SAVE_EXPR).
629 TYPE is the type that will be used for the stack slot. */
632 assign_stack_temp_for_type (mode, size, keep, type)
633 enum machine_mode mode;
634 HOST_WIDE_INT size;
635 int keep;
636 tree type;
638 unsigned int align;
639 struct temp_slot *p, *best_p = 0;
641 /* If SIZE is -1 it means that somebody tried to allocate a temporary
642 of a variable size. */
643 if (size == -1)
644 abort ();
646 if (mode == BLKmode)
647 align = BIGGEST_ALIGNMENT;
648 else
649 align = GET_MODE_ALIGNMENT (mode);
651 if (! type)
652 type = type_for_mode (mode, 0);
654 if (type)
655 align = LOCAL_ALIGNMENT (type, align);
657 /* Try to find an available, already-allocated temporary of the proper
658 mode which meets the size and alignment requirements. Choose the
659 smallest one with the closest alignment. */
660 for (p = temp_slots; p; p = p->next)
661 if (p->align >= align && p->size >= size && GET_MODE (p->slot) == mode
662 && ! p->in_use
663 && objects_must_conflict_p (p->type, type)
664 && (best_p == 0 || best_p->size > p->size
665 || (best_p->size == p->size && best_p->align > p->align)))
667 if (p->align == align && p->size == size)
669 best_p = 0;
670 break;
672 best_p = p;
675 /* Make our best, if any, the one to use. */
676 if (best_p)
678 /* If there are enough aligned bytes left over, make them into a new
679 temp_slot so that the extra bytes don't get wasted. Do this only
680 for BLKmode slots, so that we can be sure of the alignment. */
681 if (GET_MODE (best_p->slot) == BLKmode)
683 int alignment = best_p->align / BITS_PER_UNIT;
684 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
686 if (best_p->size - rounded_size >= alignment)
688 p = (struct temp_slot *) ggc_alloc (sizeof (struct temp_slot));
689 p->in_use = p->addr_taken = 0;
690 p->size = best_p->size - rounded_size;
691 p->base_offset = best_p->base_offset + rounded_size;
692 p->full_size = best_p->full_size - rounded_size;
693 p->slot = gen_rtx_MEM (BLKmode,
694 plus_constant (XEXP (best_p->slot, 0),
695 rounded_size));
696 p->align = best_p->align;
697 p->address = 0;
698 p->rtl_expr = 0;
699 p->type = best_p->type;
700 p->next = temp_slots;
701 temp_slots = p;
703 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
704 stack_slot_list);
706 best_p->size = rounded_size;
707 best_p->full_size = rounded_size;
711 p = best_p;
714 /* If we still didn't find one, make a new temporary. */
715 if (p == 0)
717 HOST_WIDE_INT frame_offset_old = frame_offset;
719 p = (struct temp_slot *) ggc_alloc (sizeof (struct temp_slot));
721 /* We are passing an explicit alignment request to assign_stack_local.
722 One side effect of that is assign_stack_local will not round SIZE
723 to ensure the frame offset remains suitably aligned.
725 So for requests which depended on the rounding of SIZE, we go ahead
726 and round it now. We also make sure ALIGNMENT is at least
727 BIGGEST_ALIGNMENT. */
728 if (mode == BLKmode && align < BIGGEST_ALIGNMENT)
729 abort();
730 p->slot = assign_stack_local (mode,
731 (mode == BLKmode
732 ? CEIL_ROUND (size, align / BITS_PER_UNIT)
733 : size),
734 align);
736 p->align = align;
738 /* The following slot size computation is necessary because we don't
739 know the actual size of the temporary slot until assign_stack_local
740 has performed all the frame alignment and size rounding for the
741 requested temporary. Note that extra space added for alignment
742 can be either above or below this stack slot depending on which
743 way the frame grows. We include the extra space if and only if it
744 is above this slot. */
745 #ifdef FRAME_GROWS_DOWNWARD
746 p->size = frame_offset_old - frame_offset;
747 #else
748 p->size = size;
749 #endif
751 /* Now define the fields used by combine_temp_slots. */
752 #ifdef FRAME_GROWS_DOWNWARD
753 p->base_offset = frame_offset;
754 p->full_size = frame_offset_old - frame_offset;
755 #else
756 p->base_offset = frame_offset_old;
757 p->full_size = frame_offset - frame_offset_old;
758 #endif
759 p->address = 0;
760 p->next = temp_slots;
761 temp_slots = p;
764 p->in_use = 1;
765 p->addr_taken = 0;
766 p->rtl_expr = seq_rtl_expr;
767 p->type = type;
769 if (keep == 2)
771 p->level = target_temp_slot_level;
772 p->keep = 0;
774 else if (keep == 3)
776 p->level = var_temp_slot_level;
777 p->keep = 0;
779 else
781 p->level = temp_slot_level;
782 p->keep = keep;
785 /* We may be reusing an old slot, so clear any MEM flags that may have been
786 set from before. */
787 RTX_UNCHANGING_P (p->slot) = 0;
788 MEM_IN_STRUCT_P (p->slot) = 0;
789 MEM_SCALAR_P (p->slot) = 0;
790 MEM_VOLATILE_P (p->slot) = 0;
791 set_mem_alias_set (p->slot, 0);
793 /* If we know the alias set for the memory that will be used, use
794 it. If there's no TYPE, then we don't know anything about the
795 alias set for the memory. */
796 set_mem_alias_set (p->slot, type ? get_alias_set (type) : 0);
797 set_mem_align (p->slot, align);
799 /* If a type is specified, set the relevant flags. */
800 if (type != 0)
802 RTX_UNCHANGING_P (p->slot) = TYPE_READONLY (type);
803 MEM_VOLATILE_P (p->slot) = TYPE_VOLATILE (type);
804 MEM_SET_IN_STRUCT_P (p->slot, AGGREGATE_TYPE_P (type));
807 return p->slot;
810 /* Allocate a temporary stack slot and record it for possible later
811 reuse. First three arguments are same as in preceding function. */
814 assign_stack_temp (mode, size, keep)
815 enum machine_mode mode;
816 HOST_WIDE_INT size;
817 int keep;
819 return assign_stack_temp_for_type (mode, size, keep, NULL_TREE);
822 /* Assign a temporary of given TYPE.
823 KEEP is as for assign_stack_temp.
824 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
825 it is 0 if a register is OK.
826 DONT_PROMOTE is 1 if we should not promote values in register
827 to wider modes. */
830 assign_temp (type, keep, memory_required, dont_promote)
831 tree type;
832 int keep;
833 int memory_required;
834 int dont_promote ATTRIBUTE_UNUSED;
836 enum machine_mode mode = TYPE_MODE (type);
837 #ifndef PROMOTE_FOR_CALL_ONLY
838 int unsignedp = TREE_UNSIGNED (type);
839 #endif
841 if (mode == BLKmode || memory_required)
843 HOST_WIDE_INT size = int_size_in_bytes (type);
844 rtx tmp;
846 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
847 problems with allocating the stack space. */
848 if (size == 0)
849 size = 1;
851 /* Unfortunately, we don't yet know how to allocate variable-sized
852 temporaries. However, sometimes we have a fixed upper limit on
853 the size (which is stored in TYPE_ARRAY_MAX_SIZE) and can use that
854 instead. This is the case for Chill variable-sized strings. */
855 if (size == -1 && TREE_CODE (type) == ARRAY_TYPE
856 && TYPE_ARRAY_MAX_SIZE (type) != NULL_TREE
857 && host_integerp (TYPE_ARRAY_MAX_SIZE (type), 1))
858 size = tree_low_cst (TYPE_ARRAY_MAX_SIZE (type), 1);
860 tmp = assign_stack_temp_for_type (mode, size, keep, type);
861 return tmp;
864 #ifndef PROMOTE_FOR_CALL_ONLY
865 if (! dont_promote)
866 mode = promote_mode (type, mode, &unsignedp, 0);
867 #endif
869 return gen_reg_rtx (mode);
872 /* Combine temporary stack slots which are adjacent on the stack.
874 This allows for better use of already allocated stack space. This is only
875 done for BLKmode slots because we can be sure that we won't have alignment
876 problems in this case. */
878 void
879 combine_temp_slots ()
881 struct temp_slot *p, *q;
882 struct temp_slot *prev_p, *prev_q;
883 int num_slots;
885 /* We can't combine slots, because the information about which slot
886 is in which alias set will be lost. */
887 if (flag_strict_aliasing)
888 return;
890 /* If there are a lot of temp slots, don't do anything unless
891 high levels of optimization. */
892 if (! flag_expensive_optimizations)
893 for (p = temp_slots, num_slots = 0; p; p = p->next, num_slots++)
894 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
895 return;
897 for (p = temp_slots, prev_p = 0; p; p = prev_p ? prev_p->next : temp_slots)
899 int delete_p = 0;
901 if (! p->in_use && GET_MODE (p->slot) == BLKmode)
902 for (q = p->next, prev_q = p; q; q = prev_q->next)
904 int delete_q = 0;
905 if (! q->in_use && GET_MODE (q->slot) == BLKmode)
907 if (p->base_offset + p->full_size == q->base_offset)
909 /* Q comes after P; combine Q into P. */
910 p->size += q->size;
911 p->full_size += q->full_size;
912 delete_q = 1;
914 else if (q->base_offset + q->full_size == p->base_offset)
916 /* P comes after Q; combine P into Q. */
917 q->size += p->size;
918 q->full_size += p->full_size;
919 delete_p = 1;
920 break;
923 /* Either delete Q or advance past it. */
924 if (delete_q)
925 prev_q->next = q->next;
926 else
927 prev_q = q;
929 /* Either delete P or advance past it. */
930 if (delete_p)
932 if (prev_p)
933 prev_p->next = p->next;
934 else
935 temp_slots = p->next;
937 else
938 prev_p = p;
942 /* Find the temp slot corresponding to the object at address X. */
944 static struct temp_slot *
945 find_temp_slot_from_address (x)
946 rtx x;
948 struct temp_slot *p;
949 rtx next;
951 for (p = temp_slots; p; p = p->next)
953 if (! p->in_use)
954 continue;
956 else if (XEXP (p->slot, 0) == x
957 || p->address == x
958 || (GET_CODE (x) == PLUS
959 && XEXP (x, 0) == virtual_stack_vars_rtx
960 && GET_CODE (XEXP (x, 1)) == CONST_INT
961 && INTVAL (XEXP (x, 1)) >= p->base_offset
962 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size))
963 return p;
965 else if (p->address != 0 && GET_CODE (p->address) == EXPR_LIST)
966 for (next = p->address; next; next = XEXP (next, 1))
967 if (XEXP (next, 0) == x)
968 return p;
971 /* If we have a sum involving a register, see if it points to a temp
972 slot. */
973 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == REG
974 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
975 return p;
976 else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG
977 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
978 return p;
980 return 0;
983 /* Indicate that NEW is an alternate way of referring to the temp slot
984 that previously was known by OLD. */
986 void
987 update_temp_slot_address (old, new)
988 rtx old, new;
990 struct temp_slot *p;
992 if (rtx_equal_p (old, new))
993 return;
995 p = find_temp_slot_from_address (old);
997 /* If we didn't find one, see if both OLD is a PLUS. If so, and NEW
998 is a register, see if one operand of the PLUS is a temporary
999 location. If so, NEW points into it. Otherwise, if both OLD and
1000 NEW are a PLUS and if there is a register in common between them.
1001 If so, try a recursive call on those values. */
1002 if (p == 0)
1004 if (GET_CODE (old) != PLUS)
1005 return;
1007 if (GET_CODE (new) == REG)
1009 update_temp_slot_address (XEXP (old, 0), new);
1010 update_temp_slot_address (XEXP (old, 1), new);
1011 return;
1013 else if (GET_CODE (new) != PLUS)
1014 return;
1016 if (rtx_equal_p (XEXP (old, 0), XEXP (new, 0)))
1017 update_temp_slot_address (XEXP (old, 1), XEXP (new, 1));
1018 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 0)))
1019 update_temp_slot_address (XEXP (old, 0), XEXP (new, 1));
1020 else if (rtx_equal_p (XEXP (old, 0), XEXP (new, 1)))
1021 update_temp_slot_address (XEXP (old, 1), XEXP (new, 0));
1022 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 1)))
1023 update_temp_slot_address (XEXP (old, 0), XEXP (new, 0));
1025 return;
1028 /* Otherwise add an alias for the temp's address. */
1029 else if (p->address == 0)
1030 p->address = new;
1031 else
1033 if (GET_CODE (p->address) != EXPR_LIST)
1034 p->address = gen_rtx_EXPR_LIST (VOIDmode, p->address, NULL_RTX);
1036 p->address = gen_rtx_EXPR_LIST (VOIDmode, new, p->address);
1040 /* If X could be a reference to a temporary slot, mark the fact that its
1041 address was taken. */
1043 void
1044 mark_temp_addr_taken (x)
1045 rtx x;
1047 struct temp_slot *p;
1049 if (x == 0)
1050 return;
1052 /* If X is not in memory or is at a constant address, it cannot be in
1053 a temporary slot. */
1054 if (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1055 return;
1057 p = find_temp_slot_from_address (XEXP (x, 0));
1058 if (p != 0)
1059 p->addr_taken = 1;
1062 /* If X could be a reference to a temporary slot, mark that slot as
1063 belonging to the to one level higher than the current level. If X
1064 matched one of our slots, just mark that one. Otherwise, we can't
1065 easily predict which it is, so upgrade all of them. Kept slots
1066 need not be touched.
1068 This is called when an ({...}) construct occurs and a statement
1069 returns a value in memory. */
1071 void
1072 preserve_temp_slots (x)
1073 rtx x;
1075 struct temp_slot *p = 0;
1077 /* If there is no result, we still might have some objects whose address
1078 were taken, so we need to make sure they stay around. */
1079 if (x == 0)
1081 for (p = temp_slots; p; p = p->next)
1082 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1083 p->level--;
1085 return;
1088 /* If X is a register that is being used as a pointer, see if we have
1089 a temporary slot we know it points to. To be consistent with
1090 the code below, we really should preserve all non-kept slots
1091 if we can't find a match, but that seems to be much too costly. */
1092 if (GET_CODE (x) == REG && REG_POINTER (x))
1093 p = find_temp_slot_from_address (x);
1095 /* If X is not in memory or is at a constant address, it cannot be in
1096 a temporary slot, but it can contain something whose address was
1097 taken. */
1098 if (p == 0 && (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0))))
1100 for (p = temp_slots; p; p = p->next)
1101 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1102 p->level--;
1104 return;
1107 /* First see if we can find a match. */
1108 if (p == 0)
1109 p = find_temp_slot_from_address (XEXP (x, 0));
1111 if (p != 0)
1113 /* Move everything at our level whose address was taken to our new
1114 level in case we used its address. */
1115 struct temp_slot *q;
1117 if (p->level == temp_slot_level)
1119 for (q = temp_slots; q; q = q->next)
1120 if (q != p && q->addr_taken && q->level == p->level)
1121 q->level--;
1123 p->level--;
1124 p->addr_taken = 0;
1126 return;
1129 /* Otherwise, preserve all non-kept slots at this level. */
1130 for (p = temp_slots; p; p = p->next)
1131 if (p->in_use && p->level == temp_slot_level && ! p->keep)
1132 p->level--;
1135 /* X is the result of an RTL_EXPR. If it is a temporary slot associated
1136 with that RTL_EXPR, promote it into a temporary slot at the present
1137 level so it will not be freed when we free slots made in the
1138 RTL_EXPR. */
1140 void
1141 preserve_rtl_expr_result (x)
1142 rtx x;
1144 struct temp_slot *p;
1146 /* If X is not in memory or is at a constant address, it cannot be in
1147 a temporary slot. */
1148 if (x == 0 || GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1149 return;
1151 /* If we can find a match, move it to our level unless it is already at
1152 an upper level. */
1153 p = find_temp_slot_from_address (XEXP (x, 0));
1154 if (p != 0)
1156 p->level = MIN (p->level, temp_slot_level);
1157 p->rtl_expr = 0;
1160 return;
1163 /* Free all temporaries used so far. This is normally called at the end
1164 of generating code for a statement. Don't free any temporaries
1165 currently in use for an RTL_EXPR that hasn't yet been emitted.
1166 We could eventually do better than this since it can be reused while
1167 generating the same RTL_EXPR, but this is complex and probably not
1168 worthwhile. */
1170 void
1171 free_temp_slots ()
1173 struct temp_slot *p;
1175 for (p = temp_slots; p; p = p->next)
1176 if (p->in_use && p->level == temp_slot_level && ! p->keep
1177 && p->rtl_expr == 0)
1178 p->in_use = 0;
1180 combine_temp_slots ();
1183 /* Free all temporary slots used in T, an RTL_EXPR node. */
1185 void
1186 free_temps_for_rtl_expr (t)
1187 tree t;
1189 struct temp_slot *p;
1191 for (p = temp_slots; p; p = p->next)
1192 if (p->rtl_expr == t)
1194 /* If this slot is below the current TEMP_SLOT_LEVEL, then it
1195 needs to be preserved. This can happen if a temporary in
1196 the RTL_EXPR was addressed; preserve_temp_slots will move
1197 the temporary into a higher level. */
1198 if (temp_slot_level <= p->level)
1199 p->in_use = 0;
1200 else
1201 p->rtl_expr = NULL_TREE;
1204 combine_temp_slots ();
1207 /* Mark all temporaries ever allocated in this function as not suitable
1208 for reuse until the current level is exited. */
1210 void
1211 mark_all_temps_used ()
1213 struct temp_slot *p;
1215 for (p = temp_slots; p; p = p->next)
1217 p->in_use = p->keep = 1;
1218 p->level = MIN (p->level, temp_slot_level);
1222 /* Push deeper into the nesting level for stack temporaries. */
1224 void
1225 push_temp_slots ()
1227 temp_slot_level++;
1230 /* Likewise, but save the new level as the place to allocate variables
1231 for blocks. */
1233 #if 0
1234 void
1235 push_temp_slots_for_block ()
1237 push_temp_slots ();
1239 var_temp_slot_level = temp_slot_level;
1242 /* Likewise, but save the new level as the place to allocate temporaries
1243 for TARGET_EXPRs. */
1245 void
1246 push_temp_slots_for_target ()
1248 push_temp_slots ();
1250 target_temp_slot_level = temp_slot_level;
1253 /* Set and get the value of target_temp_slot_level. The only
1254 permitted use of these functions is to save and restore this value. */
1257 get_target_temp_slot_level ()
1259 return target_temp_slot_level;
1262 void
1263 set_target_temp_slot_level (level)
1264 int level;
1266 target_temp_slot_level = level;
1268 #endif
1270 /* Pop a temporary nesting level. All slots in use in the current level
1271 are freed. */
1273 void
1274 pop_temp_slots ()
1276 struct temp_slot *p;
1278 for (p = temp_slots; p; p = p->next)
1279 if (p->in_use && p->level == temp_slot_level && p->rtl_expr == 0)
1280 p->in_use = 0;
1282 combine_temp_slots ();
1284 temp_slot_level--;
1287 /* Initialize temporary slots. */
1289 void
1290 init_temp_slots ()
1292 /* We have not allocated any temporaries yet. */
1293 temp_slots = 0;
1294 temp_slot_level = 0;
1295 var_temp_slot_level = 0;
1296 target_temp_slot_level = 0;
1299 /* Retroactively move an auto variable from a register to a stack slot.
1300 This is done when an address-reference to the variable is seen. */
1302 void
1303 put_var_into_stack (decl)
1304 tree decl;
1306 rtx reg;
1307 enum machine_mode promoted_mode, decl_mode;
1308 struct function *function = 0;
1309 tree context;
1310 int can_use_addressof;
1311 int volatilep = TREE_CODE (decl) != SAVE_EXPR && TREE_THIS_VOLATILE (decl);
1312 int usedp = (TREE_USED (decl)
1313 || (TREE_CODE (decl) != SAVE_EXPR && DECL_INITIAL (decl) != 0));
1315 context = decl_function_context (decl);
1317 /* Get the current rtl used for this object and its original mode. */
1318 reg = (TREE_CODE (decl) == SAVE_EXPR
1319 ? SAVE_EXPR_RTL (decl)
1320 : DECL_RTL_IF_SET (decl));
1322 /* No need to do anything if decl has no rtx yet
1323 since in that case caller is setting TREE_ADDRESSABLE
1324 and a stack slot will be assigned when the rtl is made. */
1325 if (reg == 0)
1326 return;
1328 /* Get the declared mode for this object. */
1329 decl_mode = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
1330 : DECL_MODE (decl));
1331 /* Get the mode it's actually stored in. */
1332 promoted_mode = GET_MODE (reg);
1334 /* If this variable comes from an outer function, find that
1335 function's saved context. Don't use find_function_data here,
1336 because it might not be in any active function.
1337 FIXME: Is that really supposed to happen?
1338 It does in ObjC at least. */
1339 if (context != current_function_decl && context != inline_function_decl)
1340 for (function = outer_function_chain; function; function = function->outer)
1341 if (function->decl == context)
1342 break;
1344 /* If this is a variable-size object with a pseudo to address it,
1345 put that pseudo into the stack, if the var is nonlocal. */
1346 if (TREE_CODE (decl) != SAVE_EXPR && DECL_NONLOCAL (decl)
1347 && GET_CODE (reg) == MEM
1348 && GET_CODE (XEXP (reg, 0)) == REG
1349 && REGNO (XEXP (reg, 0)) > LAST_VIRTUAL_REGISTER)
1351 reg = XEXP (reg, 0);
1352 decl_mode = promoted_mode = GET_MODE (reg);
1355 can_use_addressof
1356 = (function == 0
1357 && optimize > 0
1358 /* FIXME make it work for promoted modes too */
1359 && decl_mode == promoted_mode
1360 #ifdef NON_SAVING_SETJMP
1361 && ! (NON_SAVING_SETJMP && current_function_calls_setjmp)
1362 #endif
1365 /* If we can't use ADDRESSOF, make sure we see through one we already
1366 generated. */
1367 if (! can_use_addressof && GET_CODE (reg) == MEM
1368 && GET_CODE (XEXP (reg, 0)) == ADDRESSOF)
1369 reg = XEXP (XEXP (reg, 0), 0);
1371 /* Now we should have a value that resides in one or more pseudo regs. */
1373 if (GET_CODE (reg) == REG)
1375 /* If this variable lives in the current function and we don't need
1376 to put things in the stack for the sake of setjmp, try to keep it
1377 in a register until we know we actually need the address. */
1378 if (can_use_addressof)
1379 gen_mem_addressof (reg, decl);
1380 else
1381 put_reg_into_stack (function, reg, TREE_TYPE (decl), promoted_mode,
1382 decl_mode, volatilep, 0, usedp, 0);
1384 else if (GET_CODE (reg) == CONCAT)
1386 /* A CONCAT contains two pseudos; put them both in the stack.
1387 We do it so they end up consecutive.
1388 We fixup references to the parts only after we fixup references
1389 to the whole CONCAT, lest we do double fixups for the latter
1390 references. */
1391 enum machine_mode part_mode = GET_MODE (XEXP (reg, 0));
1392 tree part_type = type_for_mode (part_mode, 0);
1393 rtx lopart = XEXP (reg, 0);
1394 rtx hipart = XEXP (reg, 1);
1395 #ifdef FRAME_GROWS_DOWNWARD
1396 /* Since part 0 should have a lower address, do it second. */
1397 put_reg_into_stack (function, hipart, part_type, part_mode,
1398 part_mode, volatilep, 0, 0, 0);
1399 put_reg_into_stack (function, lopart, part_type, part_mode,
1400 part_mode, volatilep, 0, 0, 0);
1401 #else
1402 put_reg_into_stack (function, lopart, part_type, part_mode,
1403 part_mode, volatilep, 0, 0, 0);
1404 put_reg_into_stack (function, hipart, part_type, part_mode,
1405 part_mode, volatilep, 0, 0, 0);
1406 #endif
1408 /* Change the CONCAT into a combined MEM for both parts. */
1409 PUT_CODE (reg, MEM);
1410 MEM_ATTRS (reg) = 0;
1412 /* set_mem_attributes uses DECL_RTL to avoid re-generating of
1413 already computed alias sets. Here we want to re-generate. */
1414 if (DECL_P (decl))
1415 SET_DECL_RTL (decl, NULL);
1416 set_mem_attributes (reg, decl, 1);
1417 if (DECL_P (decl))
1418 SET_DECL_RTL (decl, reg);
1420 /* The two parts are in memory order already.
1421 Use the lower parts address as ours. */
1422 XEXP (reg, 0) = XEXP (XEXP (reg, 0), 0);
1423 /* Prevent sharing of rtl that might lose. */
1424 if (GET_CODE (XEXP (reg, 0)) == PLUS)
1425 XEXP (reg, 0) = copy_rtx (XEXP (reg, 0));
1426 if (usedp)
1428 schedule_fixup_var_refs (function, reg, TREE_TYPE (decl),
1429 promoted_mode, 0);
1430 schedule_fixup_var_refs (function, lopart, part_type, part_mode, 0);
1431 schedule_fixup_var_refs (function, hipart, part_type, part_mode, 0);
1434 else
1435 return;
1438 /* Subroutine of put_var_into_stack. This puts a single pseudo reg REG
1439 into the stack frame of FUNCTION (0 means the current function).
1440 DECL_MODE is the machine mode of the user-level data type.
1441 PROMOTED_MODE is the machine mode of the register.
1442 VOLATILE_P is nonzero if this is for a "volatile" decl.
1443 USED_P is nonzero if this reg might have already been used in an insn. */
1445 static void
1446 put_reg_into_stack (function, reg, type, promoted_mode, decl_mode, volatile_p,
1447 original_regno, used_p, ht)
1448 struct function *function;
1449 rtx reg;
1450 tree type;
1451 enum machine_mode promoted_mode, decl_mode;
1452 int volatile_p;
1453 unsigned int original_regno;
1454 int used_p;
1455 struct hash_table *ht;
1457 struct function *func = function ? function : cfun;
1458 rtx new = 0;
1459 unsigned int regno = original_regno;
1461 if (regno == 0)
1462 regno = REGNO (reg);
1464 if (regno < func->x_max_parm_reg)
1465 new = func->x_parm_reg_stack_loc[regno];
1467 if (new == 0)
1468 new = assign_stack_local_1 (decl_mode, GET_MODE_SIZE (decl_mode), 0, func);
1470 PUT_CODE (reg, MEM);
1471 PUT_MODE (reg, decl_mode);
1472 XEXP (reg, 0) = XEXP (new, 0);
1473 MEM_ATTRS (reg) = 0;
1474 /* `volatil' bit means one thing for MEMs, another entirely for REGs. */
1475 MEM_VOLATILE_P (reg) = volatile_p;
1477 /* If this is a memory ref that contains aggregate components,
1478 mark it as such for cse and loop optimize. If we are reusing a
1479 previously generated stack slot, then we need to copy the bit in
1480 case it was set for other reasons. For instance, it is set for
1481 __builtin_va_alist. */
1482 if (type)
1484 MEM_SET_IN_STRUCT_P (reg,
1485 AGGREGATE_TYPE_P (type) || MEM_IN_STRUCT_P (new));
1486 set_mem_alias_set (reg, get_alias_set (type));
1489 if (used_p)
1490 schedule_fixup_var_refs (function, reg, type, promoted_mode, ht);
1493 /* Make sure that all refs to the variable, previously made
1494 when it was a register, are fixed up to be valid again.
1495 See function above for meaning of arguments. */
1497 static void
1498 schedule_fixup_var_refs (function, reg, type, promoted_mode, ht)
1499 struct function *function;
1500 rtx reg;
1501 tree type;
1502 enum machine_mode promoted_mode;
1503 struct hash_table *ht;
1505 int unsigned_p = type ? TREE_UNSIGNED (type) : 0;
1507 if (function != 0)
1509 struct var_refs_queue *temp;
1511 temp
1512 = (struct var_refs_queue *) ggc_alloc (sizeof (struct var_refs_queue));
1513 temp->modified = reg;
1514 temp->promoted_mode = promoted_mode;
1515 temp->unsignedp = unsigned_p;
1516 temp->next = function->fixup_var_refs_queue;
1517 function->fixup_var_refs_queue = temp;
1519 else
1520 /* Variable is local; fix it up now. */
1521 fixup_var_refs (reg, promoted_mode, unsigned_p, ht);
1524 static void
1525 fixup_var_refs (var, promoted_mode, unsignedp, ht)
1526 rtx var;
1527 enum machine_mode promoted_mode;
1528 int unsignedp;
1529 struct hash_table *ht;
1531 tree pending;
1532 rtx first_insn = get_insns ();
1533 struct sequence_stack *stack = seq_stack;
1534 tree rtl_exps = rtl_expr_chain;
1536 /* If there's a hash table, it must record all uses of VAR. */
1537 if (ht)
1539 if (stack != 0)
1540 abort ();
1541 fixup_var_refs_insns_with_hash (ht, var, promoted_mode, unsignedp);
1542 return;
1545 fixup_var_refs_insns (first_insn, var, promoted_mode, unsignedp,
1546 stack == 0);
1548 /* Scan all pending sequences too. */
1549 for (; stack; stack = stack->next)
1551 push_to_full_sequence (stack->first, stack->last);
1552 fixup_var_refs_insns (stack->first, var, promoted_mode, unsignedp,
1553 stack->next != 0);
1554 /* Update remembered end of sequence
1555 in case we added an insn at the end. */
1556 stack->last = get_last_insn ();
1557 end_sequence ();
1560 /* Scan all waiting RTL_EXPRs too. */
1561 for (pending = rtl_exps; pending; pending = TREE_CHAIN (pending))
1563 rtx seq = RTL_EXPR_SEQUENCE (TREE_VALUE (pending));
1564 if (seq != const0_rtx && seq != 0)
1566 push_to_sequence (seq);
1567 fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0);
1568 end_sequence ();
1573 /* REPLACEMENTS is a pointer to a list of the struct fixup_replacement and X is
1574 some part of an insn. Return a struct fixup_replacement whose OLD
1575 value is equal to X. Allocate a new structure if no such entry exists. */
1577 static struct fixup_replacement *
1578 find_fixup_replacement (replacements, x)
1579 struct fixup_replacement **replacements;
1580 rtx x;
1582 struct fixup_replacement *p;
1584 /* See if we have already replaced this. */
1585 for (p = *replacements; p != 0 && ! rtx_equal_p (p->old, x); p = p->next)
1588 if (p == 0)
1590 p = (struct fixup_replacement *) xmalloc (sizeof (struct fixup_replacement));
1591 p->old = x;
1592 p->new = 0;
1593 p->next = *replacements;
1594 *replacements = p;
1597 return p;
1600 /* Scan the insn-chain starting with INSN for refs to VAR
1601 and fix them up. TOPLEVEL is nonzero if this chain is the
1602 main chain of insns for the current function. */
1604 static void
1605 fixup_var_refs_insns (insn, var, promoted_mode, unsignedp, toplevel)
1606 rtx insn;
1607 rtx var;
1608 enum machine_mode promoted_mode;
1609 int unsignedp;
1610 int toplevel;
1612 while (insn)
1614 /* fixup_var_refs_insn might modify insn, so save its next
1615 pointer now. */
1616 rtx next = NEXT_INSN (insn);
1618 /* CALL_PLACEHOLDERs are special; we have to switch into each of
1619 the three sequences they (potentially) contain, and process
1620 them recursively. The CALL_INSN itself is not interesting. */
1622 if (GET_CODE (insn) == CALL_INSN
1623 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1625 int i;
1627 /* Look at the Normal call, sibling call and tail recursion
1628 sequences attached to the CALL_PLACEHOLDER. */
1629 for (i = 0; i < 3; i++)
1631 rtx seq = XEXP (PATTERN (insn), i);
1632 if (seq)
1634 push_to_sequence (seq);
1635 fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0);
1636 XEXP (PATTERN (insn), i) = get_insns ();
1637 end_sequence ();
1642 else if (INSN_P (insn))
1643 fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, toplevel);
1645 insn = next;
1649 /* Look up the insns which reference VAR in HT and fix them up. Other
1650 arguments are the same as fixup_var_refs_insns.
1652 N.B. No need for special processing of CALL_PLACEHOLDERs here,
1653 because the hash table will point straight to the interesting insn
1654 (inside the CALL_PLACEHOLDER). */
1656 static void
1657 fixup_var_refs_insns_with_hash (ht, var, promoted_mode, unsignedp)
1658 struct hash_table *ht;
1659 rtx var;
1660 enum machine_mode promoted_mode;
1661 int unsignedp;
1663 struct insns_for_mem_entry *ime = (struct insns_for_mem_entry *)
1664 hash_lookup (ht, var, /*create=*/0, /*copy=*/0);
1665 rtx insn_list = ime->insns;
1667 while (insn_list)
1669 rtx insn = XEXP (insn_list, 0);
1671 if (INSN_P (insn))
1672 fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, 1);
1674 insn_list = XEXP (insn_list, 1);
1679 /* Per-insn processing by fixup_var_refs_insns(_with_hash). INSN is
1680 the insn under examination, VAR is the variable to fix up
1681 references to, PROMOTED_MODE and UNSIGNEDP describe VAR, and
1682 TOPLEVEL is nonzero if this is the main insn chain for this
1683 function. */
1685 static void
1686 fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, toplevel)
1687 rtx insn;
1688 rtx var;
1689 enum machine_mode promoted_mode;
1690 int unsignedp;
1691 int toplevel;
1693 rtx call_dest = 0;
1694 rtx set, prev, prev_set;
1695 rtx note;
1697 /* Remember the notes in case we delete the insn. */
1698 note = REG_NOTES (insn);
1700 /* If this is a CLOBBER of VAR, delete it.
1702 If it has a REG_LIBCALL note, delete the REG_LIBCALL
1703 and REG_RETVAL notes too. */
1704 if (GET_CODE (PATTERN (insn)) == CLOBBER
1705 && (XEXP (PATTERN (insn), 0) == var
1706 || (GET_CODE (XEXP (PATTERN (insn), 0)) == CONCAT
1707 && (XEXP (XEXP (PATTERN (insn), 0), 0) == var
1708 || XEXP (XEXP (PATTERN (insn), 0), 1) == var))))
1710 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0)
1711 /* The REG_LIBCALL note will go away since we are going to
1712 turn INSN into a NOTE, so just delete the
1713 corresponding REG_RETVAL note. */
1714 remove_note (XEXP (note, 0),
1715 find_reg_note (XEXP (note, 0), REG_RETVAL,
1716 NULL_RTX));
1718 delete_insn (insn);
1721 /* The insn to load VAR from a home in the arglist
1722 is now a no-op. When we see it, just delete it.
1723 Similarly if this is storing VAR from a register from which
1724 it was loaded in the previous insn. This will occur
1725 when an ADDRESSOF was made for an arglist slot. */
1726 else if (toplevel
1727 && (set = single_set (insn)) != 0
1728 && SET_DEST (set) == var
1729 /* If this represents the result of an insn group,
1730 don't delete the insn. */
1731 && find_reg_note (insn, REG_RETVAL, NULL_RTX) == 0
1732 && (rtx_equal_p (SET_SRC (set), var)
1733 || (GET_CODE (SET_SRC (set)) == REG
1734 && (prev = prev_nonnote_insn (insn)) != 0
1735 && (prev_set = single_set (prev)) != 0
1736 && SET_DEST (prev_set) == SET_SRC (set)
1737 && rtx_equal_p (SET_SRC (prev_set), var))))
1739 delete_insn (insn);
1741 else
1743 struct fixup_replacement *replacements = 0;
1744 rtx next_insn = NEXT_INSN (insn);
1746 if (SMALL_REGISTER_CLASSES)
1748 /* If the insn that copies the results of a CALL_INSN
1749 into a pseudo now references VAR, we have to use an
1750 intermediate pseudo since we want the life of the
1751 return value register to be only a single insn.
1753 If we don't use an intermediate pseudo, such things as
1754 address computations to make the address of VAR valid
1755 if it is not can be placed between the CALL_INSN and INSN.
1757 To make sure this doesn't happen, we record the destination
1758 of the CALL_INSN and see if the next insn uses both that
1759 and VAR. */
1761 if (call_dest != 0 && GET_CODE (insn) == INSN
1762 && reg_mentioned_p (var, PATTERN (insn))
1763 && reg_mentioned_p (call_dest, PATTERN (insn)))
1765 rtx temp = gen_reg_rtx (GET_MODE (call_dest));
1767 emit_insn_before (gen_move_insn (temp, call_dest), insn);
1769 PATTERN (insn) = replace_rtx (PATTERN (insn),
1770 call_dest, temp);
1773 if (GET_CODE (insn) == CALL_INSN
1774 && GET_CODE (PATTERN (insn)) == SET)
1775 call_dest = SET_DEST (PATTERN (insn));
1776 else if (GET_CODE (insn) == CALL_INSN
1777 && GET_CODE (PATTERN (insn)) == PARALLEL
1778 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1779 call_dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1780 else
1781 call_dest = 0;
1784 /* See if we have to do anything to INSN now that VAR is in
1785 memory. If it needs to be loaded into a pseudo, use a single
1786 pseudo for the entire insn in case there is a MATCH_DUP
1787 between two operands. We pass a pointer to the head of
1788 a list of struct fixup_replacements. If fixup_var_refs_1
1789 needs to allocate pseudos or replacement MEMs (for SUBREGs),
1790 it will record them in this list.
1792 If it allocated a pseudo for any replacement, we copy into
1793 it here. */
1795 fixup_var_refs_1 (var, promoted_mode, &PATTERN (insn), insn,
1796 &replacements);
1798 /* If this is last_parm_insn, and any instructions were output
1799 after it to fix it up, then we must set last_parm_insn to
1800 the last such instruction emitted. */
1801 if (insn == last_parm_insn)
1802 last_parm_insn = PREV_INSN (next_insn);
1804 while (replacements)
1806 struct fixup_replacement *next;
1808 if (GET_CODE (replacements->new) == REG)
1810 rtx insert_before;
1811 rtx seq;
1813 /* OLD might be a (subreg (mem)). */
1814 if (GET_CODE (replacements->old) == SUBREG)
1815 replacements->old
1816 = fixup_memory_subreg (replacements->old, insn, 0);
1817 else
1818 replacements->old
1819 = fixup_stack_1 (replacements->old, insn);
1821 insert_before = insn;
1823 /* If we are changing the mode, do a conversion.
1824 This might be wasteful, but combine.c will
1825 eliminate much of the waste. */
1827 if (GET_MODE (replacements->new)
1828 != GET_MODE (replacements->old))
1830 start_sequence ();
1831 convert_move (replacements->new,
1832 replacements->old, unsignedp);
1833 seq = gen_sequence ();
1834 end_sequence ();
1836 else
1837 seq = gen_move_insn (replacements->new,
1838 replacements->old);
1840 emit_insn_before (seq, insert_before);
1843 next = replacements->next;
1844 free (replacements);
1845 replacements = next;
1849 /* Also fix up any invalid exprs in the REG_NOTES of this insn.
1850 But don't touch other insns referred to by reg-notes;
1851 we will get them elsewhere. */
1852 while (note)
1854 if (GET_CODE (note) != INSN_LIST)
1855 XEXP (note, 0)
1856 = walk_fixup_memory_subreg (XEXP (note, 0), insn, 1);
1857 note = XEXP (note, 1);
1861 /* VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
1862 See if the rtx expression at *LOC in INSN needs to be changed.
1864 REPLACEMENTS is a pointer to a list head that starts out zero, but may
1865 contain a list of original rtx's and replacements. If we find that we need
1866 to modify this insn by replacing a memory reference with a pseudo or by
1867 making a new MEM to implement a SUBREG, we consult that list to see if
1868 we have already chosen a replacement. If none has already been allocated,
1869 we allocate it and update the list. fixup_var_refs_insn will copy VAR
1870 or the SUBREG, as appropriate, to the pseudo. */
1872 static void
1873 fixup_var_refs_1 (var, promoted_mode, loc, insn, replacements)
1874 rtx var;
1875 enum machine_mode promoted_mode;
1876 rtx *loc;
1877 rtx insn;
1878 struct fixup_replacement **replacements;
1880 int i;
1881 rtx x = *loc;
1882 RTX_CODE code = GET_CODE (x);
1883 const char *fmt;
1884 rtx tem, tem1;
1885 struct fixup_replacement *replacement;
1887 switch (code)
1889 case ADDRESSOF:
1890 if (XEXP (x, 0) == var)
1892 /* Prevent sharing of rtl that might lose. */
1893 rtx sub = copy_rtx (XEXP (var, 0));
1895 if (! validate_change (insn, loc, sub, 0))
1897 rtx y = gen_reg_rtx (GET_MODE (sub));
1898 rtx seq, new_insn;
1900 /* We should be able to replace with a register or all is lost.
1901 Note that we can't use validate_change to verify this, since
1902 we're not caring for replacing all dups simultaneously. */
1903 if (! validate_replace_rtx (*loc, y, insn))
1904 abort ();
1906 /* Careful! First try to recognize a direct move of the
1907 value, mimicking how things are done in gen_reload wrt
1908 PLUS. Consider what happens when insn is a conditional
1909 move instruction and addsi3 clobbers flags. */
1911 start_sequence ();
1912 new_insn = emit_insn (gen_rtx_SET (VOIDmode, y, sub));
1913 seq = gen_sequence ();
1914 end_sequence ();
1916 if (recog_memoized (new_insn) < 0)
1918 /* That failed. Fall back on force_operand and hope. */
1920 start_sequence ();
1921 sub = force_operand (sub, y);
1922 if (sub != y)
1923 emit_insn (gen_move_insn (y, sub));
1924 seq = gen_sequence ();
1925 end_sequence ();
1928 #ifdef HAVE_cc0
1929 /* Don't separate setter from user. */
1930 if (PREV_INSN (insn) && sets_cc0_p (PREV_INSN (insn)))
1931 insn = PREV_INSN (insn);
1932 #endif
1934 emit_insn_before (seq, insn);
1937 return;
1939 case MEM:
1940 if (var == x)
1942 /* If we already have a replacement, use it. Otherwise,
1943 try to fix up this address in case it is invalid. */
1945 replacement = find_fixup_replacement (replacements, var);
1946 if (replacement->new)
1948 *loc = replacement->new;
1949 return;
1952 *loc = replacement->new = x = fixup_stack_1 (x, insn);
1954 /* Unless we are forcing memory to register or we changed the mode,
1955 we can leave things the way they are if the insn is valid. */
1957 INSN_CODE (insn) = -1;
1958 if (! flag_force_mem && GET_MODE (x) == promoted_mode
1959 && recog_memoized (insn) >= 0)
1960 return;
1962 *loc = replacement->new = gen_reg_rtx (promoted_mode);
1963 return;
1966 /* If X contains VAR, we need to unshare it here so that we update
1967 each occurrence separately. But all identical MEMs in one insn
1968 must be replaced with the same rtx because of the possibility of
1969 MATCH_DUPs. */
1971 if (reg_mentioned_p (var, x))
1973 replacement = find_fixup_replacement (replacements, x);
1974 if (replacement->new == 0)
1975 replacement->new = copy_most_rtx (x, var);
1977 *loc = x = replacement->new;
1978 code = GET_CODE (x);
1980 break;
1982 case REG:
1983 case CC0:
1984 case PC:
1985 case CONST_INT:
1986 case CONST:
1987 case SYMBOL_REF:
1988 case LABEL_REF:
1989 case CONST_DOUBLE:
1990 return;
1992 case SIGN_EXTRACT:
1993 case ZERO_EXTRACT:
1994 /* Note that in some cases those types of expressions are altered
1995 by optimize_bit_field, and do not survive to get here. */
1996 if (XEXP (x, 0) == var
1997 || (GET_CODE (XEXP (x, 0)) == SUBREG
1998 && SUBREG_REG (XEXP (x, 0)) == var))
2000 /* Get TEM as a valid MEM in the mode presently in the insn.
2002 We don't worry about the possibility of MATCH_DUP here; it
2003 is highly unlikely and would be tricky to handle. */
2005 tem = XEXP (x, 0);
2006 if (GET_CODE (tem) == SUBREG)
2008 if (GET_MODE_BITSIZE (GET_MODE (tem))
2009 > GET_MODE_BITSIZE (GET_MODE (var)))
2011 replacement = find_fixup_replacement (replacements, var);
2012 if (replacement->new == 0)
2013 replacement->new = gen_reg_rtx (GET_MODE (var));
2014 SUBREG_REG (tem) = replacement->new;
2016 /* The following code works only if we have a MEM, so we
2017 need to handle the subreg here. We directly substitute
2018 it assuming that a subreg must be OK here. We already
2019 scheduled a replacement to copy the mem into the
2020 subreg. */
2021 XEXP (x, 0) = tem;
2022 return;
2024 else
2025 tem = fixup_memory_subreg (tem, insn, 0);
2027 else
2028 tem = fixup_stack_1 (tem, insn);
2030 /* Unless we want to load from memory, get TEM into the proper mode
2031 for an extract from memory. This can only be done if the
2032 extract is at a constant position and length. */
2034 if (! flag_force_mem && GET_CODE (XEXP (x, 1)) == CONST_INT
2035 && GET_CODE (XEXP (x, 2)) == CONST_INT
2036 && ! mode_dependent_address_p (XEXP (tem, 0))
2037 && ! MEM_VOLATILE_P (tem))
2039 enum machine_mode wanted_mode = VOIDmode;
2040 enum machine_mode is_mode = GET_MODE (tem);
2041 HOST_WIDE_INT pos = INTVAL (XEXP (x, 2));
2043 if (GET_CODE (x) == ZERO_EXTRACT)
2045 enum machine_mode new_mode
2046 = mode_for_extraction (EP_extzv, 1);
2047 if (new_mode != MAX_MACHINE_MODE)
2048 wanted_mode = new_mode;
2050 else if (GET_CODE (x) == SIGN_EXTRACT)
2052 enum machine_mode new_mode
2053 = mode_for_extraction (EP_extv, 1);
2054 if (new_mode != MAX_MACHINE_MODE)
2055 wanted_mode = new_mode;
2058 /* If we have a narrower mode, we can do something. */
2059 if (wanted_mode != VOIDmode
2060 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2062 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2063 rtx old_pos = XEXP (x, 2);
2064 rtx newmem;
2066 /* If the bytes and bits are counted differently, we
2067 must adjust the offset. */
2068 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2069 offset = (GET_MODE_SIZE (is_mode)
2070 - GET_MODE_SIZE (wanted_mode) - offset);
2072 pos %= GET_MODE_BITSIZE (wanted_mode);
2074 newmem = adjust_address_nv (tem, wanted_mode, offset);
2076 /* Make the change and see if the insn remains valid. */
2077 INSN_CODE (insn) = -1;
2078 XEXP (x, 0) = newmem;
2079 XEXP (x, 2) = GEN_INT (pos);
2081 if (recog_memoized (insn) >= 0)
2082 return;
2084 /* Otherwise, restore old position. XEXP (x, 0) will be
2085 restored later. */
2086 XEXP (x, 2) = old_pos;
2090 /* If we get here, the bitfield extract insn can't accept a memory
2091 reference. Copy the input into a register. */
2093 tem1 = gen_reg_rtx (GET_MODE (tem));
2094 emit_insn_before (gen_move_insn (tem1, tem), insn);
2095 XEXP (x, 0) = tem1;
2096 return;
2098 break;
2100 case SUBREG:
2101 if (SUBREG_REG (x) == var)
2103 /* If this is a special SUBREG made because VAR was promoted
2104 from a wider mode, replace it with VAR and call ourself
2105 recursively, this time saying that the object previously
2106 had its current mode (by virtue of the SUBREG). */
2108 if (SUBREG_PROMOTED_VAR_P (x))
2110 *loc = var;
2111 fixup_var_refs_1 (var, GET_MODE (var), loc, insn, replacements);
2112 return;
2115 /* If this SUBREG makes VAR wider, it has become a paradoxical
2116 SUBREG with VAR in memory, but these aren't allowed at this
2117 stage of the compilation. So load VAR into a pseudo and take
2118 a SUBREG of that pseudo. */
2119 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (var)))
2121 replacement = find_fixup_replacement (replacements, var);
2122 if (replacement->new == 0)
2123 replacement->new = gen_reg_rtx (promoted_mode);
2124 SUBREG_REG (x) = replacement->new;
2125 return;
2128 /* See if we have already found a replacement for this SUBREG.
2129 If so, use it. Otherwise, make a MEM and see if the insn
2130 is recognized. If not, or if we should force MEM into a register,
2131 make a pseudo for this SUBREG. */
2132 replacement = find_fixup_replacement (replacements, x);
2133 if (replacement->new)
2135 *loc = replacement->new;
2136 return;
2139 replacement->new = *loc = fixup_memory_subreg (x, insn, 0);
2141 INSN_CODE (insn) = -1;
2142 if (! flag_force_mem && recog_memoized (insn) >= 0)
2143 return;
2145 *loc = replacement->new = gen_reg_rtx (GET_MODE (x));
2146 return;
2148 break;
2150 case SET:
2151 /* First do special simplification of bit-field references. */
2152 if (GET_CODE (SET_DEST (x)) == SIGN_EXTRACT
2153 || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2154 optimize_bit_field (x, insn, 0);
2155 if (GET_CODE (SET_SRC (x)) == SIGN_EXTRACT
2156 || GET_CODE (SET_SRC (x)) == ZERO_EXTRACT)
2157 optimize_bit_field (x, insn, 0);
2159 /* For a paradoxical SUBREG inside a ZERO_EXTRACT, load the object
2160 into a register and then store it back out. */
2161 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2162 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG
2163 && SUBREG_REG (XEXP (SET_DEST (x), 0)) == var
2164 && (GET_MODE_SIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2165 > GET_MODE_SIZE (GET_MODE (var))))
2167 replacement = find_fixup_replacement (replacements, var);
2168 if (replacement->new == 0)
2169 replacement->new = gen_reg_rtx (GET_MODE (var));
2171 SUBREG_REG (XEXP (SET_DEST (x), 0)) = replacement->new;
2172 emit_insn_after (gen_move_insn (var, replacement->new), insn);
2175 /* If SET_DEST is now a paradoxical SUBREG, put the result of this
2176 insn into a pseudo and store the low part of the pseudo into VAR. */
2177 if (GET_CODE (SET_DEST (x)) == SUBREG
2178 && SUBREG_REG (SET_DEST (x)) == var
2179 && (GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
2180 > GET_MODE_SIZE (GET_MODE (var))))
2182 SET_DEST (x) = tem = gen_reg_rtx (GET_MODE (SET_DEST (x)));
2183 emit_insn_after (gen_move_insn (var, gen_lowpart (GET_MODE (var),
2184 tem)),
2185 insn);
2186 break;
2190 rtx dest = SET_DEST (x);
2191 rtx src = SET_SRC (x);
2192 rtx outerdest = dest;
2194 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2195 || GET_CODE (dest) == SIGN_EXTRACT
2196 || GET_CODE (dest) == ZERO_EXTRACT)
2197 dest = XEXP (dest, 0);
2199 if (GET_CODE (src) == SUBREG)
2200 src = SUBREG_REG (src);
2202 /* If VAR does not appear at the top level of the SET
2203 just scan the lower levels of the tree. */
2205 if (src != var && dest != var)
2206 break;
2208 /* We will need to rerecognize this insn. */
2209 INSN_CODE (insn) = -1;
2211 if (GET_CODE (outerdest) == ZERO_EXTRACT && dest == var
2212 && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
2214 /* Since this case will return, ensure we fixup all the
2215 operands here. */
2216 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 1),
2217 insn, replacements);
2218 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 2),
2219 insn, replacements);
2220 fixup_var_refs_1 (var, promoted_mode, &SET_SRC (x),
2221 insn, replacements);
2223 tem = XEXP (outerdest, 0);
2225 /* Clean up (SUBREG:SI (MEM:mode ...) 0)
2226 that may appear inside a ZERO_EXTRACT.
2227 This was legitimate when the MEM was a REG. */
2228 if (GET_CODE (tem) == SUBREG
2229 && SUBREG_REG (tem) == var)
2230 tem = fixup_memory_subreg (tem, insn, 0);
2231 else
2232 tem = fixup_stack_1 (tem, insn);
2234 if (GET_CODE (XEXP (outerdest, 1)) == CONST_INT
2235 && GET_CODE (XEXP (outerdest, 2)) == CONST_INT
2236 && ! mode_dependent_address_p (XEXP (tem, 0))
2237 && ! MEM_VOLATILE_P (tem))
2239 enum machine_mode wanted_mode;
2240 enum machine_mode is_mode = GET_MODE (tem);
2241 HOST_WIDE_INT pos = INTVAL (XEXP (outerdest, 2));
2243 wanted_mode = mode_for_extraction (EP_insv, 0);
2245 /* If we have a narrower mode, we can do something. */
2246 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2248 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2249 rtx old_pos = XEXP (outerdest, 2);
2250 rtx newmem;
2252 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2253 offset = (GET_MODE_SIZE (is_mode)
2254 - GET_MODE_SIZE (wanted_mode) - offset);
2256 pos %= GET_MODE_BITSIZE (wanted_mode);
2258 newmem = adjust_address_nv (tem, wanted_mode, offset);
2260 /* Make the change and see if the insn remains valid. */
2261 INSN_CODE (insn) = -1;
2262 XEXP (outerdest, 0) = newmem;
2263 XEXP (outerdest, 2) = GEN_INT (pos);
2265 if (recog_memoized (insn) >= 0)
2266 return;
2268 /* Otherwise, restore old position. XEXP (x, 0) will be
2269 restored later. */
2270 XEXP (outerdest, 2) = old_pos;
2274 /* If we get here, the bit-field store doesn't allow memory
2275 or isn't located at a constant position. Load the value into
2276 a register, do the store, and put it back into memory. */
2278 tem1 = gen_reg_rtx (GET_MODE (tem));
2279 emit_insn_before (gen_move_insn (tem1, tem), insn);
2280 emit_insn_after (gen_move_insn (tem, tem1), insn);
2281 XEXP (outerdest, 0) = tem1;
2282 return;
2285 /* STRICT_LOW_PART is a no-op on memory references
2286 and it can cause combinations to be unrecognizable,
2287 so eliminate it. */
2289 if (dest == var && GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2290 SET_DEST (x) = XEXP (SET_DEST (x), 0);
2292 /* A valid insn to copy VAR into or out of a register
2293 must be left alone, to avoid an infinite loop here.
2294 If the reference to VAR is by a subreg, fix that up,
2295 since SUBREG is not valid for a memref.
2296 Also fix up the address of the stack slot.
2298 Note that we must not try to recognize the insn until
2299 after we know that we have valid addresses and no
2300 (subreg (mem ...) ...) constructs, since these interfere
2301 with determining the validity of the insn. */
2303 if ((SET_SRC (x) == var
2304 || (GET_CODE (SET_SRC (x)) == SUBREG
2305 && SUBREG_REG (SET_SRC (x)) == var))
2306 && (GET_CODE (SET_DEST (x)) == REG
2307 || (GET_CODE (SET_DEST (x)) == SUBREG
2308 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG))
2309 && GET_MODE (var) == promoted_mode
2310 && x == single_set (insn))
2312 rtx pat, last;
2314 replacement = find_fixup_replacement (replacements, SET_SRC (x));
2315 if (replacement->new)
2316 SET_SRC (x) = replacement->new;
2317 else if (GET_CODE (SET_SRC (x)) == SUBREG)
2318 SET_SRC (x) = replacement->new
2319 = fixup_memory_subreg (SET_SRC (x), insn, 0);
2320 else
2321 SET_SRC (x) = replacement->new
2322 = fixup_stack_1 (SET_SRC (x), insn);
2324 if (recog_memoized (insn) >= 0)
2325 return;
2327 /* INSN is not valid, but we know that we want to
2328 copy SET_SRC (x) to SET_DEST (x) in some way. So
2329 we generate the move and see whether it requires more
2330 than one insn. If it does, we emit those insns and
2331 delete INSN. Otherwise, we an just replace the pattern
2332 of INSN; we have already verified above that INSN has
2333 no other function that to do X. */
2335 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2336 if (GET_CODE (pat) == SEQUENCE)
2338 last = emit_insn_before (pat, insn);
2340 /* INSN might have REG_RETVAL or other important notes, so
2341 we need to store the pattern of the last insn in the
2342 sequence into INSN similarly to the normal case. LAST
2343 should not have REG_NOTES, but we allow them if INSN has
2344 no REG_NOTES. */
2345 if (REG_NOTES (last) && REG_NOTES (insn))
2346 abort ();
2347 if (REG_NOTES (last))
2348 REG_NOTES (insn) = REG_NOTES (last);
2349 PATTERN (insn) = PATTERN (last);
2351 delete_insn (last);
2353 else
2354 PATTERN (insn) = pat;
2356 return;
2359 if ((SET_DEST (x) == var
2360 || (GET_CODE (SET_DEST (x)) == SUBREG
2361 && SUBREG_REG (SET_DEST (x)) == var))
2362 && (GET_CODE (SET_SRC (x)) == REG
2363 || (GET_CODE (SET_SRC (x)) == SUBREG
2364 && GET_CODE (SUBREG_REG (SET_SRC (x))) == REG))
2365 && GET_MODE (var) == promoted_mode
2366 && x == single_set (insn))
2368 rtx pat, last;
2370 if (GET_CODE (SET_DEST (x)) == SUBREG)
2371 SET_DEST (x) = fixup_memory_subreg (SET_DEST (x), insn, 0);
2372 else
2373 SET_DEST (x) = fixup_stack_1 (SET_DEST (x), insn);
2375 if (recog_memoized (insn) >= 0)
2376 return;
2378 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2379 if (GET_CODE (pat) == SEQUENCE)
2381 last = emit_insn_before (pat, insn);
2383 /* INSN might have REG_RETVAL or other important notes, so
2384 we need to store the pattern of the last insn in the
2385 sequence into INSN similarly to the normal case. LAST
2386 should not have REG_NOTES, but we allow them if INSN has
2387 no REG_NOTES. */
2388 if (REG_NOTES (last) && REG_NOTES (insn))
2389 abort ();
2390 if (REG_NOTES (last))
2391 REG_NOTES (insn) = REG_NOTES (last);
2392 PATTERN (insn) = PATTERN (last);
2394 delete_insn (last);
2396 else
2397 PATTERN (insn) = pat;
2399 return;
2402 /* Otherwise, storing into VAR must be handled specially
2403 by storing into a temporary and copying that into VAR
2404 with a new insn after this one. Note that this case
2405 will be used when storing into a promoted scalar since
2406 the insn will now have different modes on the input
2407 and output and hence will be invalid (except for the case
2408 of setting it to a constant, which does not need any
2409 change if it is valid). We generate extra code in that case,
2410 but combine.c will eliminate it. */
2412 if (dest == var)
2414 rtx temp;
2415 rtx fixeddest = SET_DEST (x);
2417 /* STRICT_LOW_PART can be discarded, around a MEM. */
2418 if (GET_CODE (fixeddest) == STRICT_LOW_PART)
2419 fixeddest = XEXP (fixeddest, 0);
2420 /* Convert (SUBREG (MEM)) to a MEM in a changed mode. */
2421 if (GET_CODE (fixeddest) == SUBREG)
2423 fixeddest = fixup_memory_subreg (fixeddest, insn, 0);
2424 promoted_mode = GET_MODE (fixeddest);
2426 else
2427 fixeddest = fixup_stack_1 (fixeddest, insn);
2429 temp = gen_reg_rtx (promoted_mode);
2431 emit_insn_after (gen_move_insn (fixeddest,
2432 gen_lowpart (GET_MODE (fixeddest),
2433 temp)),
2434 insn);
2436 SET_DEST (x) = temp;
2440 default:
2441 break;
2444 /* Nothing special about this RTX; fix its operands. */
2446 fmt = GET_RTX_FORMAT (code);
2447 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2449 if (fmt[i] == 'e')
2450 fixup_var_refs_1 (var, promoted_mode, &XEXP (x, i), insn, replacements);
2451 else if (fmt[i] == 'E')
2453 int j;
2454 for (j = 0; j < XVECLEN (x, i); j++)
2455 fixup_var_refs_1 (var, promoted_mode, &XVECEXP (x, i, j),
2456 insn, replacements);
2461 /* Given X, an rtx of the form (SUBREG:m1 (MEM:m2 addr)),
2462 return an rtx (MEM:m1 newaddr) which is equivalent.
2463 If any insns must be emitted to compute NEWADDR, put them before INSN.
2465 UNCRITICAL nonzero means accept paradoxical subregs.
2466 This is used for subregs found inside REG_NOTES. */
2468 static rtx
2469 fixup_memory_subreg (x, insn, uncritical)
2470 rtx x;
2471 rtx insn;
2472 int uncritical;
2474 int offset = SUBREG_BYTE (x);
2475 rtx addr = XEXP (SUBREG_REG (x), 0);
2476 enum machine_mode mode = GET_MODE (x);
2477 rtx result;
2479 /* Paradoxical SUBREGs are usually invalid during RTL generation. */
2480 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
2481 && ! uncritical)
2482 abort ();
2484 if (!flag_force_addr
2485 && memory_address_p (mode, plus_constant (addr, offset)))
2486 /* Shortcut if no insns need be emitted. */
2487 return adjust_address (SUBREG_REG (x), mode, offset);
2489 start_sequence ();
2490 result = adjust_address (SUBREG_REG (x), mode, offset);
2491 emit_insn_before (gen_sequence (), insn);
2492 end_sequence ();
2493 return result;
2496 /* Do fixup_memory_subreg on all (SUBREG (MEM ...) ...) contained in X.
2497 Replace subexpressions of X in place.
2498 If X itself is a (SUBREG (MEM ...) ...), return the replacement expression.
2499 Otherwise return X, with its contents possibly altered.
2501 If any insns must be emitted to compute NEWADDR, put them before INSN.
2503 UNCRITICAL is as in fixup_memory_subreg. */
2505 static rtx
2506 walk_fixup_memory_subreg (x, insn, uncritical)
2507 rtx x;
2508 rtx insn;
2509 int uncritical;
2511 enum rtx_code code;
2512 const char *fmt;
2513 int i;
2515 if (x == 0)
2516 return 0;
2518 code = GET_CODE (x);
2520 if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
2521 return fixup_memory_subreg (x, insn, uncritical);
2523 /* Nothing special about this RTX; fix its operands. */
2525 fmt = GET_RTX_FORMAT (code);
2526 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2528 if (fmt[i] == 'e')
2529 XEXP (x, i) = walk_fixup_memory_subreg (XEXP (x, i), insn, uncritical);
2530 else if (fmt[i] == 'E')
2532 int j;
2533 for (j = 0; j < XVECLEN (x, i); j++)
2534 XVECEXP (x, i, j)
2535 = walk_fixup_memory_subreg (XVECEXP (x, i, j), insn, uncritical);
2538 return x;
2541 /* For each memory ref within X, if it refers to a stack slot
2542 with an out of range displacement, put the address in a temp register
2543 (emitting new insns before INSN to load these registers)
2544 and alter the memory ref to use that register.
2545 Replace each such MEM rtx with a copy, to avoid clobberage. */
2547 static rtx
2548 fixup_stack_1 (x, insn)
2549 rtx x;
2550 rtx insn;
2552 int i;
2553 RTX_CODE code = GET_CODE (x);
2554 const char *fmt;
2556 if (code == MEM)
2558 rtx ad = XEXP (x, 0);
2559 /* If we have address of a stack slot but it's not valid
2560 (displacement is too large), compute the sum in a register. */
2561 if (GET_CODE (ad) == PLUS
2562 && GET_CODE (XEXP (ad, 0)) == REG
2563 && ((REGNO (XEXP (ad, 0)) >= FIRST_VIRTUAL_REGISTER
2564 && REGNO (XEXP (ad, 0)) <= LAST_VIRTUAL_REGISTER)
2565 || REGNO (XEXP (ad, 0)) == FRAME_POINTER_REGNUM
2566 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2567 || REGNO (XEXP (ad, 0)) == HARD_FRAME_POINTER_REGNUM
2568 #endif
2569 || REGNO (XEXP (ad, 0)) == STACK_POINTER_REGNUM
2570 || REGNO (XEXP (ad, 0)) == ARG_POINTER_REGNUM
2571 || XEXP (ad, 0) == current_function_internal_arg_pointer)
2572 && GET_CODE (XEXP (ad, 1)) == CONST_INT)
2574 rtx temp, seq;
2575 if (memory_address_p (GET_MODE (x), ad))
2576 return x;
2578 start_sequence ();
2579 temp = copy_to_reg (ad);
2580 seq = gen_sequence ();
2581 end_sequence ();
2582 emit_insn_before (seq, insn);
2583 return replace_equiv_address (x, temp);
2585 return x;
2588 fmt = GET_RTX_FORMAT (code);
2589 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2591 if (fmt[i] == 'e')
2592 XEXP (x, i) = fixup_stack_1 (XEXP (x, i), insn);
2593 else if (fmt[i] == 'E')
2595 int j;
2596 for (j = 0; j < XVECLEN (x, i); j++)
2597 XVECEXP (x, i, j) = fixup_stack_1 (XVECEXP (x, i, j), insn);
2600 return x;
2603 /* Optimization: a bit-field instruction whose field
2604 happens to be a byte or halfword in memory
2605 can be changed to a move instruction.
2607 We call here when INSN is an insn to examine or store into a bit-field.
2608 BODY is the SET-rtx to be altered.
2610 EQUIV_MEM is the table `reg_equiv_mem' if that is available; else 0.
2611 (Currently this is called only from function.c, and EQUIV_MEM
2612 is always 0.) */
2614 static void
2615 optimize_bit_field (body, insn, equiv_mem)
2616 rtx body;
2617 rtx insn;
2618 rtx *equiv_mem;
2620 rtx bitfield;
2621 int destflag;
2622 rtx seq = 0;
2623 enum machine_mode mode;
2625 if (GET_CODE (SET_DEST (body)) == SIGN_EXTRACT
2626 || GET_CODE (SET_DEST (body)) == ZERO_EXTRACT)
2627 bitfield = SET_DEST (body), destflag = 1;
2628 else
2629 bitfield = SET_SRC (body), destflag = 0;
2631 /* First check that the field being stored has constant size and position
2632 and is in fact a byte or halfword suitably aligned. */
2634 if (GET_CODE (XEXP (bitfield, 1)) == CONST_INT
2635 && GET_CODE (XEXP (bitfield, 2)) == CONST_INT
2636 && ((mode = mode_for_size (INTVAL (XEXP (bitfield, 1)), MODE_INT, 1))
2637 != BLKmode)
2638 && INTVAL (XEXP (bitfield, 2)) % INTVAL (XEXP (bitfield, 1)) == 0)
2640 rtx memref = 0;
2642 /* Now check that the containing word is memory, not a register,
2643 and that it is safe to change the machine mode. */
2645 if (GET_CODE (XEXP (bitfield, 0)) == MEM)
2646 memref = XEXP (bitfield, 0);
2647 else if (GET_CODE (XEXP (bitfield, 0)) == REG
2648 && equiv_mem != 0)
2649 memref = equiv_mem[REGNO (XEXP (bitfield, 0))];
2650 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2651 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == MEM)
2652 memref = SUBREG_REG (XEXP (bitfield, 0));
2653 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2654 && equiv_mem != 0
2655 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == REG)
2656 memref = equiv_mem[REGNO (SUBREG_REG (XEXP (bitfield, 0)))];
2658 if (memref
2659 && ! mode_dependent_address_p (XEXP (memref, 0))
2660 && ! MEM_VOLATILE_P (memref))
2662 /* Now adjust the address, first for any subreg'ing
2663 that we are now getting rid of,
2664 and then for which byte of the word is wanted. */
2666 HOST_WIDE_INT offset = INTVAL (XEXP (bitfield, 2));
2667 rtx insns;
2669 /* Adjust OFFSET to count bits from low-address byte. */
2670 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
2671 offset = (GET_MODE_BITSIZE (GET_MODE (XEXP (bitfield, 0)))
2672 - offset - INTVAL (XEXP (bitfield, 1)));
2674 /* Adjust OFFSET to count bytes from low-address byte. */
2675 offset /= BITS_PER_UNIT;
2676 if (GET_CODE (XEXP (bitfield, 0)) == SUBREG)
2678 offset += (SUBREG_BYTE (XEXP (bitfield, 0))
2679 / UNITS_PER_WORD) * UNITS_PER_WORD;
2680 if (BYTES_BIG_ENDIAN)
2681 offset -= (MIN (UNITS_PER_WORD,
2682 GET_MODE_SIZE (GET_MODE (XEXP (bitfield, 0))))
2683 - MIN (UNITS_PER_WORD,
2684 GET_MODE_SIZE (GET_MODE (memref))));
2687 start_sequence ();
2688 memref = adjust_address (memref, mode, offset);
2689 insns = get_insns ();
2690 end_sequence ();
2691 emit_insns_before (insns, insn);
2693 /* Store this memory reference where
2694 we found the bit field reference. */
2696 if (destflag)
2698 validate_change (insn, &SET_DEST (body), memref, 1);
2699 if (! CONSTANT_ADDRESS_P (SET_SRC (body)))
2701 rtx src = SET_SRC (body);
2702 while (GET_CODE (src) == SUBREG
2703 && SUBREG_BYTE (src) == 0)
2704 src = SUBREG_REG (src);
2705 if (GET_MODE (src) != GET_MODE (memref))
2706 src = gen_lowpart (GET_MODE (memref), SET_SRC (body));
2707 validate_change (insn, &SET_SRC (body), src, 1);
2709 else if (GET_MODE (SET_SRC (body)) != VOIDmode
2710 && GET_MODE (SET_SRC (body)) != GET_MODE (memref))
2711 /* This shouldn't happen because anything that didn't have
2712 one of these modes should have got converted explicitly
2713 and then referenced through a subreg.
2714 This is so because the original bit-field was
2715 handled by agg_mode and so its tree structure had
2716 the same mode that memref now has. */
2717 abort ();
2719 else
2721 rtx dest = SET_DEST (body);
2723 while (GET_CODE (dest) == SUBREG
2724 && SUBREG_BYTE (dest) == 0
2725 && (GET_MODE_CLASS (GET_MODE (dest))
2726 == GET_MODE_CLASS (GET_MODE (SUBREG_REG (dest))))
2727 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
2728 <= UNITS_PER_WORD))
2729 dest = SUBREG_REG (dest);
2731 validate_change (insn, &SET_DEST (body), dest, 1);
2733 if (GET_MODE (dest) == GET_MODE (memref))
2734 validate_change (insn, &SET_SRC (body), memref, 1);
2735 else
2737 /* Convert the mem ref to the destination mode. */
2738 rtx newreg = gen_reg_rtx (GET_MODE (dest));
2740 start_sequence ();
2741 convert_move (newreg, memref,
2742 GET_CODE (SET_SRC (body)) == ZERO_EXTRACT);
2743 seq = get_insns ();
2744 end_sequence ();
2746 validate_change (insn, &SET_SRC (body), newreg, 1);
2750 /* See if we can convert this extraction or insertion into
2751 a simple move insn. We might not be able to do so if this
2752 was, for example, part of a PARALLEL.
2754 If we succeed, write out any needed conversions. If we fail,
2755 it is hard to guess why we failed, so don't do anything
2756 special; just let the optimization be suppressed. */
2758 if (apply_change_group () && seq)
2759 emit_insns_before (seq, insn);
2764 /* These routines are responsible for converting virtual register references
2765 to the actual hard register references once RTL generation is complete.
2767 The following four variables are used for communication between the
2768 routines. They contain the offsets of the virtual registers from their
2769 respective hard registers. */
2771 static int in_arg_offset;
2772 static int var_offset;
2773 static int dynamic_offset;
2774 static int out_arg_offset;
2775 static int cfa_offset;
2777 /* In most machines, the stack pointer register is equivalent to the bottom
2778 of the stack. */
2780 #ifndef STACK_POINTER_OFFSET
2781 #define STACK_POINTER_OFFSET 0
2782 #endif
2784 /* If not defined, pick an appropriate default for the offset of dynamically
2785 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
2786 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
2788 #ifndef STACK_DYNAMIC_OFFSET
2790 /* The bottom of the stack points to the actual arguments. If
2791 REG_PARM_STACK_SPACE is defined, this includes the space for the register
2792 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
2793 stack space for register parameters is not pushed by the caller, but
2794 rather part of the fixed stack areas and hence not included in
2795 `current_function_outgoing_args_size'. Nevertheless, we must allow
2796 for it when allocating stack dynamic objects. */
2798 #if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
2799 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2800 ((ACCUMULATE_OUTGOING_ARGS \
2801 ? (current_function_outgoing_args_size + REG_PARM_STACK_SPACE (FNDECL)) : 0)\
2802 + (STACK_POINTER_OFFSET)) \
2804 #else
2805 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2806 ((ACCUMULATE_OUTGOING_ARGS ? current_function_outgoing_args_size : 0) \
2807 + (STACK_POINTER_OFFSET))
2808 #endif
2809 #endif
2811 /* On most machines, the CFA coincides with the first incoming parm. */
2813 #ifndef ARG_POINTER_CFA_OFFSET
2814 #define ARG_POINTER_CFA_OFFSET(FNDECL) FIRST_PARM_OFFSET (FNDECL)
2815 #endif
2817 /* Build up a (MEM (ADDRESSOF (REG))) rtx for a register REG that just had its
2818 address taken. DECL is the decl or SAVE_EXPR for the object stored in the
2819 register, for later use if we do need to force REG into the stack. REG is
2820 overwritten by the MEM like in put_reg_into_stack. */
2823 gen_mem_addressof (reg, decl)
2824 rtx reg;
2825 tree decl;
2827 rtx r = gen_rtx_ADDRESSOF (Pmode, gen_reg_rtx (GET_MODE (reg)),
2828 REGNO (reg), decl);
2830 /* Calculate this before we start messing with decl's RTL. */
2831 HOST_WIDE_INT set = decl ? get_alias_set (decl) : 0;
2833 /* If the original REG was a user-variable, then so is the REG whose
2834 address is being taken. Likewise for unchanging. */
2835 REG_USERVAR_P (XEXP (r, 0)) = REG_USERVAR_P (reg);
2836 RTX_UNCHANGING_P (XEXP (r, 0)) = RTX_UNCHANGING_P (reg);
2838 PUT_CODE (reg, MEM);
2839 MEM_ATTRS (reg) = 0;
2840 XEXP (reg, 0) = r;
2842 if (decl)
2844 tree type = TREE_TYPE (decl);
2845 enum machine_mode decl_mode
2846 = (DECL_P (decl) ? DECL_MODE (decl) : TYPE_MODE (TREE_TYPE (decl)));
2847 rtx decl_rtl = (TREE_CODE (decl) == SAVE_EXPR ? SAVE_EXPR_RTL (decl)
2848 : DECL_RTL_IF_SET (decl));
2850 PUT_MODE (reg, decl_mode);
2852 /* Clear DECL_RTL momentarily so functions below will work
2853 properly, then set it again. */
2854 if (DECL_P (decl) && decl_rtl == reg)
2855 SET_DECL_RTL (decl, 0);
2857 set_mem_attributes (reg, decl, 1);
2858 set_mem_alias_set (reg, set);
2860 if (DECL_P (decl) && decl_rtl == reg)
2861 SET_DECL_RTL (decl, reg);
2863 if (TREE_USED (decl) || (DECL_P (decl) && DECL_INITIAL (decl) != 0))
2864 fixup_var_refs (reg, GET_MODE (reg), TREE_UNSIGNED (type), 0);
2866 else
2867 fixup_var_refs (reg, GET_MODE (reg), 0, 0);
2869 return reg;
2872 /* If DECL has an RTL that is an ADDRESSOF rtx, put it into the stack. */
2874 void
2875 flush_addressof (decl)
2876 tree decl;
2878 if ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL)
2879 && DECL_RTL (decl) != 0
2880 && GET_CODE (DECL_RTL (decl)) == MEM
2881 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF
2882 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == REG)
2883 put_addressof_into_stack (XEXP (DECL_RTL (decl), 0), 0);
2886 /* Force the register pointed to by R, an ADDRESSOF rtx, into the stack. */
2888 static void
2889 put_addressof_into_stack (r, ht)
2890 rtx r;
2891 struct hash_table *ht;
2893 tree decl, type;
2894 int volatile_p, used_p;
2896 rtx reg = XEXP (r, 0);
2898 if (GET_CODE (reg) != REG)
2899 abort ();
2901 decl = ADDRESSOF_DECL (r);
2902 if (decl)
2904 type = TREE_TYPE (decl);
2905 volatile_p = (TREE_CODE (decl) != SAVE_EXPR
2906 && TREE_THIS_VOLATILE (decl));
2907 used_p = (TREE_USED (decl)
2908 || (DECL_P (decl) && DECL_INITIAL (decl) != 0));
2910 else
2912 type = NULL_TREE;
2913 volatile_p = 0;
2914 used_p = 1;
2917 put_reg_into_stack (0, reg, type, GET_MODE (reg), GET_MODE (reg),
2918 volatile_p, ADDRESSOF_REGNO (r), used_p, ht);
2921 /* List of replacements made below in purge_addressof_1 when creating
2922 bitfield insertions. */
2923 static rtx purge_bitfield_addressof_replacements;
2925 /* List of replacements made below in purge_addressof_1 for patterns
2926 (MEM (ADDRESSOF (REG ...))). The key of the list entry is the
2927 corresponding (ADDRESSOF (REG ...)) and value is a substitution for
2928 the all pattern. List PURGE_BITFIELD_ADDRESSOF_REPLACEMENTS is not
2929 enough in complex cases, e.g. when some field values can be
2930 extracted by usage MEM with narrower mode. */
2931 static rtx purge_addressof_replacements;
2933 /* Helper function for purge_addressof. See if the rtx expression at *LOC
2934 in INSN needs to be changed. If FORCE, always put any ADDRESSOFs into
2935 the stack. If the function returns FALSE then the replacement could not
2936 be made. */
2938 static bool
2939 purge_addressof_1 (loc, insn, force, store, ht)
2940 rtx *loc;
2941 rtx insn;
2942 int force, store;
2943 struct hash_table *ht;
2945 rtx x;
2946 RTX_CODE code;
2947 int i, j;
2948 const char *fmt;
2949 bool result = true;
2951 /* Re-start here to avoid recursion in common cases. */
2952 restart:
2954 x = *loc;
2955 if (x == 0)
2956 return true;
2958 code = GET_CODE (x);
2960 /* If we don't return in any of the cases below, we will recurse inside
2961 the RTX, which will normally result in any ADDRESSOF being forced into
2962 memory. */
2963 if (code == SET)
2965 result = purge_addressof_1 (&SET_DEST (x), insn, force, 1, ht);
2966 result &= purge_addressof_1 (&SET_SRC (x), insn, force, 0, ht);
2967 return result;
2969 else if (code == ADDRESSOF)
2971 rtx sub, insns;
2973 if (GET_CODE (XEXP (x, 0)) != MEM)
2975 put_addressof_into_stack (x, ht);
2976 return true;
2979 /* We must create a copy of the rtx because it was created by
2980 overwriting a REG rtx which is always shared. */
2981 sub = copy_rtx (XEXP (XEXP (x, 0), 0));
2982 if (validate_change (insn, loc, sub, 0)
2983 || validate_replace_rtx (x, sub, insn))
2984 return true;
2986 start_sequence ();
2987 sub = force_operand (sub, NULL_RTX);
2988 if (! validate_change (insn, loc, sub, 0)
2989 && ! validate_replace_rtx (x, sub, insn))
2990 abort ();
2992 insns = gen_sequence ();
2993 end_sequence ();
2994 emit_insn_before (insns, insn);
2995 return true;
2998 else if (code == MEM && GET_CODE (XEXP (x, 0)) == ADDRESSOF && ! force)
3000 rtx sub = XEXP (XEXP (x, 0), 0);
3002 if (GET_CODE (sub) == MEM)
3003 sub = adjust_address_nv (sub, GET_MODE (x), 0);
3004 else if (GET_CODE (sub) == REG
3005 && (MEM_VOLATILE_P (x) || GET_MODE (x) == BLKmode))
3007 else if (GET_CODE (sub) == REG && GET_MODE (x) != GET_MODE (sub))
3009 int size_x, size_sub;
3011 if (!insn)
3013 /* When processing REG_NOTES look at the list of
3014 replacements done on the insn to find the register that X
3015 was replaced by. */
3016 rtx tem;
3018 for (tem = purge_bitfield_addressof_replacements;
3019 tem != NULL_RTX;
3020 tem = XEXP (XEXP (tem, 1), 1))
3021 if (rtx_equal_p (x, XEXP (tem, 0)))
3023 *loc = XEXP (XEXP (tem, 1), 0);
3024 return true;
3027 /* See comment for purge_addressof_replacements. */
3028 for (tem = purge_addressof_replacements;
3029 tem != NULL_RTX;
3030 tem = XEXP (XEXP (tem, 1), 1))
3031 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3033 rtx z = XEXP (XEXP (tem, 1), 0);
3035 if (GET_MODE (x) == GET_MODE (z)
3036 || (GET_CODE (XEXP (XEXP (tem, 1), 0)) != REG
3037 && GET_CODE (XEXP (XEXP (tem, 1), 0)) != SUBREG))
3038 abort ();
3040 /* It can happen that the note may speak of things
3041 in a wider (or just different) mode than the
3042 code did. This is especially true of
3043 REG_RETVAL. */
3045 if (GET_CODE (z) == SUBREG && SUBREG_BYTE (z) == 0)
3046 z = SUBREG_REG (z);
3048 if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3049 && (GET_MODE_SIZE (GET_MODE (x))
3050 > GET_MODE_SIZE (GET_MODE (z))))
3052 /* This can occur as a result in invalid
3053 pointer casts, e.g. float f; ...
3054 *(long long int *)&f.
3055 ??? We could emit a warning here, but
3056 without a line number that wouldn't be
3057 very helpful. */
3058 z = gen_rtx_SUBREG (GET_MODE (x), z, 0);
3060 else
3061 z = gen_lowpart (GET_MODE (x), z);
3063 *loc = z;
3064 return true;
3067 /* Sometimes we may not be able to find the replacement. For
3068 example when the original insn was a MEM in a wider mode,
3069 and the note is part of a sign extension of a narrowed
3070 version of that MEM. Gcc testcase compile/990829-1.c can
3071 generate an example of this situation. Rather than complain
3072 we return false, which will prompt our caller to remove the
3073 offending note. */
3074 return false;
3077 size_x = GET_MODE_BITSIZE (GET_MODE (x));
3078 size_sub = GET_MODE_BITSIZE (GET_MODE (sub));
3080 /* Don't even consider working with paradoxical subregs,
3081 or the moral equivalent seen here. */
3082 if (size_x <= size_sub
3083 && int_mode_for_mode (GET_MODE (sub)) != BLKmode)
3085 /* Do a bitfield insertion to mirror what would happen
3086 in memory. */
3088 rtx val, seq;
3090 if (store)
3092 rtx p = PREV_INSN (insn);
3094 start_sequence ();
3095 val = gen_reg_rtx (GET_MODE (x));
3096 if (! validate_change (insn, loc, val, 0))
3098 /* Discard the current sequence and put the
3099 ADDRESSOF on stack. */
3100 end_sequence ();
3101 goto give_up;
3103 seq = gen_sequence ();
3104 end_sequence ();
3105 emit_insn_before (seq, insn);
3106 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3107 insn, ht);
3109 start_sequence ();
3110 store_bit_field (sub, size_x, 0, GET_MODE (x),
3111 val, GET_MODE_SIZE (GET_MODE (sub)));
3113 /* Make sure to unshare any shared rtl that store_bit_field
3114 might have created. */
3115 unshare_all_rtl_again (get_insns ());
3117 seq = gen_sequence ();
3118 end_sequence ();
3119 p = emit_insn_after (seq, insn);
3120 if (NEXT_INSN (insn))
3121 compute_insns_for_mem (NEXT_INSN (insn),
3122 p ? NEXT_INSN (p) : NULL_RTX,
3123 ht);
3125 else
3127 rtx p = PREV_INSN (insn);
3129 start_sequence ();
3130 val = extract_bit_field (sub, size_x, 0, 1, NULL_RTX,
3131 GET_MODE (x), GET_MODE (x),
3132 GET_MODE_SIZE (GET_MODE (sub)));
3134 if (! validate_change (insn, loc, val, 0))
3136 /* Discard the current sequence and put the
3137 ADDRESSOF on stack. */
3138 end_sequence ();
3139 goto give_up;
3142 seq = gen_sequence ();
3143 end_sequence ();
3144 emit_insn_before (seq, insn);
3145 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3146 insn, ht);
3149 /* Remember the replacement so that the same one can be done
3150 on the REG_NOTES. */
3151 purge_bitfield_addressof_replacements
3152 = gen_rtx_EXPR_LIST (VOIDmode, x,
3153 gen_rtx_EXPR_LIST
3154 (VOIDmode, val,
3155 purge_bitfield_addressof_replacements));
3157 /* We replaced with a reg -- all done. */
3158 return true;
3162 else if (validate_change (insn, loc, sub, 0))
3164 /* Remember the replacement so that the same one can be done
3165 on the REG_NOTES. */
3166 if (GET_CODE (sub) == REG || GET_CODE (sub) == SUBREG)
3168 rtx tem;
3170 for (tem = purge_addressof_replacements;
3171 tem != NULL_RTX;
3172 tem = XEXP (XEXP (tem, 1), 1))
3173 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3175 XEXP (XEXP (tem, 1), 0) = sub;
3176 return true;
3178 purge_addressof_replacements
3179 = gen_rtx (EXPR_LIST, VOIDmode, XEXP (x, 0),
3180 gen_rtx_EXPR_LIST (VOIDmode, sub,
3181 purge_addressof_replacements));
3182 return true;
3184 goto restart;
3188 give_up:
3189 /* Scan all subexpressions. */
3190 fmt = GET_RTX_FORMAT (code);
3191 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3193 if (*fmt == 'e')
3194 result &= purge_addressof_1 (&XEXP (x, i), insn, force, 0, ht);
3195 else if (*fmt == 'E')
3196 for (j = 0; j < XVECLEN (x, i); j++)
3197 result &= purge_addressof_1 (&XVECEXP (x, i, j), insn, force, 0, ht);
3200 return result;
3203 /* Return a new hash table entry in HT. */
3205 static struct hash_entry *
3206 insns_for_mem_newfunc (he, ht, k)
3207 struct hash_entry *he;
3208 struct hash_table *ht;
3209 hash_table_key k ATTRIBUTE_UNUSED;
3211 struct insns_for_mem_entry *ifmhe;
3212 if (he)
3213 return he;
3215 ifmhe = ((struct insns_for_mem_entry *)
3216 hash_allocate (ht, sizeof (struct insns_for_mem_entry)));
3217 ifmhe->insns = NULL_RTX;
3219 return &ifmhe->he;
3222 /* Return a hash value for K, a REG. */
3224 static unsigned long
3225 insns_for_mem_hash (k)
3226 hash_table_key k;
3228 /* K is really a RTX. Just use the address as the hash value. */
3229 return (unsigned long) k;
3232 /* Return non-zero if K1 and K2 (two REGs) are the same. */
3234 static bool
3235 insns_for_mem_comp (k1, k2)
3236 hash_table_key k1;
3237 hash_table_key k2;
3239 return k1 == k2;
3242 struct insns_for_mem_walk_info
3244 /* The hash table that we are using to record which INSNs use which
3245 MEMs. */
3246 struct hash_table *ht;
3248 /* The INSN we are currently processing. */
3249 rtx insn;
3251 /* Zero if we are walking to find ADDRESSOFs, one if we are walking
3252 to find the insns that use the REGs in the ADDRESSOFs. */
3253 int pass;
3256 /* Called from compute_insns_for_mem via for_each_rtx. If R is a REG
3257 that might be used in an ADDRESSOF expression, record this INSN in
3258 the hash table given by DATA (which is really a pointer to an
3259 insns_for_mem_walk_info structure). */
3261 static int
3262 insns_for_mem_walk (r, data)
3263 rtx *r;
3264 void *data;
3266 struct insns_for_mem_walk_info *ifmwi
3267 = (struct insns_for_mem_walk_info *) data;
3269 if (ifmwi->pass == 0 && *r && GET_CODE (*r) == ADDRESSOF
3270 && GET_CODE (XEXP (*r, 0)) == REG)
3271 hash_lookup (ifmwi->ht, XEXP (*r, 0), /*create=*/1, /*copy=*/0);
3272 else if (ifmwi->pass == 1 && *r && GET_CODE (*r) == REG)
3274 /* Lookup this MEM in the hashtable, creating it if necessary. */
3275 struct insns_for_mem_entry *ifme
3276 = (struct insns_for_mem_entry *) hash_lookup (ifmwi->ht,
3278 /*create=*/0,
3279 /*copy=*/0);
3281 /* If we have not already recorded this INSN, do so now. Since
3282 we process the INSNs in order, we know that if we have
3283 recorded it it must be at the front of the list. */
3284 if (ifme && (!ifme->insns || XEXP (ifme->insns, 0) != ifmwi->insn))
3285 ifme->insns = gen_rtx_EXPR_LIST (VOIDmode, ifmwi->insn,
3286 ifme->insns);
3289 return 0;
3292 /* Walk the INSNS, until we reach LAST_INSN, recording which INSNs use
3293 which REGs in HT. */
3295 static void
3296 compute_insns_for_mem (insns, last_insn, ht)
3297 rtx insns;
3298 rtx last_insn;
3299 struct hash_table *ht;
3301 rtx insn;
3302 struct insns_for_mem_walk_info ifmwi;
3303 ifmwi.ht = ht;
3305 for (ifmwi.pass = 0; ifmwi.pass < 2; ++ifmwi.pass)
3306 for (insn = insns; insn != last_insn; insn = NEXT_INSN (insn))
3307 if (INSN_P (insn))
3309 ifmwi.insn = insn;
3310 for_each_rtx (&insn, insns_for_mem_walk, &ifmwi);
3314 /* Helper function for purge_addressof called through for_each_rtx.
3315 Returns true iff the rtl is an ADDRESSOF. */
3317 static int
3318 is_addressof (rtl, data)
3319 rtx *rtl;
3320 void *data ATTRIBUTE_UNUSED;
3322 return GET_CODE (*rtl) == ADDRESSOF;
3325 /* Eliminate all occurrences of ADDRESSOF from INSNS. Elide any remaining
3326 (MEM (ADDRESSOF)) patterns, and force any needed registers into the
3327 stack. */
3329 void
3330 purge_addressof (insns)
3331 rtx insns;
3333 rtx insn;
3334 struct hash_table ht;
3336 /* When we actually purge ADDRESSOFs, we turn REGs into MEMs. That
3337 requires a fixup pass over the instruction stream to correct
3338 INSNs that depended on the REG being a REG, and not a MEM. But,
3339 these fixup passes are slow. Furthermore, most MEMs are not
3340 mentioned in very many instructions. So, we speed up the process
3341 by pre-calculating which REGs occur in which INSNs; that allows
3342 us to perform the fixup passes much more quickly. */
3343 hash_table_init (&ht,
3344 insns_for_mem_newfunc,
3345 insns_for_mem_hash,
3346 insns_for_mem_comp);
3347 compute_insns_for_mem (insns, NULL_RTX, &ht);
3349 for (insn = insns; insn; insn = NEXT_INSN (insn))
3350 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3351 || GET_CODE (insn) == CALL_INSN)
3353 if (! purge_addressof_1 (&PATTERN (insn), insn,
3354 asm_noperands (PATTERN (insn)) > 0, 0, &ht))
3355 /* If we could not replace the ADDRESSOFs in the insn,
3356 something is wrong. */
3357 abort ();
3359 if (! purge_addressof_1 (&REG_NOTES (insn), NULL_RTX, 0, 0, &ht))
3361 /* If we could not replace the ADDRESSOFs in the insn's notes,
3362 we can just remove the offending notes instead. */
3363 rtx note;
3365 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3367 /* If we find a REG_RETVAL note then the insn is a libcall.
3368 Such insns must have REG_EQUAL notes as well, in order
3369 for later passes of the compiler to work. So it is not
3370 safe to delete the notes here, and instead we abort. */
3371 if (REG_NOTE_KIND (note) == REG_RETVAL)
3372 abort ();
3373 if (for_each_rtx (&note, is_addressof, NULL))
3374 remove_note (insn, note);
3379 /* Clean up. */
3380 hash_table_free (&ht);
3381 purge_bitfield_addressof_replacements = 0;
3382 purge_addressof_replacements = 0;
3384 /* REGs are shared. purge_addressof will destructively replace a REG
3385 with a MEM, which creates shared MEMs.
3387 Unfortunately, the children of put_reg_into_stack assume that MEMs
3388 referring to the same stack slot are shared (fixup_var_refs and
3389 the associated hash table code).
3391 So, we have to do another unsharing pass after we have flushed any
3392 REGs that had their address taken into the stack.
3394 It may be worth tracking whether or not we converted any REGs into
3395 MEMs to avoid this overhead when it is not needed. */
3396 unshare_all_rtl_again (get_insns ());
3399 /* Convert a SET of a hard subreg to a set of the appropriate hard
3400 register. A subroutine of purge_hard_subreg_sets. */
3402 static void
3403 purge_single_hard_subreg_set (pattern)
3404 rtx pattern;
3406 rtx reg = SET_DEST (pattern);
3407 enum machine_mode mode = GET_MODE (SET_DEST (pattern));
3408 int offset = 0;
3410 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG
3411 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
3413 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
3414 GET_MODE (SUBREG_REG (reg)),
3415 SUBREG_BYTE (reg),
3416 GET_MODE (reg));
3417 reg = SUBREG_REG (reg);
3421 if (GET_CODE (reg) == REG && REGNO (reg) < FIRST_PSEUDO_REGISTER)
3423 reg = gen_rtx_REG (mode, REGNO (reg) + offset);
3424 SET_DEST (pattern) = reg;
3428 /* Eliminate all occurrences of SETs of hard subregs from INSNS. The
3429 only such SETs that we expect to see are those left in because
3430 integrate can't handle sets of parts of a return value register.
3432 We don't use alter_subreg because we only want to eliminate subregs
3433 of hard registers. */
3435 void
3436 purge_hard_subreg_sets (insn)
3437 rtx insn;
3439 for (; insn; insn = NEXT_INSN (insn))
3441 if (INSN_P (insn))
3443 rtx pattern = PATTERN (insn);
3444 switch (GET_CODE (pattern))
3446 case SET:
3447 if (GET_CODE (SET_DEST (pattern)) == SUBREG)
3448 purge_single_hard_subreg_set (pattern);
3449 break;
3450 case PARALLEL:
3452 int j;
3453 for (j = XVECLEN (pattern, 0) - 1; j >= 0; j--)
3455 rtx inner_pattern = XVECEXP (pattern, 0, j);
3456 if (GET_CODE (inner_pattern) == SET
3457 && GET_CODE (SET_DEST (inner_pattern)) == SUBREG)
3458 purge_single_hard_subreg_set (inner_pattern);
3461 break;
3462 default:
3463 break;
3469 /* Pass through the INSNS of function FNDECL and convert virtual register
3470 references to hard register references. */
3472 void
3473 instantiate_virtual_regs (fndecl, insns)
3474 tree fndecl;
3475 rtx insns;
3477 rtx insn;
3478 unsigned int i;
3480 /* Compute the offsets to use for this function. */
3481 in_arg_offset = FIRST_PARM_OFFSET (fndecl);
3482 var_offset = STARTING_FRAME_OFFSET;
3483 dynamic_offset = STACK_DYNAMIC_OFFSET (fndecl);
3484 out_arg_offset = STACK_POINTER_OFFSET;
3485 cfa_offset = ARG_POINTER_CFA_OFFSET (fndecl);
3487 /* Scan all variables and parameters of this function. For each that is
3488 in memory, instantiate all virtual registers if the result is a valid
3489 address. If not, we do it later. That will handle most uses of virtual
3490 regs on many machines. */
3491 instantiate_decls (fndecl, 1);
3493 /* Initialize recognition, indicating that volatile is OK. */
3494 init_recog ();
3496 /* Scan through all the insns, instantiating every virtual register still
3497 present. */
3498 for (insn = insns; insn; insn = NEXT_INSN (insn))
3499 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3500 || GET_CODE (insn) == CALL_INSN)
3502 instantiate_virtual_regs_1 (&PATTERN (insn), insn, 1);
3503 instantiate_virtual_regs_1 (&REG_NOTES (insn), NULL_RTX, 0);
3504 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
3505 if (GET_CODE (insn) == CALL_INSN)
3506 instantiate_virtual_regs_1 (&CALL_INSN_FUNCTION_USAGE (insn),
3507 NULL_RTX, 0);
3510 /* Instantiate the stack slots for the parm registers, for later use in
3511 addressof elimination. */
3512 for (i = 0; i < max_parm_reg; ++i)
3513 if (parm_reg_stack_loc[i])
3514 instantiate_virtual_regs_1 (&parm_reg_stack_loc[i], NULL_RTX, 0);
3516 /* Now instantiate the remaining register equivalences for debugging info.
3517 These will not be valid addresses. */
3518 instantiate_decls (fndecl, 0);
3520 /* Indicate that, from now on, assign_stack_local should use
3521 frame_pointer_rtx. */
3522 virtuals_instantiated = 1;
3525 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
3526 all virtual registers in their DECL_RTL's.
3528 If VALID_ONLY, do this only if the resulting address is still valid.
3529 Otherwise, always do it. */
3531 static void
3532 instantiate_decls (fndecl, valid_only)
3533 tree fndecl;
3534 int valid_only;
3536 tree decl;
3538 /* Process all parameters of the function. */
3539 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
3541 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
3542 HOST_WIDE_INT size_rtl;
3544 instantiate_decl (DECL_RTL (decl), size, valid_only);
3546 /* If the parameter was promoted, then the incoming RTL mode may be
3547 larger than the declared type size. We must use the larger of
3548 the two sizes. */
3549 size_rtl = GET_MODE_SIZE (GET_MODE (DECL_INCOMING_RTL (decl)));
3550 size = MAX (size_rtl, size);
3551 instantiate_decl (DECL_INCOMING_RTL (decl), size, valid_only);
3554 /* Now process all variables defined in the function or its subblocks. */
3555 instantiate_decls_1 (DECL_INITIAL (fndecl), valid_only);
3558 /* Subroutine of instantiate_decls: Process all decls in the given
3559 BLOCK node and all its subblocks. */
3561 static void
3562 instantiate_decls_1 (let, valid_only)
3563 tree let;
3564 int valid_only;
3566 tree t;
3568 for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
3569 if (DECL_RTL_SET_P (t))
3570 instantiate_decl (DECL_RTL (t),
3571 int_size_in_bytes (TREE_TYPE (t)),
3572 valid_only);
3574 /* Process all subblocks. */
3575 for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
3576 instantiate_decls_1 (t, valid_only);
3579 /* Subroutine of the preceding procedures: Given RTL representing a
3580 decl and the size of the object, do any instantiation required.
3582 If VALID_ONLY is non-zero, it means that the RTL should only be
3583 changed if the new address is valid. */
3585 static void
3586 instantiate_decl (x, size, valid_only)
3587 rtx x;
3588 HOST_WIDE_INT size;
3589 int valid_only;
3591 enum machine_mode mode;
3592 rtx addr;
3594 /* If this is not a MEM, no need to do anything. Similarly if the
3595 address is a constant or a register that is not a virtual register. */
3597 if (x == 0 || GET_CODE (x) != MEM)
3598 return;
3600 addr = XEXP (x, 0);
3601 if (CONSTANT_P (addr)
3602 || (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == REG)
3603 || (GET_CODE (addr) == REG
3604 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
3605 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
3606 return;
3608 /* If we should only do this if the address is valid, copy the address.
3609 We need to do this so we can undo any changes that might make the
3610 address invalid. This copy is unfortunate, but probably can't be
3611 avoided. */
3613 if (valid_only)
3614 addr = copy_rtx (addr);
3616 instantiate_virtual_regs_1 (&addr, NULL_RTX, 0);
3618 if (valid_only && size >= 0)
3620 unsigned HOST_WIDE_INT decl_size = size;
3622 /* Now verify that the resulting address is valid for every integer or
3623 floating-point mode up to and including SIZE bytes long. We do this
3624 since the object might be accessed in any mode and frame addresses
3625 are shared. */
3627 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3628 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3629 mode = GET_MODE_WIDER_MODE (mode))
3630 if (! memory_address_p (mode, addr))
3631 return;
3633 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
3634 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3635 mode = GET_MODE_WIDER_MODE (mode))
3636 if (! memory_address_p (mode, addr))
3637 return;
3640 /* Put back the address now that we have updated it and we either know
3641 it is valid or we don't care whether it is valid. */
3643 XEXP (x, 0) = addr;
3646 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
3647 is a virtual register, return the equivalent hard register and set the
3648 offset indirectly through the pointer. Otherwise, return 0. */
3650 static rtx
3651 instantiate_new_reg (x, poffset)
3652 rtx x;
3653 HOST_WIDE_INT *poffset;
3655 rtx new;
3656 HOST_WIDE_INT offset;
3658 if (x == virtual_incoming_args_rtx)
3659 new = arg_pointer_rtx, offset = in_arg_offset;
3660 else if (x == virtual_stack_vars_rtx)
3661 new = frame_pointer_rtx, offset = var_offset;
3662 else if (x == virtual_stack_dynamic_rtx)
3663 new = stack_pointer_rtx, offset = dynamic_offset;
3664 else if (x == virtual_outgoing_args_rtx)
3665 new = stack_pointer_rtx, offset = out_arg_offset;
3666 else if (x == virtual_cfa_rtx)
3667 new = arg_pointer_rtx, offset = cfa_offset;
3668 else
3669 return 0;
3671 *poffset = offset;
3672 return new;
3675 /* Given a pointer to a piece of rtx and an optional pointer to the
3676 containing object, instantiate any virtual registers present in it.
3678 If EXTRA_INSNS, we always do the replacement and generate
3679 any extra insns before OBJECT. If it zero, we do nothing if replacement
3680 is not valid.
3682 Return 1 if we either had nothing to do or if we were able to do the
3683 needed replacement. Return 0 otherwise; we only return zero if
3684 EXTRA_INSNS is zero.
3686 We first try some simple transformations to avoid the creation of extra
3687 pseudos. */
3689 static int
3690 instantiate_virtual_regs_1 (loc, object, extra_insns)
3691 rtx *loc;
3692 rtx object;
3693 int extra_insns;
3695 rtx x;
3696 RTX_CODE code;
3697 rtx new = 0;
3698 HOST_WIDE_INT offset = 0;
3699 rtx temp;
3700 rtx seq;
3701 int i, j;
3702 const char *fmt;
3704 /* Re-start here to avoid recursion in common cases. */
3705 restart:
3707 x = *loc;
3708 if (x == 0)
3709 return 1;
3711 code = GET_CODE (x);
3713 /* Check for some special cases. */
3714 switch (code)
3716 case CONST_INT:
3717 case CONST_DOUBLE:
3718 case CONST:
3719 case SYMBOL_REF:
3720 case CODE_LABEL:
3721 case PC:
3722 case CC0:
3723 case ASM_INPUT:
3724 case ADDR_VEC:
3725 case ADDR_DIFF_VEC:
3726 case RETURN:
3727 return 1;
3729 case SET:
3730 /* We are allowed to set the virtual registers. This means that
3731 the actual register should receive the source minus the
3732 appropriate offset. This is used, for example, in the handling
3733 of non-local gotos. */
3734 if ((new = instantiate_new_reg (SET_DEST (x), &offset)) != 0)
3736 rtx src = SET_SRC (x);
3738 /* We are setting the register, not using it, so the relevant
3739 offset is the negative of the offset to use were we using
3740 the register. */
3741 offset = - offset;
3742 instantiate_virtual_regs_1 (&src, NULL_RTX, 0);
3744 /* The only valid sources here are PLUS or REG. Just do
3745 the simplest possible thing to handle them. */
3746 if (GET_CODE (src) != REG && GET_CODE (src) != PLUS)
3747 abort ();
3749 start_sequence ();
3750 if (GET_CODE (src) != REG)
3751 temp = force_operand (src, NULL_RTX);
3752 else
3753 temp = src;
3754 temp = force_operand (plus_constant (temp, offset), NULL_RTX);
3755 seq = get_insns ();
3756 end_sequence ();
3758 emit_insns_before (seq, object);
3759 SET_DEST (x) = new;
3761 if (! validate_change (object, &SET_SRC (x), temp, 0)
3762 || ! extra_insns)
3763 abort ();
3765 return 1;
3768 instantiate_virtual_regs_1 (&SET_DEST (x), object, extra_insns);
3769 loc = &SET_SRC (x);
3770 goto restart;
3772 case PLUS:
3773 /* Handle special case of virtual register plus constant. */
3774 if (CONSTANT_P (XEXP (x, 1)))
3776 rtx old, new_offset;
3778 /* Check for (plus (plus VIRT foo) (const_int)) first. */
3779 if (GET_CODE (XEXP (x, 0)) == PLUS)
3781 if ((new = instantiate_new_reg (XEXP (XEXP (x, 0), 0), &offset)))
3783 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 1), object,
3784 extra_insns);
3785 new = gen_rtx_PLUS (Pmode, new, XEXP (XEXP (x, 0), 1));
3787 else
3789 loc = &XEXP (x, 0);
3790 goto restart;
3794 #ifdef POINTERS_EXTEND_UNSIGNED
3795 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
3796 we can commute the PLUS and SUBREG because pointers into the
3797 frame are well-behaved. */
3798 else if (GET_CODE (XEXP (x, 0)) == SUBREG && GET_MODE (x) == ptr_mode
3799 && GET_CODE (XEXP (x, 1)) == CONST_INT
3800 && 0 != (new
3801 = instantiate_new_reg (SUBREG_REG (XEXP (x, 0)),
3802 &offset))
3803 && validate_change (object, loc,
3804 plus_constant (gen_lowpart (ptr_mode,
3805 new),
3806 offset
3807 + INTVAL (XEXP (x, 1))),
3809 return 1;
3810 #endif
3811 else if ((new = instantiate_new_reg (XEXP (x, 0), &offset)) == 0)
3813 /* We know the second operand is a constant. Unless the
3814 first operand is a REG (which has been already checked),
3815 it needs to be checked. */
3816 if (GET_CODE (XEXP (x, 0)) != REG)
3818 loc = &XEXP (x, 0);
3819 goto restart;
3821 return 1;
3824 new_offset = plus_constant (XEXP (x, 1), offset);
3826 /* If the new constant is zero, try to replace the sum with just
3827 the register. */
3828 if (new_offset == const0_rtx
3829 && validate_change (object, loc, new, 0))
3830 return 1;
3832 /* Next try to replace the register and new offset.
3833 There are two changes to validate here and we can't assume that
3834 in the case of old offset equals new just changing the register
3835 will yield a valid insn. In the interests of a little efficiency,
3836 however, we only call validate change once (we don't queue up the
3837 changes and then call apply_change_group). */
3839 old = XEXP (x, 0);
3840 if (offset == 0
3841 ? ! validate_change (object, &XEXP (x, 0), new, 0)
3842 : (XEXP (x, 0) = new,
3843 ! validate_change (object, &XEXP (x, 1), new_offset, 0)))
3845 if (! extra_insns)
3847 XEXP (x, 0) = old;
3848 return 0;
3851 /* Otherwise copy the new constant into a register and replace
3852 constant with that register. */
3853 temp = gen_reg_rtx (Pmode);
3854 XEXP (x, 0) = new;
3855 if (validate_change (object, &XEXP (x, 1), temp, 0))
3856 emit_insn_before (gen_move_insn (temp, new_offset), object);
3857 else
3859 /* If that didn't work, replace this expression with a
3860 register containing the sum. */
3862 XEXP (x, 0) = old;
3863 new = gen_rtx_PLUS (Pmode, new, new_offset);
3865 start_sequence ();
3866 temp = force_operand (new, NULL_RTX);
3867 seq = get_insns ();
3868 end_sequence ();
3870 emit_insns_before (seq, object);
3871 if (! validate_change (object, loc, temp, 0)
3872 && ! validate_replace_rtx (x, temp, object))
3873 abort ();
3877 return 1;
3880 /* Fall through to generic two-operand expression case. */
3881 case EXPR_LIST:
3882 case CALL:
3883 case COMPARE:
3884 case MINUS:
3885 case MULT:
3886 case DIV: case UDIV:
3887 case MOD: case UMOD:
3888 case AND: case IOR: case XOR:
3889 case ROTATERT: case ROTATE:
3890 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
3891 case NE: case EQ:
3892 case GE: case GT: case GEU: case GTU:
3893 case LE: case LT: case LEU: case LTU:
3894 if (XEXP (x, 1) && ! CONSTANT_P (XEXP (x, 1)))
3895 instantiate_virtual_regs_1 (&XEXP (x, 1), object, extra_insns);
3896 loc = &XEXP (x, 0);
3897 goto restart;
3899 case MEM:
3900 /* Most cases of MEM that convert to valid addresses have already been
3901 handled by our scan of decls. The only special handling we
3902 need here is to make a copy of the rtx to ensure it isn't being
3903 shared if we have to change it to a pseudo.
3905 If the rtx is a simple reference to an address via a virtual register,
3906 it can potentially be shared. In such cases, first try to make it
3907 a valid address, which can also be shared. Otherwise, copy it and
3908 proceed normally.
3910 First check for common cases that need no processing. These are
3911 usually due to instantiation already being done on a previous instance
3912 of a shared rtx. */
3914 temp = XEXP (x, 0);
3915 if (CONSTANT_ADDRESS_P (temp)
3916 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3917 || temp == arg_pointer_rtx
3918 #endif
3919 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3920 || temp == hard_frame_pointer_rtx
3921 #endif
3922 || temp == frame_pointer_rtx)
3923 return 1;
3925 if (GET_CODE (temp) == PLUS
3926 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3927 && (XEXP (temp, 0) == frame_pointer_rtx
3928 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3929 || XEXP (temp, 0) == hard_frame_pointer_rtx
3930 #endif
3931 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3932 || XEXP (temp, 0) == arg_pointer_rtx
3933 #endif
3935 return 1;
3937 if (temp == virtual_stack_vars_rtx
3938 || temp == virtual_incoming_args_rtx
3939 || (GET_CODE (temp) == PLUS
3940 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3941 && (XEXP (temp, 0) == virtual_stack_vars_rtx
3942 || XEXP (temp, 0) == virtual_incoming_args_rtx)))
3944 /* This MEM may be shared. If the substitution can be done without
3945 the need to generate new pseudos, we want to do it in place
3946 so all copies of the shared rtx benefit. The call below will
3947 only make substitutions if the resulting address is still
3948 valid.
3950 Note that we cannot pass X as the object in the recursive call
3951 since the insn being processed may not allow all valid
3952 addresses. However, if we were not passed on object, we can
3953 only modify X without copying it if X will have a valid
3954 address.
3956 ??? Also note that this can still lose if OBJECT is an insn that
3957 has less restrictions on an address that some other insn.
3958 In that case, we will modify the shared address. This case
3959 doesn't seem very likely, though. One case where this could
3960 happen is in the case of a USE or CLOBBER reference, but we
3961 take care of that below. */
3963 if (instantiate_virtual_regs_1 (&XEXP (x, 0),
3964 object ? object : x, 0))
3965 return 1;
3967 /* Otherwise make a copy and process that copy. We copy the entire
3968 RTL expression since it might be a PLUS which could also be
3969 shared. */
3970 *loc = x = copy_rtx (x);
3973 /* Fall through to generic unary operation case. */
3974 case PREFETCH:
3975 case SUBREG:
3976 case STRICT_LOW_PART:
3977 case NEG: case NOT:
3978 case PRE_DEC: case PRE_INC: case POST_DEC: case POST_INC:
3979 case SIGN_EXTEND: case ZERO_EXTEND:
3980 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3981 case FLOAT: case FIX:
3982 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3983 case ABS:
3984 case SQRT:
3985 case FFS:
3986 /* These case either have just one operand or we know that we need not
3987 check the rest of the operands. */
3988 loc = &XEXP (x, 0);
3989 goto restart;
3991 case USE:
3992 case CLOBBER:
3993 /* If the operand is a MEM, see if the change is a valid MEM. If not,
3994 go ahead and make the invalid one, but do it to a copy. For a REG,
3995 just make the recursive call, since there's no chance of a problem. */
3997 if ((GET_CODE (XEXP (x, 0)) == MEM
3998 && instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), XEXP (x, 0),
4000 || (GET_CODE (XEXP (x, 0)) == REG
4001 && instantiate_virtual_regs_1 (&XEXP (x, 0), object, 0)))
4002 return 1;
4004 XEXP (x, 0) = copy_rtx (XEXP (x, 0));
4005 loc = &XEXP (x, 0);
4006 goto restart;
4008 case REG:
4009 /* Try to replace with a PLUS. If that doesn't work, compute the sum
4010 in front of this insn and substitute the temporary. */
4011 if ((new = instantiate_new_reg (x, &offset)) != 0)
4013 temp = plus_constant (new, offset);
4014 if (!validate_change (object, loc, temp, 0))
4016 if (! extra_insns)
4017 return 0;
4019 start_sequence ();
4020 temp = force_operand (temp, NULL_RTX);
4021 seq = get_insns ();
4022 end_sequence ();
4024 emit_insns_before (seq, object);
4025 if (! validate_change (object, loc, temp, 0)
4026 && ! validate_replace_rtx (x, temp, object))
4027 abort ();
4031 return 1;
4033 case ADDRESSOF:
4034 if (GET_CODE (XEXP (x, 0)) == REG)
4035 return 1;
4037 else if (GET_CODE (XEXP (x, 0)) == MEM)
4039 /* If we have a (addressof (mem ..)), do any instantiation inside
4040 since we know we'll be making the inside valid when we finally
4041 remove the ADDRESSOF. */
4042 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), NULL_RTX, 0);
4043 return 1;
4045 break;
4047 default:
4048 break;
4051 /* Scan all subexpressions. */
4052 fmt = GET_RTX_FORMAT (code);
4053 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
4054 if (*fmt == 'e')
4056 if (!instantiate_virtual_regs_1 (&XEXP (x, i), object, extra_insns))
4057 return 0;
4059 else if (*fmt == 'E')
4060 for (j = 0; j < XVECLEN (x, i); j++)
4061 if (! instantiate_virtual_regs_1 (&XVECEXP (x, i, j), object,
4062 extra_insns))
4063 return 0;
4065 return 1;
4068 /* Optimization: assuming this function does not receive nonlocal gotos,
4069 delete the handlers for such, as well as the insns to establish
4070 and disestablish them. */
4072 static void
4073 delete_handlers ()
4075 rtx insn;
4076 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4078 /* Delete the handler by turning off the flag that would
4079 prevent jump_optimize from deleting it.
4080 Also permit deletion of the nonlocal labels themselves
4081 if nothing local refers to them. */
4082 if (GET_CODE (insn) == CODE_LABEL)
4084 tree t, last_t;
4086 LABEL_PRESERVE_P (insn) = 0;
4088 /* Remove it from the nonlocal_label list, to avoid confusing
4089 flow. */
4090 for (t = nonlocal_labels, last_t = 0; t;
4091 last_t = t, t = TREE_CHAIN (t))
4092 if (DECL_RTL (TREE_VALUE (t)) == insn)
4093 break;
4094 if (t)
4096 if (! last_t)
4097 nonlocal_labels = TREE_CHAIN (nonlocal_labels);
4098 else
4099 TREE_CHAIN (last_t) = TREE_CHAIN (t);
4102 if (GET_CODE (insn) == INSN)
4104 int can_delete = 0;
4105 rtx t;
4106 for (t = nonlocal_goto_handler_slots; t != 0; t = XEXP (t, 1))
4107 if (reg_mentioned_p (t, PATTERN (insn)))
4109 can_delete = 1;
4110 break;
4112 if (can_delete
4113 || (nonlocal_goto_stack_level != 0
4114 && reg_mentioned_p (nonlocal_goto_stack_level,
4115 PATTERN (insn))))
4116 delete_related_insns (insn);
4122 max_parm_reg_num ()
4124 return max_parm_reg;
4127 /* Return the first insn following those generated by `assign_parms'. */
4130 get_first_nonparm_insn ()
4132 if (last_parm_insn)
4133 return NEXT_INSN (last_parm_insn);
4134 return get_insns ();
4137 /* Return the first NOTE_INSN_BLOCK_BEG note in the function.
4138 Crash if there is none. */
4141 get_first_block_beg ()
4143 rtx searcher;
4144 rtx insn = get_first_nonparm_insn ();
4146 for (searcher = insn; searcher; searcher = NEXT_INSN (searcher))
4147 if (GET_CODE (searcher) == NOTE
4148 && NOTE_LINE_NUMBER (searcher) == NOTE_INSN_BLOCK_BEG)
4149 return searcher;
4151 abort (); /* Invalid call to this function. (See comments above.) */
4152 return NULL_RTX;
4155 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
4156 This means a type for which function calls must pass an address to the
4157 function or get an address back from the function.
4158 EXP may be a type node or an expression (whose type is tested). */
4161 aggregate_value_p (exp)
4162 tree exp;
4164 int i, regno, nregs;
4165 rtx reg;
4167 tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
4169 if (TREE_CODE (type) == VOID_TYPE)
4170 return 0;
4171 if (RETURN_IN_MEMORY (type))
4172 return 1;
4173 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
4174 and thus can't be returned in registers. */
4175 if (TREE_ADDRESSABLE (type))
4176 return 1;
4177 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
4178 return 1;
4179 /* Make sure we have suitable call-clobbered regs to return
4180 the value in; if not, we must return it in memory. */
4181 reg = hard_function_value (type, 0, 0);
4183 /* If we have something other than a REG (e.g. a PARALLEL), then assume
4184 it is OK. */
4185 if (GET_CODE (reg) != REG)
4186 return 0;
4188 regno = REGNO (reg);
4189 nregs = HARD_REGNO_NREGS (regno, TYPE_MODE (type));
4190 for (i = 0; i < nregs; i++)
4191 if (! call_used_regs[regno + i])
4192 return 1;
4193 return 0;
4196 /* Assign RTL expressions to the function's parameters.
4197 This may involve copying them into registers and using
4198 those registers as the RTL for them. */
4200 void
4201 assign_parms (fndecl)
4202 tree fndecl;
4204 tree parm;
4205 rtx entry_parm = 0;
4206 rtx stack_parm = 0;
4207 CUMULATIVE_ARGS args_so_far;
4208 enum machine_mode promoted_mode, passed_mode;
4209 enum machine_mode nominal_mode, promoted_nominal_mode;
4210 int unsignedp;
4211 /* Total space needed so far for args on the stack,
4212 given as a constant and a tree-expression. */
4213 struct args_size stack_args_size;
4214 tree fntype = TREE_TYPE (fndecl);
4215 tree fnargs = DECL_ARGUMENTS (fndecl);
4216 /* This is used for the arg pointer when referring to stack args. */
4217 rtx internal_arg_pointer;
4218 /* This is a dummy PARM_DECL that we used for the function result if
4219 the function returns a structure. */
4220 tree function_result_decl = 0;
4221 #ifdef SETUP_INCOMING_VARARGS
4222 int varargs_setup = 0;
4223 #endif
4224 rtx conversion_insns = 0;
4225 struct args_size alignment_pad;
4227 /* Nonzero if the last arg is named `__builtin_va_alist',
4228 which is used on some machines for old-fashioned non-ANSI varargs.h;
4229 this should be stuck onto the stack as if it had arrived there. */
4230 int hide_last_arg
4231 = (current_function_varargs
4232 && fnargs
4233 && (parm = tree_last (fnargs)) != 0
4234 && DECL_NAME (parm)
4235 && (! strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
4236 "__builtin_va_alist")));
4238 /* Nonzero if function takes extra anonymous args.
4239 This means the last named arg must be on the stack
4240 right before the anonymous ones. */
4241 int stdarg
4242 = (TYPE_ARG_TYPES (fntype) != 0
4243 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4244 != void_type_node));
4246 current_function_stdarg = stdarg;
4248 /* If the reg that the virtual arg pointer will be translated into is
4249 not a fixed reg or is the stack pointer, make a copy of the virtual
4250 arg pointer, and address parms via the copy. The frame pointer is
4251 considered fixed even though it is not marked as such.
4253 The second time through, simply use ap to avoid generating rtx. */
4255 if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
4256 || ! (fixed_regs[ARG_POINTER_REGNUM]
4257 || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM)))
4258 internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
4259 else
4260 internal_arg_pointer = virtual_incoming_args_rtx;
4261 current_function_internal_arg_pointer = internal_arg_pointer;
4263 stack_args_size.constant = 0;
4264 stack_args_size.var = 0;
4266 /* If struct value address is treated as the first argument, make it so. */
4267 if (aggregate_value_p (DECL_RESULT (fndecl))
4268 && ! current_function_returns_pcc_struct
4269 && struct_value_incoming_rtx == 0)
4271 tree type = build_pointer_type (TREE_TYPE (fntype));
4273 function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
4275 DECL_ARG_TYPE (function_result_decl) = type;
4276 TREE_CHAIN (function_result_decl) = fnargs;
4277 fnargs = function_result_decl;
4280 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
4281 parm_reg_stack_loc = (rtx *) xcalloc (max_parm_reg, sizeof (rtx));
4283 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
4284 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far, fntype, NULL_RTX);
4285 #else
4286 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, 0);
4287 #endif
4289 /* We haven't yet found an argument that we must push and pretend the
4290 caller did. */
4291 current_function_pretend_args_size = 0;
4293 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
4295 struct args_size stack_offset;
4296 struct args_size arg_size;
4297 int passed_pointer = 0;
4298 int did_conversion = 0;
4299 tree passed_type = DECL_ARG_TYPE (parm);
4300 tree nominal_type = TREE_TYPE (parm);
4301 int pretend_named;
4302 int last_named = 0, named_arg;
4304 /* Set LAST_NAMED if this is last named arg before last
4305 anonymous args. */
4306 if (stdarg || current_function_varargs)
4308 tree tem;
4310 for (tem = TREE_CHAIN (parm); tem; tem = TREE_CHAIN (tem))
4311 if (DECL_NAME (tem))
4312 break;
4314 if (tem == 0)
4315 last_named = 1;
4317 /* Set NAMED_ARG if this arg should be treated as a named arg. For
4318 most machines, if this is a varargs/stdarg function, then we treat
4319 the last named arg as if it were anonymous too. */
4320 named_arg = STRICT_ARGUMENT_NAMING ? 1 : ! last_named;
4322 if (TREE_TYPE (parm) == error_mark_node
4323 /* This can happen after weird syntax errors
4324 or if an enum type is defined among the parms. */
4325 || TREE_CODE (parm) != PARM_DECL
4326 || passed_type == NULL)
4328 SET_DECL_RTL (parm, gen_rtx_MEM (BLKmode, const0_rtx));
4329 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4330 TREE_USED (parm) = 1;
4331 continue;
4334 /* For varargs.h function, save info about regs and stack space
4335 used by the individual args, not including the va_alist arg. */
4336 if (hide_last_arg && last_named)
4337 current_function_args_info = args_so_far;
4339 /* Find mode of arg as it is passed, and mode of arg
4340 as it should be during execution of this function. */
4341 passed_mode = TYPE_MODE (passed_type);
4342 nominal_mode = TYPE_MODE (nominal_type);
4344 /* If the parm's mode is VOID, its value doesn't matter,
4345 and avoid the usual things like emit_move_insn that could crash. */
4346 if (nominal_mode == VOIDmode)
4348 SET_DECL_RTL (parm, const0_rtx);
4349 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4350 continue;
4353 /* If the parm is to be passed as a transparent union, use the
4354 type of the first field for the tests below. We have already
4355 verified that the modes are the same. */
4356 if (DECL_TRANSPARENT_UNION (parm)
4357 || (TREE_CODE (passed_type) == UNION_TYPE
4358 && TYPE_TRANSPARENT_UNION (passed_type)))
4359 passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
4361 /* See if this arg was passed by invisible reference. It is if
4362 it is an object whose size depends on the contents of the
4363 object itself or if the machine requires these objects be passed
4364 that way. */
4366 if ((TREE_CODE (TYPE_SIZE (passed_type)) != INTEGER_CST
4367 && contains_placeholder_p (TYPE_SIZE (passed_type)))
4368 || TREE_ADDRESSABLE (passed_type)
4369 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
4370 || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, passed_mode,
4371 passed_type, named_arg)
4372 #endif
4375 passed_type = nominal_type = build_pointer_type (passed_type);
4376 passed_pointer = 1;
4377 passed_mode = nominal_mode = Pmode;
4380 promoted_mode = passed_mode;
4382 #ifdef PROMOTE_FUNCTION_ARGS
4383 /* Compute the mode in which the arg is actually extended to. */
4384 unsignedp = TREE_UNSIGNED (passed_type);
4385 promoted_mode = promote_mode (passed_type, promoted_mode, &unsignedp, 1);
4386 #endif
4388 /* Let machine desc say which reg (if any) the parm arrives in.
4389 0 means it arrives on the stack. */
4390 #ifdef FUNCTION_INCOMING_ARG
4391 entry_parm = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4392 passed_type, named_arg);
4393 #else
4394 entry_parm = FUNCTION_ARG (args_so_far, promoted_mode,
4395 passed_type, named_arg);
4396 #endif
4398 if (entry_parm == 0)
4399 promoted_mode = passed_mode;
4401 #ifdef SETUP_INCOMING_VARARGS
4402 /* If this is the last named parameter, do any required setup for
4403 varargs or stdargs. We need to know about the case of this being an
4404 addressable type, in which case we skip the registers it
4405 would have arrived in.
4407 For stdargs, LAST_NAMED will be set for two parameters, the one that
4408 is actually the last named, and the dummy parameter. We only
4409 want to do this action once.
4411 Also, indicate when RTL generation is to be suppressed. */
4412 if (last_named && !varargs_setup)
4414 SETUP_INCOMING_VARARGS (args_so_far, promoted_mode, passed_type,
4415 current_function_pretend_args_size, 0);
4416 varargs_setup = 1;
4418 #endif
4420 /* Determine parm's home in the stack,
4421 in case it arrives in the stack or we should pretend it did.
4423 Compute the stack position and rtx where the argument arrives
4424 and its size.
4426 There is one complexity here: If this was a parameter that would
4427 have been passed in registers, but wasn't only because it is
4428 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
4429 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
4430 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of
4431 0 as it was the previous time. */
4433 pretend_named = named_arg || PRETEND_OUTGOING_VARARGS_NAMED;
4434 locate_and_pad_parm (promoted_mode, passed_type,
4435 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4437 #else
4438 #ifdef FUNCTION_INCOMING_ARG
4439 FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4440 passed_type,
4441 pretend_named) != 0,
4442 #else
4443 FUNCTION_ARG (args_so_far, promoted_mode,
4444 passed_type,
4445 pretend_named) != 0,
4446 #endif
4447 #endif
4448 fndecl, &stack_args_size, &stack_offset, &arg_size,
4449 &alignment_pad);
4452 rtx offset_rtx = ARGS_SIZE_RTX (stack_offset);
4454 if (offset_rtx == const0_rtx)
4455 stack_parm = gen_rtx_MEM (promoted_mode, internal_arg_pointer);
4456 else
4457 stack_parm = gen_rtx_MEM (promoted_mode,
4458 gen_rtx_PLUS (Pmode,
4459 internal_arg_pointer,
4460 offset_rtx));
4462 set_mem_attributes (stack_parm, parm, 1);
4465 /* If this parameter was passed both in registers and in the stack,
4466 use the copy on the stack. */
4467 if (MUST_PASS_IN_STACK (promoted_mode, passed_type))
4468 entry_parm = 0;
4470 #ifdef FUNCTION_ARG_PARTIAL_NREGS
4471 /* If this parm was passed part in regs and part in memory,
4472 pretend it arrived entirely in memory
4473 by pushing the register-part onto the stack.
4475 In the special case of a DImode or DFmode that is split,
4476 we could put it together in a pseudoreg directly,
4477 but for now that's not worth bothering with. */
4479 if (entry_parm)
4481 int nregs = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, promoted_mode,
4482 passed_type, named_arg);
4484 if (nregs > 0)
4486 current_function_pretend_args_size
4487 = (((nregs * UNITS_PER_WORD) + (PARM_BOUNDARY / BITS_PER_UNIT) - 1)
4488 / (PARM_BOUNDARY / BITS_PER_UNIT)
4489 * (PARM_BOUNDARY / BITS_PER_UNIT));
4491 /* Handle calls that pass values in multiple non-contiguous
4492 locations. The Irix 6 ABI has examples of this. */
4493 if (GET_CODE (entry_parm) == PARALLEL)
4494 emit_group_store (validize_mem (stack_parm), entry_parm,
4495 int_size_in_bytes (TREE_TYPE (parm)));
4497 else
4498 move_block_from_reg (REGNO (entry_parm),
4499 validize_mem (stack_parm), nregs,
4500 int_size_in_bytes (TREE_TYPE (parm)));
4502 entry_parm = stack_parm;
4505 #endif
4507 /* If we didn't decide this parm came in a register,
4508 by default it came on the stack. */
4509 if (entry_parm == 0)
4510 entry_parm = stack_parm;
4512 /* Record permanently how this parm was passed. */
4513 DECL_INCOMING_RTL (parm) = entry_parm;
4515 /* If there is actually space on the stack for this parm,
4516 count it in stack_args_size; otherwise set stack_parm to 0
4517 to indicate there is no preallocated stack slot for the parm. */
4519 if (entry_parm == stack_parm
4520 || (GET_CODE (entry_parm) == PARALLEL
4521 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
4522 #if defined (REG_PARM_STACK_SPACE) && ! defined (MAYBE_REG_PARM_STACK_SPACE)
4523 /* On some machines, even if a parm value arrives in a register
4524 there is still an (uninitialized) stack slot allocated for it.
4526 ??? When MAYBE_REG_PARM_STACK_SPACE is defined, we can't tell
4527 whether this parameter already has a stack slot allocated,
4528 because an arg block exists only if current_function_args_size
4529 is larger than some threshold, and we haven't calculated that
4530 yet. So, for now, we just assume that stack slots never exist
4531 in this case. */
4532 || REG_PARM_STACK_SPACE (fndecl) > 0
4533 #endif
4536 stack_args_size.constant += arg_size.constant;
4537 if (arg_size.var)
4538 ADD_PARM_SIZE (stack_args_size, arg_size.var);
4540 else
4541 /* No stack slot was pushed for this parm. */
4542 stack_parm = 0;
4544 /* Update info on where next arg arrives in registers. */
4546 FUNCTION_ARG_ADVANCE (args_so_far, promoted_mode,
4547 passed_type, named_arg);
4549 /* If we can't trust the parm stack slot to be aligned enough
4550 for its ultimate type, don't use that slot after entry.
4551 We'll make another stack slot, if we need one. */
4553 unsigned int thisparm_boundary
4554 = FUNCTION_ARG_BOUNDARY (promoted_mode, passed_type);
4556 if (GET_MODE_ALIGNMENT (nominal_mode) > thisparm_boundary)
4557 stack_parm = 0;
4560 /* If parm was passed in memory, and we need to convert it on entry,
4561 don't store it back in that same slot. */
4562 if (entry_parm != 0
4563 && nominal_mode != BLKmode && nominal_mode != passed_mode)
4564 stack_parm = 0;
4566 /* When an argument is passed in multiple locations, we can't
4567 make use of this information, but we can save some copying if
4568 the whole argument is passed in a single register. */
4569 if (GET_CODE (entry_parm) == PARALLEL
4570 && nominal_mode != BLKmode && passed_mode != BLKmode)
4572 int i, len = XVECLEN (entry_parm, 0);
4574 for (i = 0; i < len; i++)
4575 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
4576 && GET_CODE (XEXP (XVECEXP (entry_parm, 0, i), 0)) == REG
4577 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
4578 == passed_mode)
4579 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
4581 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
4582 DECL_INCOMING_RTL (parm) = entry_parm;
4583 break;
4587 /* ENTRY_PARM is an RTX for the parameter as it arrives,
4588 in the mode in which it arrives.
4589 STACK_PARM is an RTX for a stack slot where the parameter can live
4590 during the function (in case we want to put it there).
4591 STACK_PARM is 0 if no stack slot was pushed for it.
4593 Now output code if necessary to convert ENTRY_PARM to
4594 the type in which this function declares it,
4595 and store that result in an appropriate place,
4596 which may be a pseudo reg, may be STACK_PARM,
4597 or may be a local stack slot if STACK_PARM is 0.
4599 Set DECL_RTL to that place. */
4601 if (nominal_mode == BLKmode || GET_CODE (entry_parm) == PARALLEL)
4603 /* If a BLKmode arrives in registers, copy it to a stack slot.
4604 Handle calls that pass values in multiple non-contiguous
4605 locations. The Irix 6 ABI has examples of this. */
4606 if (GET_CODE (entry_parm) == REG
4607 || GET_CODE (entry_parm) == PARALLEL)
4609 int size_stored
4610 = CEIL_ROUND (int_size_in_bytes (TREE_TYPE (parm)),
4611 UNITS_PER_WORD);
4613 /* Note that we will be storing an integral number of words.
4614 So we have to be careful to ensure that we allocate an
4615 integral number of words. We do this below in the
4616 assign_stack_local if space was not allocated in the argument
4617 list. If it was, this will not work if PARM_BOUNDARY is not
4618 a multiple of BITS_PER_WORD. It isn't clear how to fix this
4619 if it becomes a problem. */
4621 if (stack_parm == 0)
4623 stack_parm
4624 = assign_stack_local (GET_MODE (entry_parm),
4625 size_stored, 0);
4626 set_mem_attributes (stack_parm, parm, 1);
4629 else if (PARM_BOUNDARY % BITS_PER_WORD != 0)
4630 abort ();
4632 /* Handle calls that pass values in multiple non-contiguous
4633 locations. The Irix 6 ABI has examples of this. */
4634 if (GET_CODE (entry_parm) == PARALLEL)
4635 emit_group_store (validize_mem (stack_parm), entry_parm,
4636 int_size_in_bytes (TREE_TYPE (parm)));
4637 else
4638 move_block_from_reg (REGNO (entry_parm),
4639 validize_mem (stack_parm),
4640 size_stored / UNITS_PER_WORD,
4641 int_size_in_bytes (TREE_TYPE (parm)));
4643 SET_DECL_RTL (parm, stack_parm);
4645 else if (! ((! optimize
4646 && ! DECL_REGISTER (parm)
4647 && ! DECL_INLINE (fndecl))
4648 || TREE_SIDE_EFFECTS (parm)
4649 /* If -ffloat-store specified, don't put explicit
4650 float variables into registers. */
4651 || (flag_float_store
4652 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
4653 /* Always assign pseudo to structure return or item passed
4654 by invisible reference. */
4655 || passed_pointer || parm == function_result_decl)
4657 /* Store the parm in a pseudoregister during the function, but we
4658 may need to do it in a wider mode. */
4660 rtx parmreg;
4661 unsigned int regno, regnoi = 0, regnor = 0;
4663 unsignedp = TREE_UNSIGNED (TREE_TYPE (parm));
4665 promoted_nominal_mode
4666 = promote_mode (TREE_TYPE (parm), nominal_mode, &unsignedp, 0);
4668 parmreg = gen_reg_rtx (promoted_nominal_mode);
4669 mark_user_reg (parmreg);
4671 /* If this was an item that we received a pointer to, set DECL_RTL
4672 appropriately. */
4673 if (passed_pointer)
4675 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (passed_type)),
4676 parmreg);
4677 set_mem_attributes (x, parm, 1);
4678 SET_DECL_RTL (parm, x);
4680 else
4682 SET_DECL_RTL (parm, parmreg);
4683 maybe_set_unchanging (DECL_RTL (parm), parm);
4686 /* Copy the value into the register. */
4687 if (nominal_mode != passed_mode
4688 || promoted_nominal_mode != promoted_mode)
4690 int save_tree_used;
4691 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
4692 mode, by the caller. We now have to convert it to
4693 NOMINAL_MODE, if different. However, PARMREG may be in
4694 a different mode than NOMINAL_MODE if it is being stored
4695 promoted.
4697 If ENTRY_PARM is a hard register, it might be in a register
4698 not valid for operating in its mode (e.g., an odd-numbered
4699 register for a DFmode). In that case, moves are the only
4700 thing valid, so we can't do a convert from there. This
4701 occurs when the calling sequence allow such misaligned
4702 usages.
4704 In addition, the conversion may involve a call, which could
4705 clobber parameters which haven't been copied to pseudo
4706 registers yet. Therefore, we must first copy the parm to
4707 a pseudo reg here, and save the conversion until after all
4708 parameters have been moved. */
4710 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4712 emit_move_insn (tempreg, validize_mem (entry_parm));
4714 push_to_sequence (conversion_insns);
4715 tempreg = convert_to_mode (nominal_mode, tempreg, unsignedp);
4717 if (GET_CODE (tempreg) == SUBREG
4718 && GET_MODE (tempreg) == nominal_mode
4719 && GET_CODE (SUBREG_REG (tempreg)) == REG
4720 && nominal_mode == passed_mode
4721 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (entry_parm)
4722 && GET_MODE_SIZE (GET_MODE (tempreg))
4723 < GET_MODE_SIZE (GET_MODE (entry_parm)))
4725 /* The argument is already sign/zero extended, so note it
4726 into the subreg. */
4727 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
4728 SUBREG_PROMOTED_UNSIGNED_P (tempreg) = unsignedp;
4731 /* TREE_USED gets set erroneously during expand_assignment. */
4732 save_tree_used = TREE_USED (parm);
4733 expand_assignment (parm,
4734 make_tree (nominal_type, tempreg), 0, 0);
4735 TREE_USED (parm) = save_tree_used;
4736 conversion_insns = get_insns ();
4737 did_conversion = 1;
4738 end_sequence ();
4740 else
4741 emit_move_insn (parmreg, validize_mem (entry_parm));
4743 /* If we were passed a pointer but the actual value
4744 can safely live in a register, put it in one. */
4745 if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
4746 && ! ((! optimize
4747 && ! DECL_REGISTER (parm)
4748 && ! DECL_INLINE (fndecl))
4749 || TREE_SIDE_EFFECTS (parm)
4750 /* If -ffloat-store specified, don't put explicit
4751 float variables into registers. */
4752 || (flag_float_store
4753 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE)))
4755 /* We can't use nominal_mode, because it will have been set to
4756 Pmode above. We must use the actual mode of the parm. */
4757 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
4758 mark_user_reg (parmreg);
4759 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
4761 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
4762 int unsigned_p = TREE_UNSIGNED (TREE_TYPE (parm));
4763 push_to_sequence (conversion_insns);
4764 emit_move_insn (tempreg, DECL_RTL (parm));
4765 SET_DECL_RTL (parm,
4766 convert_to_mode (GET_MODE (parmreg),
4767 tempreg,
4768 unsigned_p));
4769 emit_move_insn (parmreg, DECL_RTL (parm));
4770 conversion_insns = get_insns();
4771 did_conversion = 1;
4772 end_sequence ();
4774 else
4775 emit_move_insn (parmreg, DECL_RTL (parm));
4776 SET_DECL_RTL (parm, parmreg);
4777 /* STACK_PARM is the pointer, not the parm, and PARMREG is
4778 now the parm. */
4779 stack_parm = 0;
4781 #ifdef FUNCTION_ARG_CALLEE_COPIES
4782 /* If we are passed an arg by reference and it is our responsibility
4783 to make a copy, do it now.
4784 PASSED_TYPE and PASSED mode now refer to the pointer, not the
4785 original argument, so we must recreate them in the call to
4786 FUNCTION_ARG_CALLEE_COPIES. */
4787 /* ??? Later add code to handle the case that if the argument isn't
4788 modified, don't do the copy. */
4790 else if (passed_pointer
4791 && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
4792 TYPE_MODE (DECL_ARG_TYPE (parm)),
4793 DECL_ARG_TYPE (parm),
4794 named_arg)
4795 && ! TREE_ADDRESSABLE (DECL_ARG_TYPE (parm)))
4797 rtx copy;
4798 tree type = DECL_ARG_TYPE (parm);
4800 /* This sequence may involve a library call perhaps clobbering
4801 registers that haven't been copied to pseudos yet. */
4803 push_to_sequence (conversion_insns);
4805 if (!COMPLETE_TYPE_P (type)
4806 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4807 /* This is a variable sized object. */
4808 copy = gen_rtx_MEM (BLKmode,
4809 allocate_dynamic_stack_space
4810 (expr_size (parm), NULL_RTX,
4811 TYPE_ALIGN (type)));
4812 else
4813 copy = assign_stack_temp (TYPE_MODE (type),
4814 int_size_in_bytes (type), 1);
4815 set_mem_attributes (copy, parm, 1);
4817 store_expr (parm, copy, 0);
4818 emit_move_insn (parmreg, XEXP (copy, 0));
4819 conversion_insns = get_insns ();
4820 did_conversion = 1;
4821 end_sequence ();
4823 #endif /* FUNCTION_ARG_CALLEE_COPIES */
4825 /* In any case, record the parm's desired stack location
4826 in case we later discover it must live in the stack.
4828 If it is a COMPLEX value, store the stack location for both
4829 halves. */
4831 if (GET_CODE (parmreg) == CONCAT)
4832 regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
4833 else
4834 regno = REGNO (parmreg);
4836 if (regno >= max_parm_reg)
4838 rtx *new;
4839 int old_max_parm_reg = max_parm_reg;
4841 /* It's slow to expand this one register at a time,
4842 but it's also rare and we need max_parm_reg to be
4843 precisely correct. */
4844 max_parm_reg = regno + 1;
4845 new = (rtx *) xrealloc (parm_reg_stack_loc,
4846 max_parm_reg * sizeof (rtx));
4847 memset ((char *) (new + old_max_parm_reg), 0,
4848 (max_parm_reg - old_max_parm_reg) * sizeof (rtx));
4849 parm_reg_stack_loc = new;
4852 if (GET_CODE (parmreg) == CONCAT)
4854 enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
4856 regnor = REGNO (gen_realpart (submode, parmreg));
4857 regnoi = REGNO (gen_imagpart (submode, parmreg));
4859 if (stack_parm != 0)
4861 parm_reg_stack_loc[regnor]
4862 = gen_realpart (submode, stack_parm);
4863 parm_reg_stack_loc[regnoi]
4864 = gen_imagpart (submode, stack_parm);
4866 else
4868 parm_reg_stack_loc[regnor] = 0;
4869 parm_reg_stack_loc[regnoi] = 0;
4872 else
4873 parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
4875 /* Mark the register as eliminable if we did no conversion
4876 and it was copied from memory at a fixed offset,
4877 and the arg pointer was not copied to a pseudo-reg.
4878 If the arg pointer is a pseudo reg or the offset formed
4879 an invalid address, such memory-equivalences
4880 as we make here would screw up life analysis for it. */
4881 if (nominal_mode == passed_mode
4882 && ! did_conversion
4883 && stack_parm != 0
4884 && GET_CODE (stack_parm) == MEM
4885 && stack_offset.var == 0
4886 && reg_mentioned_p (virtual_incoming_args_rtx,
4887 XEXP (stack_parm, 0)))
4889 rtx linsn = get_last_insn ();
4890 rtx sinsn, set;
4892 /* Mark complex types separately. */
4893 if (GET_CODE (parmreg) == CONCAT)
4894 /* Scan backwards for the set of the real and
4895 imaginary parts. */
4896 for (sinsn = linsn; sinsn != 0;
4897 sinsn = prev_nonnote_insn (sinsn))
4899 set = single_set (sinsn);
4900 if (set != 0
4901 && SET_DEST (set) == regno_reg_rtx [regnoi])
4902 REG_NOTES (sinsn)
4903 = gen_rtx_EXPR_LIST (REG_EQUIV,
4904 parm_reg_stack_loc[regnoi],
4905 REG_NOTES (sinsn));
4906 else if (set != 0
4907 && SET_DEST (set) == regno_reg_rtx [regnor])
4908 REG_NOTES (sinsn)
4909 = gen_rtx_EXPR_LIST (REG_EQUIV,
4910 parm_reg_stack_loc[regnor],
4911 REG_NOTES (sinsn));
4913 else if ((set = single_set (linsn)) != 0
4914 && SET_DEST (set) == parmreg)
4915 REG_NOTES (linsn)
4916 = gen_rtx_EXPR_LIST (REG_EQUIV,
4917 stack_parm, REG_NOTES (linsn));
4920 /* For pointer data type, suggest pointer register. */
4921 if (POINTER_TYPE_P (TREE_TYPE (parm)))
4922 mark_reg_pointer (parmreg,
4923 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4925 /* If something wants our address, try to use ADDRESSOF. */
4926 if (TREE_ADDRESSABLE (parm))
4928 /* If we end up putting something into the stack,
4929 fixup_var_refs_insns will need to make a pass over
4930 all the instructions. It looks through the pending
4931 sequences -- but it can't see the ones in the
4932 CONVERSION_INSNS, if they're not on the sequence
4933 stack. So, we go back to that sequence, just so that
4934 the fixups will happen. */
4935 push_to_sequence (conversion_insns);
4936 put_var_into_stack (parm);
4937 conversion_insns = get_insns ();
4938 end_sequence ();
4941 else
4943 /* Value must be stored in the stack slot STACK_PARM
4944 during function execution. */
4946 if (promoted_mode != nominal_mode)
4948 /* Conversion is required. */
4949 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4951 emit_move_insn (tempreg, validize_mem (entry_parm));
4953 push_to_sequence (conversion_insns);
4954 entry_parm = convert_to_mode (nominal_mode, tempreg,
4955 TREE_UNSIGNED (TREE_TYPE (parm)));
4956 if (stack_parm)
4957 /* ??? This may need a big-endian conversion on sparc64. */
4958 stack_parm = adjust_address (stack_parm, nominal_mode, 0);
4960 conversion_insns = get_insns ();
4961 did_conversion = 1;
4962 end_sequence ();
4965 if (entry_parm != stack_parm)
4967 if (stack_parm == 0)
4969 stack_parm
4970 = assign_stack_local (GET_MODE (entry_parm),
4971 GET_MODE_SIZE (GET_MODE (entry_parm)), 0);
4972 set_mem_attributes (stack_parm, parm, 1);
4975 if (promoted_mode != nominal_mode)
4977 push_to_sequence (conversion_insns);
4978 emit_move_insn (validize_mem (stack_parm),
4979 validize_mem (entry_parm));
4980 conversion_insns = get_insns ();
4981 end_sequence ();
4983 else
4984 emit_move_insn (validize_mem (stack_parm),
4985 validize_mem (entry_parm));
4988 SET_DECL_RTL (parm, stack_parm);
4991 /* If this "parameter" was the place where we are receiving the
4992 function's incoming structure pointer, set up the result. */
4993 if (parm == function_result_decl)
4995 tree result = DECL_RESULT (fndecl);
4996 rtx addr = DECL_RTL (parm);
4997 rtx x;
4999 #ifdef POINTERS_EXTEND_UNSIGNED
5000 if (GET_MODE (addr) != Pmode)
5001 addr = convert_memory_address (Pmode, addr);
5002 #endif
5004 x = gen_rtx_MEM (DECL_MODE (result), addr);
5005 set_mem_attributes (x, result, 1);
5006 SET_DECL_RTL (result, x);
5009 if (GET_CODE (DECL_RTL (parm)) == REG)
5010 REGNO_DECL (REGNO (DECL_RTL (parm))) = parm;
5011 else if (GET_CODE (DECL_RTL (parm)) == CONCAT)
5013 REGNO_DECL (REGNO (XEXP (DECL_RTL (parm), 0))) = parm;
5014 REGNO_DECL (REGNO (XEXP (DECL_RTL (parm), 1))) = parm;
5019 /* Output all parameter conversion instructions (possibly including calls)
5020 now that all parameters have been copied out of hard registers. */
5021 emit_insns (conversion_insns);
5023 last_parm_insn = get_last_insn ();
5025 current_function_args_size = stack_args_size.constant;
5027 /* Adjust function incoming argument size for alignment and
5028 minimum length. */
5030 #ifdef REG_PARM_STACK_SPACE
5031 #ifndef MAYBE_REG_PARM_STACK_SPACE
5032 current_function_args_size = MAX (current_function_args_size,
5033 REG_PARM_STACK_SPACE (fndecl));
5034 #endif
5035 #endif
5037 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
5039 current_function_args_size
5040 = ((current_function_args_size + STACK_BYTES - 1)
5041 / STACK_BYTES) * STACK_BYTES;
5043 #ifdef ARGS_GROW_DOWNWARD
5044 current_function_arg_offset_rtx
5045 = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
5046 : expand_expr (size_diffop (stack_args_size.var,
5047 size_int (-stack_args_size.constant)),
5048 NULL_RTX, VOIDmode, 0));
5049 #else
5050 current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
5051 #endif
5053 /* See how many bytes, if any, of its args a function should try to pop
5054 on return. */
5056 current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
5057 current_function_args_size);
5059 /* For stdarg.h function, save info about
5060 regs and stack space used by the named args. */
5062 if (!hide_last_arg)
5063 current_function_args_info = args_so_far;
5065 /* Set the rtx used for the function return value. Put this in its
5066 own variable so any optimizers that need this information don't have
5067 to include tree.h. Do this here so it gets done when an inlined
5068 function gets output. */
5070 current_function_return_rtx
5071 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
5072 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
5075 /* Indicate whether REGNO is an incoming argument to the current function
5076 that was promoted to a wider mode. If so, return the RTX for the
5077 register (to get its mode). PMODE and PUNSIGNEDP are set to the mode
5078 that REGNO is promoted from and whether the promotion was signed or
5079 unsigned. */
5081 #ifdef PROMOTE_FUNCTION_ARGS
5084 promoted_input_arg (regno, pmode, punsignedp)
5085 unsigned int regno;
5086 enum machine_mode *pmode;
5087 int *punsignedp;
5089 tree arg;
5091 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
5092 arg = TREE_CHAIN (arg))
5093 if (GET_CODE (DECL_INCOMING_RTL (arg)) == REG
5094 && REGNO (DECL_INCOMING_RTL (arg)) == regno
5095 && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
5097 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
5098 int unsignedp = TREE_UNSIGNED (TREE_TYPE (arg));
5100 mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
5101 if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
5102 && mode != DECL_MODE (arg))
5104 *pmode = DECL_MODE (arg);
5105 *punsignedp = unsignedp;
5106 return DECL_INCOMING_RTL (arg);
5110 return 0;
5113 #endif
5115 /* Compute the size and offset from the start of the stacked arguments for a
5116 parm passed in mode PASSED_MODE and with type TYPE.
5118 INITIAL_OFFSET_PTR points to the current offset into the stacked
5119 arguments.
5121 The starting offset and size for this parm are returned in *OFFSET_PTR
5122 and *ARG_SIZE_PTR, respectively.
5124 IN_REGS is non-zero if the argument will be passed in registers. It will
5125 never be set if REG_PARM_STACK_SPACE is not defined.
5127 FNDECL is the function in which the argument was defined.
5129 There are two types of rounding that are done. The first, controlled by
5130 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
5131 list to be aligned to the specific boundary (in bits). This rounding
5132 affects the initial and starting offsets, but not the argument size.
5134 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
5135 optionally rounds the size of the parm to PARM_BOUNDARY. The
5136 initial offset is not affected by this rounding, while the size always
5137 is and the starting offset may be. */
5139 /* offset_ptr will be negative for ARGS_GROW_DOWNWARD case;
5140 initial_offset_ptr is positive because locate_and_pad_parm's
5141 callers pass in the total size of args so far as
5142 initial_offset_ptr. arg_size_ptr is always positive.*/
5144 void
5145 locate_and_pad_parm (passed_mode, type, in_regs, fndecl,
5146 initial_offset_ptr, offset_ptr, arg_size_ptr,
5147 alignment_pad)
5148 enum machine_mode passed_mode;
5149 tree type;
5150 int in_regs ATTRIBUTE_UNUSED;
5151 tree fndecl ATTRIBUTE_UNUSED;
5152 struct args_size *initial_offset_ptr;
5153 struct args_size *offset_ptr;
5154 struct args_size *arg_size_ptr;
5155 struct args_size *alignment_pad;
5158 tree sizetree
5159 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
5160 enum direction where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
5161 int boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
5163 #ifdef REG_PARM_STACK_SPACE
5164 /* If we have found a stack parm before we reach the end of the
5165 area reserved for registers, skip that area. */
5166 if (! in_regs)
5168 int reg_parm_stack_space = 0;
5170 #ifdef MAYBE_REG_PARM_STACK_SPACE
5171 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
5172 #else
5173 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
5174 #endif
5175 if (reg_parm_stack_space > 0)
5177 if (initial_offset_ptr->var)
5179 initial_offset_ptr->var
5180 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
5181 ssize_int (reg_parm_stack_space));
5182 initial_offset_ptr->constant = 0;
5184 else if (initial_offset_ptr->constant < reg_parm_stack_space)
5185 initial_offset_ptr->constant = reg_parm_stack_space;
5188 #endif /* REG_PARM_STACK_SPACE */
5190 arg_size_ptr->var = 0;
5191 arg_size_ptr->constant = 0;
5192 alignment_pad->var = 0;
5193 alignment_pad->constant = 0;
5195 #ifdef ARGS_GROW_DOWNWARD
5196 if (initial_offset_ptr->var)
5198 offset_ptr->constant = 0;
5199 offset_ptr->var = size_binop (MINUS_EXPR, ssize_int (0),
5200 initial_offset_ptr->var);
5202 else
5204 offset_ptr->constant = -initial_offset_ptr->constant;
5205 offset_ptr->var = 0;
5207 if (where_pad != none
5208 && (!host_integerp (sizetree, 1)
5209 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5210 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5211 SUB_PARM_SIZE (*offset_ptr, sizetree);
5212 if (where_pad != downward)
5213 pad_to_arg_alignment (offset_ptr, boundary, alignment_pad);
5214 if (initial_offset_ptr->var)
5215 arg_size_ptr->var = size_binop (MINUS_EXPR,
5216 size_binop (MINUS_EXPR,
5217 ssize_int (0),
5218 initial_offset_ptr->var),
5219 offset_ptr->var);
5221 else
5222 arg_size_ptr->constant = (-initial_offset_ptr->constant
5223 - offset_ptr->constant);
5225 #else /* !ARGS_GROW_DOWNWARD */
5226 if (!in_regs
5227 #ifdef REG_PARM_STACK_SPACE
5228 || REG_PARM_STACK_SPACE (fndecl) > 0
5229 #endif
5231 pad_to_arg_alignment (initial_offset_ptr, boundary, alignment_pad);
5232 *offset_ptr = *initial_offset_ptr;
5234 #ifdef PUSH_ROUNDING
5235 if (passed_mode != BLKmode)
5236 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
5237 #endif
5239 /* Pad_below needs the pre-rounded size to know how much to pad below
5240 so this must be done before rounding up. */
5241 if (where_pad == downward
5242 /* However, BLKmode args passed in regs have their padding done elsewhere.
5243 The stack slot must be able to hold the entire register. */
5244 && !(in_regs && passed_mode == BLKmode))
5245 pad_below (offset_ptr, passed_mode, sizetree);
5247 if (where_pad != none
5248 && (!host_integerp (sizetree, 1)
5249 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5250 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5252 ADD_PARM_SIZE (*arg_size_ptr, sizetree);
5253 #endif /* ARGS_GROW_DOWNWARD */
5256 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
5257 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
5259 static void
5260 pad_to_arg_alignment (offset_ptr, boundary, alignment_pad)
5261 struct args_size *offset_ptr;
5262 int boundary;
5263 struct args_size *alignment_pad;
5265 tree save_var = NULL_TREE;
5266 HOST_WIDE_INT save_constant = 0;
5268 int boundary_in_bytes = boundary / BITS_PER_UNIT;
5270 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5272 save_var = offset_ptr->var;
5273 save_constant = offset_ptr->constant;
5276 alignment_pad->var = NULL_TREE;
5277 alignment_pad->constant = 0;
5279 if (boundary > BITS_PER_UNIT)
5281 if (offset_ptr->var)
5283 offset_ptr->var =
5284 #ifdef ARGS_GROW_DOWNWARD
5285 round_down
5286 #else
5287 round_up
5288 #endif
5289 (ARGS_SIZE_TREE (*offset_ptr),
5290 boundary / BITS_PER_UNIT);
5291 offset_ptr->constant = 0; /*?*/
5292 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5293 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
5294 save_var);
5296 else
5298 offset_ptr->constant =
5299 #ifdef ARGS_GROW_DOWNWARD
5300 FLOOR_ROUND (offset_ptr->constant, boundary_in_bytes);
5301 #else
5302 CEIL_ROUND (offset_ptr->constant, boundary_in_bytes);
5303 #endif
5304 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5305 alignment_pad->constant = offset_ptr->constant - save_constant;
5310 #ifndef ARGS_GROW_DOWNWARD
5311 static void
5312 pad_below (offset_ptr, passed_mode, sizetree)
5313 struct args_size *offset_ptr;
5314 enum machine_mode passed_mode;
5315 tree sizetree;
5317 if (passed_mode != BLKmode)
5319 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
5320 offset_ptr->constant
5321 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
5322 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
5323 - GET_MODE_SIZE (passed_mode));
5325 else
5327 if (TREE_CODE (sizetree) != INTEGER_CST
5328 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
5330 /* Round the size up to multiple of PARM_BOUNDARY bits. */
5331 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5332 /* Add it in. */
5333 ADD_PARM_SIZE (*offset_ptr, s2);
5334 SUB_PARM_SIZE (*offset_ptr, sizetree);
5338 #endif
5340 /* Walk the tree of blocks describing the binding levels within a function
5341 and warn about uninitialized variables.
5342 This is done after calling flow_analysis and before global_alloc
5343 clobbers the pseudo-regs to hard regs. */
5345 void
5346 uninitialized_vars_warning (block)
5347 tree block;
5349 tree decl, sub;
5350 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5352 if (warn_uninitialized
5353 && TREE_CODE (decl) == VAR_DECL
5354 /* These warnings are unreliable for and aggregates
5355 because assigning the fields one by one can fail to convince
5356 flow.c that the entire aggregate was initialized.
5357 Unions are troublesome because members may be shorter. */
5358 && ! AGGREGATE_TYPE_P (TREE_TYPE (decl))
5359 && DECL_RTL (decl) != 0
5360 && GET_CODE (DECL_RTL (decl)) == REG
5361 /* Global optimizations can make it difficult to determine if a
5362 particular variable has been initialized. However, a VAR_DECL
5363 with a nonzero DECL_INITIAL had an initializer, so do not
5364 claim it is potentially uninitialized.
5366 We do not care about the actual value in DECL_INITIAL, so we do
5367 not worry that it may be a dangling pointer. */
5368 && DECL_INITIAL (decl) == NULL_TREE
5369 && regno_uninitialized (REGNO (DECL_RTL (decl))))
5370 warning_with_decl (decl,
5371 "`%s' might be used uninitialized in this function");
5372 if (extra_warnings
5373 && TREE_CODE (decl) == VAR_DECL
5374 && DECL_RTL (decl) != 0
5375 && GET_CODE (DECL_RTL (decl)) == REG
5376 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5377 warning_with_decl (decl,
5378 "variable `%s' might be clobbered by `longjmp' or `vfork'");
5380 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5381 uninitialized_vars_warning (sub);
5384 /* Do the appropriate part of uninitialized_vars_warning
5385 but for arguments instead of local variables. */
5387 void
5388 setjmp_args_warning ()
5390 tree decl;
5391 for (decl = DECL_ARGUMENTS (current_function_decl);
5392 decl; decl = TREE_CHAIN (decl))
5393 if (DECL_RTL (decl) != 0
5394 && GET_CODE (DECL_RTL (decl)) == REG
5395 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5396 warning_with_decl (decl,
5397 "argument `%s' might be clobbered by `longjmp' or `vfork'");
5400 /* If this function call setjmp, put all vars into the stack
5401 unless they were declared `register'. */
5403 void
5404 setjmp_protect (block)
5405 tree block;
5407 tree decl, sub;
5408 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5409 if ((TREE_CODE (decl) == VAR_DECL
5410 || TREE_CODE (decl) == PARM_DECL)
5411 && DECL_RTL (decl) != 0
5412 && (GET_CODE (DECL_RTL (decl)) == REG
5413 || (GET_CODE (DECL_RTL (decl)) == MEM
5414 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5415 /* If this variable came from an inline function, it must be
5416 that its life doesn't overlap the setjmp. If there was a
5417 setjmp in the function, it would already be in memory. We
5418 must exclude such variable because their DECL_RTL might be
5419 set to strange things such as virtual_stack_vars_rtx. */
5420 && ! DECL_FROM_INLINE (decl)
5421 && (
5422 #ifdef NON_SAVING_SETJMP
5423 /* If longjmp doesn't restore the registers,
5424 don't put anything in them. */
5425 NON_SAVING_SETJMP
5427 #endif
5428 ! DECL_REGISTER (decl)))
5429 put_var_into_stack (decl);
5430 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5431 setjmp_protect (sub);
5434 /* Like the previous function, but for args instead of local variables. */
5436 void
5437 setjmp_protect_args ()
5439 tree decl;
5440 for (decl = DECL_ARGUMENTS (current_function_decl);
5441 decl; decl = TREE_CHAIN (decl))
5442 if ((TREE_CODE (decl) == VAR_DECL
5443 || TREE_CODE (decl) == PARM_DECL)
5444 && DECL_RTL (decl) != 0
5445 && (GET_CODE (DECL_RTL (decl)) == REG
5446 || (GET_CODE (DECL_RTL (decl)) == MEM
5447 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5448 && (
5449 /* If longjmp doesn't restore the registers,
5450 don't put anything in them. */
5451 #ifdef NON_SAVING_SETJMP
5452 NON_SAVING_SETJMP
5454 #endif
5455 ! DECL_REGISTER (decl)))
5456 put_var_into_stack (decl);
5459 /* Return the context-pointer register corresponding to DECL,
5460 or 0 if it does not need one. */
5463 lookup_static_chain (decl)
5464 tree decl;
5466 tree context = decl_function_context (decl);
5467 tree link;
5469 if (context == 0
5470 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_NO_STATIC_CHAIN (decl)))
5471 return 0;
5473 /* We treat inline_function_decl as an alias for the current function
5474 because that is the inline function whose vars, types, etc.
5475 are being merged into the current function.
5476 See expand_inline_function. */
5477 if (context == current_function_decl || context == inline_function_decl)
5478 return virtual_stack_vars_rtx;
5480 for (link = context_display; link; link = TREE_CHAIN (link))
5481 if (TREE_PURPOSE (link) == context)
5482 return RTL_EXPR_RTL (TREE_VALUE (link));
5484 abort ();
5487 /* Convert a stack slot address ADDR for variable VAR
5488 (from a containing function)
5489 into an address valid in this function (using a static chain). */
5492 fix_lexical_addr (addr, var)
5493 rtx addr;
5494 tree var;
5496 rtx basereg;
5497 HOST_WIDE_INT displacement;
5498 tree context = decl_function_context (var);
5499 struct function *fp;
5500 rtx base = 0;
5502 /* If this is the present function, we need not do anything. */
5503 if (context == current_function_decl || context == inline_function_decl)
5504 return addr;
5506 fp = find_function_data (context);
5508 if (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == MEM)
5509 addr = XEXP (XEXP (addr, 0), 0);
5511 /* Decode given address as base reg plus displacement. */
5512 if (GET_CODE (addr) == REG)
5513 basereg = addr, displacement = 0;
5514 else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
5515 basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
5516 else
5517 abort ();
5519 /* We accept vars reached via the containing function's
5520 incoming arg pointer and via its stack variables pointer. */
5521 if (basereg == fp->internal_arg_pointer)
5523 /* If reached via arg pointer, get the arg pointer value
5524 out of that function's stack frame.
5526 There are two cases: If a separate ap is needed, allocate a
5527 slot in the outer function for it and dereference it that way.
5528 This is correct even if the real ap is actually a pseudo.
5529 Otherwise, just adjust the offset from the frame pointer to
5530 compensate. */
5532 #ifdef NEED_SEPARATE_AP
5533 rtx addr;
5535 addr = get_arg_pointer_save_area (fp);
5536 addr = fix_lexical_addr (XEXP (addr, 0), var);
5537 addr = memory_address (Pmode, addr);
5539 base = gen_rtx_MEM (Pmode, addr);
5540 set_mem_alias_set (base, get_frame_alias_set ());
5541 base = copy_to_reg (base);
5542 #else
5543 displacement += (FIRST_PARM_OFFSET (context) - STARTING_FRAME_OFFSET);
5544 base = lookup_static_chain (var);
5545 #endif
5548 else if (basereg == virtual_stack_vars_rtx)
5550 /* This is the same code as lookup_static_chain, duplicated here to
5551 avoid an extra call to decl_function_context. */
5552 tree link;
5554 for (link = context_display; link; link = TREE_CHAIN (link))
5555 if (TREE_PURPOSE (link) == context)
5557 base = RTL_EXPR_RTL (TREE_VALUE (link));
5558 break;
5562 if (base == 0)
5563 abort ();
5565 /* Use same offset, relative to appropriate static chain or argument
5566 pointer. */
5567 return plus_constant (base, displacement);
5570 /* Return the address of the trampoline for entering nested fn FUNCTION.
5571 If necessary, allocate a trampoline (in the stack frame)
5572 and emit rtl to initialize its contents (at entry to this function). */
5575 trampoline_address (function)
5576 tree function;
5578 tree link;
5579 tree rtlexp;
5580 rtx tramp;
5581 struct function *fp;
5582 tree fn_context;
5584 /* Find an existing trampoline and return it. */
5585 for (link = trampoline_list; link; link = TREE_CHAIN (link))
5586 if (TREE_PURPOSE (link) == function)
5587 return
5588 adjust_trampoline_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0));
5590 for (fp = outer_function_chain; fp; fp = fp->outer)
5591 for (link = fp->x_trampoline_list; link; link = TREE_CHAIN (link))
5592 if (TREE_PURPOSE (link) == function)
5594 tramp = fix_lexical_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0),
5595 function);
5596 return adjust_trampoline_addr (tramp);
5599 /* None exists; we must make one. */
5601 /* Find the `struct function' for the function containing FUNCTION. */
5602 fp = 0;
5603 fn_context = decl_function_context (function);
5604 if (fn_context != current_function_decl
5605 && fn_context != inline_function_decl)
5606 fp = find_function_data (fn_context);
5608 /* Allocate run-time space for this trampoline
5609 (usually in the defining function's stack frame). */
5610 #ifdef ALLOCATE_TRAMPOLINE
5611 tramp = ALLOCATE_TRAMPOLINE (fp);
5612 #else
5613 /* If rounding needed, allocate extra space
5614 to ensure we have TRAMPOLINE_SIZE bytes left after rounding up. */
5615 #ifdef TRAMPOLINE_ALIGNMENT
5616 #define TRAMPOLINE_REAL_SIZE \
5617 (TRAMPOLINE_SIZE + (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT) - 1)
5618 #else
5619 #define TRAMPOLINE_REAL_SIZE (TRAMPOLINE_SIZE)
5620 #endif
5621 tramp = assign_stack_local_1 (BLKmode, TRAMPOLINE_REAL_SIZE, 0,
5622 fp ? fp : cfun);
5623 #endif
5625 /* Record the trampoline for reuse and note it for later initialization
5626 by expand_function_end. */
5627 if (fp != 0)
5629 rtlexp = make_node (RTL_EXPR);
5630 RTL_EXPR_RTL (rtlexp) = tramp;
5631 fp->x_trampoline_list = tree_cons (function, rtlexp,
5632 fp->x_trampoline_list);
5634 else
5636 /* Make the RTL_EXPR node temporary, not momentary, so that the
5637 trampoline_list doesn't become garbage. */
5638 rtlexp = make_node (RTL_EXPR);
5640 RTL_EXPR_RTL (rtlexp) = tramp;
5641 trampoline_list = tree_cons (function, rtlexp, trampoline_list);
5644 tramp = fix_lexical_addr (XEXP (tramp, 0), function);
5645 return adjust_trampoline_addr (tramp);
5648 /* Given a trampoline address,
5649 round it to multiple of TRAMPOLINE_ALIGNMENT. */
5651 static rtx
5652 round_trampoline_addr (tramp)
5653 rtx tramp;
5655 #ifdef TRAMPOLINE_ALIGNMENT
5656 /* Round address up to desired boundary. */
5657 rtx temp = gen_reg_rtx (Pmode);
5658 rtx addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5659 rtx mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5661 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5662 temp, 0, OPTAB_LIB_WIDEN);
5663 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5664 temp, 0, OPTAB_LIB_WIDEN);
5665 #endif
5666 return tramp;
5669 /* Given a trampoline address, round it then apply any
5670 platform-specific adjustments so that the result can be used for a
5671 function call . */
5673 static rtx
5674 adjust_trampoline_addr (tramp)
5675 rtx tramp;
5677 tramp = round_trampoline_addr (tramp);
5678 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5679 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5680 #endif
5681 return tramp;
5684 /* Put all this function's BLOCK nodes including those that are chained
5685 onto the first block into a vector, and return it.
5686 Also store in each NOTE for the beginning or end of a block
5687 the index of that block in the vector.
5688 The arguments are BLOCK, the chain of top-level blocks of the function,
5689 and INSNS, the insn chain of the function. */
5691 void
5692 identify_blocks ()
5694 int n_blocks;
5695 tree *block_vector, *last_block_vector;
5696 tree *block_stack;
5697 tree block = DECL_INITIAL (current_function_decl);
5699 if (block == 0)
5700 return;
5702 /* Fill the BLOCK_VECTOR with all of the BLOCKs in this function, in
5703 depth-first order. */
5704 block_vector = get_block_vector (block, &n_blocks);
5705 block_stack = (tree *) xmalloc (n_blocks * sizeof (tree));
5707 last_block_vector = identify_blocks_1 (get_insns (),
5708 block_vector + 1,
5709 block_vector + n_blocks,
5710 block_stack);
5712 /* If we didn't use all of the subblocks, we've misplaced block notes. */
5713 /* ??? This appears to happen all the time. Latent bugs elsewhere? */
5714 if (0 && last_block_vector != block_vector + n_blocks)
5715 abort ();
5717 free (block_vector);
5718 free (block_stack);
5721 /* Subroutine of identify_blocks. Do the block substitution on the
5722 insn chain beginning with INSNS. Recurse for CALL_PLACEHOLDER chains.
5724 BLOCK_STACK is pushed and popped for each BLOCK_BEGIN/BLOCK_END pair.
5725 BLOCK_VECTOR is incremented for each block seen. */
5727 static tree *
5728 identify_blocks_1 (insns, block_vector, end_block_vector, orig_block_stack)
5729 rtx insns;
5730 tree *block_vector;
5731 tree *end_block_vector;
5732 tree *orig_block_stack;
5734 rtx insn;
5735 tree *block_stack = orig_block_stack;
5737 for (insn = insns; insn; insn = NEXT_INSN (insn))
5739 if (GET_CODE (insn) == NOTE)
5741 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5743 tree b;
5745 /* If there are more block notes than BLOCKs, something
5746 is badly wrong. */
5747 if (block_vector == end_block_vector)
5748 abort ();
5750 b = *block_vector++;
5751 NOTE_BLOCK (insn) = b;
5752 *block_stack++ = b;
5754 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5756 /* If there are more NOTE_INSN_BLOCK_ENDs than
5757 NOTE_INSN_BLOCK_BEGs, something is badly wrong. */
5758 if (block_stack == orig_block_stack)
5759 abort ();
5761 NOTE_BLOCK (insn) = *--block_stack;
5764 else if (GET_CODE (insn) == CALL_INSN
5765 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5767 rtx cp = PATTERN (insn);
5769 block_vector = identify_blocks_1 (XEXP (cp, 0), block_vector,
5770 end_block_vector, block_stack);
5771 if (XEXP (cp, 1))
5772 block_vector = identify_blocks_1 (XEXP (cp, 1), block_vector,
5773 end_block_vector, block_stack);
5774 if (XEXP (cp, 2))
5775 block_vector = identify_blocks_1 (XEXP (cp, 2), block_vector,
5776 end_block_vector, block_stack);
5780 /* If there are more NOTE_INSN_BLOCK_BEGINs than NOTE_INSN_BLOCK_ENDs,
5781 something is badly wrong. */
5782 if (block_stack != orig_block_stack)
5783 abort ();
5785 return block_vector;
5788 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
5789 and create duplicate blocks. */
5790 /* ??? Need an option to either create block fragments or to create
5791 abstract origin duplicates of a source block. It really depends
5792 on what optimization has been performed. */
5794 void
5795 reorder_blocks ()
5797 tree block = DECL_INITIAL (current_function_decl);
5798 varray_type block_stack;
5800 if (block == NULL_TREE)
5801 return;
5803 VARRAY_TREE_INIT (block_stack, 10, "block_stack");
5805 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
5806 reorder_blocks_0 (block);
5808 /* Prune the old trees away, so that they don't get in the way. */
5809 BLOCK_SUBBLOCKS (block) = NULL_TREE;
5810 BLOCK_CHAIN (block) = NULL_TREE;
5812 /* Recreate the block tree from the note nesting. */
5813 reorder_blocks_1 (get_insns (), block, &block_stack);
5814 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
5816 /* Remove deleted blocks from the block fragment chains. */
5817 reorder_fix_fragments (block);
5819 VARRAY_FREE (block_stack);
5822 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
5824 static void
5825 reorder_blocks_0 (block)
5826 tree block;
5828 while (block)
5830 TREE_ASM_WRITTEN (block) = 0;
5831 reorder_blocks_0 (BLOCK_SUBBLOCKS (block));
5832 block = BLOCK_CHAIN (block);
5836 static void
5837 reorder_blocks_1 (insns, current_block, p_block_stack)
5838 rtx insns;
5839 tree current_block;
5840 varray_type *p_block_stack;
5842 rtx insn;
5844 for (insn = insns; insn; insn = NEXT_INSN (insn))
5846 if (GET_CODE (insn) == NOTE)
5848 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5850 tree block = NOTE_BLOCK (insn);
5852 /* If we have seen this block before, that means it now
5853 spans multiple address regions. Create a new fragment. */
5854 if (TREE_ASM_WRITTEN (block))
5856 tree new_block = copy_node (block);
5857 tree origin;
5859 origin = (BLOCK_FRAGMENT_ORIGIN (block)
5860 ? BLOCK_FRAGMENT_ORIGIN (block)
5861 : block);
5862 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
5863 BLOCK_FRAGMENT_CHAIN (new_block)
5864 = BLOCK_FRAGMENT_CHAIN (origin);
5865 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
5867 NOTE_BLOCK (insn) = new_block;
5868 block = new_block;
5871 BLOCK_SUBBLOCKS (block) = 0;
5872 TREE_ASM_WRITTEN (block) = 1;
5873 BLOCK_SUPERCONTEXT (block) = current_block;
5874 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
5875 BLOCK_SUBBLOCKS (current_block) = block;
5876 current_block = block;
5877 VARRAY_PUSH_TREE (*p_block_stack, block);
5879 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5881 NOTE_BLOCK (insn) = VARRAY_TOP_TREE (*p_block_stack);
5882 VARRAY_POP (*p_block_stack);
5883 BLOCK_SUBBLOCKS (current_block)
5884 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
5885 current_block = BLOCK_SUPERCONTEXT (current_block);
5888 else if (GET_CODE (insn) == CALL_INSN
5889 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5891 rtx cp = PATTERN (insn);
5892 reorder_blocks_1 (XEXP (cp, 0), current_block, p_block_stack);
5893 if (XEXP (cp, 1))
5894 reorder_blocks_1 (XEXP (cp, 1), current_block, p_block_stack);
5895 if (XEXP (cp, 2))
5896 reorder_blocks_1 (XEXP (cp, 2), current_block, p_block_stack);
5901 /* Rationalize BLOCK_FRAGMENT_ORIGIN. If an origin block no longer
5902 appears in the block tree, select one of the fragments to become
5903 the new origin block. */
5905 static void
5906 reorder_fix_fragments (block)
5907 tree block;
5909 while (block)
5911 tree dup_origin = BLOCK_FRAGMENT_ORIGIN (block);
5912 tree new_origin = NULL_TREE;
5914 if (dup_origin)
5916 if (! TREE_ASM_WRITTEN (dup_origin))
5918 new_origin = BLOCK_FRAGMENT_CHAIN (dup_origin);
5920 /* Find the first of the remaining fragments. There must
5921 be at least one -- the current block. */
5922 while (! TREE_ASM_WRITTEN (new_origin))
5923 new_origin = BLOCK_FRAGMENT_CHAIN (new_origin);
5924 BLOCK_FRAGMENT_ORIGIN (new_origin) = NULL_TREE;
5927 else if (! dup_origin)
5928 new_origin = block;
5930 /* Re-root the rest of the fragments to the new origin. In the
5931 case that DUP_ORIGIN was null, that means BLOCK was the origin
5932 of a chain of fragments and we want to remove those fragments
5933 that didn't make it to the output. */
5934 if (new_origin)
5936 tree *pp = &BLOCK_FRAGMENT_CHAIN (new_origin);
5937 tree chain = *pp;
5939 while (chain)
5941 if (TREE_ASM_WRITTEN (chain))
5943 BLOCK_FRAGMENT_ORIGIN (chain) = new_origin;
5944 *pp = chain;
5945 pp = &BLOCK_FRAGMENT_CHAIN (chain);
5947 chain = BLOCK_FRAGMENT_CHAIN (chain);
5949 *pp = NULL_TREE;
5952 reorder_fix_fragments (BLOCK_SUBBLOCKS (block));
5953 block = BLOCK_CHAIN (block);
5957 /* Reverse the order of elements in the chain T of blocks,
5958 and return the new head of the chain (old last element). */
5960 static tree
5961 blocks_nreverse (t)
5962 tree t;
5964 tree prev = 0, decl, next;
5965 for (decl = t; decl; decl = next)
5967 next = BLOCK_CHAIN (decl);
5968 BLOCK_CHAIN (decl) = prev;
5969 prev = decl;
5971 return prev;
5974 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
5975 non-NULL, list them all into VECTOR, in a depth-first preorder
5976 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
5977 blocks. */
5979 static int
5980 all_blocks (block, vector)
5981 tree block;
5982 tree *vector;
5984 int n_blocks = 0;
5986 while (block)
5988 TREE_ASM_WRITTEN (block) = 0;
5990 /* Record this block. */
5991 if (vector)
5992 vector[n_blocks] = block;
5994 ++n_blocks;
5996 /* Record the subblocks, and their subblocks... */
5997 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
5998 vector ? vector + n_blocks : 0);
5999 block = BLOCK_CHAIN (block);
6002 return n_blocks;
6005 /* Return a vector containing all the blocks rooted at BLOCK. The
6006 number of elements in the vector is stored in N_BLOCKS_P. The
6007 vector is dynamically allocated; it is the caller's responsibility
6008 to call `free' on the pointer returned. */
6010 static tree *
6011 get_block_vector (block, n_blocks_p)
6012 tree block;
6013 int *n_blocks_p;
6015 tree *block_vector;
6017 *n_blocks_p = all_blocks (block, NULL);
6018 block_vector = (tree *) xmalloc (*n_blocks_p * sizeof (tree));
6019 all_blocks (block, block_vector);
6021 return block_vector;
6024 static int next_block_index = 2;
6026 /* Set BLOCK_NUMBER for all the blocks in FN. */
6028 void
6029 number_blocks (fn)
6030 tree fn;
6032 int i;
6033 int n_blocks;
6034 tree *block_vector;
6036 /* For SDB and XCOFF debugging output, we start numbering the blocks
6037 from 1 within each function, rather than keeping a running
6038 count. */
6039 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
6040 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
6041 next_block_index = 1;
6042 #endif
6044 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
6046 /* The top-level BLOCK isn't numbered at all. */
6047 for (i = 1; i < n_blocks; ++i)
6048 /* We number the blocks from two. */
6049 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
6051 free (block_vector);
6053 return;
6056 /* If VAR is present in a subblock of BLOCK, return the subblock. */
6058 tree
6059 debug_find_var_in_block_tree (var, block)
6060 tree var;
6061 tree block;
6063 tree t;
6065 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
6066 if (t == var)
6067 return block;
6069 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
6071 tree ret = debug_find_var_in_block_tree (var, t);
6072 if (ret)
6073 return ret;
6076 return NULL_TREE;
6079 /* Allocate a function structure and reset its contents to the defaults. */
6081 static void
6082 prepare_function_start ()
6084 cfun = (struct function *) ggc_alloc_cleared (sizeof (struct function));
6086 init_stmt_for_function ();
6087 init_eh_for_function ();
6089 cse_not_expected = ! optimize;
6091 /* Caller save not needed yet. */
6092 caller_save_needed = 0;
6094 /* No stack slots have been made yet. */
6095 stack_slot_list = 0;
6097 current_function_has_nonlocal_label = 0;
6098 current_function_has_nonlocal_goto = 0;
6100 /* There is no stack slot for handling nonlocal gotos. */
6101 nonlocal_goto_handler_slots = 0;
6102 nonlocal_goto_stack_level = 0;
6104 /* No labels have been declared for nonlocal use. */
6105 nonlocal_labels = 0;
6106 nonlocal_goto_handler_labels = 0;
6108 /* No function calls so far in this function. */
6109 function_call_count = 0;
6111 /* No parm regs have been allocated.
6112 (This is important for output_inline_function.) */
6113 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
6115 /* Initialize the RTL mechanism. */
6116 init_emit ();
6118 /* Initialize the queue of pending postincrement and postdecrements,
6119 and some other info in expr.c. */
6120 init_expr ();
6122 /* We haven't done register allocation yet. */
6123 reg_renumber = 0;
6125 init_varasm_status (cfun);
6127 /* Clear out data used for inlining. */
6128 cfun->inlinable = 0;
6129 cfun->original_decl_initial = 0;
6130 cfun->original_arg_vector = 0;
6132 cfun->stack_alignment_needed = STACK_BOUNDARY;
6133 cfun->preferred_stack_boundary = STACK_BOUNDARY;
6135 /* Set if a call to setjmp is seen. */
6136 current_function_calls_setjmp = 0;
6138 /* Set if a call to longjmp is seen. */
6139 current_function_calls_longjmp = 0;
6141 current_function_calls_alloca = 0;
6142 current_function_contains_functions = 0;
6143 current_function_is_leaf = 0;
6144 current_function_nothrow = 0;
6145 current_function_sp_is_unchanging = 0;
6146 current_function_uses_only_leaf_regs = 0;
6147 current_function_has_computed_jump = 0;
6148 current_function_is_thunk = 0;
6150 current_function_returns_pcc_struct = 0;
6151 current_function_returns_struct = 0;
6152 current_function_epilogue_delay_list = 0;
6153 current_function_uses_const_pool = 0;
6154 current_function_uses_pic_offset_table = 0;
6155 current_function_cannot_inline = 0;
6157 /* We have not yet needed to make a label to jump to for tail-recursion. */
6158 tail_recursion_label = 0;
6160 /* We haven't had a need to make a save area for ap yet. */
6161 arg_pointer_save_area = 0;
6163 /* No stack slots allocated yet. */
6164 frame_offset = 0;
6166 /* No SAVE_EXPRs in this function yet. */
6167 save_expr_regs = 0;
6169 /* No RTL_EXPRs in this function yet. */
6170 rtl_expr_chain = 0;
6172 /* Set up to allocate temporaries. */
6173 init_temp_slots ();
6175 /* Indicate that we need to distinguish between the return value of the
6176 present function and the return value of a function being called. */
6177 rtx_equal_function_value_matters = 1;
6179 /* Indicate that we have not instantiated virtual registers yet. */
6180 virtuals_instantiated = 0;
6182 /* Indicate that we want CONCATs now. */
6183 generating_concat_p = 1;
6185 /* Indicate we have no need of a frame pointer yet. */
6186 frame_pointer_needed = 0;
6188 /* By default assume not varargs or stdarg. */
6189 current_function_varargs = 0;
6190 current_function_stdarg = 0;
6192 /* We haven't made any trampolines for this function yet. */
6193 trampoline_list = 0;
6195 init_pending_stack_adjust ();
6196 inhibit_defer_pop = 0;
6198 current_function_outgoing_args_size = 0;
6200 if (init_lang_status)
6201 (*init_lang_status) (cfun);
6202 if (init_machine_status)
6203 (*init_machine_status) (cfun);
6206 /* Initialize the rtl expansion mechanism so that we can do simple things
6207 like generate sequences. This is used to provide a context during global
6208 initialization of some passes. */
6209 void
6210 init_dummy_function_start ()
6212 prepare_function_start ();
6215 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
6216 and initialize static variables for generating RTL for the statements
6217 of the function. */
6219 void
6220 init_function_start (subr, filename, line)
6221 tree subr;
6222 const char *filename;
6223 int line;
6225 prepare_function_start ();
6227 current_function_name = (*decl_printable_name) (subr, 2);
6228 cfun->decl = subr;
6230 /* Nonzero if this is a nested function that uses a static chain. */
6232 current_function_needs_context
6233 = (decl_function_context (current_function_decl) != 0
6234 && ! DECL_NO_STATIC_CHAIN (current_function_decl));
6236 /* Within function body, compute a type's size as soon it is laid out. */
6237 immediate_size_expand++;
6239 /* Prevent ever trying to delete the first instruction of a function.
6240 Also tell final how to output a linenum before the function prologue.
6241 Note linenums could be missing, e.g. when compiling a Java .class file. */
6242 if (line > 0)
6243 emit_line_note (filename, line);
6245 /* Make sure first insn is a note even if we don't want linenums.
6246 This makes sure the first insn will never be deleted.
6247 Also, final expects a note to appear there. */
6248 emit_note (NULL, NOTE_INSN_DELETED);
6250 /* Set flags used by final.c. */
6251 if (aggregate_value_p (DECL_RESULT (subr)))
6253 #ifdef PCC_STATIC_STRUCT_RETURN
6254 current_function_returns_pcc_struct = 1;
6255 #endif
6256 current_function_returns_struct = 1;
6259 /* Warn if this value is an aggregate type,
6260 regardless of which calling convention we are using for it. */
6261 if (warn_aggregate_return
6262 && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
6263 warning ("function returns an aggregate");
6265 current_function_returns_pointer
6266 = POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
6269 /* Make sure all values used by the optimization passes have sane
6270 defaults. */
6271 void
6272 init_function_for_compilation ()
6274 reg_renumber = 0;
6276 /* No prologue/epilogue insns yet. */
6277 VARRAY_GROW (prologue, 0);
6278 VARRAY_GROW (epilogue, 0);
6279 VARRAY_GROW (sibcall_epilogue, 0);
6282 /* Indicate that the current function uses extra args
6283 not explicitly mentioned in the argument list in any fashion. */
6285 void
6286 mark_varargs ()
6288 current_function_varargs = 1;
6291 /* Expand a call to __main at the beginning of a possible main function. */
6293 #if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
6294 #undef HAS_INIT_SECTION
6295 #define HAS_INIT_SECTION
6296 #endif
6298 void
6299 expand_main_function ()
6301 #ifdef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
6302 if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN)
6304 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
6305 rtx tmp, seq;
6307 start_sequence ();
6308 /* Forcibly align the stack. */
6309 #ifdef STACK_GROWS_DOWNWARD
6310 tmp = expand_simple_binop (Pmode, AND, stack_pointer_rtx, GEN_INT(-align),
6311 stack_pointer_rtx, 1, OPTAB_WIDEN);
6312 #else
6313 tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
6314 GEN_INT (align - 1), NULL_RTX, 1, OPTAB_WIDEN);
6315 tmp = expand_simple_binop (Pmode, AND, tmp, GEN_INT (-align),
6316 stack_pointer_rtx, 1, OPTAB_WIDEN);
6317 #endif
6318 if (tmp != stack_pointer_rtx)
6319 emit_move_insn (stack_pointer_rtx, tmp);
6321 /* Enlist allocate_dynamic_stack_space to pick up the pieces. */
6322 tmp = force_reg (Pmode, const0_rtx);
6323 allocate_dynamic_stack_space (tmp, NULL_RTX, BIGGEST_ALIGNMENT);
6324 seq = gen_sequence ();
6325 end_sequence ();
6327 for (tmp = get_last_insn (); tmp; tmp = PREV_INSN (tmp))
6328 if (NOTE_P (tmp) && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_FUNCTION_BEG)
6329 break;
6330 if (tmp)
6331 emit_insn_before (seq, tmp);
6332 else
6333 emit_insn (seq);
6335 #endif
6337 #ifndef HAS_INIT_SECTION
6338 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, NAME__MAIN), LCT_NORMAL,
6339 VOIDmode, 0);
6340 #endif
6343 extern struct obstack permanent_obstack;
6345 /* The PENDING_SIZES represent the sizes of variable-sized types.
6346 Create RTL for the various sizes now (using temporary variables),
6347 so that we can refer to the sizes from the RTL we are generating
6348 for the current function. The PENDING_SIZES are a TREE_LIST. The
6349 TREE_VALUE of each node is a SAVE_EXPR. */
6351 void
6352 expand_pending_sizes (pending_sizes)
6353 tree pending_sizes;
6355 tree tem;
6357 /* Evaluate now the sizes of any types declared among the arguments. */
6358 for (tem = pending_sizes; tem; tem = TREE_CHAIN (tem))
6360 expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode, 0);
6361 /* Flush the queue in case this parameter declaration has
6362 side-effects. */
6363 emit_queue ();
6367 /* Start the RTL for a new function, and set variables used for
6368 emitting RTL.
6369 SUBR is the FUNCTION_DECL node.
6370 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
6371 the function's parameters, which must be run at any return statement. */
6373 void
6374 expand_function_start (subr, parms_have_cleanups)
6375 tree subr;
6376 int parms_have_cleanups;
6378 tree tem;
6379 rtx last_ptr = NULL_RTX;
6381 /* Make sure volatile mem refs aren't considered
6382 valid operands of arithmetic insns. */
6383 init_recog_no_volatile ();
6385 current_function_instrument_entry_exit
6386 = (flag_instrument_function_entry_exit
6387 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6389 current_function_limit_stack
6390 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
6392 /* If function gets a static chain arg, store it in the stack frame.
6393 Do this first, so it gets the first stack slot offset. */
6394 if (current_function_needs_context)
6396 last_ptr = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
6398 /* Delay copying static chain if it is not a register to avoid
6399 conflicts with regs used for parameters. */
6400 if (! SMALL_REGISTER_CLASSES
6401 || GET_CODE (static_chain_incoming_rtx) == REG)
6402 emit_move_insn (last_ptr, static_chain_incoming_rtx);
6405 /* If the parameters of this function need cleaning up, get a label
6406 for the beginning of the code which executes those cleanups. This must
6407 be done before doing anything with return_label. */
6408 if (parms_have_cleanups)
6409 cleanup_label = gen_label_rtx ();
6410 else
6411 cleanup_label = 0;
6413 /* Make the label for return statements to jump to. Do not special
6414 case machines with special return instructions -- they will be
6415 handled later during jump, ifcvt, or epilogue creation. */
6416 return_label = gen_label_rtx ();
6418 /* Initialize rtx used to return the value. */
6419 /* Do this before assign_parms so that we copy the struct value address
6420 before any library calls that assign parms might generate. */
6422 /* Decide whether to return the value in memory or in a register. */
6423 if (aggregate_value_p (DECL_RESULT (subr)))
6425 /* Returning something that won't go in a register. */
6426 rtx value_address = 0;
6428 #ifdef PCC_STATIC_STRUCT_RETURN
6429 if (current_function_returns_pcc_struct)
6431 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
6432 value_address = assemble_static_space (size);
6434 else
6435 #endif
6437 /* Expect to be passed the address of a place to store the value.
6438 If it is passed as an argument, assign_parms will take care of
6439 it. */
6440 if (struct_value_incoming_rtx)
6442 value_address = gen_reg_rtx (Pmode);
6443 emit_move_insn (value_address, struct_value_incoming_rtx);
6446 if (value_address)
6448 rtx x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), value_address);
6449 set_mem_attributes (x, DECL_RESULT (subr), 1);
6450 SET_DECL_RTL (DECL_RESULT (subr), x);
6453 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
6454 /* If return mode is void, this decl rtl should not be used. */
6455 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
6456 else
6458 /* Compute the return values into a pseudo reg, which we will copy
6459 into the true return register after the cleanups are done. */
6461 /* In order to figure out what mode to use for the pseudo, we
6462 figure out what the mode of the eventual return register will
6463 actually be, and use that. */
6464 rtx hard_reg
6465 = hard_function_value (TREE_TYPE (DECL_RESULT (subr)),
6466 subr, 1);
6468 /* Structures that are returned in registers are not aggregate_value_p,
6469 so we may see a PARALLEL. Don't play pseudo games with this. */
6470 if (! REG_P (hard_reg))
6471 SET_DECL_RTL (DECL_RESULT (subr), hard_reg);
6472 else
6474 /* Create the pseudo. */
6475 SET_DECL_RTL (DECL_RESULT (subr), gen_reg_rtx (GET_MODE (hard_reg)));
6477 /* Needed because we may need to move this to memory
6478 in case it's a named return value whose address is taken. */
6479 DECL_REGISTER (DECL_RESULT (subr)) = 1;
6483 /* Initialize rtx for parameters and local variables.
6484 In some cases this requires emitting insns. */
6486 assign_parms (subr);
6488 /* Copy the static chain now if it wasn't a register. The delay is to
6489 avoid conflicts with the parameter passing registers. */
6491 if (SMALL_REGISTER_CLASSES && current_function_needs_context)
6492 if (GET_CODE (static_chain_incoming_rtx) != REG)
6493 emit_move_insn (last_ptr, static_chain_incoming_rtx);
6495 /* The following was moved from init_function_start.
6496 The move is supposed to make sdb output more accurate. */
6497 /* Indicate the beginning of the function body,
6498 as opposed to parm setup. */
6499 emit_note (NULL, NOTE_INSN_FUNCTION_BEG);
6501 if (GET_CODE (get_last_insn ()) != NOTE)
6502 emit_note (NULL, NOTE_INSN_DELETED);
6503 parm_birth_insn = get_last_insn ();
6505 context_display = 0;
6506 if (current_function_needs_context)
6508 /* Fetch static chain values for containing functions. */
6509 tem = decl_function_context (current_function_decl);
6510 /* Copy the static chain pointer into a pseudo. If we have
6511 small register classes, copy the value from memory if
6512 static_chain_incoming_rtx is a REG. */
6513 if (tem)
6515 /* If the static chain originally came in a register, put it back
6516 there, then move it out in the next insn. The reason for
6517 this peculiar code is to satisfy function integration. */
6518 if (SMALL_REGISTER_CLASSES
6519 && GET_CODE (static_chain_incoming_rtx) == REG)
6520 emit_move_insn (static_chain_incoming_rtx, last_ptr);
6521 last_ptr = copy_to_reg (static_chain_incoming_rtx);
6524 while (tem)
6526 tree rtlexp = make_node (RTL_EXPR);
6528 RTL_EXPR_RTL (rtlexp) = last_ptr;
6529 context_display = tree_cons (tem, rtlexp, context_display);
6530 tem = decl_function_context (tem);
6531 if (tem == 0)
6532 break;
6533 /* Chain thru stack frames, assuming pointer to next lexical frame
6534 is found at the place we always store it. */
6535 #ifdef FRAME_GROWS_DOWNWARD
6536 last_ptr = plus_constant (last_ptr,
6537 -(HOST_WIDE_INT) GET_MODE_SIZE (Pmode));
6538 #endif
6539 last_ptr = gen_rtx_MEM (Pmode, memory_address (Pmode, last_ptr));
6540 set_mem_alias_set (last_ptr, get_frame_alias_set ());
6541 last_ptr = copy_to_reg (last_ptr);
6543 /* If we are not optimizing, ensure that we know that this
6544 piece of context is live over the entire function. */
6545 if (! optimize)
6546 save_expr_regs = gen_rtx_EXPR_LIST (VOIDmode, last_ptr,
6547 save_expr_regs);
6551 if (current_function_instrument_entry_exit)
6553 rtx fun = DECL_RTL (current_function_decl);
6554 if (GET_CODE (fun) == MEM)
6555 fun = XEXP (fun, 0);
6556 else
6557 abort ();
6558 emit_library_call (profile_function_entry_libfunc, LCT_NORMAL, VOIDmode,
6559 2, fun, Pmode,
6560 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6562 hard_frame_pointer_rtx),
6563 Pmode);
6566 #ifdef PROFILE_HOOK
6567 if (profile_flag)
6568 PROFILE_HOOK (profile_label_no);
6569 #endif
6571 /* After the display initializations is where the tail-recursion label
6572 should go, if we end up needing one. Ensure we have a NOTE here
6573 since some things (like trampolines) get placed before this. */
6574 tail_recursion_reentry = emit_note (NULL, NOTE_INSN_DELETED);
6576 /* Evaluate now the sizes of any types declared among the arguments. */
6577 expand_pending_sizes (nreverse (get_pending_sizes ()));
6579 /* Make sure there is a line number after the function entry setup code. */
6580 force_next_line_note ();
6583 /* Undo the effects of init_dummy_function_start. */
6584 void
6585 expand_dummy_function_end ()
6587 /* End any sequences that failed to be closed due to syntax errors. */
6588 while (in_sequence_p ())
6589 end_sequence ();
6591 /* Outside function body, can't compute type's actual size
6592 until next function's body starts. */
6594 free_after_parsing (cfun);
6595 free_after_compilation (cfun);
6596 cfun = 0;
6599 /* Call DOIT for each hard register used as a return value from
6600 the current function. */
6602 void
6603 diddle_return_value (doit, arg)
6604 void (*doit) PARAMS ((rtx, void *));
6605 void *arg;
6607 rtx outgoing = current_function_return_rtx;
6609 if (! outgoing)
6610 return;
6612 if (GET_CODE (outgoing) == REG)
6613 (*doit) (outgoing, arg);
6614 else if (GET_CODE (outgoing) == PARALLEL)
6616 int i;
6618 for (i = 0; i < XVECLEN (outgoing, 0); i++)
6620 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
6622 if (GET_CODE (x) == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
6623 (*doit) (x, arg);
6628 static void
6629 do_clobber_return_reg (reg, arg)
6630 rtx reg;
6631 void *arg ATTRIBUTE_UNUSED;
6633 emit_insn (gen_rtx_CLOBBER (VOIDmode, reg));
6636 void
6637 clobber_return_register ()
6639 diddle_return_value (do_clobber_return_reg, NULL);
6641 /* In case we do use pseudo to return value, clobber it too. */
6642 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6644 tree decl_result = DECL_RESULT (current_function_decl);
6645 rtx decl_rtl = DECL_RTL (decl_result);
6646 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
6648 do_clobber_return_reg (decl_rtl, NULL);
6653 static void
6654 do_use_return_reg (reg, arg)
6655 rtx reg;
6656 void *arg ATTRIBUTE_UNUSED;
6658 emit_insn (gen_rtx_USE (VOIDmode, reg));
6661 void
6662 use_return_register ()
6664 diddle_return_value (do_use_return_reg, NULL);
6667 /* Generate RTL for the end of the current function.
6668 FILENAME and LINE are the current position in the source file.
6670 It is up to language-specific callers to do cleanups for parameters--
6671 or else, supply 1 for END_BINDINGS and we will call expand_end_bindings. */
6673 void
6674 expand_function_end (filename, line, end_bindings)
6675 const char *filename;
6676 int line;
6677 int end_bindings;
6679 tree link;
6680 rtx clobber_after;
6682 #ifdef TRAMPOLINE_TEMPLATE
6683 static rtx initial_trampoline;
6684 #endif
6686 finish_expr_for_function ();
6688 /* If arg_pointer_save_area was referenced only from a nested
6689 function, we will not have initialized it yet. Do that now. */
6690 if (arg_pointer_save_area && ! cfun->arg_pointer_save_area_init)
6691 get_arg_pointer_save_area (cfun);
6693 #ifdef NON_SAVING_SETJMP
6694 /* Don't put any variables in registers if we call setjmp
6695 on a machine that fails to restore the registers. */
6696 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
6698 if (DECL_INITIAL (current_function_decl) != error_mark_node)
6699 setjmp_protect (DECL_INITIAL (current_function_decl));
6701 setjmp_protect_args ();
6703 #endif
6705 /* Initialize any trampolines required by this function. */
6706 for (link = trampoline_list; link; link = TREE_CHAIN (link))
6708 tree function = TREE_PURPOSE (link);
6709 rtx context ATTRIBUTE_UNUSED = lookup_static_chain (function);
6710 rtx tramp = RTL_EXPR_RTL (TREE_VALUE (link));
6711 #ifdef TRAMPOLINE_TEMPLATE
6712 rtx blktramp;
6713 #endif
6714 rtx seq;
6716 #ifdef TRAMPOLINE_TEMPLATE
6717 /* First make sure this compilation has a template for
6718 initializing trampolines. */
6719 if (initial_trampoline == 0)
6721 initial_trampoline
6722 = gen_rtx_MEM (BLKmode, assemble_trampoline_template ());
6723 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
6725 ggc_add_rtx_root (&initial_trampoline, 1);
6727 #endif
6729 /* Generate insns to initialize the trampoline. */
6730 start_sequence ();
6731 tramp = round_trampoline_addr (XEXP (tramp, 0));
6732 #ifdef TRAMPOLINE_TEMPLATE
6733 blktramp = replace_equiv_address (initial_trampoline, tramp);
6734 emit_block_move (blktramp, initial_trampoline,
6735 GEN_INT (TRAMPOLINE_SIZE));
6736 #endif
6737 INITIALIZE_TRAMPOLINE (tramp, XEXP (DECL_RTL (function), 0), context);
6738 seq = get_insns ();
6739 end_sequence ();
6741 /* Put those insns at entry to the containing function (this one). */
6742 emit_insns_before (seq, tail_recursion_reentry);
6745 /* If we are doing stack checking and this function makes calls,
6746 do a stack probe at the start of the function to ensure we have enough
6747 space for another stack frame. */
6748 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
6750 rtx insn, seq;
6752 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6753 if (GET_CODE (insn) == CALL_INSN)
6755 start_sequence ();
6756 probe_stack_range (STACK_CHECK_PROTECT,
6757 GEN_INT (STACK_CHECK_MAX_FRAME_SIZE));
6758 seq = get_insns ();
6759 end_sequence ();
6760 emit_insns_before (seq, tail_recursion_reentry);
6761 break;
6765 /* Warn about unused parms if extra warnings were specified. */
6766 /* Either ``-W -Wunused'' or ``-Wunused-parameter'' enables this
6767 warning. WARN_UNUSED_PARAMETER is negative when set by
6768 -Wunused. */
6769 if (warn_unused_parameter > 0
6770 || (warn_unused_parameter < 0 && extra_warnings))
6772 tree decl;
6774 for (decl = DECL_ARGUMENTS (current_function_decl);
6775 decl; decl = TREE_CHAIN (decl))
6776 if (! TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
6777 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
6778 warning_with_decl (decl, "unused parameter `%s'");
6781 /* Delete handlers for nonlocal gotos if nothing uses them. */
6782 if (nonlocal_goto_handler_slots != 0
6783 && ! current_function_has_nonlocal_label)
6784 delete_handlers ();
6786 /* End any sequences that failed to be closed due to syntax errors. */
6787 while (in_sequence_p ())
6788 end_sequence ();
6790 /* Outside function body, can't compute type's actual size
6791 until next function's body starts. */
6792 immediate_size_expand--;
6794 clear_pending_stack_adjust ();
6795 do_pending_stack_adjust ();
6797 /* Mark the end of the function body.
6798 If control reaches this insn, the function can drop through
6799 without returning a value. */
6800 emit_note (NULL, NOTE_INSN_FUNCTION_END);
6802 /* Must mark the last line number note in the function, so that the test
6803 coverage code can avoid counting the last line twice. This just tells
6804 the code to ignore the immediately following line note, since there
6805 already exists a copy of this note somewhere above. This line number
6806 note is still needed for debugging though, so we can't delete it. */
6807 if (flag_test_coverage)
6808 emit_note (NULL, NOTE_INSN_REPEATED_LINE_NUMBER);
6810 /* Output a linenumber for the end of the function.
6811 SDB depends on this. */
6812 emit_line_note_force (filename, line);
6814 /* Before the return label (if any), clobber the return
6815 registers so that they are not propagated live to the rest of
6816 the function. This can only happen with functions that drop
6817 through; if there had been a return statement, there would
6818 have either been a return rtx, or a jump to the return label.
6820 We delay actual code generation after the current_function_value_rtx
6821 is computed. */
6822 clobber_after = get_last_insn ();
6824 /* Output the label for the actual return from the function,
6825 if one is expected. This happens either because a function epilogue
6826 is used instead of a return instruction, or because a return was done
6827 with a goto in order to run local cleanups, or because of pcc-style
6828 structure returning. */
6829 if (return_label)
6830 emit_label (return_label);
6832 /* C++ uses this. */
6833 if (end_bindings)
6834 expand_end_bindings (0, 0, 0);
6836 if (current_function_instrument_entry_exit)
6838 rtx fun = DECL_RTL (current_function_decl);
6839 if (GET_CODE (fun) == MEM)
6840 fun = XEXP (fun, 0);
6841 else
6842 abort ();
6843 emit_library_call (profile_function_exit_libfunc, LCT_NORMAL, VOIDmode,
6844 2, fun, Pmode,
6845 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6847 hard_frame_pointer_rtx),
6848 Pmode);
6851 /* Let except.c know where it should emit the call to unregister
6852 the function context for sjlj exceptions. */
6853 if (flag_exceptions && USING_SJLJ_EXCEPTIONS)
6854 sjlj_emit_function_exit_after (get_last_insn ());
6856 /* If we had calls to alloca, and this machine needs
6857 an accurate stack pointer to exit the function,
6858 insert some code to save and restore the stack pointer. */
6859 #ifdef EXIT_IGNORE_STACK
6860 if (! EXIT_IGNORE_STACK)
6861 #endif
6862 if (current_function_calls_alloca)
6864 rtx tem = 0;
6866 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
6867 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
6870 /* If scalar return value was computed in a pseudo-reg, or was a named
6871 return value that got dumped to the stack, copy that to the hard
6872 return register. */
6873 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6875 tree decl_result = DECL_RESULT (current_function_decl);
6876 rtx decl_rtl = DECL_RTL (decl_result);
6878 if (REG_P (decl_rtl)
6879 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
6880 : DECL_REGISTER (decl_result))
6882 rtx real_decl_rtl;
6884 #ifdef FUNCTION_OUTGOING_VALUE
6885 real_decl_rtl = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl_result),
6886 current_function_decl);
6887 #else
6888 real_decl_rtl = FUNCTION_VALUE (TREE_TYPE (decl_result),
6889 current_function_decl);
6890 #endif
6891 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
6893 /* If this is a BLKmode structure being returned in registers,
6894 then use the mode computed in expand_return. Note that if
6895 decl_rtl is memory, then its mode may have been changed,
6896 but that current_function_return_rtx has not. */
6897 if (GET_MODE (real_decl_rtl) == BLKmode)
6898 PUT_MODE (real_decl_rtl, GET_MODE (current_function_return_rtx));
6900 /* If a named return value dumped decl_return to memory, then
6901 we may need to re-do the PROMOTE_MODE signed/unsigned
6902 extension. */
6903 if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
6905 int unsignedp = TREE_UNSIGNED (TREE_TYPE (decl_result));
6907 #ifdef PROMOTE_FUNCTION_RETURN
6908 promote_mode (TREE_TYPE (decl_result), GET_MODE (decl_rtl),
6909 &unsignedp, 1);
6910 #endif
6912 convert_move (real_decl_rtl, decl_rtl, unsignedp);
6914 else if (GET_CODE (real_decl_rtl) == PARALLEL)
6915 emit_group_load (real_decl_rtl, decl_rtl,
6916 int_size_in_bytes (TREE_TYPE (decl_result)));
6917 else
6918 emit_move_insn (real_decl_rtl, decl_rtl);
6920 /* The delay slot scheduler assumes that current_function_return_rtx
6921 holds the hard register containing the return value, not a
6922 temporary pseudo. */
6923 current_function_return_rtx = real_decl_rtl;
6927 /* If returning a structure, arrange to return the address of the value
6928 in a place where debuggers expect to find it.
6930 If returning a structure PCC style,
6931 the caller also depends on this value.
6932 And current_function_returns_pcc_struct is not necessarily set. */
6933 if (current_function_returns_struct
6934 || current_function_returns_pcc_struct)
6936 rtx value_address
6937 = XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6938 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
6939 #ifdef FUNCTION_OUTGOING_VALUE
6940 rtx outgoing
6941 = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
6942 current_function_decl);
6943 #else
6944 rtx outgoing
6945 = FUNCTION_VALUE (build_pointer_type (type), current_function_decl);
6946 #endif
6948 /* Mark this as a function return value so integrate will delete the
6949 assignment and USE below when inlining this function. */
6950 REG_FUNCTION_VALUE_P (outgoing) = 1;
6952 #ifdef POINTERS_EXTEND_UNSIGNED
6953 /* The address may be ptr_mode and OUTGOING may be Pmode. */
6954 if (GET_MODE (outgoing) != GET_MODE (value_address))
6955 value_address = convert_memory_address (GET_MODE (outgoing),
6956 value_address);
6957 #endif
6959 emit_move_insn (outgoing, value_address);
6961 /* Show return register used to hold result (in this case the address
6962 of the result. */
6963 current_function_return_rtx = outgoing;
6966 /* If this is an implementation of throw, do what's necessary to
6967 communicate between __builtin_eh_return and the epilogue. */
6968 expand_eh_return ();
6970 /* Emit the actual code to clobber return register. */
6972 rtx seq, after;
6974 start_sequence ();
6975 clobber_return_register ();
6976 seq = gen_sequence ();
6977 end_sequence ();
6979 after = emit_insn_after (seq, clobber_after);
6981 if (clobber_after != after)
6982 cfun->x_clobber_return_insn = after;
6985 /* ??? This should no longer be necessary since stupid is no longer with
6986 us, but there are some parts of the compiler (eg reload_combine, and
6987 sh mach_dep_reorg) that still try and compute their own lifetime info
6988 instead of using the general framework. */
6989 use_return_register ();
6991 /* Fix up any gotos that jumped out to the outermost
6992 binding level of the function.
6993 Must follow emitting RETURN_LABEL. */
6995 /* If you have any cleanups to do at this point,
6996 and they need to create temporary variables,
6997 then you will lose. */
6998 expand_fixups (get_insns ());
7002 get_arg_pointer_save_area (f)
7003 struct function *f;
7005 rtx ret = f->x_arg_pointer_save_area;
7007 if (! ret)
7009 ret = assign_stack_local_1 (Pmode, GET_MODE_SIZE (Pmode), 0, f);
7010 f->x_arg_pointer_save_area = ret;
7013 if (f == cfun && ! f->arg_pointer_save_area_init)
7015 rtx seq;
7017 /* Save the arg pointer at the beginning of the function. The
7018 generated stack slot may not be a valid memory address, so we
7019 have to check it and fix it if necessary. */
7020 start_sequence ();
7021 emit_move_insn (validize_mem (ret), virtual_incoming_args_rtx);
7022 seq = gen_sequence ();
7023 end_sequence ();
7025 push_topmost_sequence ();
7026 emit_insn_after (seq, get_insns ());
7027 pop_topmost_sequence ();
7030 return ret;
7033 /* Extend a vector that records the INSN_UIDs of INSNS (either a
7034 sequence or a single insn). */
7036 static void
7037 record_insns (insns, vecp)
7038 rtx insns;
7039 varray_type *vecp;
7041 if (GET_CODE (insns) == SEQUENCE)
7043 int len = XVECLEN (insns, 0);
7044 int i = VARRAY_SIZE (*vecp);
7046 VARRAY_GROW (*vecp, i + len);
7047 while (--len >= 0)
7049 VARRAY_INT (*vecp, i) = INSN_UID (XVECEXP (insns, 0, len));
7050 ++i;
7053 else
7055 int i = VARRAY_SIZE (*vecp);
7056 VARRAY_GROW (*vecp, i + 1);
7057 VARRAY_INT (*vecp, i) = INSN_UID (insns);
7061 /* Determine how many INSN_UIDs in VEC are part of INSN. */
7063 static int
7064 contains (insn, vec)
7065 rtx insn;
7066 varray_type vec;
7068 int i, j;
7070 if (GET_CODE (insn) == INSN
7071 && GET_CODE (PATTERN (insn)) == SEQUENCE)
7073 int count = 0;
7074 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
7075 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7076 if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == VARRAY_INT (vec, j))
7077 count++;
7078 return count;
7080 else
7082 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7083 if (INSN_UID (insn) == VARRAY_INT (vec, j))
7084 return 1;
7086 return 0;
7090 prologue_epilogue_contains (insn)
7091 rtx insn;
7093 if (contains (insn, prologue))
7094 return 1;
7095 if (contains (insn, epilogue))
7096 return 1;
7097 return 0;
7101 sibcall_epilogue_contains (insn)
7102 rtx insn;
7104 if (sibcall_epilogue)
7105 return contains (insn, sibcall_epilogue);
7106 return 0;
7109 #ifdef HAVE_return
7110 /* Insert gen_return at the end of block BB. This also means updating
7111 block_for_insn appropriately. */
7113 static void
7114 emit_return_into_block (bb, line_note)
7115 basic_block bb;
7116 rtx line_note;
7118 rtx p, end;
7120 p = NEXT_INSN (bb->end);
7121 end = emit_jump_insn_after (gen_return (), bb->end);
7122 if (line_note)
7123 emit_line_note_after (NOTE_SOURCE_FILE (line_note),
7124 NOTE_LINE_NUMBER (line_note), PREV_INSN (bb->end));
7126 #endif /* HAVE_return */
7128 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
7130 /* These functions convert the epilogue into a variant that does not modify the
7131 stack pointer. This is used in cases where a function returns an object
7132 whose size is not known until it is computed. The called function leaves the
7133 object on the stack, leaves the stack depressed, and returns a pointer to
7134 the object.
7136 What we need to do is track all modifications and references to the stack
7137 pointer, deleting the modifications and changing the references to point to
7138 the location the stack pointer would have pointed to had the modifications
7139 taken place.
7141 These functions need to be portable so we need to make as few assumptions
7142 about the epilogue as we can. However, the epilogue basically contains
7143 three things: instructions to reset the stack pointer, instructions to
7144 reload registers, possibly including the frame pointer, and an
7145 instruction to return to the caller.
7147 If we can't be sure of what a relevant epilogue insn is doing, we abort.
7148 We also make no attempt to validate the insns we make since if they are
7149 invalid, we probably can't do anything valid. The intent is that these
7150 routines get "smarter" as more and more machines start to use them and
7151 they try operating on different epilogues.
7153 We use the following structure to track what the part of the epilogue that
7154 we've already processed has done. We keep two copies of the SP equivalence,
7155 one for use during the insn we are processing and one for use in the next
7156 insn. The difference is because one part of a PARALLEL may adjust SP
7157 and the other may use it. */
7159 struct epi_info
7161 rtx sp_equiv_reg; /* REG that SP is set from, perhaps SP. */
7162 HOST_WIDE_INT sp_offset; /* Offset from SP_EQUIV_REG of present SP. */
7163 rtx new_sp_equiv_reg; /* REG to be used at end of insn. */
7164 HOST_WIDE_INT new_sp_offset; /* Offset to be used at end of insn. */
7165 rtx equiv_reg_src; /* If nonzero, the value that SP_EQUIV_REG
7166 should be set to once we no longer need
7167 its value. */
7170 static void handle_epilogue_set PARAMS ((rtx, struct epi_info *));
7171 static void emit_equiv_load PARAMS ((struct epi_info *));
7173 /* Modify SEQ, a SEQUENCE that is part of the epilogue, to no modifications
7174 to the stack pointer. Return the new sequence. */
7176 static rtx
7177 keep_stack_depressed (seq)
7178 rtx seq;
7180 int i, j;
7181 struct epi_info info;
7183 /* If the epilogue is just a single instruction, it ust be OK as is. */
7185 if (GET_CODE (seq) != SEQUENCE)
7186 return seq;
7188 /* Otherwise, start a sequence, initialize the information we have, and
7189 process all the insns we were given. */
7190 start_sequence ();
7192 info.sp_equiv_reg = stack_pointer_rtx;
7193 info.sp_offset = 0;
7194 info.equiv_reg_src = 0;
7196 for (i = 0; i < XVECLEN (seq, 0); i++)
7198 rtx insn = XVECEXP (seq, 0, i);
7200 if (!INSN_P (insn))
7202 add_insn (insn);
7203 continue;
7206 /* If this insn references the register that SP is equivalent to and
7207 we have a pending load to that register, we must force out the load
7208 first and then indicate we no longer know what SP's equivalent is. */
7209 if (info.equiv_reg_src != 0
7210 && reg_referenced_p (info.sp_equiv_reg, PATTERN (insn)))
7212 emit_equiv_load (&info);
7213 info.sp_equiv_reg = 0;
7216 info.new_sp_equiv_reg = info.sp_equiv_reg;
7217 info.new_sp_offset = info.sp_offset;
7219 /* If this is a (RETURN) and the return address is on the stack,
7220 update the address and change to an indirect jump. */
7221 if (GET_CODE (PATTERN (insn)) == RETURN
7222 || (GET_CODE (PATTERN (insn)) == PARALLEL
7223 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == RETURN))
7225 rtx retaddr = INCOMING_RETURN_ADDR_RTX;
7226 rtx base = 0;
7227 HOST_WIDE_INT offset = 0;
7228 rtx jump_insn, jump_set;
7230 /* If the return address is in a register, we can emit the insn
7231 unchanged. Otherwise, it must be a MEM and we see what the
7232 base register and offset are. In any case, we have to emit any
7233 pending load to the equivalent reg of SP, if any. */
7234 if (GET_CODE (retaddr) == REG)
7236 emit_equiv_load (&info);
7237 add_insn (insn);
7238 continue;
7240 else if (GET_CODE (retaddr) == MEM
7241 && GET_CODE (XEXP (retaddr, 0)) == REG)
7242 base = gen_rtx_REG (Pmode, REGNO (XEXP (retaddr, 0))), offset = 0;
7243 else if (GET_CODE (retaddr) == MEM
7244 && GET_CODE (XEXP (retaddr, 0)) == PLUS
7245 && GET_CODE (XEXP (XEXP (retaddr, 0), 0)) == REG
7246 && GET_CODE (XEXP (XEXP (retaddr, 0), 1)) == CONST_INT)
7248 base = gen_rtx_REG (Pmode, REGNO (XEXP (XEXP (retaddr, 0), 0)));
7249 offset = INTVAL (XEXP (XEXP (retaddr, 0), 1));
7251 else
7252 abort ();
7254 /* If the base of the location containing the return pointer
7255 is SP, we must update it with the replacement address. Otherwise,
7256 just build the necessary MEM. */
7257 retaddr = plus_constant (base, offset);
7258 if (base == stack_pointer_rtx)
7259 retaddr = simplify_replace_rtx (retaddr, stack_pointer_rtx,
7260 plus_constant (info.sp_equiv_reg,
7261 info.sp_offset));
7263 retaddr = gen_rtx_MEM (Pmode, retaddr);
7265 /* If there is a pending load to the equivalent register for SP
7266 and we reference that register, we must load our address into
7267 a scratch register and then do that load. */
7268 if (info.equiv_reg_src
7269 && reg_overlap_mentioned_p (info.equiv_reg_src, retaddr))
7271 unsigned int regno;
7272 rtx reg;
7274 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
7275 if (HARD_REGNO_MODE_OK (regno, Pmode)
7276 && !fixed_regs[regno]
7277 && TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)
7278 && !REGNO_REG_SET_P (EXIT_BLOCK_PTR->global_live_at_start,
7279 regno)
7280 && !refers_to_regno_p (regno,
7281 regno + HARD_REGNO_NREGS (regno,
7282 Pmode),
7283 info.equiv_reg_src, NULL))
7284 break;
7286 if (regno == FIRST_PSEUDO_REGISTER)
7287 abort ();
7289 reg = gen_rtx_REG (Pmode, regno);
7290 emit_move_insn (reg, retaddr);
7291 retaddr = reg;
7294 emit_equiv_load (&info);
7295 jump_insn = emit_jump_insn (gen_indirect_jump (retaddr));
7297 /* Show the SET in the above insn is a RETURN. */
7298 jump_set = single_set (jump_insn);
7299 if (jump_set == 0)
7300 abort ();
7301 else
7302 SET_IS_RETURN_P (jump_set) = 1;
7305 /* If SP is not mentioned in the pattern and its equivalent register, if
7306 any, is not modified, just emit it. Otherwise, if neither is set,
7307 replace the reference to SP and emit the insn. If none of those are
7308 true, handle each SET individually. */
7309 else if (!reg_mentioned_p (stack_pointer_rtx, PATTERN (insn))
7310 && (info.sp_equiv_reg == stack_pointer_rtx
7311 || !reg_set_p (info.sp_equiv_reg, insn)))
7312 add_insn (insn);
7313 else if (! reg_set_p (stack_pointer_rtx, insn)
7314 && (info.sp_equiv_reg == stack_pointer_rtx
7315 || !reg_set_p (info.sp_equiv_reg, insn)))
7317 if (! validate_replace_rtx (stack_pointer_rtx,
7318 plus_constant (info.sp_equiv_reg,
7319 info.sp_offset),
7320 insn))
7321 abort ();
7323 add_insn (insn);
7325 else if (GET_CODE (PATTERN (insn)) == SET)
7326 handle_epilogue_set (PATTERN (insn), &info);
7327 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
7329 for (j = 0; j < XVECLEN (PATTERN (insn), 0); j++)
7330 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET)
7331 handle_epilogue_set (XVECEXP (PATTERN (insn), 0, j), &info);
7333 else
7334 add_insn (insn);
7336 info.sp_equiv_reg = info.new_sp_equiv_reg;
7337 info.sp_offset = info.new_sp_offset;
7340 seq = gen_sequence ();
7341 end_sequence ();
7342 return seq;
7345 /* SET is a SET from an insn in the epilogue. P is a pointer to the epi_info
7346 structure that contains information about what we've seen so far. We
7347 process this SET by either updating that data or by emitting one or
7348 more insns. */
7350 static void
7351 handle_epilogue_set (set, p)
7352 rtx set;
7353 struct epi_info *p;
7355 /* First handle the case where we are setting SP. Record what it is being
7356 set from. If unknown, abort. */
7357 if (reg_set_p (stack_pointer_rtx, set))
7359 if (SET_DEST (set) != stack_pointer_rtx)
7360 abort ();
7362 if (GET_CODE (SET_SRC (set)) == PLUS
7363 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
7365 p->new_sp_equiv_reg = XEXP (SET_SRC (set), 0);
7366 p->new_sp_offset = INTVAL (XEXP (SET_SRC (set), 1));
7368 else
7369 p->new_sp_equiv_reg = SET_SRC (set), p->new_sp_offset = 0;
7371 /* If we are adjusting SP, we adjust from the old data. */
7372 if (p->new_sp_equiv_reg == stack_pointer_rtx)
7374 p->new_sp_equiv_reg = p->sp_equiv_reg;
7375 p->new_sp_offset += p->sp_offset;
7378 if (p->new_sp_equiv_reg == 0 || GET_CODE (p->new_sp_equiv_reg) != REG)
7379 abort ();
7381 return;
7384 /* Next handle the case where we are setting SP's equivalent register.
7385 If we already have a value to set it to, abort. We could update, but
7386 there seems little point in handling that case. Note that we have
7387 to allow for the case where we are setting the register set in
7388 the previous part of a PARALLEL inside a single insn. But use the
7389 old offset for any updates within this insn. */
7390 else if (p->new_sp_equiv_reg != 0 && reg_set_p (p->new_sp_equiv_reg, set))
7392 if (!rtx_equal_p (p->new_sp_equiv_reg, SET_DEST (set))
7393 || p->equiv_reg_src != 0)
7394 abort ();
7395 else
7396 p->equiv_reg_src
7397 = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7398 plus_constant (p->sp_equiv_reg,
7399 p->sp_offset));
7402 /* Otherwise, replace any references to SP in the insn to its new value
7403 and emit the insn. */
7404 else
7406 SET_SRC (set) = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7407 plus_constant (p->sp_equiv_reg,
7408 p->sp_offset));
7409 SET_DEST (set) = simplify_replace_rtx (SET_DEST (set), stack_pointer_rtx,
7410 plus_constant (p->sp_equiv_reg,
7411 p->sp_offset));
7412 emit_insn (set);
7416 /* Emit an insn to do the load shown in p->equiv_reg_src, if needed. */
7418 static void
7419 emit_equiv_load (p)
7420 struct epi_info *p;
7422 if (p->equiv_reg_src != 0)
7423 emit_move_insn (p->sp_equiv_reg, p->equiv_reg_src);
7425 p->equiv_reg_src = 0;
7427 #endif
7429 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
7430 this into place with notes indicating where the prologue ends and where
7431 the epilogue begins. Update the basic block information when possible. */
7433 void
7434 thread_prologue_and_epilogue_insns (f)
7435 rtx f ATTRIBUTE_UNUSED;
7437 int inserted = 0;
7438 edge e;
7439 #if defined (HAVE_sibcall_epilogue) || defined (HAVE_epilogue) || defined (HAVE_return) || defined (HAVE_prologue)
7440 rtx seq;
7441 #endif
7442 #ifdef HAVE_prologue
7443 rtx prologue_end = NULL_RTX;
7444 #endif
7445 #if defined (HAVE_epilogue) || defined(HAVE_return)
7446 rtx epilogue_end = NULL_RTX;
7447 #endif
7449 #ifdef HAVE_prologue
7450 if (HAVE_prologue)
7452 start_sequence ();
7453 seq = gen_prologue ();
7454 emit_insn (seq);
7456 /* Retain a map of the prologue insns. */
7457 if (GET_CODE (seq) != SEQUENCE)
7458 seq = get_insns ();
7459 record_insns (seq, &prologue);
7460 prologue_end = emit_note (NULL, NOTE_INSN_PROLOGUE_END);
7462 seq = gen_sequence ();
7463 end_sequence ();
7465 /* Can't deal with multiple successors of the entry block
7466 at the moment. Function should always have at least one
7467 entry point. */
7468 if (!ENTRY_BLOCK_PTR->succ || ENTRY_BLOCK_PTR->succ->succ_next)
7469 abort ();
7471 insert_insn_on_edge (seq, ENTRY_BLOCK_PTR->succ);
7472 inserted = 1;
7474 #endif
7476 /* If the exit block has no non-fake predecessors, we don't need
7477 an epilogue. */
7478 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7479 if ((e->flags & EDGE_FAKE) == 0)
7480 break;
7481 if (e == NULL)
7482 goto epilogue_done;
7484 #ifdef HAVE_return
7485 if (optimize && HAVE_return)
7487 /* If we're allowed to generate a simple return instruction,
7488 then by definition we don't need a full epilogue. Examine
7489 the block that falls through to EXIT. If it does not
7490 contain any code, examine its predecessors and try to
7491 emit (conditional) return instructions. */
7493 basic_block last;
7494 edge e_next;
7495 rtx label;
7497 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7498 if (e->flags & EDGE_FALLTHRU)
7499 break;
7500 if (e == NULL)
7501 goto epilogue_done;
7502 last = e->src;
7504 /* Verify that there are no active instructions in the last block. */
7505 label = last->end;
7506 while (label && GET_CODE (label) != CODE_LABEL)
7508 if (active_insn_p (label))
7509 break;
7510 label = PREV_INSN (label);
7513 if (last->head == label && GET_CODE (label) == CODE_LABEL)
7515 rtx epilogue_line_note = NULL_RTX;
7517 /* Locate the line number associated with the closing brace,
7518 if we can find one. */
7519 for (seq = get_last_insn ();
7520 seq && ! active_insn_p (seq);
7521 seq = PREV_INSN (seq))
7522 if (GET_CODE (seq) == NOTE && NOTE_LINE_NUMBER (seq) > 0)
7524 epilogue_line_note = seq;
7525 break;
7528 for (e = last->pred; e; e = e_next)
7530 basic_block bb = e->src;
7531 rtx jump;
7533 e_next = e->pred_next;
7534 if (bb == ENTRY_BLOCK_PTR)
7535 continue;
7537 jump = bb->end;
7538 if ((GET_CODE (jump) != JUMP_INSN) || JUMP_LABEL (jump) != label)
7539 continue;
7541 /* If we have an unconditional jump, we can replace that
7542 with a simple return instruction. */
7543 if (simplejump_p (jump))
7545 emit_return_into_block (bb, epilogue_line_note);
7546 delete_insn (jump);
7549 /* If we have a conditional jump, we can try to replace
7550 that with a conditional return instruction. */
7551 else if (condjump_p (jump))
7553 rtx ret, *loc;
7555 ret = SET_SRC (PATTERN (jump));
7556 if (GET_CODE (XEXP (ret, 1)) == LABEL_REF)
7557 loc = &XEXP (ret, 1);
7558 else
7559 loc = &XEXP (ret, 2);
7560 ret = gen_rtx_RETURN (VOIDmode);
7562 if (! validate_change (jump, loc, ret, 0))
7563 continue;
7564 if (JUMP_LABEL (jump))
7565 LABEL_NUSES (JUMP_LABEL (jump))--;
7567 /* If this block has only one successor, it both jumps
7568 and falls through to the fallthru block, so we can't
7569 delete the edge. */
7570 if (bb->succ->succ_next == NULL)
7571 continue;
7573 else
7574 continue;
7576 /* Fix up the CFG for the successful change we just made. */
7577 redirect_edge_succ (e, EXIT_BLOCK_PTR);
7580 /* Emit a return insn for the exit fallthru block. Whether
7581 this is still reachable will be determined later. */
7583 emit_barrier_after (last->end);
7584 emit_return_into_block (last, epilogue_line_note);
7585 epilogue_end = last->end;
7586 last->succ->flags &= ~EDGE_FALLTHRU;
7587 goto epilogue_done;
7590 #endif
7591 #ifdef HAVE_epilogue
7592 if (HAVE_epilogue)
7594 /* Find the edge that falls through to EXIT. Other edges may exist
7595 due to RETURN instructions, but those don't need epilogues.
7596 There really shouldn't be a mixture -- either all should have
7597 been converted or none, however... */
7599 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7600 if (e->flags & EDGE_FALLTHRU)
7601 break;
7602 if (e == NULL)
7603 goto epilogue_done;
7605 start_sequence ();
7606 epilogue_end = emit_note (NULL, NOTE_INSN_EPILOGUE_BEG);
7608 seq = gen_epilogue ();
7610 #ifdef INCOMING_RETURN_ADDR_RTX
7611 /* If this function returns with the stack depressed and we can support
7612 it, massage the epilogue to actually do that. */
7613 if (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
7614 && TYPE_RETURNS_STACK_DEPRESSED (TREE_TYPE (current_function_decl)))
7615 seq = keep_stack_depressed (seq);
7616 #endif
7618 emit_jump_insn (seq);
7620 /* Retain a map of the epilogue insns. */
7621 if (GET_CODE (seq) != SEQUENCE)
7622 seq = get_insns ();
7623 record_insns (seq, &epilogue);
7625 seq = gen_sequence ();
7626 end_sequence ();
7628 insert_insn_on_edge (seq, e);
7629 inserted = 1;
7631 #endif
7632 epilogue_done:
7634 if (inserted)
7635 commit_edge_insertions ();
7637 #ifdef HAVE_sibcall_epilogue
7638 /* Emit sibling epilogues before any sibling call sites. */
7639 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7641 basic_block bb = e->src;
7642 rtx insn = bb->end;
7643 rtx i;
7644 rtx newinsn;
7646 if (GET_CODE (insn) != CALL_INSN
7647 || ! SIBLING_CALL_P (insn))
7648 continue;
7650 start_sequence ();
7651 seq = gen_sibcall_epilogue ();
7652 end_sequence ();
7654 i = PREV_INSN (insn);
7655 newinsn = emit_insn_before (seq, insn);
7657 /* Retain a map of the epilogue insns. Used in life analysis to
7658 avoid getting rid of sibcall epilogue insns. */
7659 record_insns (GET_CODE (seq) == SEQUENCE
7660 ? seq : newinsn, &sibcall_epilogue);
7662 #endif
7664 #ifdef HAVE_prologue
7665 if (prologue_end)
7667 rtx insn, prev;
7669 /* GDB handles `break f' by setting a breakpoint on the first
7670 line note after the prologue. Which means (1) that if
7671 there are line number notes before where we inserted the
7672 prologue we should move them, and (2) we should generate a
7673 note before the end of the first basic block, if there isn't
7674 one already there.
7676 ??? This behaviour is completely broken when dealing with
7677 multiple entry functions. We simply place the note always
7678 into first basic block and let alternate entry points
7679 to be missed.
7682 for (insn = prologue_end; insn; insn = prev)
7684 prev = PREV_INSN (insn);
7685 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7687 /* Note that we cannot reorder the first insn in the
7688 chain, since rest_of_compilation relies on that
7689 remaining constant. */
7690 if (prev == NULL)
7691 break;
7692 reorder_insns (insn, insn, prologue_end);
7696 /* Find the last line number note in the first block. */
7697 for (insn = BASIC_BLOCK (0)->end;
7698 insn != prologue_end && insn;
7699 insn = PREV_INSN (insn))
7700 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7701 break;
7703 /* If we didn't find one, make a copy of the first line number
7704 we run across. */
7705 if (! insn)
7707 for (insn = next_active_insn (prologue_end);
7708 insn;
7709 insn = PREV_INSN (insn))
7710 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7712 emit_line_note_after (NOTE_SOURCE_FILE (insn),
7713 NOTE_LINE_NUMBER (insn),
7714 prologue_end);
7715 break;
7719 #endif
7720 #ifdef HAVE_epilogue
7721 if (epilogue_end)
7723 rtx insn, next;
7725 /* Similarly, move any line notes that appear after the epilogue.
7726 There is no need, however, to be quite so anal about the existence
7727 of such a note. */
7728 for (insn = epilogue_end; insn; insn = next)
7730 next = NEXT_INSN (insn);
7731 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7732 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
7735 #endif
7738 /* Reposition the prologue-end and epilogue-begin notes after instruction
7739 scheduling and delayed branch scheduling. */
7741 void
7742 reposition_prologue_and_epilogue_notes (f)
7743 rtx f ATTRIBUTE_UNUSED;
7745 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
7746 int len;
7748 if ((len = VARRAY_SIZE (prologue)) > 0)
7750 rtx insn, note = 0;
7752 /* Scan from the beginning until we reach the last prologue insn.
7753 We apparently can't depend on basic_block_{head,end} after
7754 reorg has run. */
7755 for (insn = f; len && insn; insn = NEXT_INSN (insn))
7757 if (GET_CODE (insn) == NOTE)
7759 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
7760 note = insn;
7762 else if ((len -= contains (insn, prologue)) == 0)
7764 rtx next;
7765 /* Find the prologue-end note if we haven't already, and
7766 move it to just after the last prologue insn. */
7767 if (note == 0)
7769 for (note = insn; (note = NEXT_INSN (note));)
7770 if (GET_CODE (note) == NOTE
7771 && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
7772 break;
7775 next = NEXT_INSN (note);
7777 /* Whether or not we can depend on BLOCK_HEAD,
7778 attempt to keep it up-to-date. */
7779 if (BLOCK_HEAD (0) == note)
7780 BLOCK_HEAD (0) = next;
7782 remove_insn (note);
7783 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
7784 if (GET_CODE (insn) == CODE_LABEL)
7785 insn = NEXT_INSN (insn);
7786 add_insn_after (note, insn);
7791 if ((len = VARRAY_SIZE (epilogue)) > 0)
7793 rtx insn, note = 0;
7795 /* Scan from the end until we reach the first epilogue insn.
7796 We apparently can't depend on basic_block_{head,end} after
7797 reorg has run. */
7798 for (insn = get_last_insn (); len && insn; insn = PREV_INSN (insn))
7800 if (GET_CODE (insn) == NOTE)
7802 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
7803 note = insn;
7805 else if ((len -= contains (insn, epilogue)) == 0)
7807 /* Find the epilogue-begin note if we haven't already, and
7808 move it to just before the first epilogue insn. */
7809 if (note == 0)
7811 for (note = insn; (note = PREV_INSN (note));)
7812 if (GET_CODE (note) == NOTE
7813 && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
7814 break;
7817 /* Whether or not we can depend on BLOCK_HEAD,
7818 attempt to keep it up-to-date. */
7819 if (n_basic_blocks
7820 && BLOCK_HEAD (n_basic_blocks-1) == insn)
7821 BLOCK_HEAD (n_basic_blocks-1) = note;
7823 remove_insn (note);
7824 add_insn_before (note, insn);
7828 #endif /* HAVE_prologue or HAVE_epilogue */
7831 /* Mark P for GC. */
7833 static void
7834 mark_function_status (p)
7835 struct function *p;
7837 struct var_refs_queue *q;
7838 struct temp_slot *t;
7839 int i;
7840 rtx *r;
7842 if (p == 0)
7843 return;
7845 ggc_mark_rtx (p->arg_offset_rtx);
7847 if (p->x_parm_reg_stack_loc)
7848 for (i = p->x_max_parm_reg, r = p->x_parm_reg_stack_loc;
7849 i > 0; --i, ++r)
7850 ggc_mark_rtx (*r);
7852 ggc_mark_rtx (p->return_rtx);
7853 ggc_mark_rtx (p->x_cleanup_label);
7854 ggc_mark_rtx (p->x_return_label);
7855 ggc_mark_rtx (p->x_save_expr_regs);
7856 ggc_mark_rtx (p->x_stack_slot_list);
7857 ggc_mark_rtx (p->x_parm_birth_insn);
7858 ggc_mark_rtx (p->x_tail_recursion_label);
7859 ggc_mark_rtx (p->x_tail_recursion_reentry);
7860 ggc_mark_rtx (p->internal_arg_pointer);
7861 ggc_mark_rtx (p->x_arg_pointer_save_area);
7862 ggc_mark_tree (p->x_rtl_expr_chain);
7863 ggc_mark_rtx (p->x_last_parm_insn);
7864 ggc_mark_tree (p->x_context_display);
7865 ggc_mark_tree (p->x_trampoline_list);
7866 ggc_mark_rtx (p->epilogue_delay_list);
7867 ggc_mark_rtx (p->x_clobber_return_insn);
7869 for (t = p->x_temp_slots; t != 0; t = t->next)
7871 ggc_mark (t);
7872 ggc_mark_rtx (t->slot);
7873 ggc_mark_rtx (t->address);
7874 ggc_mark_tree (t->rtl_expr);
7875 ggc_mark_tree (t->type);
7878 for (q = p->fixup_var_refs_queue; q != 0; q = q->next)
7880 ggc_mark (q);
7881 ggc_mark_rtx (q->modified);
7884 ggc_mark_rtx (p->x_nonlocal_goto_handler_slots);
7885 ggc_mark_rtx (p->x_nonlocal_goto_handler_labels);
7886 ggc_mark_rtx (p->x_nonlocal_goto_stack_level);
7887 ggc_mark_tree (p->x_nonlocal_labels);
7889 mark_hard_reg_initial_vals (p);
7892 /* Mark the struct function pointed to by *ARG for GC, if it is not
7893 NULL. This is used to mark the current function and the outer
7894 function chain. */
7896 static void
7897 maybe_mark_struct_function (arg)
7898 void *arg;
7900 struct function *f = *(struct function **) arg;
7902 if (f == 0)
7903 return;
7905 ggc_mark_struct_function (f);
7908 /* Mark a struct function * for GC. This is called from ggc-common.c. */
7910 void
7911 ggc_mark_struct_function (f)
7912 struct function *f;
7914 ggc_mark (f);
7915 ggc_mark_tree (f->decl);
7917 mark_function_status (f);
7918 mark_eh_status (f->eh);
7919 mark_stmt_status (f->stmt);
7920 mark_expr_status (f->expr);
7921 mark_emit_status (f->emit);
7922 mark_varasm_status (f->varasm);
7924 if (mark_machine_status)
7925 (*mark_machine_status) (f);
7926 if (mark_lang_status)
7927 (*mark_lang_status) (f);
7929 if (f->original_arg_vector)
7930 ggc_mark_rtvec ((rtvec) f->original_arg_vector);
7931 if (f->original_decl_initial)
7932 ggc_mark_tree (f->original_decl_initial);
7933 if (f->outer)
7934 ggc_mark_struct_function (f->outer);
7937 /* Called once, at initialization, to initialize function.c. */
7939 void
7940 init_function_once ()
7942 ggc_add_root (&cfun, 1, sizeof cfun, maybe_mark_struct_function);
7943 ggc_add_root (&outer_function_chain, 1, sizeof outer_function_chain,
7944 maybe_mark_struct_function);
7946 VARRAY_INT_INIT (prologue, 0, "prologue");
7947 VARRAY_INT_INIT (epilogue, 0, "epilogue");
7948 VARRAY_INT_INIT (sibcall_epilogue, 0, "sibcall_epilogue");