Add hppa-openbsd target
[official-gcc.git] / gcc / function.c
blob47fa24de05a56ff66a93bd3d3643af90dadf8b1d
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, 2002 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 "toplev.h"
57 #include "hashtab.h"
58 #include "ggc.h"
59 #include "tm_p.h"
60 #include "integrate.h"
61 #include "langhooks.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 #endif
79 /* Round a value to the lowest integer less than it that is a multiple of
80 the required alignment. Avoid using division in case the value is
81 negative. Assume the alignment is a power of two. */
82 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
84 /* Similar, but round to the next highest integer that meets the
85 alignment. */
86 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
88 /* NEED_SEPARATE_AP means that we cannot derive ap from the value of fp
89 during rtl generation. If they are different register numbers, this is
90 always true. It may also be true if
91 FIRST_PARM_OFFSET - STARTING_FRAME_OFFSET is not a constant during rtl
92 generation. See fix_lexical_addr for details. */
94 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
95 #define NEED_SEPARATE_AP
96 #endif
98 /* Nonzero if function being compiled doesn't contain any calls
99 (ignoring the prologue and epilogue). This is set prior to
100 local register allocation and is valid for the remaining
101 compiler passes. */
102 int current_function_is_leaf;
104 /* Nonzero if function being compiled doesn't contain any instructions
105 that can throw an exception. This is set prior to final. */
107 int current_function_nothrow;
109 /* Nonzero if function being compiled doesn't modify the stack pointer
110 (ignoring the prologue and epilogue). This is only valid after
111 life_analysis has run. */
112 int current_function_sp_is_unchanging;
114 /* Nonzero if the function being compiled is a leaf function which only
115 uses leaf registers. This is valid after reload (specifically after
116 sched2) and is useful only if the port defines LEAF_REGISTERS. */
117 int current_function_uses_only_leaf_regs;
119 /* Nonzero once virtual register instantiation has been done.
120 assign_stack_local uses frame_pointer_rtx when this is nonzero.
121 calls.c:emit_library_call_value_1 uses it to set up
122 post-instantiation libcalls. */
123 int virtuals_instantiated;
125 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
126 static int funcdef_no;
128 /* These variables hold pointers to functions to create and destroy
129 target specific, per-function data structures. */
130 struct machine_function * (*init_machine_status) PARAMS ((void));
132 /* The FUNCTION_DECL for an inline function currently being expanded. */
133 tree inline_function_decl;
135 /* The currently compiled function. */
136 struct function *cfun = 0;
138 /* These arrays record the INSN_UIDs of the prologue and epilogue insns. */
139 static GTY(()) varray_type prologue;
140 static GTY(()) varray_type epilogue;
142 /* Array of INSN_UIDs to hold the INSN_UIDs for each sibcall epilogue
143 in this function. */
144 static GTY(()) varray_type sibcall_epilogue;
146 /* In order to evaluate some expressions, such as function calls returning
147 structures in memory, we need to temporarily allocate stack locations.
148 We record each allocated temporary in the following structure.
150 Associated with each temporary slot is a nesting level. When we pop up
151 one level, all temporaries associated with the previous level are freed.
152 Normally, all temporaries are freed after the execution of the statement
153 in which they were created. However, if we are inside a ({...}) grouping,
154 the result may be in a temporary and hence must be preserved. If the
155 result could be in a temporary, we preserve it if we can determine which
156 one it is in. If we cannot determine which temporary may contain the
157 result, all temporaries are preserved. A temporary is preserved by
158 pretending it was allocated at the previous nesting level.
160 Automatic variables are also assigned temporary slots, at the nesting
161 level where they are defined. They are marked a "kept" so that
162 free_temp_slots will not free them. */
164 struct temp_slot GTY(())
166 /* Points to next temporary slot. */
167 struct temp_slot *next;
168 /* The rtx to used to reference the slot. */
169 rtx slot;
170 /* The rtx used to represent the address if not the address of the
171 slot above. May be an EXPR_LIST if multiple addresses exist. */
172 rtx address;
173 /* The alignment (in bits) of the slot. */
174 unsigned int align;
175 /* The size, in units, of the slot. */
176 HOST_WIDE_INT size;
177 /* The type of the object in the slot, or zero if it doesn't correspond
178 to a type. We use this to determine whether a slot can be reused.
179 It can be reused if objects of the type of the new slot will always
180 conflict with objects of the type of the old slot. */
181 tree type;
182 /* The value of `sequence_rtl_expr' when this temporary is allocated. */
183 tree rtl_expr;
184 /* Non-zero if this temporary is currently in use. */
185 char in_use;
186 /* Non-zero if this temporary has its address taken. */
187 char addr_taken;
188 /* Nesting level at which this slot is being used. */
189 int level;
190 /* Non-zero if this should survive a call to free_temp_slots. */
191 int keep;
192 /* The offset of the slot from the frame_pointer, including extra space
193 for alignment. This info is for combine_temp_slots. */
194 HOST_WIDE_INT base_offset;
195 /* The size of the slot, including extra space for alignment. This
196 info is for combine_temp_slots. */
197 HOST_WIDE_INT full_size;
200 /* This structure is used to record MEMs or pseudos used to replace VAR, any
201 SUBREGs of VAR, and any MEMs containing VAR as an address. We need to
202 maintain this list in case two operands of an insn were required to match;
203 in that case we must ensure we use the same replacement. */
205 struct fixup_replacement GTY(())
207 rtx old;
208 rtx new;
209 struct fixup_replacement *next;
212 struct insns_for_mem_entry
214 /* A MEM. */
215 rtx key;
216 /* These are the INSNs which reference the MEM. */
217 rtx insns;
220 /* Forward declarations. */
222 static rtx assign_stack_local_1 PARAMS ((enum machine_mode, HOST_WIDE_INT,
223 int, struct function *));
224 static struct temp_slot *find_temp_slot_from_address PARAMS ((rtx));
225 static void put_reg_into_stack PARAMS ((struct function *, rtx, tree,
226 enum machine_mode, enum machine_mode,
227 int, unsigned int, int,
228 htab_t));
229 static void schedule_fixup_var_refs PARAMS ((struct function *, rtx, tree,
230 enum machine_mode,
231 htab_t));
232 static void fixup_var_refs PARAMS ((rtx, enum machine_mode, int, rtx,
233 htab_t));
234 static struct fixup_replacement
235 *find_fixup_replacement PARAMS ((struct fixup_replacement **, rtx));
236 static void fixup_var_refs_insns PARAMS ((rtx, rtx, enum machine_mode,
237 int, int, rtx));
238 static void fixup_var_refs_insns_with_hash
239 PARAMS ((htab_t, rtx,
240 enum machine_mode, int, rtx));
241 static void fixup_var_refs_insn PARAMS ((rtx, rtx, enum machine_mode,
242 int, int, rtx));
243 static void fixup_var_refs_1 PARAMS ((rtx, enum machine_mode, rtx *, rtx,
244 struct fixup_replacement **, rtx));
245 static rtx fixup_memory_subreg PARAMS ((rtx, rtx, enum machine_mode, int));
246 static rtx walk_fixup_memory_subreg PARAMS ((rtx, rtx, enum machine_mode,
247 int));
248 static rtx fixup_stack_1 PARAMS ((rtx, rtx));
249 static void optimize_bit_field PARAMS ((rtx, rtx, rtx *));
250 static void instantiate_decls PARAMS ((tree, int));
251 static void instantiate_decls_1 PARAMS ((tree, int));
252 static void instantiate_decl PARAMS ((rtx, HOST_WIDE_INT, int));
253 static rtx instantiate_new_reg PARAMS ((rtx, HOST_WIDE_INT *));
254 static int instantiate_virtual_regs_1 PARAMS ((rtx *, rtx, int));
255 static void delete_handlers PARAMS ((void));
256 static void pad_to_arg_alignment PARAMS ((struct args_size *, int,
257 struct args_size *));
258 #ifndef ARGS_GROW_DOWNWARD
259 static void pad_below PARAMS ((struct args_size *, enum machine_mode,
260 tree));
261 #endif
262 static rtx round_trampoline_addr PARAMS ((rtx));
263 static rtx adjust_trampoline_addr PARAMS ((rtx));
264 static tree *identify_blocks_1 PARAMS ((rtx, tree *, tree *, tree *));
265 static void reorder_blocks_0 PARAMS ((tree));
266 static void reorder_blocks_1 PARAMS ((rtx, tree, varray_type *));
267 static void reorder_fix_fragments PARAMS ((tree));
268 static tree blocks_nreverse PARAMS ((tree));
269 static int all_blocks PARAMS ((tree, tree *));
270 static tree *get_block_vector PARAMS ((tree, int *));
271 extern tree debug_find_var_in_block_tree PARAMS ((tree, tree));
272 /* We always define `record_insns' even if its not used so that we
273 can always export `prologue_epilogue_contains'. */
274 static void record_insns PARAMS ((rtx, varray_type *)) ATTRIBUTE_UNUSED;
275 static int contains PARAMS ((rtx, varray_type));
276 #ifdef HAVE_return
277 static void emit_return_into_block PARAMS ((basic_block, rtx));
278 #endif
279 static void put_addressof_into_stack PARAMS ((rtx, htab_t));
280 static bool purge_addressof_1 PARAMS ((rtx *, rtx, int, int,
281 htab_t));
282 static void purge_single_hard_subreg_set PARAMS ((rtx));
283 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
284 static rtx keep_stack_depressed PARAMS ((rtx));
285 #endif
286 static int is_addressof PARAMS ((rtx *, void *));
287 static hashval_t insns_for_mem_hash PARAMS ((const void *));
288 static int insns_for_mem_comp PARAMS ((const void *, const void *));
289 static int insns_for_mem_walk PARAMS ((rtx *, void *));
290 static void compute_insns_for_mem PARAMS ((rtx, rtx, htab_t));
291 static void prepare_function_start PARAMS ((void));
292 static void do_clobber_return_reg PARAMS ((rtx, void *));
293 static void do_use_return_reg PARAMS ((rtx, void *));
295 /* Pointer to chain of `struct function' for containing functions. */
296 static GTY(()) struct function *outer_function_chain;
298 /* Given a function decl for a containing function,
299 return the `struct function' for it. */
301 struct function *
302 find_function_data (decl)
303 tree decl;
305 struct function *p;
307 for (p = outer_function_chain; p; p = p->outer)
308 if (p->decl == decl)
309 return p;
311 abort ();
314 /* Save the current context for compilation of a nested function.
315 This is called from language-specific code. The caller should use
316 the enter_nested langhook to save any language-specific state,
317 since this function knows only about language-independent
318 variables. */
320 void
321 push_function_context_to (context)
322 tree context;
324 struct function *p;
326 if (context)
328 if (context == current_function_decl)
329 cfun->contains_functions = 1;
330 else
332 struct function *containing = find_function_data (context);
333 containing->contains_functions = 1;
337 if (cfun == 0)
338 init_dummy_function_start ();
339 p = cfun;
341 p->outer = outer_function_chain;
342 outer_function_chain = p;
343 p->fixup_var_refs_queue = 0;
345 (*lang_hooks.function.enter_nested) (p);
347 cfun = 0;
350 void
351 push_function_context ()
353 push_function_context_to (current_function_decl);
356 /* Restore the last saved context, at the end of a nested function.
357 This function is called from language-specific code. */
359 void
360 pop_function_context_from (context)
361 tree context ATTRIBUTE_UNUSED;
363 struct function *p = outer_function_chain;
364 struct var_refs_queue *queue;
366 cfun = p;
367 outer_function_chain = p->outer;
369 current_function_decl = p->decl;
370 reg_renumber = 0;
372 restore_emit_status (p);
374 (*lang_hooks.function.leave_nested) (p);
376 /* Finish doing put_var_into_stack for any of our variables which became
377 addressable during the nested function. If only one entry has to be
378 fixed up, just do that one. Otherwise, first make a list of MEMs that
379 are not to be unshared. */
380 if (p->fixup_var_refs_queue == 0)
382 else if (p->fixup_var_refs_queue->next == 0)
383 fixup_var_refs (p->fixup_var_refs_queue->modified,
384 p->fixup_var_refs_queue->promoted_mode,
385 p->fixup_var_refs_queue->unsignedp,
386 p->fixup_var_refs_queue->modified, 0);
387 else
389 rtx list = 0;
391 for (queue = p->fixup_var_refs_queue; queue; queue = queue->next)
392 list = gen_rtx_EXPR_LIST (VOIDmode, queue->modified, list);
394 for (queue = p->fixup_var_refs_queue; queue; queue = queue->next)
395 fixup_var_refs (queue->modified, queue->promoted_mode,
396 queue->unsignedp, list, 0);
400 p->fixup_var_refs_queue = 0;
402 /* Reset variables that have known state during rtx generation. */
403 rtx_equal_function_value_matters = 1;
404 virtuals_instantiated = 0;
405 generating_concat_p = 1;
408 void
409 pop_function_context ()
411 pop_function_context_from (current_function_decl);
414 /* Clear out all parts of the state in F that can safely be discarded
415 after the function has been parsed, but not compiled, to let
416 garbage collection reclaim the memory. */
418 void
419 free_after_parsing (f)
420 struct function *f;
422 /* f->expr->forced_labels is used by code generation. */
423 /* f->emit->regno_reg_rtx is used by code generation. */
424 /* f->varasm is used by code generation. */
425 /* f->eh->eh_return_stub_label is used by code generation. */
427 (*lang_hooks.function.final) (f);
428 f->stmt = NULL;
431 /* Clear out all parts of the state in F that can safely be discarded
432 after the function has been compiled, to let garbage collection
433 reclaim the memory. */
435 void
436 free_after_compilation (f)
437 struct function *f;
439 f->eh = NULL;
440 f->expr = NULL;
441 f->emit = NULL;
442 f->varasm = NULL;
443 f->machine = NULL;
445 f->x_temp_slots = NULL;
446 f->arg_offset_rtx = NULL;
447 f->return_rtx = NULL;
448 f->internal_arg_pointer = NULL;
449 f->x_nonlocal_labels = NULL;
450 f->x_nonlocal_goto_handler_slots = NULL;
451 f->x_nonlocal_goto_handler_labels = NULL;
452 f->x_nonlocal_goto_stack_level = NULL;
453 f->x_cleanup_label = NULL;
454 f->x_return_label = NULL;
455 f->x_save_expr_regs = NULL;
456 f->x_stack_slot_list = NULL;
457 f->x_rtl_expr_chain = NULL;
458 f->x_tail_recursion_label = NULL;
459 f->x_tail_recursion_reentry = NULL;
460 f->x_arg_pointer_save_area = NULL;
461 f->x_clobber_return_insn = NULL;
462 f->x_context_display = NULL;
463 f->x_trampoline_list = NULL;
464 f->x_parm_birth_insn = NULL;
465 f->x_last_parm_insn = NULL;
466 f->x_parm_reg_stack_loc = NULL;
467 f->fixup_var_refs_queue = NULL;
468 f->original_arg_vector = NULL;
469 f->original_decl_initial = NULL;
470 f->inl_last_parm_insn = NULL;
471 f->epilogue_delay_list = NULL;
474 /* Allocate fixed slots in the stack frame of the current function. */
476 /* Return size needed for stack frame based on slots so far allocated in
477 function F.
478 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
479 the caller may have to do that. */
481 HOST_WIDE_INT
482 get_func_frame_size (f)
483 struct function *f;
485 #ifdef FRAME_GROWS_DOWNWARD
486 return -f->x_frame_offset;
487 #else
488 return f->x_frame_offset;
489 #endif
492 /* Return size needed for stack frame based on slots so far allocated.
493 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
494 the caller may have to do that. */
495 HOST_WIDE_INT
496 get_frame_size ()
498 return get_func_frame_size (cfun);
501 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
502 with machine mode MODE.
504 ALIGN controls the amount of alignment for the address of the slot:
505 0 means according to MODE,
506 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
507 positive specifies alignment boundary in bits.
509 We do not round to stack_boundary here.
511 FUNCTION specifies the function to allocate in. */
513 static rtx
514 assign_stack_local_1 (mode, size, align, function)
515 enum machine_mode mode;
516 HOST_WIDE_INT size;
517 int align;
518 struct function *function;
520 rtx x, addr;
521 int bigend_correction = 0;
522 int alignment;
523 int frame_off, frame_alignment, frame_phase;
525 if (align == 0)
527 tree type;
529 if (mode == BLKmode)
530 alignment = BIGGEST_ALIGNMENT;
531 else
532 alignment = GET_MODE_ALIGNMENT (mode);
534 /* Allow the target to (possibly) increase the alignment of this
535 stack slot. */
536 type = (*lang_hooks.types.type_for_mode) (mode, 0);
537 if (type)
538 alignment = LOCAL_ALIGNMENT (type, alignment);
540 alignment /= BITS_PER_UNIT;
542 else if (align == -1)
544 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
545 size = CEIL_ROUND (size, alignment);
547 else
548 alignment = align / BITS_PER_UNIT;
550 #ifdef FRAME_GROWS_DOWNWARD
551 function->x_frame_offset -= size;
552 #endif
554 /* Ignore alignment we can't do with expected alignment of the boundary. */
555 if (alignment * BITS_PER_UNIT > PREFERRED_STACK_BOUNDARY)
556 alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
558 if (function->stack_alignment_needed < alignment * BITS_PER_UNIT)
559 function->stack_alignment_needed = alignment * BITS_PER_UNIT;
561 /* Calculate how many bytes the start of local variables is off from
562 stack alignment. */
563 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
564 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
565 frame_phase = frame_off ? frame_alignment - frame_off : 0;
567 /* Round frame offset to that alignment.
568 We must be careful here, since FRAME_OFFSET might be negative and
569 division with a negative dividend isn't as well defined as we might
570 like. So we instead assume that ALIGNMENT is a power of two and
571 use logical operations which are unambiguous. */
572 #ifdef FRAME_GROWS_DOWNWARD
573 function->x_frame_offset = FLOOR_ROUND (function->x_frame_offset - frame_phase, alignment) + frame_phase;
574 #else
575 function->x_frame_offset = CEIL_ROUND (function->x_frame_offset - frame_phase, alignment) + frame_phase;
576 #endif
578 /* On a big-endian machine, if we are allocating more space than we will use,
579 use the least significant bytes of those that are allocated. */
580 if (BYTES_BIG_ENDIAN && mode != BLKmode)
581 bigend_correction = size - GET_MODE_SIZE (mode);
583 /* If we have already instantiated virtual registers, return the actual
584 address relative to the frame pointer. */
585 if (function == cfun && virtuals_instantiated)
586 addr = plus_constant (frame_pointer_rtx,
587 (frame_offset + bigend_correction
588 + STARTING_FRAME_OFFSET));
589 else
590 addr = plus_constant (virtual_stack_vars_rtx,
591 function->x_frame_offset + bigend_correction);
593 #ifndef FRAME_GROWS_DOWNWARD
594 function->x_frame_offset += size;
595 #endif
597 x = gen_rtx_MEM (mode, addr);
599 function->x_stack_slot_list
600 = gen_rtx_EXPR_LIST (VOIDmode, x, function->x_stack_slot_list);
602 return x;
605 /* Wrapper around assign_stack_local_1; assign a local stack slot for the
606 current function. */
609 assign_stack_local (mode, size, align)
610 enum machine_mode mode;
611 HOST_WIDE_INT size;
612 int align;
614 return assign_stack_local_1 (mode, size, align, cfun);
617 /* Allocate a temporary stack slot and record it for possible later
618 reuse.
620 MODE is the machine mode to be given to the returned rtx.
622 SIZE is the size in units of the space required. We do no rounding here
623 since assign_stack_local will do any required rounding.
625 KEEP is 1 if this slot is to be retained after a call to
626 free_temp_slots. Automatic variables for a block are allocated
627 with this flag. KEEP is 2 if we allocate a longer term temporary,
628 whose lifetime is controlled by CLEANUP_POINT_EXPRs. KEEP is 3
629 if we are to allocate something at an inner level to be treated as
630 a variable in the block (e.g., a SAVE_EXPR).
632 TYPE is the type that will be used for the stack slot. */
635 assign_stack_temp_for_type (mode, size, keep, type)
636 enum machine_mode mode;
637 HOST_WIDE_INT size;
638 int keep;
639 tree type;
641 unsigned int align;
642 struct temp_slot *p, *best_p = 0;
643 rtx slot;
645 /* If SIZE is -1 it means that somebody tried to allocate a temporary
646 of a variable size. */
647 if (size == -1)
648 abort ();
650 if (mode == BLKmode)
651 align = BIGGEST_ALIGNMENT;
652 else
653 align = GET_MODE_ALIGNMENT (mode);
655 if (! type)
656 type = (*lang_hooks.types.type_for_mode) (mode, 0);
658 if (type)
659 align = LOCAL_ALIGNMENT (type, align);
661 /* Try to find an available, already-allocated temporary of the proper
662 mode which meets the size and alignment requirements. Choose the
663 smallest one with the closest alignment. */
664 for (p = temp_slots; p; p = p->next)
665 if (p->align >= align && p->size >= size && GET_MODE (p->slot) == mode
666 && ! p->in_use
667 && objects_must_conflict_p (p->type, type)
668 && (best_p == 0 || best_p->size > p->size
669 || (best_p->size == p->size && best_p->align > p->align)))
671 if (p->align == align && p->size == size)
673 best_p = 0;
674 break;
676 best_p = p;
679 /* Make our best, if any, the one to use. */
680 if (best_p)
682 /* If there are enough aligned bytes left over, make them into a new
683 temp_slot so that the extra bytes don't get wasted. Do this only
684 for BLKmode slots, so that we can be sure of the alignment. */
685 if (GET_MODE (best_p->slot) == BLKmode)
687 int alignment = best_p->align / BITS_PER_UNIT;
688 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
690 if (best_p->size - rounded_size >= alignment)
692 p = (struct temp_slot *) ggc_alloc (sizeof (struct temp_slot));
693 p->in_use = p->addr_taken = 0;
694 p->size = best_p->size - rounded_size;
695 p->base_offset = best_p->base_offset + rounded_size;
696 p->full_size = best_p->full_size - rounded_size;
697 p->slot = gen_rtx_MEM (BLKmode,
698 plus_constant (XEXP (best_p->slot, 0),
699 rounded_size));
700 p->align = best_p->align;
701 p->address = 0;
702 p->rtl_expr = 0;
703 p->type = best_p->type;
704 p->next = temp_slots;
705 temp_slots = p;
707 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
708 stack_slot_list);
710 best_p->size = rounded_size;
711 best_p->full_size = rounded_size;
715 p = best_p;
718 /* If we still didn't find one, make a new temporary. */
719 if (p == 0)
721 HOST_WIDE_INT frame_offset_old = frame_offset;
723 p = (struct temp_slot *) ggc_alloc (sizeof (struct temp_slot));
725 /* We are passing an explicit alignment request to assign_stack_local.
726 One side effect of that is assign_stack_local will not round SIZE
727 to ensure the frame offset remains suitably aligned.
729 So for requests which depended on the rounding of SIZE, we go ahead
730 and round it now. We also make sure ALIGNMENT is at least
731 BIGGEST_ALIGNMENT. */
732 if (mode == BLKmode && align < BIGGEST_ALIGNMENT)
733 abort ();
734 p->slot = assign_stack_local (mode,
735 (mode == BLKmode
736 ? CEIL_ROUND (size, align / BITS_PER_UNIT)
737 : size),
738 align);
740 p->align = align;
742 /* The following slot size computation is necessary because we don't
743 know the actual size of the temporary slot until assign_stack_local
744 has performed all the frame alignment and size rounding for the
745 requested temporary. Note that extra space added for alignment
746 can be either above or below this stack slot depending on which
747 way the frame grows. We include the extra space if and only if it
748 is above this slot. */
749 #ifdef FRAME_GROWS_DOWNWARD
750 p->size = frame_offset_old - frame_offset;
751 #else
752 p->size = size;
753 #endif
755 /* Now define the fields used by combine_temp_slots. */
756 #ifdef FRAME_GROWS_DOWNWARD
757 p->base_offset = frame_offset;
758 p->full_size = frame_offset_old - frame_offset;
759 #else
760 p->base_offset = frame_offset_old;
761 p->full_size = frame_offset - frame_offset_old;
762 #endif
763 p->address = 0;
764 p->next = temp_slots;
765 temp_slots = p;
768 p->in_use = 1;
769 p->addr_taken = 0;
770 p->rtl_expr = seq_rtl_expr;
771 p->type = type;
773 if (keep == 2)
775 p->level = target_temp_slot_level;
776 p->keep = 0;
778 else if (keep == 3)
780 p->level = var_temp_slot_level;
781 p->keep = 0;
783 else
785 p->level = temp_slot_level;
786 p->keep = keep;
790 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
791 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
792 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
794 /* If we know the alias set for the memory that will be used, use
795 it. If there's no TYPE, then we don't know anything about the
796 alias set for the memory. */
797 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
798 set_mem_align (slot, align);
800 /* If a type is specified, set the relevant flags. */
801 if (type != 0)
803 RTX_UNCHANGING_P (slot) = TYPE_READONLY (type);
804 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
805 MEM_SET_IN_STRUCT_P (slot, AGGREGATE_TYPE_P (type));
808 return slot;
811 /* Allocate a temporary stack slot and record it for possible later
812 reuse. First three arguments are same as in preceding function. */
815 assign_stack_temp (mode, size, keep)
816 enum machine_mode mode;
817 HOST_WIDE_INT size;
818 int keep;
820 return assign_stack_temp_for_type (mode, size, keep, NULL_TREE);
823 /* Assign a temporary.
824 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
825 and so that should be used in error messages. In either case, we
826 allocate of the given type.
827 KEEP is as for assign_stack_temp.
828 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
829 it is 0 if a register is OK.
830 DONT_PROMOTE is 1 if we should not promote values in register
831 to wider modes. */
834 assign_temp (type_or_decl, keep, memory_required, dont_promote)
835 tree type_or_decl;
836 int keep;
837 int memory_required;
838 int dont_promote ATTRIBUTE_UNUSED;
840 tree type, decl;
841 enum machine_mode mode;
842 #ifndef PROMOTE_FOR_CALL_ONLY
843 int unsignedp;
844 #endif
846 if (DECL_P (type_or_decl))
847 decl = type_or_decl, type = TREE_TYPE (decl);
848 else
849 decl = NULL, type = type_or_decl;
851 mode = TYPE_MODE (type);
852 #ifndef PROMOTE_FOR_CALL_ONLY
853 unsignedp = TREE_UNSIGNED (type);
854 #endif
856 if (mode == BLKmode || memory_required)
858 HOST_WIDE_INT size = int_size_in_bytes (type);
859 rtx tmp;
861 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
862 problems with allocating the stack space. */
863 if (size == 0)
864 size = 1;
866 /* Unfortunately, we don't yet know how to allocate variable-sized
867 temporaries. However, sometimes we have a fixed upper limit on
868 the size (which is stored in TYPE_ARRAY_MAX_SIZE) and can use that
869 instead. This is the case for Chill variable-sized strings. */
870 if (size == -1 && TREE_CODE (type) == ARRAY_TYPE
871 && TYPE_ARRAY_MAX_SIZE (type) != NULL_TREE
872 && host_integerp (TYPE_ARRAY_MAX_SIZE (type), 1))
873 size = tree_low_cst (TYPE_ARRAY_MAX_SIZE (type), 1);
875 /* The size of the temporary may be too large to fit into an integer. */
876 /* ??? Not sure this should happen except for user silliness, so limit
877 this to things that aren't compiler-generated temporaries. The
878 rest of the time we'll abort in assign_stack_temp_for_type. */
879 if (decl && size == -1
880 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
882 error_with_decl (decl, "size of variable `%s' is too large");
883 size = 1;
886 tmp = assign_stack_temp_for_type (mode, size, keep, type);
887 return tmp;
890 #ifndef PROMOTE_FOR_CALL_ONLY
891 if (! dont_promote)
892 mode = promote_mode (type, mode, &unsignedp, 0);
893 #endif
895 return gen_reg_rtx (mode);
898 /* Combine temporary stack slots which are adjacent on the stack.
900 This allows for better use of already allocated stack space. This is only
901 done for BLKmode slots because we can be sure that we won't have alignment
902 problems in this case. */
904 void
905 combine_temp_slots ()
907 struct temp_slot *p, *q;
908 struct temp_slot *prev_p, *prev_q;
909 int num_slots;
911 /* We can't combine slots, because the information about which slot
912 is in which alias set will be lost. */
913 if (flag_strict_aliasing)
914 return;
916 /* If there are a lot of temp slots, don't do anything unless
917 high levels of optimization. */
918 if (! flag_expensive_optimizations)
919 for (p = temp_slots, num_slots = 0; p; p = p->next, num_slots++)
920 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
921 return;
923 for (p = temp_slots, prev_p = 0; p; p = prev_p ? prev_p->next : temp_slots)
925 int delete_p = 0;
927 if (! p->in_use && GET_MODE (p->slot) == BLKmode)
928 for (q = p->next, prev_q = p; q; q = prev_q->next)
930 int delete_q = 0;
931 if (! q->in_use && GET_MODE (q->slot) == BLKmode)
933 if (p->base_offset + p->full_size == q->base_offset)
935 /* Q comes after P; combine Q into P. */
936 p->size += q->size;
937 p->full_size += q->full_size;
938 delete_q = 1;
940 else if (q->base_offset + q->full_size == p->base_offset)
942 /* P comes after Q; combine P into Q. */
943 q->size += p->size;
944 q->full_size += p->full_size;
945 delete_p = 1;
946 break;
949 /* Either delete Q or advance past it. */
950 if (delete_q)
951 prev_q->next = q->next;
952 else
953 prev_q = q;
955 /* Either delete P or advance past it. */
956 if (delete_p)
958 if (prev_p)
959 prev_p->next = p->next;
960 else
961 temp_slots = p->next;
963 else
964 prev_p = p;
968 /* Find the temp slot corresponding to the object at address X. */
970 static struct temp_slot *
971 find_temp_slot_from_address (x)
972 rtx x;
974 struct temp_slot *p;
975 rtx next;
977 for (p = temp_slots; p; p = p->next)
979 if (! p->in_use)
980 continue;
982 else if (XEXP (p->slot, 0) == x
983 || p->address == x
984 || (GET_CODE (x) == PLUS
985 && XEXP (x, 0) == virtual_stack_vars_rtx
986 && GET_CODE (XEXP (x, 1)) == CONST_INT
987 && INTVAL (XEXP (x, 1)) >= p->base_offset
988 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size))
989 return p;
991 else if (p->address != 0 && GET_CODE (p->address) == EXPR_LIST)
992 for (next = p->address; next; next = XEXP (next, 1))
993 if (XEXP (next, 0) == x)
994 return p;
997 /* If we have a sum involving a register, see if it points to a temp
998 slot. */
999 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == REG
1000 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
1001 return p;
1002 else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG
1003 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
1004 return p;
1006 return 0;
1009 /* Indicate that NEW is an alternate way of referring to the temp slot
1010 that previously was known by OLD. */
1012 void
1013 update_temp_slot_address (old, new)
1014 rtx old, new;
1016 struct temp_slot *p;
1018 if (rtx_equal_p (old, new))
1019 return;
1021 p = find_temp_slot_from_address (old);
1023 /* If we didn't find one, see if both OLD is a PLUS. If so, and NEW
1024 is a register, see if one operand of the PLUS is a temporary
1025 location. If so, NEW points into it. Otherwise, if both OLD and
1026 NEW are a PLUS and if there is a register in common between them.
1027 If so, try a recursive call on those values. */
1028 if (p == 0)
1030 if (GET_CODE (old) != PLUS)
1031 return;
1033 if (GET_CODE (new) == REG)
1035 update_temp_slot_address (XEXP (old, 0), new);
1036 update_temp_slot_address (XEXP (old, 1), new);
1037 return;
1039 else if (GET_CODE (new) != PLUS)
1040 return;
1042 if (rtx_equal_p (XEXP (old, 0), XEXP (new, 0)))
1043 update_temp_slot_address (XEXP (old, 1), XEXP (new, 1));
1044 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 0)))
1045 update_temp_slot_address (XEXP (old, 0), XEXP (new, 1));
1046 else if (rtx_equal_p (XEXP (old, 0), XEXP (new, 1)))
1047 update_temp_slot_address (XEXP (old, 1), XEXP (new, 0));
1048 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 1)))
1049 update_temp_slot_address (XEXP (old, 0), XEXP (new, 0));
1051 return;
1054 /* Otherwise add an alias for the temp's address. */
1055 else if (p->address == 0)
1056 p->address = new;
1057 else
1059 if (GET_CODE (p->address) != EXPR_LIST)
1060 p->address = gen_rtx_EXPR_LIST (VOIDmode, p->address, NULL_RTX);
1062 p->address = gen_rtx_EXPR_LIST (VOIDmode, new, p->address);
1066 /* If X could be a reference to a temporary slot, mark the fact that its
1067 address was taken. */
1069 void
1070 mark_temp_addr_taken (x)
1071 rtx x;
1073 struct temp_slot *p;
1075 if (x == 0)
1076 return;
1078 /* If X is not in memory or is at a constant address, it cannot be in
1079 a temporary slot. */
1080 if (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1081 return;
1083 p = find_temp_slot_from_address (XEXP (x, 0));
1084 if (p != 0)
1085 p->addr_taken = 1;
1088 /* If X could be a reference to a temporary slot, mark that slot as
1089 belonging to the to one level higher than the current level. If X
1090 matched one of our slots, just mark that one. Otherwise, we can't
1091 easily predict which it is, so upgrade all of them. Kept slots
1092 need not be touched.
1094 This is called when an ({...}) construct occurs and a statement
1095 returns a value in memory. */
1097 void
1098 preserve_temp_slots (x)
1099 rtx x;
1101 struct temp_slot *p = 0;
1103 /* If there is no result, we still might have some objects whose address
1104 were taken, so we need to make sure they stay around. */
1105 if (x == 0)
1107 for (p = temp_slots; p; p = p->next)
1108 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1109 p->level--;
1111 return;
1114 /* If X is a register that is being used as a pointer, see if we have
1115 a temporary slot we know it points to. To be consistent with
1116 the code below, we really should preserve all non-kept slots
1117 if we can't find a match, but that seems to be much too costly. */
1118 if (GET_CODE (x) == REG && REG_POINTER (x))
1119 p = find_temp_slot_from_address (x);
1121 /* If X is not in memory or is at a constant address, it cannot be in
1122 a temporary slot, but it can contain something whose address was
1123 taken. */
1124 if (p == 0 && (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0))))
1126 for (p = temp_slots; p; p = p->next)
1127 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1128 p->level--;
1130 return;
1133 /* First see if we can find a match. */
1134 if (p == 0)
1135 p = find_temp_slot_from_address (XEXP (x, 0));
1137 if (p != 0)
1139 /* Move everything at our level whose address was taken to our new
1140 level in case we used its address. */
1141 struct temp_slot *q;
1143 if (p->level == temp_slot_level)
1145 for (q = temp_slots; q; q = q->next)
1146 if (q != p && q->addr_taken && q->level == p->level)
1147 q->level--;
1149 p->level--;
1150 p->addr_taken = 0;
1152 return;
1155 /* Otherwise, preserve all non-kept slots at this level. */
1156 for (p = temp_slots; p; p = p->next)
1157 if (p->in_use && p->level == temp_slot_level && ! p->keep)
1158 p->level--;
1161 /* X is the result of an RTL_EXPR. If it is a temporary slot associated
1162 with that RTL_EXPR, promote it into a temporary slot at the present
1163 level so it will not be freed when we free slots made in the
1164 RTL_EXPR. */
1166 void
1167 preserve_rtl_expr_result (x)
1168 rtx x;
1170 struct temp_slot *p;
1172 /* If X is not in memory or is at a constant address, it cannot be in
1173 a temporary slot. */
1174 if (x == 0 || GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1175 return;
1177 /* If we can find a match, move it to our level unless it is already at
1178 an upper level. */
1179 p = find_temp_slot_from_address (XEXP (x, 0));
1180 if (p != 0)
1182 p->level = MIN (p->level, temp_slot_level);
1183 p->rtl_expr = 0;
1186 return;
1189 /* Free all temporaries used so far. This is normally called at the end
1190 of generating code for a statement. Don't free any temporaries
1191 currently in use for an RTL_EXPR that hasn't yet been emitted.
1192 We could eventually do better than this since it can be reused while
1193 generating the same RTL_EXPR, but this is complex and probably not
1194 worthwhile. */
1196 void
1197 free_temp_slots ()
1199 struct temp_slot *p;
1201 for (p = temp_slots; p; p = p->next)
1202 if (p->in_use && p->level == temp_slot_level && ! p->keep
1203 && p->rtl_expr == 0)
1204 p->in_use = 0;
1206 combine_temp_slots ();
1209 /* Free all temporary slots used in T, an RTL_EXPR node. */
1211 void
1212 free_temps_for_rtl_expr (t)
1213 tree t;
1215 struct temp_slot *p;
1217 for (p = temp_slots; p; p = p->next)
1218 if (p->rtl_expr == t)
1220 /* If this slot is below the current TEMP_SLOT_LEVEL, then it
1221 needs to be preserved. This can happen if a temporary in
1222 the RTL_EXPR was addressed; preserve_temp_slots will move
1223 the temporary into a higher level. */
1224 if (temp_slot_level <= p->level)
1225 p->in_use = 0;
1226 else
1227 p->rtl_expr = NULL_TREE;
1230 combine_temp_slots ();
1233 /* Mark all temporaries ever allocated in this function as not suitable
1234 for reuse until the current level is exited. */
1236 void
1237 mark_all_temps_used ()
1239 struct temp_slot *p;
1241 for (p = temp_slots; p; p = p->next)
1243 p->in_use = p->keep = 1;
1244 p->level = MIN (p->level, temp_slot_level);
1248 /* Push deeper into the nesting level for stack temporaries. */
1250 void
1251 push_temp_slots ()
1253 temp_slot_level++;
1256 /* Likewise, but save the new level as the place to allocate variables
1257 for blocks. */
1259 #if 0
1260 void
1261 push_temp_slots_for_block ()
1263 push_temp_slots ();
1265 var_temp_slot_level = temp_slot_level;
1268 /* Likewise, but save the new level as the place to allocate temporaries
1269 for TARGET_EXPRs. */
1271 void
1272 push_temp_slots_for_target ()
1274 push_temp_slots ();
1276 target_temp_slot_level = temp_slot_level;
1279 /* Set and get the value of target_temp_slot_level. The only
1280 permitted use of these functions is to save and restore this value. */
1283 get_target_temp_slot_level ()
1285 return target_temp_slot_level;
1288 void
1289 set_target_temp_slot_level (level)
1290 int level;
1292 target_temp_slot_level = level;
1294 #endif
1296 /* Pop a temporary nesting level. All slots in use in the current level
1297 are freed. */
1299 void
1300 pop_temp_slots ()
1302 struct temp_slot *p;
1304 for (p = temp_slots; p; p = p->next)
1305 if (p->in_use && p->level == temp_slot_level && p->rtl_expr == 0)
1306 p->in_use = 0;
1308 combine_temp_slots ();
1310 temp_slot_level--;
1313 /* Initialize temporary slots. */
1315 void
1316 init_temp_slots ()
1318 /* We have not allocated any temporaries yet. */
1319 temp_slots = 0;
1320 temp_slot_level = 0;
1321 var_temp_slot_level = 0;
1322 target_temp_slot_level = 0;
1325 /* Retroactively move an auto variable from a register to a stack slot.
1326 This is done when an address-reference to the variable is seen. */
1328 void
1329 put_var_into_stack (decl)
1330 tree decl;
1332 rtx reg;
1333 enum machine_mode promoted_mode, decl_mode;
1334 struct function *function = 0;
1335 tree context;
1336 int can_use_addressof;
1337 int volatilep = TREE_CODE (decl) != SAVE_EXPR && TREE_THIS_VOLATILE (decl);
1338 int usedp = (TREE_USED (decl)
1339 || (TREE_CODE (decl) != SAVE_EXPR && DECL_INITIAL (decl) != 0));
1341 context = decl_function_context (decl);
1343 /* Get the current rtl used for this object and its original mode. */
1344 reg = (TREE_CODE (decl) == SAVE_EXPR
1345 ? SAVE_EXPR_RTL (decl)
1346 : DECL_RTL_IF_SET (decl));
1348 /* No need to do anything if decl has no rtx yet
1349 since in that case caller is setting TREE_ADDRESSABLE
1350 and a stack slot will be assigned when the rtl is made. */
1351 if (reg == 0)
1352 return;
1354 /* Get the declared mode for this object. */
1355 decl_mode = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
1356 : DECL_MODE (decl));
1357 /* Get the mode it's actually stored in. */
1358 promoted_mode = GET_MODE (reg);
1360 /* If this variable comes from an outer function, find that
1361 function's saved context. Don't use find_function_data here,
1362 because it might not be in any active function.
1363 FIXME: Is that really supposed to happen?
1364 It does in ObjC at least. */
1365 if (context != current_function_decl && context != inline_function_decl)
1366 for (function = outer_function_chain; function; function = function->outer)
1367 if (function->decl == context)
1368 break;
1370 /* If this is a variable-size object with a pseudo to address it,
1371 put that pseudo into the stack, if the var is nonlocal. */
1372 if (TREE_CODE (decl) != SAVE_EXPR && DECL_NONLOCAL (decl)
1373 && GET_CODE (reg) == MEM
1374 && GET_CODE (XEXP (reg, 0)) == REG
1375 && REGNO (XEXP (reg, 0)) > LAST_VIRTUAL_REGISTER)
1377 reg = XEXP (reg, 0);
1378 decl_mode = promoted_mode = GET_MODE (reg);
1381 can_use_addressof
1382 = (function == 0
1383 && optimize > 0
1384 /* FIXME make it work for promoted modes too */
1385 && decl_mode == promoted_mode
1386 #ifdef NON_SAVING_SETJMP
1387 && ! (NON_SAVING_SETJMP && current_function_calls_setjmp)
1388 #endif
1391 /* If we can't use ADDRESSOF, make sure we see through one we already
1392 generated. */
1393 if (! can_use_addressof && GET_CODE (reg) == MEM
1394 && GET_CODE (XEXP (reg, 0)) == ADDRESSOF)
1395 reg = XEXP (XEXP (reg, 0), 0);
1397 /* Now we should have a value that resides in one or more pseudo regs. */
1399 if (GET_CODE (reg) == REG)
1401 /* If this variable lives in the current function and we don't need
1402 to put things in the stack for the sake of setjmp, try to keep it
1403 in a register until we know we actually need the address. */
1404 if (can_use_addressof)
1405 gen_mem_addressof (reg, decl);
1406 else
1407 put_reg_into_stack (function, reg, TREE_TYPE (decl), promoted_mode,
1408 decl_mode, volatilep, 0, usedp, 0);
1410 else if (GET_CODE (reg) == CONCAT)
1412 /* A CONCAT contains two pseudos; put them both in the stack.
1413 We do it so they end up consecutive.
1414 We fixup references to the parts only after we fixup references
1415 to the whole CONCAT, lest we do double fixups for the latter
1416 references. */
1417 enum machine_mode part_mode = GET_MODE (XEXP (reg, 0));
1418 tree part_type = (*lang_hooks.types.type_for_mode) (part_mode, 0);
1419 rtx lopart = XEXP (reg, 0);
1420 rtx hipart = XEXP (reg, 1);
1421 #ifdef FRAME_GROWS_DOWNWARD
1422 /* Since part 0 should have a lower address, do it second. */
1423 put_reg_into_stack (function, hipart, part_type, part_mode,
1424 part_mode, volatilep, 0, 0, 0);
1425 put_reg_into_stack (function, lopart, part_type, part_mode,
1426 part_mode, volatilep, 0, 0, 0);
1427 #else
1428 put_reg_into_stack (function, lopart, part_type, part_mode,
1429 part_mode, volatilep, 0, 0, 0);
1430 put_reg_into_stack (function, hipart, part_type, part_mode,
1431 part_mode, volatilep, 0, 0, 0);
1432 #endif
1434 /* Change the CONCAT into a combined MEM for both parts. */
1435 PUT_CODE (reg, MEM);
1436 MEM_ATTRS (reg) = 0;
1438 /* set_mem_attributes uses DECL_RTL to avoid re-generating of
1439 already computed alias sets. Here we want to re-generate. */
1440 if (DECL_P (decl))
1441 SET_DECL_RTL (decl, NULL);
1442 set_mem_attributes (reg, decl, 1);
1443 if (DECL_P (decl))
1444 SET_DECL_RTL (decl, reg);
1446 /* The two parts are in memory order already.
1447 Use the lower parts address as ours. */
1448 XEXP (reg, 0) = XEXP (XEXP (reg, 0), 0);
1449 /* Prevent sharing of rtl that might lose. */
1450 if (GET_CODE (XEXP (reg, 0)) == PLUS)
1451 XEXP (reg, 0) = copy_rtx (XEXP (reg, 0));
1452 if (usedp)
1454 schedule_fixup_var_refs (function, reg, TREE_TYPE (decl),
1455 promoted_mode, 0);
1456 schedule_fixup_var_refs (function, lopart, part_type, part_mode, 0);
1457 schedule_fixup_var_refs (function, hipart, part_type, part_mode, 0);
1460 else
1461 return;
1464 /* Subroutine of put_var_into_stack. This puts a single pseudo reg REG
1465 into the stack frame of FUNCTION (0 means the current function).
1466 DECL_MODE is the machine mode of the user-level data type.
1467 PROMOTED_MODE is the machine mode of the register.
1468 VOLATILE_P is nonzero if this is for a "volatile" decl.
1469 USED_P is nonzero if this reg might have already been used in an insn. */
1471 static void
1472 put_reg_into_stack (function, reg, type, promoted_mode, decl_mode, volatile_p,
1473 original_regno, used_p, ht)
1474 struct function *function;
1475 rtx reg;
1476 tree type;
1477 enum machine_mode promoted_mode, decl_mode;
1478 int volatile_p;
1479 unsigned int original_regno;
1480 int used_p;
1481 htab_t ht;
1483 struct function *func = function ? function : cfun;
1484 rtx new = 0;
1485 unsigned int regno = original_regno;
1487 if (regno == 0)
1488 regno = REGNO (reg);
1490 if (regno < func->x_max_parm_reg)
1491 new = func->x_parm_reg_stack_loc[regno];
1493 if (new == 0)
1494 new = assign_stack_local_1 (decl_mode, GET_MODE_SIZE (decl_mode), 0, func);
1496 PUT_CODE (reg, MEM);
1497 PUT_MODE (reg, decl_mode);
1498 XEXP (reg, 0) = XEXP (new, 0);
1499 MEM_ATTRS (reg) = 0;
1500 /* `volatil' bit means one thing for MEMs, another entirely for REGs. */
1501 MEM_VOLATILE_P (reg) = volatile_p;
1503 /* If this is a memory ref that contains aggregate components,
1504 mark it as such for cse and loop optimize. If we are reusing a
1505 previously generated stack slot, then we need to copy the bit in
1506 case it was set for other reasons. For instance, it is set for
1507 __builtin_va_alist. */
1508 if (type)
1510 MEM_SET_IN_STRUCT_P (reg,
1511 AGGREGATE_TYPE_P (type) || MEM_IN_STRUCT_P (new));
1512 set_mem_alias_set (reg, get_alias_set (type));
1515 if (used_p)
1516 schedule_fixup_var_refs (function, reg, type, promoted_mode, ht);
1519 /* Make sure that all refs to the variable, previously made
1520 when it was a register, are fixed up to be valid again.
1521 See function above for meaning of arguments. */
1523 static void
1524 schedule_fixup_var_refs (function, reg, type, promoted_mode, ht)
1525 struct function *function;
1526 rtx reg;
1527 tree type;
1528 enum machine_mode promoted_mode;
1529 htab_t ht;
1531 int unsigned_p = type ? TREE_UNSIGNED (type) : 0;
1533 if (function != 0)
1535 struct var_refs_queue *temp;
1537 temp
1538 = (struct var_refs_queue *) ggc_alloc (sizeof (struct var_refs_queue));
1539 temp->modified = reg;
1540 temp->promoted_mode = promoted_mode;
1541 temp->unsignedp = unsigned_p;
1542 temp->next = function->fixup_var_refs_queue;
1543 function->fixup_var_refs_queue = temp;
1545 else
1546 /* Variable is local; fix it up now. */
1547 fixup_var_refs (reg, promoted_mode, unsigned_p, reg, ht);
1550 static void
1551 fixup_var_refs (var, promoted_mode, unsignedp, may_share, ht)
1552 rtx var;
1553 enum machine_mode promoted_mode;
1554 int unsignedp;
1555 htab_t ht;
1556 rtx may_share;
1558 tree pending;
1559 rtx first_insn = get_insns ();
1560 struct sequence_stack *stack = seq_stack;
1561 tree rtl_exps = rtl_expr_chain;
1563 /* If there's a hash table, it must record all uses of VAR. */
1564 if (ht)
1566 if (stack != 0)
1567 abort ();
1568 fixup_var_refs_insns_with_hash (ht, var, promoted_mode, unsignedp,
1569 may_share);
1570 return;
1573 fixup_var_refs_insns (first_insn, var, promoted_mode, unsignedp,
1574 stack == 0, may_share);
1576 /* Scan all pending sequences too. */
1577 for (; stack; stack = stack->next)
1579 push_to_full_sequence (stack->first, stack->last);
1580 fixup_var_refs_insns (stack->first, var, promoted_mode, unsignedp,
1581 stack->next != 0, may_share);
1582 /* Update remembered end of sequence
1583 in case we added an insn at the end. */
1584 stack->last = get_last_insn ();
1585 end_sequence ();
1588 /* Scan all waiting RTL_EXPRs too. */
1589 for (pending = rtl_exps; pending; pending = TREE_CHAIN (pending))
1591 rtx seq = RTL_EXPR_SEQUENCE (TREE_VALUE (pending));
1592 if (seq != const0_rtx && seq != 0)
1594 push_to_sequence (seq);
1595 fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0,
1596 may_share);
1597 end_sequence ();
1602 /* REPLACEMENTS is a pointer to a list of the struct fixup_replacement and X is
1603 some part of an insn. Return a struct fixup_replacement whose OLD
1604 value is equal to X. Allocate a new structure if no such entry exists. */
1606 static struct fixup_replacement *
1607 find_fixup_replacement (replacements, x)
1608 struct fixup_replacement **replacements;
1609 rtx x;
1611 struct fixup_replacement *p;
1613 /* See if we have already replaced this. */
1614 for (p = *replacements; p != 0 && ! rtx_equal_p (p->old, x); p = p->next)
1617 if (p == 0)
1619 p = (struct fixup_replacement *) xmalloc (sizeof (struct fixup_replacement));
1620 p->old = x;
1621 p->new = 0;
1622 p->next = *replacements;
1623 *replacements = p;
1626 return p;
1629 /* Scan the insn-chain starting with INSN for refs to VAR and fix them
1630 up. TOPLEVEL is nonzero if this chain is the main chain of insns
1631 for the current function. MAY_SHARE is either a MEM that is not
1632 to be unshared or a list of them. */
1634 static void
1635 fixup_var_refs_insns (insn, var, promoted_mode, unsignedp, toplevel, may_share)
1636 rtx insn;
1637 rtx var;
1638 enum machine_mode promoted_mode;
1639 int unsignedp;
1640 int toplevel;
1641 rtx may_share;
1643 while (insn)
1645 /* fixup_var_refs_insn might modify insn, so save its next
1646 pointer now. */
1647 rtx next = NEXT_INSN (insn);
1649 /* CALL_PLACEHOLDERs are special; we have to switch into each of
1650 the three sequences they (potentially) contain, and process
1651 them recursively. The CALL_INSN itself is not interesting. */
1653 if (GET_CODE (insn) == CALL_INSN
1654 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1656 int i;
1658 /* Look at the Normal call, sibling call and tail recursion
1659 sequences attached to the CALL_PLACEHOLDER. */
1660 for (i = 0; i < 3; i++)
1662 rtx seq = XEXP (PATTERN (insn), i);
1663 if (seq)
1665 push_to_sequence (seq);
1666 fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0,
1667 may_share);
1668 XEXP (PATTERN (insn), i) = get_insns ();
1669 end_sequence ();
1674 else if (INSN_P (insn))
1675 fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, toplevel,
1676 may_share);
1678 insn = next;
1682 /* Look up the insns which reference VAR in HT and fix them up. Other
1683 arguments are the same as fixup_var_refs_insns.
1685 N.B. No need for special processing of CALL_PLACEHOLDERs here,
1686 because the hash table will point straight to the interesting insn
1687 (inside the CALL_PLACEHOLDER). */
1689 static void
1690 fixup_var_refs_insns_with_hash (ht, var, promoted_mode, unsignedp, may_share)
1691 htab_t ht;
1692 rtx var;
1693 enum machine_mode promoted_mode;
1694 int unsignedp;
1695 rtx may_share;
1697 struct insns_for_mem_entry tmp;
1698 struct insns_for_mem_entry *ime;
1699 rtx insn_list;
1701 tmp.key = var;
1702 ime = (struct insns_for_mem_entry *) htab_find (ht, &tmp);
1703 for (insn_list = ime->insns; insn_list != 0; insn_list = XEXP (insn_list, 1))
1704 if (INSN_P (XEXP (insn_list, 0)))
1705 fixup_var_refs_insn (XEXP (insn_list, 0), var, promoted_mode,
1706 unsignedp, 1, may_share);
1710 /* Per-insn processing by fixup_var_refs_insns(_with_hash). INSN is
1711 the insn under examination, VAR is the variable to fix up
1712 references to, PROMOTED_MODE and UNSIGNEDP describe VAR, and
1713 TOPLEVEL is nonzero if this is the main insn chain for this
1714 function. */
1716 static void
1717 fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, toplevel, no_share)
1718 rtx insn;
1719 rtx var;
1720 enum machine_mode promoted_mode;
1721 int unsignedp;
1722 int toplevel;
1723 rtx no_share;
1725 rtx call_dest = 0;
1726 rtx set, prev, prev_set;
1727 rtx note;
1729 /* Remember the notes in case we delete the insn. */
1730 note = REG_NOTES (insn);
1732 /* If this is a CLOBBER of VAR, delete it.
1734 If it has a REG_LIBCALL note, delete the REG_LIBCALL
1735 and REG_RETVAL notes too. */
1736 if (GET_CODE (PATTERN (insn)) == CLOBBER
1737 && (XEXP (PATTERN (insn), 0) == var
1738 || (GET_CODE (XEXP (PATTERN (insn), 0)) == CONCAT
1739 && (XEXP (XEXP (PATTERN (insn), 0), 0) == var
1740 || XEXP (XEXP (PATTERN (insn), 0), 1) == var))))
1742 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0)
1743 /* The REG_LIBCALL note will go away since we are going to
1744 turn INSN into a NOTE, so just delete the
1745 corresponding REG_RETVAL note. */
1746 remove_note (XEXP (note, 0),
1747 find_reg_note (XEXP (note, 0), REG_RETVAL,
1748 NULL_RTX));
1750 delete_insn (insn);
1753 /* The insn to load VAR from a home in the arglist
1754 is now a no-op. When we see it, just delete it.
1755 Similarly if this is storing VAR from a register from which
1756 it was loaded in the previous insn. This will occur
1757 when an ADDRESSOF was made for an arglist slot. */
1758 else if (toplevel
1759 && (set = single_set (insn)) != 0
1760 && SET_DEST (set) == var
1761 /* If this represents the result of an insn group,
1762 don't delete the insn. */
1763 && find_reg_note (insn, REG_RETVAL, NULL_RTX) == 0
1764 && (rtx_equal_p (SET_SRC (set), var)
1765 || (GET_CODE (SET_SRC (set)) == REG
1766 && (prev = prev_nonnote_insn (insn)) != 0
1767 && (prev_set = single_set (prev)) != 0
1768 && SET_DEST (prev_set) == SET_SRC (set)
1769 && rtx_equal_p (SET_SRC (prev_set), var))))
1771 delete_insn (insn);
1773 else
1775 struct fixup_replacement *replacements = 0;
1776 rtx next_insn = NEXT_INSN (insn);
1778 if (SMALL_REGISTER_CLASSES)
1780 /* If the insn that copies the results of a CALL_INSN
1781 into a pseudo now references VAR, we have to use an
1782 intermediate pseudo since we want the life of the
1783 return value register to be only a single insn.
1785 If we don't use an intermediate pseudo, such things as
1786 address computations to make the address of VAR valid
1787 if it is not can be placed between the CALL_INSN and INSN.
1789 To make sure this doesn't happen, we record the destination
1790 of the CALL_INSN and see if the next insn uses both that
1791 and VAR. */
1793 if (call_dest != 0 && GET_CODE (insn) == INSN
1794 && reg_mentioned_p (var, PATTERN (insn))
1795 && reg_mentioned_p (call_dest, PATTERN (insn)))
1797 rtx temp = gen_reg_rtx (GET_MODE (call_dest));
1799 emit_insn_before (gen_move_insn (temp, call_dest), insn);
1801 PATTERN (insn) = replace_rtx (PATTERN (insn),
1802 call_dest, temp);
1805 if (GET_CODE (insn) == CALL_INSN
1806 && GET_CODE (PATTERN (insn)) == SET)
1807 call_dest = SET_DEST (PATTERN (insn));
1808 else if (GET_CODE (insn) == CALL_INSN
1809 && GET_CODE (PATTERN (insn)) == PARALLEL
1810 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1811 call_dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1812 else
1813 call_dest = 0;
1816 /* See if we have to do anything to INSN now that VAR is in
1817 memory. If it needs to be loaded into a pseudo, use a single
1818 pseudo for the entire insn in case there is a MATCH_DUP
1819 between two operands. We pass a pointer to the head of
1820 a list of struct fixup_replacements. If fixup_var_refs_1
1821 needs to allocate pseudos or replacement MEMs (for SUBREGs),
1822 it will record them in this list.
1824 If it allocated a pseudo for any replacement, we copy into
1825 it here. */
1827 fixup_var_refs_1 (var, promoted_mode, &PATTERN (insn), insn,
1828 &replacements, no_share);
1830 /* If this is last_parm_insn, and any instructions were output
1831 after it to fix it up, then we must set last_parm_insn to
1832 the last such instruction emitted. */
1833 if (insn == last_parm_insn)
1834 last_parm_insn = PREV_INSN (next_insn);
1836 while (replacements)
1838 struct fixup_replacement *next;
1840 if (GET_CODE (replacements->new) == REG)
1842 rtx insert_before;
1843 rtx seq;
1845 /* OLD might be a (subreg (mem)). */
1846 if (GET_CODE (replacements->old) == SUBREG)
1847 replacements->old
1848 = fixup_memory_subreg (replacements->old, insn,
1849 promoted_mode, 0);
1850 else
1851 replacements->old
1852 = fixup_stack_1 (replacements->old, insn);
1854 insert_before = insn;
1856 /* If we are changing the mode, do a conversion.
1857 This might be wasteful, but combine.c will
1858 eliminate much of the waste. */
1860 if (GET_MODE (replacements->new)
1861 != GET_MODE (replacements->old))
1863 start_sequence ();
1864 convert_move (replacements->new,
1865 replacements->old, unsignedp);
1866 seq = get_insns ();
1867 end_sequence ();
1869 else
1870 seq = gen_move_insn (replacements->new,
1871 replacements->old);
1873 emit_insn_before (seq, insert_before);
1876 next = replacements->next;
1877 free (replacements);
1878 replacements = next;
1882 /* Also fix up any invalid exprs in the REG_NOTES of this insn.
1883 But don't touch other insns referred to by reg-notes;
1884 we will get them elsewhere. */
1885 while (note)
1887 if (GET_CODE (note) != INSN_LIST)
1888 XEXP (note, 0)
1889 = walk_fixup_memory_subreg (XEXP (note, 0), insn,
1890 promoted_mode, 1);
1891 note = XEXP (note, 1);
1895 /* VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
1896 See if the rtx expression at *LOC in INSN needs to be changed.
1898 REPLACEMENTS is a pointer to a list head that starts out zero, but may
1899 contain a list of original rtx's and replacements. If we find that we need
1900 to modify this insn by replacing a memory reference with a pseudo or by
1901 making a new MEM to implement a SUBREG, we consult that list to see if
1902 we have already chosen a replacement. If none has already been allocated,
1903 we allocate it and update the list. fixup_var_refs_insn will copy VAR
1904 or the SUBREG, as appropriate, to the pseudo. */
1906 static void
1907 fixup_var_refs_1 (var, promoted_mode, loc, insn, replacements, no_share)
1908 rtx var;
1909 enum machine_mode promoted_mode;
1910 rtx *loc;
1911 rtx insn;
1912 struct fixup_replacement **replacements;
1913 rtx no_share;
1915 int i;
1916 rtx x = *loc;
1917 RTX_CODE code = GET_CODE (x);
1918 const char *fmt;
1919 rtx tem, tem1;
1920 struct fixup_replacement *replacement;
1922 switch (code)
1924 case ADDRESSOF:
1925 if (XEXP (x, 0) == var)
1927 /* Prevent sharing of rtl that might lose. */
1928 rtx sub = copy_rtx (XEXP (var, 0));
1930 if (! validate_change (insn, loc, sub, 0))
1932 rtx y = gen_reg_rtx (GET_MODE (sub));
1933 rtx seq, new_insn;
1935 /* We should be able to replace with a register or all is lost.
1936 Note that we can't use validate_change to verify this, since
1937 we're not caring for replacing all dups simultaneously. */
1938 if (! validate_replace_rtx (*loc, y, insn))
1939 abort ();
1941 /* Careful! First try to recognize a direct move of the
1942 value, mimicking how things are done in gen_reload wrt
1943 PLUS. Consider what happens when insn is a conditional
1944 move instruction and addsi3 clobbers flags. */
1946 start_sequence ();
1947 new_insn = emit_insn (gen_rtx_SET (VOIDmode, y, sub));
1948 seq = get_insns ();
1949 end_sequence ();
1951 if (recog_memoized (new_insn) < 0)
1953 /* That failed. Fall back on force_operand and hope. */
1955 start_sequence ();
1956 sub = force_operand (sub, y);
1957 if (sub != y)
1958 emit_insn (gen_move_insn (y, sub));
1959 seq = get_insns ();
1960 end_sequence ();
1963 #ifdef HAVE_cc0
1964 /* Don't separate setter from user. */
1965 if (PREV_INSN (insn) && sets_cc0_p (PREV_INSN (insn)))
1966 insn = PREV_INSN (insn);
1967 #endif
1969 emit_insn_before (seq, insn);
1972 return;
1974 case MEM:
1975 if (var == x)
1977 /* If we already have a replacement, use it. Otherwise,
1978 try to fix up this address in case it is invalid. */
1980 replacement = find_fixup_replacement (replacements, var);
1981 if (replacement->new)
1983 *loc = replacement->new;
1984 return;
1987 *loc = replacement->new = x = fixup_stack_1 (x, insn);
1989 /* Unless we are forcing memory to register or we changed the mode,
1990 we can leave things the way they are if the insn is valid. */
1992 INSN_CODE (insn) = -1;
1993 if (! flag_force_mem && GET_MODE (x) == promoted_mode
1994 && recog_memoized (insn) >= 0)
1995 return;
1997 *loc = replacement->new = gen_reg_rtx (promoted_mode);
1998 return;
2001 /* If X contains VAR, we need to unshare it here so that we update
2002 each occurrence separately. But all identical MEMs in one insn
2003 must be replaced with the same rtx because of the possibility of
2004 MATCH_DUPs. */
2006 if (reg_mentioned_p (var, x))
2008 replacement = find_fixup_replacement (replacements, x);
2009 if (replacement->new == 0)
2010 replacement->new = copy_most_rtx (x, no_share);
2012 *loc = x = replacement->new;
2013 code = GET_CODE (x);
2015 break;
2017 case REG:
2018 case CC0:
2019 case PC:
2020 case CONST_INT:
2021 case CONST:
2022 case SYMBOL_REF:
2023 case LABEL_REF:
2024 case CONST_DOUBLE:
2025 case CONST_VECTOR:
2026 return;
2028 case SIGN_EXTRACT:
2029 case ZERO_EXTRACT:
2030 /* Note that in some cases those types of expressions are altered
2031 by optimize_bit_field, and do not survive to get here. */
2032 if (XEXP (x, 0) == var
2033 || (GET_CODE (XEXP (x, 0)) == SUBREG
2034 && SUBREG_REG (XEXP (x, 0)) == var))
2036 /* Get TEM as a valid MEM in the mode presently in the insn.
2038 We don't worry about the possibility of MATCH_DUP here; it
2039 is highly unlikely and would be tricky to handle. */
2041 tem = XEXP (x, 0);
2042 if (GET_CODE (tem) == SUBREG)
2044 if (GET_MODE_BITSIZE (GET_MODE (tem))
2045 > GET_MODE_BITSIZE (GET_MODE (var)))
2047 replacement = find_fixup_replacement (replacements, var);
2048 if (replacement->new == 0)
2049 replacement->new = gen_reg_rtx (GET_MODE (var));
2050 SUBREG_REG (tem) = replacement->new;
2052 /* The following code works only if we have a MEM, so we
2053 need to handle the subreg here. We directly substitute
2054 it assuming that a subreg must be OK here. We already
2055 scheduled a replacement to copy the mem into the
2056 subreg. */
2057 XEXP (x, 0) = tem;
2058 return;
2060 else
2061 tem = fixup_memory_subreg (tem, insn, promoted_mode, 0);
2063 else
2064 tem = fixup_stack_1 (tem, insn);
2066 /* Unless we want to load from memory, get TEM into the proper mode
2067 for an extract from memory. This can only be done if the
2068 extract is at a constant position and length. */
2070 if (! flag_force_mem && GET_CODE (XEXP (x, 1)) == CONST_INT
2071 && GET_CODE (XEXP (x, 2)) == CONST_INT
2072 && ! mode_dependent_address_p (XEXP (tem, 0))
2073 && ! MEM_VOLATILE_P (tem))
2075 enum machine_mode wanted_mode = VOIDmode;
2076 enum machine_mode is_mode = GET_MODE (tem);
2077 HOST_WIDE_INT pos = INTVAL (XEXP (x, 2));
2079 if (GET_CODE (x) == ZERO_EXTRACT)
2081 enum machine_mode new_mode
2082 = mode_for_extraction (EP_extzv, 1);
2083 if (new_mode != MAX_MACHINE_MODE)
2084 wanted_mode = new_mode;
2086 else if (GET_CODE (x) == SIGN_EXTRACT)
2088 enum machine_mode new_mode
2089 = mode_for_extraction (EP_extv, 1);
2090 if (new_mode != MAX_MACHINE_MODE)
2091 wanted_mode = new_mode;
2094 /* If we have a narrower mode, we can do something. */
2095 if (wanted_mode != VOIDmode
2096 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2098 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2099 rtx old_pos = XEXP (x, 2);
2100 rtx newmem;
2102 /* If the bytes and bits are counted differently, we
2103 must adjust the offset. */
2104 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2105 offset = (GET_MODE_SIZE (is_mode)
2106 - GET_MODE_SIZE (wanted_mode) - offset);
2108 pos %= GET_MODE_BITSIZE (wanted_mode);
2110 newmem = adjust_address_nv (tem, wanted_mode, offset);
2112 /* Make the change and see if the insn remains valid. */
2113 INSN_CODE (insn) = -1;
2114 XEXP (x, 0) = newmem;
2115 XEXP (x, 2) = GEN_INT (pos);
2117 if (recog_memoized (insn) >= 0)
2118 return;
2120 /* Otherwise, restore old position. XEXP (x, 0) will be
2121 restored later. */
2122 XEXP (x, 2) = old_pos;
2126 /* If we get here, the bitfield extract insn can't accept a memory
2127 reference. Copy the input into a register. */
2129 tem1 = gen_reg_rtx (GET_MODE (tem));
2130 emit_insn_before (gen_move_insn (tem1, tem), insn);
2131 XEXP (x, 0) = tem1;
2132 return;
2134 break;
2136 case SUBREG:
2137 if (SUBREG_REG (x) == var)
2139 /* If this is a special SUBREG made because VAR was promoted
2140 from a wider mode, replace it with VAR and call ourself
2141 recursively, this time saying that the object previously
2142 had its current mode (by virtue of the SUBREG). */
2144 if (SUBREG_PROMOTED_VAR_P (x))
2146 *loc = var;
2147 fixup_var_refs_1 (var, GET_MODE (var), loc, insn, replacements,
2148 no_share);
2149 return;
2152 /* If this SUBREG makes VAR wider, it has become a paradoxical
2153 SUBREG with VAR in memory, but these aren't allowed at this
2154 stage of the compilation. So load VAR into a pseudo and take
2155 a SUBREG of that pseudo. */
2156 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (var)))
2158 replacement = find_fixup_replacement (replacements, var);
2159 if (replacement->new == 0)
2160 replacement->new = gen_reg_rtx (promoted_mode);
2161 SUBREG_REG (x) = replacement->new;
2162 return;
2165 /* See if we have already found a replacement for this SUBREG.
2166 If so, use it. Otherwise, make a MEM and see if the insn
2167 is recognized. If not, or if we should force MEM into a register,
2168 make a pseudo for this SUBREG. */
2169 replacement = find_fixup_replacement (replacements, x);
2170 if (replacement->new)
2172 *loc = replacement->new;
2173 return;
2176 replacement->new = *loc = fixup_memory_subreg (x, insn,
2177 promoted_mode, 0);
2179 INSN_CODE (insn) = -1;
2180 if (! flag_force_mem && recog_memoized (insn) >= 0)
2181 return;
2183 *loc = replacement->new = gen_reg_rtx (GET_MODE (x));
2184 return;
2186 break;
2188 case SET:
2189 /* First do special simplification of bit-field references. */
2190 if (GET_CODE (SET_DEST (x)) == SIGN_EXTRACT
2191 || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2192 optimize_bit_field (x, insn, 0);
2193 if (GET_CODE (SET_SRC (x)) == SIGN_EXTRACT
2194 || GET_CODE (SET_SRC (x)) == ZERO_EXTRACT)
2195 optimize_bit_field (x, insn, 0);
2197 /* For a paradoxical SUBREG inside a ZERO_EXTRACT, load the object
2198 into a register and then store it back out. */
2199 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2200 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG
2201 && SUBREG_REG (XEXP (SET_DEST (x), 0)) == var
2202 && (GET_MODE_SIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2203 > GET_MODE_SIZE (GET_MODE (var))))
2205 replacement = find_fixup_replacement (replacements, var);
2206 if (replacement->new == 0)
2207 replacement->new = gen_reg_rtx (GET_MODE (var));
2209 SUBREG_REG (XEXP (SET_DEST (x), 0)) = replacement->new;
2210 emit_insn_after (gen_move_insn (var, replacement->new), insn);
2213 /* If SET_DEST is now a paradoxical SUBREG, put the result of this
2214 insn into a pseudo and store the low part of the pseudo into VAR. */
2215 if (GET_CODE (SET_DEST (x)) == SUBREG
2216 && SUBREG_REG (SET_DEST (x)) == var
2217 && (GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
2218 > GET_MODE_SIZE (GET_MODE (var))))
2220 SET_DEST (x) = tem = gen_reg_rtx (GET_MODE (SET_DEST (x)));
2221 emit_insn_after (gen_move_insn (var, gen_lowpart (GET_MODE (var),
2222 tem)),
2223 insn);
2224 break;
2228 rtx dest = SET_DEST (x);
2229 rtx src = SET_SRC (x);
2230 rtx outerdest = dest;
2232 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2233 || GET_CODE (dest) == SIGN_EXTRACT
2234 || GET_CODE (dest) == ZERO_EXTRACT)
2235 dest = XEXP (dest, 0);
2237 if (GET_CODE (src) == SUBREG)
2238 src = SUBREG_REG (src);
2240 /* If VAR does not appear at the top level of the SET
2241 just scan the lower levels of the tree. */
2243 if (src != var && dest != var)
2244 break;
2246 /* We will need to rerecognize this insn. */
2247 INSN_CODE (insn) = -1;
2249 if (GET_CODE (outerdest) == ZERO_EXTRACT && dest == var
2250 && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
2252 /* Since this case will return, ensure we fixup all the
2253 operands here. */
2254 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 1),
2255 insn, replacements, no_share);
2256 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 2),
2257 insn, replacements, no_share);
2258 fixup_var_refs_1 (var, promoted_mode, &SET_SRC (x),
2259 insn, replacements, no_share);
2261 tem = XEXP (outerdest, 0);
2263 /* Clean up (SUBREG:SI (MEM:mode ...) 0)
2264 that may appear inside a ZERO_EXTRACT.
2265 This was legitimate when the MEM was a REG. */
2266 if (GET_CODE (tem) == SUBREG
2267 && SUBREG_REG (tem) == var)
2268 tem = fixup_memory_subreg (tem, insn, promoted_mode, 0);
2269 else
2270 tem = fixup_stack_1 (tem, insn);
2272 if (GET_CODE (XEXP (outerdest, 1)) == CONST_INT
2273 && GET_CODE (XEXP (outerdest, 2)) == CONST_INT
2274 && ! mode_dependent_address_p (XEXP (tem, 0))
2275 && ! MEM_VOLATILE_P (tem))
2277 enum machine_mode wanted_mode;
2278 enum machine_mode is_mode = GET_MODE (tem);
2279 HOST_WIDE_INT pos = INTVAL (XEXP (outerdest, 2));
2281 wanted_mode = mode_for_extraction (EP_insv, 0);
2283 /* If we have a narrower mode, we can do something. */
2284 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2286 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2287 rtx old_pos = XEXP (outerdest, 2);
2288 rtx newmem;
2290 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2291 offset = (GET_MODE_SIZE (is_mode)
2292 - GET_MODE_SIZE (wanted_mode) - offset);
2294 pos %= GET_MODE_BITSIZE (wanted_mode);
2296 newmem = adjust_address_nv (tem, wanted_mode, offset);
2298 /* Make the change and see if the insn remains valid. */
2299 INSN_CODE (insn) = -1;
2300 XEXP (outerdest, 0) = newmem;
2301 XEXP (outerdest, 2) = GEN_INT (pos);
2303 if (recog_memoized (insn) >= 0)
2304 return;
2306 /* Otherwise, restore old position. XEXP (x, 0) will be
2307 restored later. */
2308 XEXP (outerdest, 2) = old_pos;
2312 /* If we get here, the bit-field store doesn't allow memory
2313 or isn't located at a constant position. Load the value into
2314 a register, do the store, and put it back into memory. */
2316 tem1 = gen_reg_rtx (GET_MODE (tem));
2317 emit_insn_before (gen_move_insn (tem1, tem), insn);
2318 emit_insn_after (gen_move_insn (tem, tem1), insn);
2319 XEXP (outerdest, 0) = tem1;
2320 return;
2323 /* STRICT_LOW_PART is a no-op on memory references
2324 and it can cause combinations to be unrecognizable,
2325 so eliminate it. */
2327 if (dest == var && GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2328 SET_DEST (x) = XEXP (SET_DEST (x), 0);
2330 /* A valid insn to copy VAR into or out of a register
2331 must be left alone, to avoid an infinite loop here.
2332 If the reference to VAR is by a subreg, fix that up,
2333 since SUBREG is not valid for a memref.
2334 Also fix up the address of the stack slot.
2336 Note that we must not try to recognize the insn until
2337 after we know that we have valid addresses and no
2338 (subreg (mem ...) ...) constructs, since these interfere
2339 with determining the validity of the insn. */
2341 if ((SET_SRC (x) == var
2342 || (GET_CODE (SET_SRC (x)) == SUBREG
2343 && SUBREG_REG (SET_SRC (x)) == var))
2344 && (GET_CODE (SET_DEST (x)) == REG
2345 || (GET_CODE (SET_DEST (x)) == SUBREG
2346 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG))
2347 && GET_MODE (var) == promoted_mode
2348 && x == single_set (insn))
2350 rtx pat, last;
2352 if (GET_CODE (SET_SRC (x)) == SUBREG
2353 && (GET_MODE_SIZE (GET_MODE (SET_SRC (x)))
2354 > GET_MODE_SIZE (GET_MODE (var))))
2356 /* This (subreg VAR) is now a paradoxical subreg. We need
2357 to replace VAR instead of the subreg. */
2358 replacement = find_fixup_replacement (replacements, var);
2359 if (replacement->new == NULL_RTX)
2360 replacement->new = gen_reg_rtx (GET_MODE (var));
2361 SUBREG_REG (SET_SRC (x)) = replacement->new;
2363 else
2365 replacement = find_fixup_replacement (replacements, SET_SRC (x));
2366 if (replacement->new)
2367 SET_SRC (x) = replacement->new;
2368 else if (GET_CODE (SET_SRC (x)) == SUBREG)
2369 SET_SRC (x) = replacement->new
2370 = fixup_memory_subreg (SET_SRC (x), insn, promoted_mode,
2372 else
2373 SET_SRC (x) = replacement->new
2374 = fixup_stack_1 (SET_SRC (x), insn);
2377 if (recog_memoized (insn) >= 0)
2378 return;
2380 /* INSN is not valid, but we know that we want to
2381 copy SET_SRC (x) to SET_DEST (x) in some way. So
2382 we generate the move and see whether it requires more
2383 than one insn. If it does, we emit those insns and
2384 delete INSN. Otherwise, we an just replace the pattern
2385 of INSN; we have already verified above that INSN has
2386 no other function that to do X. */
2388 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2389 if (NEXT_INSN (pat) != NULL_RTX)
2391 last = emit_insn_before (pat, insn);
2393 /* INSN might have REG_RETVAL or other important notes, so
2394 we need to store the pattern of the last insn in the
2395 sequence into INSN similarly to the normal case. LAST
2396 should not have REG_NOTES, but we allow them if INSN has
2397 no REG_NOTES. */
2398 if (REG_NOTES (last) && REG_NOTES (insn))
2399 abort ();
2400 if (REG_NOTES (last))
2401 REG_NOTES (insn) = REG_NOTES (last);
2402 PATTERN (insn) = PATTERN (last);
2404 delete_insn (last);
2406 else
2407 PATTERN (insn) = PATTERN (pat);
2409 return;
2412 if ((SET_DEST (x) == var
2413 || (GET_CODE (SET_DEST (x)) == SUBREG
2414 && SUBREG_REG (SET_DEST (x)) == var))
2415 && (GET_CODE (SET_SRC (x)) == REG
2416 || (GET_CODE (SET_SRC (x)) == SUBREG
2417 && GET_CODE (SUBREG_REG (SET_SRC (x))) == REG))
2418 && GET_MODE (var) == promoted_mode
2419 && x == single_set (insn))
2421 rtx pat, last;
2423 if (GET_CODE (SET_DEST (x)) == SUBREG)
2424 SET_DEST (x) = fixup_memory_subreg (SET_DEST (x), insn,
2425 promoted_mode, 0);
2426 else
2427 SET_DEST (x) = fixup_stack_1 (SET_DEST (x), insn);
2429 if (recog_memoized (insn) >= 0)
2430 return;
2432 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2433 if (NEXT_INSN (pat) != NULL_RTX)
2435 last = emit_insn_before (pat, insn);
2437 /* INSN might have REG_RETVAL or other important notes, so
2438 we need to store the pattern of the last insn in the
2439 sequence into INSN similarly to the normal case. LAST
2440 should not have REG_NOTES, but we allow them if INSN has
2441 no REG_NOTES. */
2442 if (REG_NOTES (last) && REG_NOTES (insn))
2443 abort ();
2444 if (REG_NOTES (last))
2445 REG_NOTES (insn) = REG_NOTES (last);
2446 PATTERN (insn) = PATTERN (last);
2448 delete_insn (last);
2450 else
2451 PATTERN (insn) = PATTERN (pat);
2453 return;
2456 /* Otherwise, storing into VAR must be handled specially
2457 by storing into a temporary and copying that into VAR
2458 with a new insn after this one. Note that this case
2459 will be used when storing into a promoted scalar since
2460 the insn will now have different modes on the input
2461 and output and hence will be invalid (except for the case
2462 of setting it to a constant, which does not need any
2463 change if it is valid). We generate extra code in that case,
2464 but combine.c will eliminate it. */
2466 if (dest == var)
2468 rtx temp;
2469 rtx fixeddest = SET_DEST (x);
2470 enum machine_mode temp_mode;
2472 /* STRICT_LOW_PART can be discarded, around a MEM. */
2473 if (GET_CODE (fixeddest) == STRICT_LOW_PART)
2474 fixeddest = XEXP (fixeddest, 0);
2475 /* Convert (SUBREG (MEM)) to a MEM in a changed mode. */
2476 if (GET_CODE (fixeddest) == SUBREG)
2478 fixeddest = fixup_memory_subreg (fixeddest, insn,
2479 promoted_mode, 0);
2480 temp_mode = GET_MODE (fixeddest);
2482 else
2484 fixeddest = fixup_stack_1 (fixeddest, insn);
2485 temp_mode = promoted_mode;
2488 temp = gen_reg_rtx (temp_mode);
2490 emit_insn_after (gen_move_insn (fixeddest,
2491 gen_lowpart (GET_MODE (fixeddest),
2492 temp)),
2493 insn);
2495 SET_DEST (x) = temp;
2499 default:
2500 break;
2503 /* Nothing special about this RTX; fix its operands. */
2505 fmt = GET_RTX_FORMAT (code);
2506 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2508 if (fmt[i] == 'e')
2509 fixup_var_refs_1 (var, promoted_mode, &XEXP (x, i), insn, replacements,
2510 no_share);
2511 else if (fmt[i] == 'E')
2513 int j;
2514 for (j = 0; j < XVECLEN (x, i); j++)
2515 fixup_var_refs_1 (var, promoted_mode, &XVECEXP (x, i, j),
2516 insn, replacements, no_share);
2521 /* Previously, X had the form (SUBREG:m1 (REG:PROMOTED_MODE ...)).
2522 The REG was placed on the stack, so X now has the form (SUBREG:m1
2523 (MEM:m2 ...)).
2525 Return an rtx (MEM:m1 newaddr) which is equivalent. If any insns
2526 must be emitted to compute NEWADDR, put them before INSN.
2528 UNCRITICAL nonzero means accept paradoxical subregs.
2529 This is used for subregs found inside REG_NOTES. */
2531 static rtx
2532 fixup_memory_subreg (x, insn, promoted_mode, uncritical)
2533 rtx x;
2534 rtx insn;
2535 enum machine_mode promoted_mode;
2536 int uncritical;
2538 int offset;
2539 rtx mem = SUBREG_REG (x);
2540 rtx addr = XEXP (mem, 0);
2541 enum machine_mode mode = GET_MODE (x);
2542 rtx result, seq;
2544 /* Paradoxical SUBREGs are usually invalid during RTL generation. */
2545 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (mem)) && ! uncritical)
2546 abort ();
2548 offset = SUBREG_BYTE (x);
2549 if (BYTES_BIG_ENDIAN)
2550 /* If the PROMOTED_MODE is wider than the mode of the MEM, adjust
2551 the offset so that it points to the right location within the
2552 MEM. */
2553 offset -= (GET_MODE_SIZE (promoted_mode) - GET_MODE_SIZE (GET_MODE (mem)));
2555 if (!flag_force_addr
2556 && memory_address_p (mode, plus_constant (addr, offset)))
2557 /* Shortcut if no insns need be emitted. */
2558 return adjust_address (mem, mode, offset);
2560 start_sequence ();
2561 result = adjust_address (mem, mode, offset);
2562 seq = get_insns ();
2563 end_sequence ();
2565 emit_insn_before (seq, insn);
2566 return result;
2569 /* Do fixup_memory_subreg on all (SUBREG (MEM ...) ...) contained in X.
2570 Replace subexpressions of X in place.
2571 If X itself is a (SUBREG (MEM ...) ...), return the replacement expression.
2572 Otherwise return X, with its contents possibly altered.
2574 INSN, PROMOTED_MODE and UNCRITICAL are as for
2575 fixup_memory_subreg. */
2577 static rtx
2578 walk_fixup_memory_subreg (x, insn, promoted_mode, uncritical)
2579 rtx x;
2580 rtx insn;
2581 enum machine_mode promoted_mode;
2582 int uncritical;
2584 enum rtx_code code;
2585 const char *fmt;
2586 int i;
2588 if (x == 0)
2589 return 0;
2591 code = GET_CODE (x);
2593 if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
2594 return fixup_memory_subreg (x, insn, promoted_mode, uncritical);
2596 /* Nothing special about this RTX; fix its operands. */
2598 fmt = GET_RTX_FORMAT (code);
2599 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2601 if (fmt[i] == 'e')
2602 XEXP (x, i) = walk_fixup_memory_subreg (XEXP (x, i), insn,
2603 promoted_mode, uncritical);
2604 else if (fmt[i] == 'E')
2606 int j;
2607 for (j = 0; j < XVECLEN (x, i); j++)
2608 XVECEXP (x, i, j)
2609 = walk_fixup_memory_subreg (XVECEXP (x, i, j), insn,
2610 promoted_mode, uncritical);
2613 return x;
2616 /* For each memory ref within X, if it refers to a stack slot
2617 with an out of range displacement, put the address in a temp register
2618 (emitting new insns before INSN to load these registers)
2619 and alter the memory ref to use that register.
2620 Replace each such MEM rtx with a copy, to avoid clobberage. */
2622 static rtx
2623 fixup_stack_1 (x, insn)
2624 rtx x;
2625 rtx insn;
2627 int i;
2628 RTX_CODE code = GET_CODE (x);
2629 const char *fmt;
2631 if (code == MEM)
2633 rtx ad = XEXP (x, 0);
2634 /* If we have address of a stack slot but it's not valid
2635 (displacement is too large), compute the sum in a register. */
2636 if (GET_CODE (ad) == PLUS
2637 && GET_CODE (XEXP (ad, 0)) == REG
2638 && ((REGNO (XEXP (ad, 0)) >= FIRST_VIRTUAL_REGISTER
2639 && REGNO (XEXP (ad, 0)) <= LAST_VIRTUAL_REGISTER)
2640 || REGNO (XEXP (ad, 0)) == FRAME_POINTER_REGNUM
2641 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2642 || REGNO (XEXP (ad, 0)) == HARD_FRAME_POINTER_REGNUM
2643 #endif
2644 || REGNO (XEXP (ad, 0)) == STACK_POINTER_REGNUM
2645 || REGNO (XEXP (ad, 0)) == ARG_POINTER_REGNUM
2646 || XEXP (ad, 0) == current_function_internal_arg_pointer)
2647 && GET_CODE (XEXP (ad, 1)) == CONST_INT)
2649 rtx temp, seq;
2650 if (memory_address_p (GET_MODE (x), ad))
2651 return x;
2653 start_sequence ();
2654 temp = copy_to_reg (ad);
2655 seq = get_insns ();
2656 end_sequence ();
2657 emit_insn_before (seq, insn);
2658 return replace_equiv_address (x, temp);
2660 return x;
2663 fmt = GET_RTX_FORMAT (code);
2664 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2666 if (fmt[i] == 'e')
2667 XEXP (x, i) = fixup_stack_1 (XEXP (x, i), insn);
2668 else if (fmt[i] == 'E')
2670 int j;
2671 for (j = 0; j < XVECLEN (x, i); j++)
2672 XVECEXP (x, i, j) = fixup_stack_1 (XVECEXP (x, i, j), insn);
2675 return x;
2678 /* Optimization: a bit-field instruction whose field
2679 happens to be a byte or halfword in memory
2680 can be changed to a move instruction.
2682 We call here when INSN is an insn to examine or store into a bit-field.
2683 BODY is the SET-rtx to be altered.
2685 EQUIV_MEM is the table `reg_equiv_mem' if that is available; else 0.
2686 (Currently this is called only from function.c, and EQUIV_MEM
2687 is always 0.) */
2689 static void
2690 optimize_bit_field (body, insn, equiv_mem)
2691 rtx body;
2692 rtx insn;
2693 rtx *equiv_mem;
2695 rtx bitfield;
2696 int destflag;
2697 rtx seq = 0;
2698 enum machine_mode mode;
2700 if (GET_CODE (SET_DEST (body)) == SIGN_EXTRACT
2701 || GET_CODE (SET_DEST (body)) == ZERO_EXTRACT)
2702 bitfield = SET_DEST (body), destflag = 1;
2703 else
2704 bitfield = SET_SRC (body), destflag = 0;
2706 /* First check that the field being stored has constant size and position
2707 and is in fact a byte or halfword suitably aligned. */
2709 if (GET_CODE (XEXP (bitfield, 1)) == CONST_INT
2710 && GET_CODE (XEXP (bitfield, 2)) == CONST_INT
2711 && ((mode = mode_for_size (INTVAL (XEXP (bitfield, 1)), MODE_INT, 1))
2712 != BLKmode)
2713 && INTVAL (XEXP (bitfield, 2)) % INTVAL (XEXP (bitfield, 1)) == 0)
2715 rtx memref = 0;
2717 /* Now check that the containing word is memory, not a register,
2718 and that it is safe to change the machine mode. */
2720 if (GET_CODE (XEXP (bitfield, 0)) == MEM)
2721 memref = XEXP (bitfield, 0);
2722 else if (GET_CODE (XEXP (bitfield, 0)) == REG
2723 && equiv_mem != 0)
2724 memref = equiv_mem[REGNO (XEXP (bitfield, 0))];
2725 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2726 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == MEM)
2727 memref = SUBREG_REG (XEXP (bitfield, 0));
2728 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2729 && equiv_mem != 0
2730 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == REG)
2731 memref = equiv_mem[REGNO (SUBREG_REG (XEXP (bitfield, 0)))];
2733 if (memref
2734 && ! mode_dependent_address_p (XEXP (memref, 0))
2735 && ! MEM_VOLATILE_P (memref))
2737 /* Now adjust the address, first for any subreg'ing
2738 that we are now getting rid of,
2739 and then for which byte of the word is wanted. */
2741 HOST_WIDE_INT offset = INTVAL (XEXP (bitfield, 2));
2742 rtx insns;
2744 /* Adjust OFFSET to count bits from low-address byte. */
2745 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
2746 offset = (GET_MODE_BITSIZE (GET_MODE (XEXP (bitfield, 0)))
2747 - offset - INTVAL (XEXP (bitfield, 1)));
2749 /* Adjust OFFSET to count bytes from low-address byte. */
2750 offset /= BITS_PER_UNIT;
2751 if (GET_CODE (XEXP (bitfield, 0)) == SUBREG)
2753 offset += (SUBREG_BYTE (XEXP (bitfield, 0))
2754 / UNITS_PER_WORD) * UNITS_PER_WORD;
2755 if (BYTES_BIG_ENDIAN)
2756 offset -= (MIN (UNITS_PER_WORD,
2757 GET_MODE_SIZE (GET_MODE (XEXP (bitfield, 0))))
2758 - MIN (UNITS_PER_WORD,
2759 GET_MODE_SIZE (GET_MODE (memref))));
2762 start_sequence ();
2763 memref = adjust_address (memref, mode, offset);
2764 insns = get_insns ();
2765 end_sequence ();
2766 emit_insn_before (insns, insn);
2768 /* Store this memory reference where
2769 we found the bit field reference. */
2771 if (destflag)
2773 validate_change (insn, &SET_DEST (body), memref, 1);
2774 if (! CONSTANT_ADDRESS_P (SET_SRC (body)))
2776 rtx src = SET_SRC (body);
2777 while (GET_CODE (src) == SUBREG
2778 && SUBREG_BYTE (src) == 0)
2779 src = SUBREG_REG (src);
2780 if (GET_MODE (src) != GET_MODE (memref))
2781 src = gen_lowpart (GET_MODE (memref), SET_SRC (body));
2782 validate_change (insn, &SET_SRC (body), src, 1);
2784 else if (GET_MODE (SET_SRC (body)) != VOIDmode
2785 && GET_MODE (SET_SRC (body)) != GET_MODE (memref))
2786 /* This shouldn't happen because anything that didn't have
2787 one of these modes should have got converted explicitly
2788 and then referenced through a subreg.
2789 This is so because the original bit-field was
2790 handled by agg_mode and so its tree structure had
2791 the same mode that memref now has. */
2792 abort ();
2794 else
2796 rtx dest = SET_DEST (body);
2798 while (GET_CODE (dest) == SUBREG
2799 && SUBREG_BYTE (dest) == 0
2800 && (GET_MODE_CLASS (GET_MODE (dest))
2801 == GET_MODE_CLASS (GET_MODE (SUBREG_REG (dest))))
2802 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
2803 <= UNITS_PER_WORD))
2804 dest = SUBREG_REG (dest);
2806 validate_change (insn, &SET_DEST (body), dest, 1);
2808 if (GET_MODE (dest) == GET_MODE (memref))
2809 validate_change (insn, &SET_SRC (body), memref, 1);
2810 else
2812 /* Convert the mem ref to the destination mode. */
2813 rtx newreg = gen_reg_rtx (GET_MODE (dest));
2815 start_sequence ();
2816 convert_move (newreg, memref,
2817 GET_CODE (SET_SRC (body)) == ZERO_EXTRACT);
2818 seq = get_insns ();
2819 end_sequence ();
2821 validate_change (insn, &SET_SRC (body), newreg, 1);
2825 /* See if we can convert this extraction or insertion into
2826 a simple move insn. We might not be able to do so if this
2827 was, for example, part of a PARALLEL.
2829 If we succeed, write out any needed conversions. If we fail,
2830 it is hard to guess why we failed, so don't do anything
2831 special; just let the optimization be suppressed. */
2833 if (apply_change_group () && seq)
2834 emit_insn_before (seq, insn);
2839 /* These routines are responsible for converting virtual register references
2840 to the actual hard register references once RTL generation is complete.
2842 The following four variables are used for communication between the
2843 routines. They contain the offsets of the virtual registers from their
2844 respective hard registers. */
2846 static int in_arg_offset;
2847 static int var_offset;
2848 static int dynamic_offset;
2849 static int out_arg_offset;
2850 static int cfa_offset;
2852 /* In most machines, the stack pointer register is equivalent to the bottom
2853 of the stack. */
2855 #ifndef STACK_POINTER_OFFSET
2856 #define STACK_POINTER_OFFSET 0
2857 #endif
2859 /* If not defined, pick an appropriate default for the offset of dynamically
2860 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
2861 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
2863 #ifndef STACK_DYNAMIC_OFFSET
2865 /* The bottom of the stack points to the actual arguments. If
2866 REG_PARM_STACK_SPACE is defined, this includes the space for the register
2867 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
2868 stack space for register parameters is not pushed by the caller, but
2869 rather part of the fixed stack areas and hence not included in
2870 `current_function_outgoing_args_size'. Nevertheless, we must allow
2871 for it when allocating stack dynamic objects. */
2873 #if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
2874 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2875 ((ACCUMULATE_OUTGOING_ARGS \
2876 ? (current_function_outgoing_args_size + REG_PARM_STACK_SPACE (FNDECL)) : 0)\
2877 + (STACK_POINTER_OFFSET)) \
2879 #else
2880 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2881 ((ACCUMULATE_OUTGOING_ARGS ? current_function_outgoing_args_size : 0) \
2882 + (STACK_POINTER_OFFSET))
2883 #endif
2884 #endif
2886 /* On most machines, the CFA coincides with the first incoming parm. */
2888 #ifndef ARG_POINTER_CFA_OFFSET
2889 #define ARG_POINTER_CFA_OFFSET(FNDECL) FIRST_PARM_OFFSET (FNDECL)
2890 #endif
2892 /* Build up a (MEM (ADDRESSOF (REG))) rtx for a register REG that just had its
2893 address taken. DECL is the decl or SAVE_EXPR for the object stored in the
2894 register, for later use if we do need to force REG into the stack. REG is
2895 overwritten by the MEM like in put_reg_into_stack. */
2898 gen_mem_addressof (reg, decl)
2899 rtx reg;
2900 tree decl;
2902 rtx r = gen_rtx_ADDRESSOF (Pmode, gen_reg_rtx (GET_MODE (reg)),
2903 REGNO (reg), decl);
2905 /* Calculate this before we start messing with decl's RTL. */
2906 HOST_WIDE_INT set = decl ? get_alias_set (decl) : 0;
2908 /* If the original REG was a user-variable, then so is the REG whose
2909 address is being taken. Likewise for unchanging. */
2910 REG_USERVAR_P (XEXP (r, 0)) = REG_USERVAR_P (reg);
2911 RTX_UNCHANGING_P (XEXP (r, 0)) = RTX_UNCHANGING_P (reg);
2913 PUT_CODE (reg, MEM);
2914 MEM_ATTRS (reg) = 0;
2915 XEXP (reg, 0) = r;
2917 if (decl)
2919 tree type = TREE_TYPE (decl);
2920 enum machine_mode decl_mode
2921 = (DECL_P (decl) ? DECL_MODE (decl) : TYPE_MODE (TREE_TYPE (decl)));
2922 rtx decl_rtl = (TREE_CODE (decl) == SAVE_EXPR ? SAVE_EXPR_RTL (decl)
2923 : DECL_RTL_IF_SET (decl));
2925 PUT_MODE (reg, decl_mode);
2927 /* Clear DECL_RTL momentarily so functions below will work
2928 properly, then set it again. */
2929 if (DECL_P (decl) && decl_rtl == reg)
2930 SET_DECL_RTL (decl, 0);
2932 set_mem_attributes (reg, decl, 1);
2933 set_mem_alias_set (reg, set);
2935 if (DECL_P (decl) && decl_rtl == reg)
2936 SET_DECL_RTL (decl, reg);
2938 if (TREE_USED (decl) || (DECL_P (decl) && DECL_INITIAL (decl) != 0))
2939 fixup_var_refs (reg, GET_MODE (reg), TREE_UNSIGNED (type), reg, 0);
2941 else
2942 fixup_var_refs (reg, GET_MODE (reg), 0, reg, 0);
2944 return reg;
2947 /* If DECL has an RTL that is an ADDRESSOF rtx, put it into the stack. */
2949 void
2950 flush_addressof (decl)
2951 tree decl;
2953 if ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL)
2954 && DECL_RTL (decl) != 0
2955 && GET_CODE (DECL_RTL (decl)) == MEM
2956 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF
2957 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == REG)
2958 put_addressof_into_stack (XEXP (DECL_RTL (decl), 0), 0);
2961 /* Force the register pointed to by R, an ADDRESSOF rtx, into the stack. */
2963 static void
2964 put_addressof_into_stack (r, ht)
2965 rtx r;
2966 htab_t ht;
2968 tree decl, type;
2969 int volatile_p, used_p;
2971 rtx reg = XEXP (r, 0);
2973 if (GET_CODE (reg) != REG)
2974 abort ();
2976 decl = ADDRESSOF_DECL (r);
2977 if (decl)
2979 type = TREE_TYPE (decl);
2980 volatile_p = (TREE_CODE (decl) != SAVE_EXPR
2981 && TREE_THIS_VOLATILE (decl));
2982 used_p = (TREE_USED (decl)
2983 || (DECL_P (decl) && DECL_INITIAL (decl) != 0));
2985 else
2987 type = NULL_TREE;
2988 volatile_p = 0;
2989 used_p = 1;
2992 put_reg_into_stack (0, reg, type, GET_MODE (reg), GET_MODE (reg),
2993 volatile_p, ADDRESSOF_REGNO (r), used_p, ht);
2996 /* List of replacements made below in purge_addressof_1 when creating
2997 bitfield insertions. */
2998 static rtx purge_bitfield_addressof_replacements;
3000 /* List of replacements made below in purge_addressof_1 for patterns
3001 (MEM (ADDRESSOF (REG ...))). The key of the list entry is the
3002 corresponding (ADDRESSOF (REG ...)) and value is a substitution for
3003 the all pattern. List PURGE_BITFIELD_ADDRESSOF_REPLACEMENTS is not
3004 enough in complex cases, e.g. when some field values can be
3005 extracted by usage MEM with narrower mode. */
3006 static rtx purge_addressof_replacements;
3008 /* Helper function for purge_addressof. See if the rtx expression at *LOC
3009 in INSN needs to be changed. If FORCE, always put any ADDRESSOFs into
3010 the stack. If the function returns FALSE then the replacement could not
3011 be made. */
3013 static bool
3014 purge_addressof_1 (loc, insn, force, store, ht)
3015 rtx *loc;
3016 rtx insn;
3017 int force, store;
3018 htab_t ht;
3020 rtx x;
3021 RTX_CODE code;
3022 int i, j;
3023 const char *fmt;
3024 bool result = true;
3026 /* Re-start here to avoid recursion in common cases. */
3027 restart:
3029 x = *loc;
3030 if (x == 0)
3031 return true;
3033 code = GET_CODE (x);
3035 /* If we don't return in any of the cases below, we will recurse inside
3036 the RTX, which will normally result in any ADDRESSOF being forced into
3037 memory. */
3038 if (code == SET)
3040 result = purge_addressof_1 (&SET_DEST (x), insn, force, 1, ht);
3041 result &= purge_addressof_1 (&SET_SRC (x), insn, force, 0, ht);
3042 return result;
3044 else if (code == ADDRESSOF)
3046 rtx sub, insns;
3048 if (GET_CODE (XEXP (x, 0)) != MEM)
3050 put_addressof_into_stack (x, ht);
3051 return true;
3054 /* We must create a copy of the rtx because it was created by
3055 overwriting a REG rtx which is always shared. */
3056 sub = copy_rtx (XEXP (XEXP (x, 0), 0));
3057 if (validate_change (insn, loc, sub, 0)
3058 || validate_replace_rtx (x, sub, insn))
3059 return true;
3061 start_sequence ();
3062 sub = force_operand (sub, NULL_RTX);
3063 if (! validate_change (insn, loc, sub, 0)
3064 && ! validate_replace_rtx (x, sub, insn))
3065 abort ();
3067 insns = get_insns ();
3068 end_sequence ();
3069 emit_insn_before (insns, insn);
3070 return true;
3073 else if (code == MEM && GET_CODE (XEXP (x, 0)) == ADDRESSOF && ! force)
3075 rtx sub = XEXP (XEXP (x, 0), 0);
3077 if (GET_CODE (sub) == MEM)
3078 sub = adjust_address_nv (sub, GET_MODE (x), 0);
3079 else if (GET_CODE (sub) == REG
3080 && (MEM_VOLATILE_P (x) || GET_MODE (x) == BLKmode))
3082 else if (GET_CODE (sub) == REG && GET_MODE (x) != GET_MODE (sub))
3084 int size_x, size_sub;
3086 if (!insn)
3088 /* When processing REG_NOTES look at the list of
3089 replacements done on the insn to find the register that X
3090 was replaced by. */
3091 rtx tem;
3093 for (tem = purge_bitfield_addressof_replacements;
3094 tem != NULL_RTX;
3095 tem = XEXP (XEXP (tem, 1), 1))
3096 if (rtx_equal_p (x, XEXP (tem, 0)))
3098 *loc = XEXP (XEXP (tem, 1), 0);
3099 return true;
3102 /* See comment for purge_addressof_replacements. */
3103 for (tem = purge_addressof_replacements;
3104 tem != NULL_RTX;
3105 tem = XEXP (XEXP (tem, 1), 1))
3106 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3108 rtx z = XEXP (XEXP (tem, 1), 0);
3110 if (GET_MODE (x) == GET_MODE (z)
3111 || (GET_CODE (XEXP (XEXP (tem, 1), 0)) != REG
3112 && GET_CODE (XEXP (XEXP (tem, 1), 0)) != SUBREG))
3113 abort ();
3115 /* It can happen that the note may speak of things
3116 in a wider (or just different) mode than the
3117 code did. This is especially true of
3118 REG_RETVAL. */
3120 if (GET_CODE (z) == SUBREG && SUBREG_BYTE (z) == 0)
3121 z = SUBREG_REG (z);
3123 if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3124 && (GET_MODE_SIZE (GET_MODE (x))
3125 > GET_MODE_SIZE (GET_MODE (z))))
3127 /* This can occur as a result in invalid
3128 pointer casts, e.g. float f; ...
3129 *(long long int *)&f.
3130 ??? We could emit a warning here, but
3131 without a line number that wouldn't be
3132 very helpful. */
3133 z = gen_rtx_SUBREG (GET_MODE (x), z, 0);
3135 else
3136 z = gen_lowpart (GET_MODE (x), z);
3138 *loc = z;
3139 return true;
3142 /* Sometimes we may not be able to find the replacement. For
3143 example when the original insn was a MEM in a wider mode,
3144 and the note is part of a sign extension of a narrowed
3145 version of that MEM. Gcc testcase compile/990829-1.c can
3146 generate an example of this situation. Rather than complain
3147 we return false, which will prompt our caller to remove the
3148 offending note. */
3149 return false;
3152 size_x = GET_MODE_BITSIZE (GET_MODE (x));
3153 size_sub = GET_MODE_BITSIZE (GET_MODE (sub));
3155 /* Don't even consider working with paradoxical subregs,
3156 or the moral equivalent seen here. */
3157 if (size_x <= size_sub
3158 && int_mode_for_mode (GET_MODE (sub)) != BLKmode)
3160 /* Do a bitfield insertion to mirror what would happen
3161 in memory. */
3163 rtx val, seq;
3165 if (store)
3167 rtx p = PREV_INSN (insn);
3169 start_sequence ();
3170 val = gen_reg_rtx (GET_MODE (x));
3171 if (! validate_change (insn, loc, val, 0))
3173 /* Discard the current sequence and put the
3174 ADDRESSOF on stack. */
3175 end_sequence ();
3176 goto give_up;
3178 seq = get_insns ();
3179 end_sequence ();
3180 emit_insn_before (seq, insn);
3181 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3182 insn, ht);
3184 start_sequence ();
3185 store_bit_field (sub, size_x, 0, GET_MODE (x),
3186 val, GET_MODE_SIZE (GET_MODE (sub)));
3188 /* Make sure to unshare any shared rtl that store_bit_field
3189 might have created. */
3190 unshare_all_rtl_again (get_insns ());
3192 seq = get_insns ();
3193 end_sequence ();
3194 p = emit_insn_after (seq, insn);
3195 if (NEXT_INSN (insn))
3196 compute_insns_for_mem (NEXT_INSN (insn),
3197 p ? NEXT_INSN (p) : NULL_RTX,
3198 ht);
3200 else
3202 rtx p = PREV_INSN (insn);
3204 start_sequence ();
3205 val = extract_bit_field (sub, size_x, 0, 1, NULL_RTX,
3206 GET_MODE (x), GET_MODE (x),
3207 GET_MODE_SIZE (GET_MODE (sub)));
3209 if (! validate_change (insn, loc, val, 0))
3211 /* Discard the current sequence and put the
3212 ADDRESSOF on stack. */
3213 end_sequence ();
3214 goto give_up;
3217 seq = get_insns ();
3218 end_sequence ();
3219 emit_insn_before (seq, insn);
3220 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3221 insn, ht);
3224 /* Remember the replacement so that the same one can be done
3225 on the REG_NOTES. */
3226 purge_bitfield_addressof_replacements
3227 = gen_rtx_EXPR_LIST (VOIDmode, x,
3228 gen_rtx_EXPR_LIST
3229 (VOIDmode, val,
3230 purge_bitfield_addressof_replacements));
3232 /* We replaced with a reg -- all done. */
3233 return true;
3237 else if (validate_change (insn, loc, sub, 0))
3239 /* Remember the replacement so that the same one can be done
3240 on the REG_NOTES. */
3241 if (GET_CODE (sub) == REG || GET_CODE (sub) == SUBREG)
3243 rtx tem;
3245 for (tem = purge_addressof_replacements;
3246 tem != NULL_RTX;
3247 tem = XEXP (XEXP (tem, 1), 1))
3248 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3250 XEXP (XEXP (tem, 1), 0) = sub;
3251 return true;
3253 purge_addressof_replacements
3254 = gen_rtx (EXPR_LIST, VOIDmode, XEXP (x, 0),
3255 gen_rtx_EXPR_LIST (VOIDmode, sub,
3256 purge_addressof_replacements));
3257 return true;
3259 goto restart;
3263 give_up:
3264 /* Scan all subexpressions. */
3265 fmt = GET_RTX_FORMAT (code);
3266 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3268 if (*fmt == 'e')
3269 result &= purge_addressof_1 (&XEXP (x, i), insn, force, 0, ht);
3270 else if (*fmt == 'E')
3271 for (j = 0; j < XVECLEN (x, i); j++)
3272 result &= purge_addressof_1 (&XVECEXP (x, i, j), insn, force, 0, ht);
3275 return result;
3278 /* Return a hash value for K, a REG. */
3280 static hashval_t
3281 insns_for_mem_hash (k)
3282 const void * k;
3284 /* Use the address of the key for the hash value. */
3285 struct insns_for_mem_entry *m = (struct insns_for_mem_entry *) k;
3286 return (hashval_t) m->key;
3289 /* Return non-zero if K1 and K2 (two REGs) are the same. */
3291 static int
3292 insns_for_mem_comp (k1, k2)
3293 const void * k1;
3294 const void * k2;
3296 struct insns_for_mem_entry *m1 = (struct insns_for_mem_entry *) k1;
3297 struct insns_for_mem_entry *m2 = (struct insns_for_mem_entry *) k2;
3298 return m1->key == m2->key;
3301 struct insns_for_mem_walk_info
3303 /* The hash table that we are using to record which INSNs use which
3304 MEMs. */
3305 htab_t ht;
3307 /* The INSN we are currently processing. */
3308 rtx insn;
3310 /* Zero if we are walking to find ADDRESSOFs, one if we are walking
3311 to find the insns that use the REGs in the ADDRESSOFs. */
3312 int pass;
3315 /* Called from compute_insns_for_mem via for_each_rtx. If R is a REG
3316 that might be used in an ADDRESSOF expression, record this INSN in
3317 the hash table given by DATA (which is really a pointer to an
3318 insns_for_mem_walk_info structure). */
3320 static int
3321 insns_for_mem_walk (r, data)
3322 rtx *r;
3323 void *data;
3325 struct insns_for_mem_walk_info *ifmwi
3326 = (struct insns_for_mem_walk_info *) data;
3327 struct insns_for_mem_entry tmp;
3328 tmp.insns = NULL_RTX;
3330 if (ifmwi->pass == 0 && *r && GET_CODE (*r) == ADDRESSOF
3331 && GET_CODE (XEXP (*r, 0)) == REG)
3333 PTR *e;
3334 tmp.key = XEXP (*r, 0);
3335 e = htab_find_slot (ifmwi->ht, &tmp, INSERT);
3336 if (*e == NULL)
3338 *e = ggc_alloc (sizeof (tmp));
3339 memcpy (*e, &tmp, sizeof (tmp));
3342 else if (ifmwi->pass == 1 && *r && GET_CODE (*r) == REG)
3344 struct insns_for_mem_entry *ifme;
3345 tmp.key = *r;
3346 ifme = (struct insns_for_mem_entry *) htab_find (ifmwi->ht, &tmp);
3348 /* If we have not already recorded this INSN, do so now. Since
3349 we process the INSNs in order, we know that if we have
3350 recorded it it must be at the front of the list. */
3351 if (ifme && (!ifme->insns || XEXP (ifme->insns, 0) != ifmwi->insn))
3352 ifme->insns = gen_rtx_EXPR_LIST (VOIDmode, ifmwi->insn,
3353 ifme->insns);
3356 return 0;
3359 /* Walk the INSNS, until we reach LAST_INSN, recording which INSNs use
3360 which REGs in HT. */
3362 static void
3363 compute_insns_for_mem (insns, last_insn, ht)
3364 rtx insns;
3365 rtx last_insn;
3366 htab_t ht;
3368 rtx insn;
3369 struct insns_for_mem_walk_info ifmwi;
3370 ifmwi.ht = ht;
3372 for (ifmwi.pass = 0; ifmwi.pass < 2; ++ifmwi.pass)
3373 for (insn = insns; insn != last_insn; insn = NEXT_INSN (insn))
3374 if (INSN_P (insn))
3376 ifmwi.insn = insn;
3377 for_each_rtx (&insn, insns_for_mem_walk, &ifmwi);
3381 /* Helper function for purge_addressof called through for_each_rtx.
3382 Returns true iff the rtl is an ADDRESSOF. */
3384 static int
3385 is_addressof (rtl, data)
3386 rtx *rtl;
3387 void *data ATTRIBUTE_UNUSED;
3389 return GET_CODE (*rtl) == ADDRESSOF;
3392 /* Eliminate all occurrences of ADDRESSOF from INSNS. Elide any remaining
3393 (MEM (ADDRESSOF)) patterns, and force any needed registers into the
3394 stack. */
3396 void
3397 purge_addressof (insns)
3398 rtx insns;
3400 rtx insn;
3401 htab_t ht;
3403 /* When we actually purge ADDRESSOFs, we turn REGs into MEMs. That
3404 requires a fixup pass over the instruction stream to correct
3405 INSNs that depended on the REG being a REG, and not a MEM. But,
3406 these fixup passes are slow. Furthermore, most MEMs are not
3407 mentioned in very many instructions. So, we speed up the process
3408 by pre-calculating which REGs occur in which INSNs; that allows
3409 us to perform the fixup passes much more quickly. */
3410 ht = htab_create_ggc (1000, insns_for_mem_hash, insns_for_mem_comp, NULL);
3411 compute_insns_for_mem (insns, NULL_RTX, ht);
3413 for (insn = insns; insn; insn = NEXT_INSN (insn))
3414 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3415 || GET_CODE (insn) == CALL_INSN)
3417 if (! purge_addressof_1 (&PATTERN (insn), insn,
3418 asm_noperands (PATTERN (insn)) > 0, 0, ht))
3419 /* If we could not replace the ADDRESSOFs in the insn,
3420 something is wrong. */
3421 abort ();
3423 if (! purge_addressof_1 (&REG_NOTES (insn), NULL_RTX, 0, 0, ht))
3425 /* If we could not replace the ADDRESSOFs in the insn's notes,
3426 we can just remove the offending notes instead. */
3427 rtx note;
3429 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3431 /* If we find a REG_RETVAL note then the insn is a libcall.
3432 Such insns must have REG_EQUAL notes as well, in order
3433 for later passes of the compiler to work. So it is not
3434 safe to delete the notes here, and instead we abort. */
3435 if (REG_NOTE_KIND (note) == REG_RETVAL)
3436 abort ();
3437 if (for_each_rtx (&note, is_addressof, NULL))
3438 remove_note (insn, note);
3443 /* Clean up. */
3444 purge_bitfield_addressof_replacements = 0;
3445 purge_addressof_replacements = 0;
3447 /* REGs are shared. purge_addressof will destructively replace a REG
3448 with a MEM, which creates shared MEMs.
3450 Unfortunately, the children of put_reg_into_stack assume that MEMs
3451 referring to the same stack slot are shared (fixup_var_refs and
3452 the associated hash table code).
3454 So, we have to do another unsharing pass after we have flushed any
3455 REGs that had their address taken into the stack.
3457 It may be worth tracking whether or not we converted any REGs into
3458 MEMs to avoid this overhead when it is not needed. */
3459 unshare_all_rtl_again (get_insns ());
3462 /* Convert a SET of a hard subreg to a set of the appropriate hard
3463 register. A subroutine of purge_hard_subreg_sets. */
3465 static void
3466 purge_single_hard_subreg_set (pattern)
3467 rtx pattern;
3469 rtx reg = SET_DEST (pattern);
3470 enum machine_mode mode = GET_MODE (SET_DEST (pattern));
3471 int offset = 0;
3473 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG
3474 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
3476 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
3477 GET_MODE (SUBREG_REG (reg)),
3478 SUBREG_BYTE (reg),
3479 GET_MODE (reg));
3480 reg = SUBREG_REG (reg);
3484 if (GET_CODE (reg) == REG && REGNO (reg) < FIRST_PSEUDO_REGISTER)
3486 reg = gen_rtx_REG (mode, REGNO (reg) + offset);
3487 SET_DEST (pattern) = reg;
3491 /* Eliminate all occurrences of SETs of hard subregs from INSNS. The
3492 only such SETs that we expect to see are those left in because
3493 integrate can't handle sets of parts of a return value register.
3495 We don't use alter_subreg because we only want to eliminate subregs
3496 of hard registers. */
3498 void
3499 purge_hard_subreg_sets (insn)
3500 rtx insn;
3502 for (; insn; insn = NEXT_INSN (insn))
3504 if (INSN_P (insn))
3506 rtx pattern = PATTERN (insn);
3507 switch (GET_CODE (pattern))
3509 case SET:
3510 if (GET_CODE (SET_DEST (pattern)) == SUBREG)
3511 purge_single_hard_subreg_set (pattern);
3512 break;
3513 case PARALLEL:
3515 int j;
3516 for (j = XVECLEN (pattern, 0) - 1; j >= 0; j--)
3518 rtx inner_pattern = XVECEXP (pattern, 0, j);
3519 if (GET_CODE (inner_pattern) == SET
3520 && GET_CODE (SET_DEST (inner_pattern)) == SUBREG)
3521 purge_single_hard_subreg_set (inner_pattern);
3524 break;
3525 default:
3526 break;
3532 /* Pass through the INSNS of function FNDECL and convert virtual register
3533 references to hard register references. */
3535 void
3536 instantiate_virtual_regs (fndecl, insns)
3537 tree fndecl;
3538 rtx insns;
3540 rtx insn;
3541 unsigned int i;
3543 /* Compute the offsets to use for this function. */
3544 in_arg_offset = FIRST_PARM_OFFSET (fndecl);
3545 var_offset = STARTING_FRAME_OFFSET;
3546 dynamic_offset = STACK_DYNAMIC_OFFSET (fndecl);
3547 out_arg_offset = STACK_POINTER_OFFSET;
3548 cfa_offset = ARG_POINTER_CFA_OFFSET (fndecl);
3550 /* Scan all variables and parameters of this function. For each that is
3551 in memory, instantiate all virtual registers if the result is a valid
3552 address. If not, we do it later. That will handle most uses of virtual
3553 regs on many machines. */
3554 instantiate_decls (fndecl, 1);
3556 /* Initialize recognition, indicating that volatile is OK. */
3557 init_recog ();
3559 /* Scan through all the insns, instantiating every virtual register still
3560 present. */
3561 for (insn = insns; insn; insn = NEXT_INSN (insn))
3562 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3563 || GET_CODE (insn) == CALL_INSN)
3565 instantiate_virtual_regs_1 (&PATTERN (insn), insn, 1);
3566 instantiate_virtual_regs_1 (&REG_NOTES (insn), NULL_RTX, 0);
3567 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
3568 if (GET_CODE (insn) == CALL_INSN)
3569 instantiate_virtual_regs_1 (&CALL_INSN_FUNCTION_USAGE (insn),
3570 NULL_RTX, 0);
3573 /* Instantiate the stack slots for the parm registers, for later use in
3574 addressof elimination. */
3575 for (i = 0; i < max_parm_reg; ++i)
3576 if (parm_reg_stack_loc[i])
3577 instantiate_virtual_regs_1 (&parm_reg_stack_loc[i], NULL_RTX, 0);
3579 /* Now instantiate the remaining register equivalences for debugging info.
3580 These will not be valid addresses. */
3581 instantiate_decls (fndecl, 0);
3583 /* Indicate that, from now on, assign_stack_local should use
3584 frame_pointer_rtx. */
3585 virtuals_instantiated = 1;
3588 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
3589 all virtual registers in their DECL_RTL's.
3591 If VALID_ONLY, do this only if the resulting address is still valid.
3592 Otherwise, always do it. */
3594 static void
3595 instantiate_decls (fndecl, valid_only)
3596 tree fndecl;
3597 int valid_only;
3599 tree decl;
3601 /* Process all parameters of the function. */
3602 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
3604 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
3605 HOST_WIDE_INT size_rtl;
3607 instantiate_decl (DECL_RTL (decl), size, valid_only);
3609 /* If the parameter was promoted, then the incoming RTL mode may be
3610 larger than the declared type size. We must use the larger of
3611 the two sizes. */
3612 size_rtl = GET_MODE_SIZE (GET_MODE (DECL_INCOMING_RTL (decl)));
3613 size = MAX (size_rtl, size);
3614 instantiate_decl (DECL_INCOMING_RTL (decl), size, valid_only);
3617 /* Now process all variables defined in the function or its subblocks. */
3618 instantiate_decls_1 (DECL_INITIAL (fndecl), valid_only);
3621 /* Subroutine of instantiate_decls: Process all decls in the given
3622 BLOCK node and all its subblocks. */
3624 static void
3625 instantiate_decls_1 (let, valid_only)
3626 tree let;
3627 int valid_only;
3629 tree t;
3631 for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
3632 if (DECL_RTL_SET_P (t))
3633 instantiate_decl (DECL_RTL (t),
3634 int_size_in_bytes (TREE_TYPE (t)),
3635 valid_only);
3637 /* Process all subblocks. */
3638 for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
3639 instantiate_decls_1 (t, valid_only);
3642 /* Subroutine of the preceding procedures: Given RTL representing a
3643 decl and the size of the object, do any instantiation required.
3645 If VALID_ONLY is non-zero, it means that the RTL should only be
3646 changed if the new address is valid. */
3648 static void
3649 instantiate_decl (x, size, valid_only)
3650 rtx x;
3651 HOST_WIDE_INT size;
3652 int valid_only;
3654 enum machine_mode mode;
3655 rtx addr;
3657 /* If this is not a MEM, no need to do anything. Similarly if the
3658 address is a constant or a register that is not a virtual register. */
3660 if (x == 0 || GET_CODE (x) != MEM)
3661 return;
3663 addr = XEXP (x, 0);
3664 if (CONSTANT_P (addr)
3665 || (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == REG)
3666 || (GET_CODE (addr) == REG
3667 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
3668 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
3669 return;
3671 /* If we should only do this if the address is valid, copy the address.
3672 We need to do this so we can undo any changes that might make the
3673 address invalid. This copy is unfortunate, but probably can't be
3674 avoided. */
3676 if (valid_only)
3677 addr = copy_rtx (addr);
3679 instantiate_virtual_regs_1 (&addr, NULL_RTX, 0);
3681 if (valid_only && size >= 0)
3683 unsigned HOST_WIDE_INT decl_size = size;
3685 /* Now verify that the resulting address is valid for every integer or
3686 floating-point mode up to and including SIZE bytes long. We do this
3687 since the object might be accessed in any mode and frame addresses
3688 are shared. */
3690 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3691 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3692 mode = GET_MODE_WIDER_MODE (mode))
3693 if (! memory_address_p (mode, addr))
3694 return;
3696 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
3697 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3698 mode = GET_MODE_WIDER_MODE (mode))
3699 if (! memory_address_p (mode, addr))
3700 return;
3703 /* Put back the address now that we have updated it and we either know
3704 it is valid or we don't care whether it is valid. */
3706 XEXP (x, 0) = addr;
3709 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
3710 is a virtual register, return the equivalent hard register and set the
3711 offset indirectly through the pointer. Otherwise, return 0. */
3713 static rtx
3714 instantiate_new_reg (x, poffset)
3715 rtx x;
3716 HOST_WIDE_INT *poffset;
3718 rtx new;
3719 HOST_WIDE_INT offset;
3721 if (x == virtual_incoming_args_rtx)
3722 new = arg_pointer_rtx, offset = in_arg_offset;
3723 else if (x == virtual_stack_vars_rtx)
3724 new = frame_pointer_rtx, offset = var_offset;
3725 else if (x == virtual_stack_dynamic_rtx)
3726 new = stack_pointer_rtx, offset = dynamic_offset;
3727 else if (x == virtual_outgoing_args_rtx)
3728 new = stack_pointer_rtx, offset = out_arg_offset;
3729 else if (x == virtual_cfa_rtx)
3730 new = arg_pointer_rtx, offset = cfa_offset;
3731 else
3732 return 0;
3734 *poffset = offset;
3735 return new;
3738 /* Given a pointer to a piece of rtx and an optional pointer to the
3739 containing object, instantiate any virtual registers present in it.
3741 If EXTRA_INSNS, we always do the replacement and generate
3742 any extra insns before OBJECT. If it zero, we do nothing if replacement
3743 is not valid.
3745 Return 1 if we either had nothing to do or if we were able to do the
3746 needed replacement. Return 0 otherwise; we only return zero if
3747 EXTRA_INSNS is zero.
3749 We first try some simple transformations to avoid the creation of extra
3750 pseudos. */
3752 static int
3753 instantiate_virtual_regs_1 (loc, object, extra_insns)
3754 rtx *loc;
3755 rtx object;
3756 int extra_insns;
3758 rtx x;
3759 RTX_CODE code;
3760 rtx new = 0;
3761 HOST_WIDE_INT offset = 0;
3762 rtx temp;
3763 rtx seq;
3764 int i, j;
3765 const char *fmt;
3767 /* Re-start here to avoid recursion in common cases. */
3768 restart:
3770 x = *loc;
3771 if (x == 0)
3772 return 1;
3774 code = GET_CODE (x);
3776 /* Check for some special cases. */
3777 switch (code)
3779 case CONST_INT:
3780 case CONST_DOUBLE:
3781 case CONST_VECTOR:
3782 case CONST:
3783 case SYMBOL_REF:
3784 case CODE_LABEL:
3785 case PC:
3786 case CC0:
3787 case ASM_INPUT:
3788 case ADDR_VEC:
3789 case ADDR_DIFF_VEC:
3790 case RETURN:
3791 return 1;
3793 case SET:
3794 /* We are allowed to set the virtual registers. This means that
3795 the actual register should receive the source minus the
3796 appropriate offset. This is used, for example, in the handling
3797 of non-local gotos. */
3798 if ((new = instantiate_new_reg (SET_DEST (x), &offset)) != 0)
3800 rtx src = SET_SRC (x);
3802 /* We are setting the register, not using it, so the relevant
3803 offset is the negative of the offset to use were we using
3804 the register. */
3805 offset = - offset;
3806 instantiate_virtual_regs_1 (&src, NULL_RTX, 0);
3808 /* The only valid sources here are PLUS or REG. Just do
3809 the simplest possible thing to handle them. */
3810 if (GET_CODE (src) != REG && GET_CODE (src) != PLUS)
3811 abort ();
3813 start_sequence ();
3814 if (GET_CODE (src) != REG)
3815 temp = force_operand (src, NULL_RTX);
3816 else
3817 temp = src;
3818 temp = force_operand (plus_constant (temp, offset), NULL_RTX);
3819 seq = get_insns ();
3820 end_sequence ();
3822 emit_insn_before (seq, object);
3823 SET_DEST (x) = new;
3825 if (! validate_change (object, &SET_SRC (x), temp, 0)
3826 || ! extra_insns)
3827 abort ();
3829 return 1;
3832 instantiate_virtual_regs_1 (&SET_DEST (x), object, extra_insns);
3833 loc = &SET_SRC (x);
3834 goto restart;
3836 case PLUS:
3837 /* Handle special case of virtual register plus constant. */
3838 if (CONSTANT_P (XEXP (x, 1)))
3840 rtx old, new_offset;
3842 /* Check for (plus (plus VIRT foo) (const_int)) first. */
3843 if (GET_CODE (XEXP (x, 0)) == PLUS)
3845 if ((new = instantiate_new_reg (XEXP (XEXP (x, 0), 0), &offset)))
3847 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 1), object,
3848 extra_insns);
3849 new = gen_rtx_PLUS (Pmode, new, XEXP (XEXP (x, 0), 1));
3851 else
3853 loc = &XEXP (x, 0);
3854 goto restart;
3858 #ifdef POINTERS_EXTEND_UNSIGNED
3859 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
3860 we can commute the PLUS and SUBREG because pointers into the
3861 frame are well-behaved. */
3862 else if (GET_CODE (XEXP (x, 0)) == SUBREG && GET_MODE (x) == ptr_mode
3863 && GET_CODE (XEXP (x, 1)) == CONST_INT
3864 && 0 != (new
3865 = instantiate_new_reg (SUBREG_REG (XEXP (x, 0)),
3866 &offset))
3867 && validate_change (object, loc,
3868 plus_constant (gen_lowpart (ptr_mode,
3869 new),
3870 offset
3871 + INTVAL (XEXP (x, 1))),
3873 return 1;
3874 #endif
3875 else if ((new = instantiate_new_reg (XEXP (x, 0), &offset)) == 0)
3877 /* We know the second operand is a constant. Unless the
3878 first operand is a REG (which has been already checked),
3879 it needs to be checked. */
3880 if (GET_CODE (XEXP (x, 0)) != REG)
3882 loc = &XEXP (x, 0);
3883 goto restart;
3885 return 1;
3888 new_offset = plus_constant (XEXP (x, 1), offset);
3890 /* If the new constant is zero, try to replace the sum with just
3891 the register. */
3892 if (new_offset == const0_rtx
3893 && validate_change (object, loc, new, 0))
3894 return 1;
3896 /* Next try to replace the register and new offset.
3897 There are two changes to validate here and we can't assume that
3898 in the case of old offset equals new just changing the register
3899 will yield a valid insn. In the interests of a little efficiency,
3900 however, we only call validate change once (we don't queue up the
3901 changes and then call apply_change_group). */
3903 old = XEXP (x, 0);
3904 if (offset == 0
3905 ? ! validate_change (object, &XEXP (x, 0), new, 0)
3906 : (XEXP (x, 0) = new,
3907 ! validate_change (object, &XEXP (x, 1), new_offset, 0)))
3909 if (! extra_insns)
3911 XEXP (x, 0) = old;
3912 return 0;
3915 /* Otherwise copy the new constant into a register and replace
3916 constant with that register. */
3917 temp = gen_reg_rtx (Pmode);
3918 XEXP (x, 0) = new;
3919 if (validate_change (object, &XEXP (x, 1), temp, 0))
3920 emit_insn_before (gen_move_insn (temp, new_offset), object);
3921 else
3923 /* If that didn't work, replace this expression with a
3924 register containing the sum. */
3926 XEXP (x, 0) = old;
3927 new = gen_rtx_PLUS (Pmode, new, new_offset);
3929 start_sequence ();
3930 temp = force_operand (new, NULL_RTX);
3931 seq = get_insns ();
3932 end_sequence ();
3934 emit_insn_before (seq, object);
3935 if (! validate_change (object, loc, temp, 0)
3936 && ! validate_replace_rtx (x, temp, object))
3937 abort ();
3941 return 1;
3944 /* Fall through to generic two-operand expression case. */
3945 case EXPR_LIST:
3946 case CALL:
3947 case COMPARE:
3948 case MINUS:
3949 case MULT:
3950 case DIV: case UDIV:
3951 case MOD: case UMOD:
3952 case AND: case IOR: case XOR:
3953 case ROTATERT: case ROTATE:
3954 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
3955 case NE: case EQ:
3956 case GE: case GT: case GEU: case GTU:
3957 case LE: case LT: case LEU: case LTU:
3958 if (XEXP (x, 1) && ! CONSTANT_P (XEXP (x, 1)))
3959 instantiate_virtual_regs_1 (&XEXP (x, 1), object, extra_insns);
3960 loc = &XEXP (x, 0);
3961 goto restart;
3963 case MEM:
3964 /* Most cases of MEM that convert to valid addresses have already been
3965 handled by our scan of decls. The only special handling we
3966 need here is to make a copy of the rtx to ensure it isn't being
3967 shared if we have to change it to a pseudo.
3969 If the rtx is a simple reference to an address via a virtual register,
3970 it can potentially be shared. In such cases, first try to make it
3971 a valid address, which can also be shared. Otherwise, copy it and
3972 proceed normally.
3974 First check for common cases that need no processing. These are
3975 usually due to instantiation already being done on a previous instance
3976 of a shared rtx. */
3978 temp = XEXP (x, 0);
3979 if (CONSTANT_ADDRESS_P (temp)
3980 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3981 || temp == arg_pointer_rtx
3982 #endif
3983 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3984 || temp == hard_frame_pointer_rtx
3985 #endif
3986 || temp == frame_pointer_rtx)
3987 return 1;
3989 if (GET_CODE (temp) == PLUS
3990 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3991 && (XEXP (temp, 0) == frame_pointer_rtx
3992 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3993 || XEXP (temp, 0) == hard_frame_pointer_rtx
3994 #endif
3995 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3996 || XEXP (temp, 0) == arg_pointer_rtx
3997 #endif
3999 return 1;
4001 if (temp == virtual_stack_vars_rtx
4002 || temp == virtual_incoming_args_rtx
4003 || (GET_CODE (temp) == PLUS
4004 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
4005 && (XEXP (temp, 0) == virtual_stack_vars_rtx
4006 || XEXP (temp, 0) == virtual_incoming_args_rtx)))
4008 /* This MEM may be shared. If the substitution can be done without
4009 the need to generate new pseudos, we want to do it in place
4010 so all copies of the shared rtx benefit. The call below will
4011 only make substitutions if the resulting address is still
4012 valid.
4014 Note that we cannot pass X as the object in the recursive call
4015 since the insn being processed may not allow all valid
4016 addresses. However, if we were not passed on object, we can
4017 only modify X without copying it if X will have a valid
4018 address.
4020 ??? Also note that this can still lose if OBJECT is an insn that
4021 has less restrictions on an address that some other insn.
4022 In that case, we will modify the shared address. This case
4023 doesn't seem very likely, though. One case where this could
4024 happen is in the case of a USE or CLOBBER reference, but we
4025 take care of that below. */
4027 if (instantiate_virtual_regs_1 (&XEXP (x, 0),
4028 object ? object : x, 0))
4029 return 1;
4031 /* Otherwise make a copy and process that copy. We copy the entire
4032 RTL expression since it might be a PLUS which could also be
4033 shared. */
4034 *loc = x = copy_rtx (x);
4037 /* Fall through to generic unary operation case. */
4038 case PREFETCH:
4039 case SUBREG:
4040 case STRICT_LOW_PART:
4041 case NEG: case NOT:
4042 case PRE_DEC: case PRE_INC: case POST_DEC: case POST_INC:
4043 case SIGN_EXTEND: case ZERO_EXTEND:
4044 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
4045 case FLOAT: case FIX:
4046 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
4047 case ABS:
4048 case SQRT:
4049 case FFS:
4050 /* These case either have just one operand or we know that we need not
4051 check the rest of the operands. */
4052 loc = &XEXP (x, 0);
4053 goto restart;
4055 case USE:
4056 case CLOBBER:
4057 /* If the operand is a MEM, see if the change is a valid MEM. If not,
4058 go ahead and make the invalid one, but do it to a copy. For a REG,
4059 just make the recursive call, since there's no chance of a problem. */
4061 if ((GET_CODE (XEXP (x, 0)) == MEM
4062 && instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), XEXP (x, 0),
4064 || (GET_CODE (XEXP (x, 0)) == REG
4065 && instantiate_virtual_regs_1 (&XEXP (x, 0), object, 0)))
4066 return 1;
4068 XEXP (x, 0) = copy_rtx (XEXP (x, 0));
4069 loc = &XEXP (x, 0);
4070 goto restart;
4072 case REG:
4073 /* Try to replace with a PLUS. If that doesn't work, compute the sum
4074 in front of this insn and substitute the temporary. */
4075 if ((new = instantiate_new_reg (x, &offset)) != 0)
4077 temp = plus_constant (new, offset);
4078 if (!validate_change (object, loc, temp, 0))
4080 if (! extra_insns)
4081 return 0;
4083 start_sequence ();
4084 temp = force_operand (temp, NULL_RTX);
4085 seq = get_insns ();
4086 end_sequence ();
4088 emit_insn_before (seq, object);
4089 if (! validate_change (object, loc, temp, 0)
4090 && ! validate_replace_rtx (x, temp, object))
4091 abort ();
4095 return 1;
4097 case ADDRESSOF:
4098 if (GET_CODE (XEXP (x, 0)) == REG)
4099 return 1;
4101 else if (GET_CODE (XEXP (x, 0)) == MEM)
4103 /* If we have a (addressof (mem ..)), do any instantiation inside
4104 since we know we'll be making the inside valid when we finally
4105 remove the ADDRESSOF. */
4106 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), NULL_RTX, 0);
4107 return 1;
4109 break;
4111 default:
4112 break;
4115 /* Scan all subexpressions. */
4116 fmt = GET_RTX_FORMAT (code);
4117 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
4118 if (*fmt == 'e')
4120 if (!instantiate_virtual_regs_1 (&XEXP (x, i), object, extra_insns))
4121 return 0;
4123 else if (*fmt == 'E')
4124 for (j = 0; j < XVECLEN (x, i); j++)
4125 if (! instantiate_virtual_regs_1 (&XVECEXP (x, i, j), object,
4126 extra_insns))
4127 return 0;
4129 return 1;
4132 /* Optimization: assuming this function does not receive nonlocal gotos,
4133 delete the handlers for such, as well as the insns to establish
4134 and disestablish them. */
4136 static void
4137 delete_handlers ()
4139 rtx insn;
4140 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4142 /* Delete the handler by turning off the flag that would
4143 prevent jump_optimize from deleting it.
4144 Also permit deletion of the nonlocal labels themselves
4145 if nothing local refers to them. */
4146 if (GET_CODE (insn) == CODE_LABEL)
4148 tree t, last_t;
4150 LABEL_PRESERVE_P (insn) = 0;
4152 /* Remove it from the nonlocal_label list, to avoid confusing
4153 flow. */
4154 for (t = nonlocal_labels, last_t = 0; t;
4155 last_t = t, t = TREE_CHAIN (t))
4156 if (DECL_RTL (TREE_VALUE (t)) == insn)
4157 break;
4158 if (t)
4160 if (! last_t)
4161 nonlocal_labels = TREE_CHAIN (nonlocal_labels);
4162 else
4163 TREE_CHAIN (last_t) = TREE_CHAIN (t);
4166 if (GET_CODE (insn) == INSN)
4168 int can_delete = 0;
4169 rtx t;
4170 for (t = nonlocal_goto_handler_slots; t != 0; t = XEXP (t, 1))
4171 if (reg_mentioned_p (t, PATTERN (insn)))
4173 can_delete = 1;
4174 break;
4176 if (can_delete
4177 || (nonlocal_goto_stack_level != 0
4178 && reg_mentioned_p (nonlocal_goto_stack_level,
4179 PATTERN (insn))))
4180 delete_related_insns (insn);
4186 max_parm_reg_num ()
4188 return max_parm_reg;
4191 /* Return the first insn following those generated by `assign_parms'. */
4194 get_first_nonparm_insn ()
4196 if (last_parm_insn)
4197 return NEXT_INSN (last_parm_insn);
4198 return get_insns ();
4201 /* Return the first NOTE_INSN_BLOCK_BEG note in the function.
4202 Crash if there is none. */
4205 get_first_block_beg ()
4207 rtx searcher;
4208 rtx insn = get_first_nonparm_insn ();
4210 for (searcher = insn; searcher; searcher = NEXT_INSN (searcher))
4211 if (GET_CODE (searcher) == NOTE
4212 && NOTE_LINE_NUMBER (searcher) == NOTE_INSN_BLOCK_BEG)
4213 return searcher;
4215 abort (); /* Invalid call to this function. (See comments above.) */
4216 return NULL_RTX;
4219 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
4220 This means a type for which function calls must pass an address to the
4221 function or get an address back from the function.
4222 EXP may be a type node or an expression (whose type is tested). */
4225 aggregate_value_p (exp)
4226 tree exp;
4228 int i, regno, nregs;
4229 rtx reg;
4231 tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
4233 if (TREE_CODE (type) == VOID_TYPE)
4234 return 0;
4235 if (RETURN_IN_MEMORY (type))
4236 return 1;
4237 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
4238 and thus can't be returned in registers. */
4239 if (TREE_ADDRESSABLE (type))
4240 return 1;
4241 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
4242 return 1;
4243 /* Make sure we have suitable call-clobbered regs to return
4244 the value in; if not, we must return it in memory. */
4245 reg = hard_function_value (type, 0, 0);
4247 /* If we have something other than a REG (e.g. a PARALLEL), then assume
4248 it is OK. */
4249 if (GET_CODE (reg) != REG)
4250 return 0;
4252 regno = REGNO (reg);
4253 nregs = HARD_REGNO_NREGS (regno, TYPE_MODE (type));
4254 for (i = 0; i < nregs; i++)
4255 if (! call_used_regs[regno + i])
4256 return 1;
4257 return 0;
4260 /* Assign RTL expressions to the function's parameters.
4261 This may involve copying them into registers and using
4262 those registers as the RTL for them. */
4264 void
4265 assign_parms (fndecl)
4266 tree fndecl;
4268 tree parm;
4269 rtx entry_parm = 0;
4270 rtx stack_parm = 0;
4271 CUMULATIVE_ARGS args_so_far;
4272 enum machine_mode promoted_mode, passed_mode;
4273 enum machine_mode nominal_mode, promoted_nominal_mode;
4274 int unsignedp;
4275 /* Total space needed so far for args on the stack,
4276 given as a constant and a tree-expression. */
4277 struct args_size stack_args_size;
4278 tree fntype = TREE_TYPE (fndecl);
4279 tree fnargs = DECL_ARGUMENTS (fndecl);
4280 /* This is used for the arg pointer when referring to stack args. */
4281 rtx internal_arg_pointer;
4282 /* This is a dummy PARM_DECL that we used for the function result if
4283 the function returns a structure. */
4284 tree function_result_decl = 0;
4285 #ifdef SETUP_INCOMING_VARARGS
4286 int varargs_setup = 0;
4287 #endif
4288 rtx conversion_insns = 0;
4289 struct args_size alignment_pad;
4291 /* Nonzero if function takes extra anonymous args.
4292 This means the last named arg must be on the stack
4293 right before the anonymous ones. */
4294 int stdarg
4295 = (TYPE_ARG_TYPES (fntype) != 0
4296 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4297 != void_type_node));
4299 current_function_stdarg = stdarg;
4301 /* If the reg that the virtual arg pointer will be translated into is
4302 not a fixed reg or is the stack pointer, make a copy of the virtual
4303 arg pointer, and address parms via the copy. The frame pointer is
4304 considered fixed even though it is not marked as such.
4306 The second time through, simply use ap to avoid generating rtx. */
4308 if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
4309 || ! (fixed_regs[ARG_POINTER_REGNUM]
4310 || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM)))
4311 internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
4312 else
4313 internal_arg_pointer = virtual_incoming_args_rtx;
4314 current_function_internal_arg_pointer = internal_arg_pointer;
4316 stack_args_size.constant = 0;
4317 stack_args_size.var = 0;
4319 /* If struct value address is treated as the first argument, make it so. */
4320 if (aggregate_value_p (DECL_RESULT (fndecl))
4321 && ! current_function_returns_pcc_struct
4322 && struct_value_incoming_rtx == 0)
4324 tree type = build_pointer_type (TREE_TYPE (fntype));
4326 function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
4328 DECL_ARG_TYPE (function_result_decl) = type;
4329 TREE_CHAIN (function_result_decl) = fnargs;
4330 fnargs = function_result_decl;
4333 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
4334 parm_reg_stack_loc = (rtx *) ggc_alloc_cleared (max_parm_reg * sizeof (rtx));
4336 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
4337 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far, fntype, NULL_RTX);
4338 #else
4339 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, 0);
4340 #endif
4342 /* We haven't yet found an argument that we must push and pretend the
4343 caller did. */
4344 current_function_pretend_args_size = 0;
4346 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
4348 struct args_size stack_offset;
4349 struct args_size arg_size;
4350 int passed_pointer = 0;
4351 int did_conversion = 0;
4352 tree passed_type = DECL_ARG_TYPE (parm);
4353 tree nominal_type = TREE_TYPE (parm);
4354 int pretend_named;
4355 int last_named = 0, named_arg;
4357 /* Set LAST_NAMED if this is last named arg before last
4358 anonymous args. */
4359 if (stdarg)
4361 tree tem;
4363 for (tem = TREE_CHAIN (parm); tem; tem = TREE_CHAIN (tem))
4364 if (DECL_NAME (tem))
4365 break;
4367 if (tem == 0)
4368 last_named = 1;
4370 /* Set NAMED_ARG if this arg should be treated as a named arg. For
4371 most machines, if this is a varargs/stdarg function, then we treat
4372 the last named arg as if it were anonymous too. */
4373 named_arg = STRICT_ARGUMENT_NAMING ? 1 : ! last_named;
4375 if (TREE_TYPE (parm) == error_mark_node
4376 /* This can happen after weird syntax errors
4377 or if an enum type is defined among the parms. */
4378 || TREE_CODE (parm) != PARM_DECL
4379 || passed_type == NULL)
4381 SET_DECL_RTL (parm, gen_rtx_MEM (BLKmode, const0_rtx));
4382 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4383 TREE_USED (parm) = 1;
4384 continue;
4387 /* Find mode of arg as it is passed, and mode of arg
4388 as it should be during execution of this function. */
4389 passed_mode = TYPE_MODE (passed_type);
4390 nominal_mode = TYPE_MODE (nominal_type);
4392 /* If the parm's mode is VOID, its value doesn't matter,
4393 and avoid the usual things like emit_move_insn that could crash. */
4394 if (nominal_mode == VOIDmode)
4396 SET_DECL_RTL (parm, const0_rtx);
4397 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4398 continue;
4401 /* If the parm is to be passed as a transparent union, use the
4402 type of the first field for the tests below. We have already
4403 verified that the modes are the same. */
4404 if (DECL_TRANSPARENT_UNION (parm)
4405 || (TREE_CODE (passed_type) == UNION_TYPE
4406 && TYPE_TRANSPARENT_UNION (passed_type)))
4407 passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
4409 /* See if this arg was passed by invisible reference. It is if
4410 it is an object whose size depends on the contents of the
4411 object itself or if the machine requires these objects be passed
4412 that way. */
4414 if ((TREE_CODE (TYPE_SIZE (passed_type)) != INTEGER_CST
4415 && contains_placeholder_p (TYPE_SIZE (passed_type)))
4416 || TREE_ADDRESSABLE (passed_type)
4417 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
4418 || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, passed_mode,
4419 passed_type, named_arg)
4420 #endif
4423 passed_type = nominal_type = build_pointer_type (passed_type);
4424 passed_pointer = 1;
4425 passed_mode = nominal_mode = Pmode;
4427 /* See if the frontend wants to pass this by invisible reference. */
4428 else if (passed_type != nominal_type
4429 && POINTER_TYPE_P (passed_type)
4430 && TREE_TYPE (passed_type) == nominal_type)
4432 nominal_type = passed_type;
4433 passed_pointer = 1;
4434 passed_mode = nominal_mode = Pmode;
4437 promoted_mode = passed_mode;
4439 #ifdef PROMOTE_FUNCTION_ARGS
4440 /* Compute the mode in which the arg is actually extended to. */
4441 unsignedp = TREE_UNSIGNED (passed_type);
4442 promoted_mode = promote_mode (passed_type, promoted_mode, &unsignedp, 1);
4443 #endif
4445 /* Let machine desc say which reg (if any) the parm arrives in.
4446 0 means it arrives on the stack. */
4447 #ifdef FUNCTION_INCOMING_ARG
4448 entry_parm = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4449 passed_type, named_arg);
4450 #else
4451 entry_parm = FUNCTION_ARG (args_so_far, promoted_mode,
4452 passed_type, named_arg);
4453 #endif
4455 if (entry_parm == 0)
4456 promoted_mode = passed_mode;
4458 #ifdef SETUP_INCOMING_VARARGS
4459 /* If this is the last named parameter, do any required setup for
4460 varargs or stdargs. We need to know about the case of this being an
4461 addressable type, in which case we skip the registers it
4462 would have arrived in.
4464 For stdargs, LAST_NAMED will be set for two parameters, the one that
4465 is actually the last named, and the dummy parameter. We only
4466 want to do this action once.
4468 Also, indicate when RTL generation is to be suppressed. */
4469 if (last_named && !varargs_setup)
4471 SETUP_INCOMING_VARARGS (args_so_far, promoted_mode, passed_type,
4472 current_function_pretend_args_size, 0);
4473 varargs_setup = 1;
4475 #endif
4477 /* Determine parm's home in the stack,
4478 in case it arrives in the stack or we should pretend it did.
4480 Compute the stack position and rtx where the argument arrives
4481 and its size.
4483 There is one complexity here: If this was a parameter that would
4484 have been passed in registers, but wasn't only because it is
4485 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
4486 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
4487 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of
4488 0 as it was the previous time. */
4490 pretend_named = named_arg || PRETEND_OUTGOING_VARARGS_NAMED;
4491 locate_and_pad_parm (promoted_mode, passed_type,
4492 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4494 #else
4495 #ifdef FUNCTION_INCOMING_ARG
4496 FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4497 passed_type,
4498 pretend_named) != 0,
4499 #else
4500 FUNCTION_ARG (args_so_far, promoted_mode,
4501 passed_type,
4502 pretend_named) != 0,
4503 #endif
4504 #endif
4505 fndecl, &stack_args_size, &stack_offset, &arg_size,
4506 &alignment_pad);
4509 rtx offset_rtx = ARGS_SIZE_RTX (stack_offset);
4511 if (offset_rtx == const0_rtx)
4512 stack_parm = gen_rtx_MEM (promoted_mode, internal_arg_pointer);
4513 else
4514 stack_parm = gen_rtx_MEM (promoted_mode,
4515 gen_rtx_PLUS (Pmode,
4516 internal_arg_pointer,
4517 offset_rtx));
4519 set_mem_attributes (stack_parm, parm, 1);
4522 /* If this parameter was passed both in registers and in the stack,
4523 use the copy on the stack. */
4524 if (MUST_PASS_IN_STACK (promoted_mode, passed_type))
4525 entry_parm = 0;
4527 #ifdef FUNCTION_ARG_PARTIAL_NREGS
4528 /* If this parm was passed part in regs and part in memory,
4529 pretend it arrived entirely in memory
4530 by pushing the register-part onto the stack.
4532 In the special case of a DImode or DFmode that is split,
4533 we could put it together in a pseudoreg directly,
4534 but for now that's not worth bothering with. */
4536 if (entry_parm)
4538 int nregs = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, promoted_mode,
4539 passed_type, named_arg);
4541 if (nregs > 0)
4543 current_function_pretend_args_size
4544 = (((nregs * UNITS_PER_WORD) + (PARM_BOUNDARY / BITS_PER_UNIT) - 1)
4545 / (PARM_BOUNDARY / BITS_PER_UNIT)
4546 * (PARM_BOUNDARY / BITS_PER_UNIT));
4548 /* Handle calls that pass values in multiple non-contiguous
4549 locations. The Irix 6 ABI has examples of this. */
4550 if (GET_CODE (entry_parm) == PARALLEL)
4551 emit_group_store (validize_mem (stack_parm), entry_parm,
4552 int_size_in_bytes (TREE_TYPE (parm)));
4554 else
4555 move_block_from_reg (REGNO (entry_parm),
4556 validize_mem (stack_parm), nregs,
4557 int_size_in_bytes (TREE_TYPE (parm)));
4559 entry_parm = stack_parm;
4562 #endif
4564 /* If we didn't decide this parm came in a register,
4565 by default it came on the stack. */
4566 if (entry_parm == 0)
4567 entry_parm = stack_parm;
4569 /* Record permanently how this parm was passed. */
4570 DECL_INCOMING_RTL (parm) = entry_parm;
4572 /* If there is actually space on the stack for this parm,
4573 count it in stack_args_size; otherwise set stack_parm to 0
4574 to indicate there is no preallocated stack slot for the parm. */
4576 if (entry_parm == stack_parm
4577 || (GET_CODE (entry_parm) == PARALLEL
4578 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
4579 #if defined (REG_PARM_STACK_SPACE) && ! defined (MAYBE_REG_PARM_STACK_SPACE)
4580 /* On some machines, even if a parm value arrives in a register
4581 there is still an (uninitialized) stack slot allocated for it.
4583 ??? When MAYBE_REG_PARM_STACK_SPACE is defined, we can't tell
4584 whether this parameter already has a stack slot allocated,
4585 because an arg block exists only if current_function_args_size
4586 is larger than some threshold, and we haven't calculated that
4587 yet. So, for now, we just assume that stack slots never exist
4588 in this case. */
4589 || REG_PARM_STACK_SPACE (fndecl) > 0
4590 #endif
4593 stack_args_size.constant += arg_size.constant;
4594 if (arg_size.var)
4595 ADD_PARM_SIZE (stack_args_size, arg_size.var);
4597 else
4598 /* No stack slot was pushed for this parm. */
4599 stack_parm = 0;
4601 /* Update info on where next arg arrives in registers. */
4603 FUNCTION_ARG_ADVANCE (args_so_far, promoted_mode,
4604 passed_type, named_arg);
4606 /* If we can't trust the parm stack slot to be aligned enough
4607 for its ultimate type, don't use that slot after entry.
4608 We'll make another stack slot, if we need one. */
4610 unsigned int thisparm_boundary
4611 = FUNCTION_ARG_BOUNDARY (promoted_mode, passed_type);
4613 if (GET_MODE_ALIGNMENT (nominal_mode) > thisparm_boundary)
4614 stack_parm = 0;
4617 /* If parm was passed in memory, and we need to convert it on entry,
4618 don't store it back in that same slot. */
4619 if (entry_parm != 0
4620 && nominal_mode != BLKmode && nominal_mode != passed_mode)
4621 stack_parm = 0;
4623 /* When an argument is passed in multiple locations, we can't
4624 make use of this information, but we can save some copying if
4625 the whole argument is passed in a single register. */
4626 if (GET_CODE (entry_parm) == PARALLEL
4627 && nominal_mode != BLKmode && passed_mode != BLKmode)
4629 int i, len = XVECLEN (entry_parm, 0);
4631 for (i = 0; i < len; i++)
4632 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
4633 && GET_CODE (XEXP (XVECEXP (entry_parm, 0, i), 0)) == REG
4634 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
4635 == passed_mode)
4636 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
4638 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
4639 DECL_INCOMING_RTL (parm) = entry_parm;
4640 break;
4644 /* ENTRY_PARM is an RTX for the parameter as it arrives,
4645 in the mode in which it arrives.
4646 STACK_PARM is an RTX for a stack slot where the parameter can live
4647 during the function (in case we want to put it there).
4648 STACK_PARM is 0 if no stack slot was pushed for it.
4650 Now output code if necessary to convert ENTRY_PARM to
4651 the type in which this function declares it,
4652 and store that result in an appropriate place,
4653 which may be a pseudo reg, may be STACK_PARM,
4654 or may be a local stack slot if STACK_PARM is 0.
4656 Set DECL_RTL to that place. */
4658 if (nominal_mode == BLKmode || GET_CODE (entry_parm) == PARALLEL)
4660 /* If a BLKmode arrives in registers, copy it to a stack slot.
4661 Handle calls that pass values in multiple non-contiguous
4662 locations. The Irix 6 ABI has examples of this. */
4663 if (GET_CODE (entry_parm) == REG
4664 || GET_CODE (entry_parm) == PARALLEL)
4666 int size_stored
4667 = CEIL_ROUND (int_size_in_bytes (TREE_TYPE (parm)),
4668 UNITS_PER_WORD);
4670 /* Note that we will be storing an integral number of words.
4671 So we have to be careful to ensure that we allocate an
4672 integral number of words. We do this below in the
4673 assign_stack_local if space was not allocated in the argument
4674 list. If it was, this will not work if PARM_BOUNDARY is not
4675 a multiple of BITS_PER_WORD. It isn't clear how to fix this
4676 if it becomes a problem. */
4678 if (stack_parm == 0)
4680 stack_parm
4681 = assign_stack_local (GET_MODE (entry_parm),
4682 size_stored, 0);
4683 set_mem_attributes (stack_parm, parm, 1);
4686 else if (PARM_BOUNDARY % BITS_PER_WORD != 0)
4687 abort ();
4689 /* Handle calls that pass values in multiple non-contiguous
4690 locations. The Irix 6 ABI has examples of this. */
4691 if (GET_CODE (entry_parm) == PARALLEL)
4692 emit_group_store (validize_mem (stack_parm), entry_parm,
4693 int_size_in_bytes (TREE_TYPE (parm)));
4694 else
4695 move_block_from_reg (REGNO (entry_parm),
4696 validize_mem (stack_parm),
4697 size_stored / UNITS_PER_WORD,
4698 int_size_in_bytes (TREE_TYPE (parm)));
4700 SET_DECL_RTL (parm, stack_parm);
4702 else if (! ((! optimize
4703 && ! DECL_REGISTER (parm))
4704 || TREE_SIDE_EFFECTS (parm)
4705 /* If -ffloat-store specified, don't put explicit
4706 float variables into registers. */
4707 || (flag_float_store
4708 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
4709 /* Always assign pseudo to structure return or item passed
4710 by invisible reference. */
4711 || passed_pointer || parm == function_result_decl)
4713 /* Store the parm in a pseudoregister during the function, but we
4714 may need to do it in a wider mode. */
4716 rtx parmreg;
4717 unsigned int regno, regnoi = 0, regnor = 0;
4719 unsignedp = TREE_UNSIGNED (TREE_TYPE (parm));
4721 promoted_nominal_mode
4722 = promote_mode (TREE_TYPE (parm), nominal_mode, &unsignedp, 0);
4724 parmreg = gen_reg_rtx (promoted_nominal_mode);
4725 mark_user_reg (parmreg);
4727 /* If this was an item that we received a pointer to, set DECL_RTL
4728 appropriately. */
4729 if (passed_pointer)
4731 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (passed_type)),
4732 parmreg);
4733 set_mem_attributes (x, parm, 1);
4734 SET_DECL_RTL (parm, x);
4736 else
4738 SET_DECL_RTL (parm, parmreg);
4739 maybe_set_unchanging (DECL_RTL (parm), parm);
4742 /* Copy the value into the register. */
4743 if (nominal_mode != passed_mode
4744 || promoted_nominal_mode != promoted_mode)
4746 int save_tree_used;
4747 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
4748 mode, by the caller. We now have to convert it to
4749 NOMINAL_MODE, if different. However, PARMREG may be in
4750 a different mode than NOMINAL_MODE if it is being stored
4751 promoted.
4753 If ENTRY_PARM is a hard register, it might be in a register
4754 not valid for operating in its mode (e.g., an odd-numbered
4755 register for a DFmode). In that case, moves are the only
4756 thing valid, so we can't do a convert from there. This
4757 occurs when the calling sequence allow such misaligned
4758 usages.
4760 In addition, the conversion may involve a call, which could
4761 clobber parameters which haven't been copied to pseudo
4762 registers yet. Therefore, we must first copy the parm to
4763 a pseudo reg here, and save the conversion until after all
4764 parameters have been moved. */
4766 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4768 emit_move_insn (tempreg, validize_mem (entry_parm));
4770 push_to_sequence (conversion_insns);
4771 tempreg = convert_to_mode (nominal_mode, tempreg, unsignedp);
4773 if (GET_CODE (tempreg) == SUBREG
4774 && GET_MODE (tempreg) == nominal_mode
4775 && GET_CODE (SUBREG_REG (tempreg)) == REG
4776 && nominal_mode == passed_mode
4777 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (entry_parm)
4778 && GET_MODE_SIZE (GET_MODE (tempreg))
4779 < GET_MODE_SIZE (GET_MODE (entry_parm)))
4781 /* The argument is already sign/zero extended, so note it
4782 into the subreg. */
4783 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
4784 SUBREG_PROMOTED_UNSIGNED_SET (tempreg, unsignedp);
4787 /* TREE_USED gets set erroneously during expand_assignment. */
4788 save_tree_used = TREE_USED (parm);
4789 expand_assignment (parm,
4790 make_tree (nominal_type, tempreg), 0, 0);
4791 TREE_USED (parm) = save_tree_used;
4792 conversion_insns = get_insns ();
4793 did_conversion = 1;
4794 end_sequence ();
4796 else
4797 emit_move_insn (parmreg, validize_mem (entry_parm));
4799 /* If we were passed a pointer but the actual value
4800 can safely live in a register, put it in one. */
4801 if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
4802 /* If by-reference argument was promoted, demote it. */
4803 && (TYPE_MODE (TREE_TYPE (parm)) != GET_MODE (DECL_RTL (parm))
4804 || ! ((! optimize
4805 && ! DECL_REGISTER (parm))
4806 || TREE_SIDE_EFFECTS (parm)
4807 /* If -ffloat-store specified, don't put explicit
4808 float variables into registers. */
4809 || (flag_float_store
4810 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))))
4812 /* We can't use nominal_mode, because it will have been set to
4813 Pmode above. We must use the actual mode of the parm. */
4814 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
4815 mark_user_reg (parmreg);
4816 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
4818 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
4819 int unsigned_p = TREE_UNSIGNED (TREE_TYPE (parm));
4820 push_to_sequence (conversion_insns);
4821 emit_move_insn (tempreg, DECL_RTL (parm));
4822 SET_DECL_RTL (parm,
4823 convert_to_mode (GET_MODE (parmreg),
4824 tempreg,
4825 unsigned_p));
4826 emit_move_insn (parmreg, DECL_RTL (parm));
4827 conversion_insns = get_insns();
4828 did_conversion = 1;
4829 end_sequence ();
4831 else
4832 emit_move_insn (parmreg, DECL_RTL (parm));
4833 SET_DECL_RTL (parm, parmreg);
4834 /* STACK_PARM is the pointer, not the parm, and PARMREG is
4835 now the parm. */
4836 stack_parm = 0;
4838 #ifdef FUNCTION_ARG_CALLEE_COPIES
4839 /* If we are passed an arg by reference and it is our responsibility
4840 to make a copy, do it now.
4841 PASSED_TYPE and PASSED mode now refer to the pointer, not the
4842 original argument, so we must recreate them in the call to
4843 FUNCTION_ARG_CALLEE_COPIES. */
4844 /* ??? Later add code to handle the case that if the argument isn't
4845 modified, don't do the copy. */
4847 else if (passed_pointer
4848 && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
4849 TYPE_MODE (DECL_ARG_TYPE (parm)),
4850 DECL_ARG_TYPE (parm),
4851 named_arg)
4852 && ! TREE_ADDRESSABLE (DECL_ARG_TYPE (parm)))
4854 rtx copy;
4855 tree type = DECL_ARG_TYPE (parm);
4857 /* This sequence may involve a library call perhaps clobbering
4858 registers that haven't been copied to pseudos yet. */
4860 push_to_sequence (conversion_insns);
4862 if (!COMPLETE_TYPE_P (type)
4863 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4864 /* This is a variable sized object. */
4865 copy = gen_rtx_MEM (BLKmode,
4866 allocate_dynamic_stack_space
4867 (expr_size (parm), NULL_RTX,
4868 TYPE_ALIGN (type)));
4869 else
4870 copy = assign_stack_temp (TYPE_MODE (type),
4871 int_size_in_bytes (type), 1);
4872 set_mem_attributes (copy, parm, 1);
4874 store_expr (parm, copy, 0);
4875 emit_move_insn (parmreg, XEXP (copy, 0));
4876 conversion_insns = get_insns ();
4877 did_conversion = 1;
4878 end_sequence ();
4880 #endif /* FUNCTION_ARG_CALLEE_COPIES */
4882 /* In any case, record the parm's desired stack location
4883 in case we later discover it must live in the stack.
4885 If it is a COMPLEX value, store the stack location for both
4886 halves. */
4888 if (GET_CODE (parmreg) == CONCAT)
4889 regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
4890 else
4891 regno = REGNO (parmreg);
4893 if (regno >= max_parm_reg)
4895 rtx *new;
4896 int old_max_parm_reg = max_parm_reg;
4898 /* It's slow to expand this one register at a time,
4899 but it's also rare and we need max_parm_reg to be
4900 precisely correct. */
4901 max_parm_reg = regno + 1;
4902 new = (rtx *) ggc_realloc (parm_reg_stack_loc,
4903 max_parm_reg * sizeof (rtx));
4904 memset ((char *) (new + old_max_parm_reg), 0,
4905 (max_parm_reg - old_max_parm_reg) * sizeof (rtx));
4906 parm_reg_stack_loc = new;
4909 if (GET_CODE (parmreg) == CONCAT)
4911 enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
4913 regnor = REGNO (gen_realpart (submode, parmreg));
4914 regnoi = REGNO (gen_imagpart (submode, parmreg));
4916 if (stack_parm != 0)
4918 parm_reg_stack_loc[regnor]
4919 = gen_realpart (submode, stack_parm);
4920 parm_reg_stack_loc[regnoi]
4921 = gen_imagpart (submode, stack_parm);
4923 else
4925 parm_reg_stack_loc[regnor] = 0;
4926 parm_reg_stack_loc[regnoi] = 0;
4929 else
4930 parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
4932 /* Mark the register as eliminable if we did no conversion
4933 and it was copied from memory at a fixed offset,
4934 and the arg pointer was not copied to a pseudo-reg.
4935 If the arg pointer is a pseudo reg or the offset formed
4936 an invalid address, such memory-equivalences
4937 as we make here would screw up life analysis for it. */
4938 if (nominal_mode == passed_mode
4939 && ! did_conversion
4940 && stack_parm != 0
4941 && GET_CODE (stack_parm) == MEM
4942 && stack_offset.var == 0
4943 && reg_mentioned_p (virtual_incoming_args_rtx,
4944 XEXP (stack_parm, 0)))
4946 rtx linsn = get_last_insn ();
4947 rtx sinsn, set;
4949 /* Mark complex types separately. */
4950 if (GET_CODE (parmreg) == CONCAT)
4951 /* Scan backwards for the set of the real and
4952 imaginary parts. */
4953 for (sinsn = linsn; sinsn != 0;
4954 sinsn = prev_nonnote_insn (sinsn))
4956 set = single_set (sinsn);
4957 if (set != 0
4958 && SET_DEST (set) == regno_reg_rtx [regnoi])
4959 REG_NOTES (sinsn)
4960 = gen_rtx_EXPR_LIST (REG_EQUIV,
4961 parm_reg_stack_loc[regnoi],
4962 REG_NOTES (sinsn));
4963 else if (set != 0
4964 && SET_DEST (set) == regno_reg_rtx [regnor])
4965 REG_NOTES (sinsn)
4966 = gen_rtx_EXPR_LIST (REG_EQUIV,
4967 parm_reg_stack_loc[regnor],
4968 REG_NOTES (sinsn));
4970 else if ((set = single_set (linsn)) != 0
4971 && SET_DEST (set) == parmreg)
4972 REG_NOTES (linsn)
4973 = gen_rtx_EXPR_LIST (REG_EQUIV,
4974 stack_parm, REG_NOTES (linsn));
4977 /* For pointer data type, suggest pointer register. */
4978 if (POINTER_TYPE_P (TREE_TYPE (parm)))
4979 mark_reg_pointer (parmreg,
4980 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4982 /* If something wants our address, try to use ADDRESSOF. */
4983 if (TREE_ADDRESSABLE (parm))
4985 /* If we end up putting something into the stack,
4986 fixup_var_refs_insns will need to make a pass over
4987 all the instructions. It looks through the pending
4988 sequences -- but it can't see the ones in the
4989 CONVERSION_INSNS, if they're not on the sequence
4990 stack. So, we go back to that sequence, just so that
4991 the fixups will happen. */
4992 push_to_sequence (conversion_insns);
4993 put_var_into_stack (parm);
4994 conversion_insns = get_insns ();
4995 end_sequence ();
4998 else
5000 /* Value must be stored in the stack slot STACK_PARM
5001 during function execution. */
5003 if (promoted_mode != nominal_mode)
5005 /* Conversion is required. */
5006 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
5008 emit_move_insn (tempreg, validize_mem (entry_parm));
5010 push_to_sequence (conversion_insns);
5011 entry_parm = convert_to_mode (nominal_mode, tempreg,
5012 TREE_UNSIGNED (TREE_TYPE (parm)));
5013 if (stack_parm)
5014 /* ??? This may need a big-endian conversion on sparc64. */
5015 stack_parm = adjust_address (stack_parm, nominal_mode, 0);
5017 conversion_insns = get_insns ();
5018 did_conversion = 1;
5019 end_sequence ();
5022 if (entry_parm != stack_parm)
5024 if (stack_parm == 0)
5026 stack_parm
5027 = assign_stack_local (GET_MODE (entry_parm),
5028 GET_MODE_SIZE (GET_MODE (entry_parm)), 0);
5029 set_mem_attributes (stack_parm, parm, 1);
5032 if (promoted_mode != nominal_mode)
5034 push_to_sequence (conversion_insns);
5035 emit_move_insn (validize_mem (stack_parm),
5036 validize_mem (entry_parm));
5037 conversion_insns = get_insns ();
5038 end_sequence ();
5040 else
5041 emit_move_insn (validize_mem (stack_parm),
5042 validize_mem (entry_parm));
5045 SET_DECL_RTL (parm, stack_parm);
5048 /* If this "parameter" was the place where we are receiving the
5049 function's incoming structure pointer, set up the result. */
5050 if (parm == function_result_decl)
5052 tree result = DECL_RESULT (fndecl);
5053 rtx addr = DECL_RTL (parm);
5054 rtx x;
5056 #ifdef POINTERS_EXTEND_UNSIGNED
5057 if (GET_MODE (addr) != Pmode)
5058 addr = convert_memory_address (Pmode, addr);
5059 #endif
5061 x = gen_rtx_MEM (DECL_MODE (result), addr);
5062 set_mem_attributes (x, result, 1);
5063 SET_DECL_RTL (result, x);
5066 if (GET_CODE (DECL_RTL (parm)) == REG)
5067 REGNO_DECL (REGNO (DECL_RTL (parm))) = parm;
5068 else if (GET_CODE (DECL_RTL (parm)) == CONCAT)
5070 REGNO_DECL (REGNO (XEXP (DECL_RTL (parm), 0))) = parm;
5071 REGNO_DECL (REGNO (XEXP (DECL_RTL (parm), 1))) = parm;
5076 /* Output all parameter conversion instructions (possibly including calls)
5077 now that all parameters have been copied out of hard registers. */
5078 emit_insn (conversion_insns);
5080 last_parm_insn = get_last_insn ();
5082 current_function_args_size = stack_args_size.constant;
5084 /* Adjust function incoming argument size for alignment and
5085 minimum length. */
5087 #ifdef REG_PARM_STACK_SPACE
5088 #ifndef MAYBE_REG_PARM_STACK_SPACE
5089 current_function_args_size = MAX (current_function_args_size,
5090 REG_PARM_STACK_SPACE (fndecl));
5091 #endif
5092 #endif
5094 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
5096 current_function_args_size
5097 = ((current_function_args_size + STACK_BYTES - 1)
5098 / STACK_BYTES) * STACK_BYTES;
5100 #ifdef ARGS_GROW_DOWNWARD
5101 current_function_arg_offset_rtx
5102 = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
5103 : expand_expr (size_diffop (stack_args_size.var,
5104 size_int (-stack_args_size.constant)),
5105 NULL_RTX, VOIDmode, 0));
5106 #else
5107 current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
5108 #endif
5110 /* See how many bytes, if any, of its args a function should try to pop
5111 on return. */
5113 current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
5114 current_function_args_size);
5116 /* For stdarg.h function, save info about
5117 regs and stack space used by the named args. */
5119 current_function_args_info = args_so_far;
5121 /* Set the rtx used for the function return value. Put this in its
5122 own variable so any optimizers that need this information don't have
5123 to include tree.h. Do this here so it gets done when an inlined
5124 function gets output. */
5126 current_function_return_rtx
5127 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
5128 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
5130 /* If scalar return value was computed in a pseudo-reg, or was a named
5131 return value that got dumped to the stack, copy that to the hard
5132 return register. */
5133 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
5135 tree decl_result = DECL_RESULT (fndecl);
5136 rtx decl_rtl = DECL_RTL (decl_result);
5138 if (REG_P (decl_rtl)
5139 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5140 : DECL_REGISTER (decl_result))
5142 rtx real_decl_rtl;
5144 #ifdef FUNCTION_OUTGOING_VALUE
5145 real_decl_rtl = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl_result),
5146 fndecl);
5147 #else
5148 real_decl_rtl = FUNCTION_VALUE (TREE_TYPE (decl_result),
5149 fndecl);
5150 #endif
5151 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
5152 /* The delay slot scheduler assumes that current_function_return_rtx
5153 holds the hard register containing the return value, not a
5154 temporary pseudo. */
5155 current_function_return_rtx = real_decl_rtl;
5160 /* Indicate whether REGNO is an incoming argument to the current function
5161 that was promoted to a wider mode. If so, return the RTX for the
5162 register (to get its mode). PMODE and PUNSIGNEDP are set to the mode
5163 that REGNO is promoted from and whether the promotion was signed or
5164 unsigned. */
5166 #ifdef PROMOTE_FUNCTION_ARGS
5169 promoted_input_arg (regno, pmode, punsignedp)
5170 unsigned int regno;
5171 enum machine_mode *pmode;
5172 int *punsignedp;
5174 tree arg;
5176 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
5177 arg = TREE_CHAIN (arg))
5178 if (GET_CODE (DECL_INCOMING_RTL (arg)) == REG
5179 && REGNO (DECL_INCOMING_RTL (arg)) == regno
5180 && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
5182 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
5183 int unsignedp = TREE_UNSIGNED (TREE_TYPE (arg));
5185 mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
5186 if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
5187 && mode != DECL_MODE (arg))
5189 *pmode = DECL_MODE (arg);
5190 *punsignedp = unsignedp;
5191 return DECL_INCOMING_RTL (arg);
5195 return 0;
5198 #endif
5200 /* Compute the size and offset from the start of the stacked arguments for a
5201 parm passed in mode PASSED_MODE and with type TYPE.
5203 INITIAL_OFFSET_PTR points to the current offset into the stacked
5204 arguments.
5206 The starting offset and size for this parm are returned in *OFFSET_PTR
5207 and *ARG_SIZE_PTR, respectively.
5209 IN_REGS is non-zero if the argument will be passed in registers. It will
5210 never be set if REG_PARM_STACK_SPACE is not defined.
5212 FNDECL is the function in which the argument was defined.
5214 There are two types of rounding that are done. The first, controlled by
5215 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
5216 list to be aligned to the specific boundary (in bits). This rounding
5217 affects the initial and starting offsets, but not the argument size.
5219 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
5220 optionally rounds the size of the parm to PARM_BOUNDARY. The
5221 initial offset is not affected by this rounding, while the size always
5222 is and the starting offset may be. */
5224 /* offset_ptr will be negative for ARGS_GROW_DOWNWARD case;
5225 initial_offset_ptr is positive because locate_and_pad_parm's
5226 callers pass in the total size of args so far as
5227 initial_offset_ptr. arg_size_ptr is always positive. */
5229 void
5230 locate_and_pad_parm (passed_mode, type, in_regs, fndecl,
5231 initial_offset_ptr, offset_ptr, arg_size_ptr,
5232 alignment_pad)
5233 enum machine_mode passed_mode;
5234 tree type;
5235 int in_regs ATTRIBUTE_UNUSED;
5236 tree fndecl ATTRIBUTE_UNUSED;
5237 struct args_size *initial_offset_ptr;
5238 struct args_size *offset_ptr;
5239 struct args_size *arg_size_ptr;
5240 struct args_size *alignment_pad;
5243 tree sizetree
5244 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
5245 enum direction where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
5246 int boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
5248 #ifdef REG_PARM_STACK_SPACE
5249 /* If we have found a stack parm before we reach the end of the
5250 area reserved for registers, skip that area. */
5251 if (! in_regs)
5253 int reg_parm_stack_space = 0;
5255 #ifdef MAYBE_REG_PARM_STACK_SPACE
5256 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
5257 #else
5258 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
5259 #endif
5260 if (reg_parm_stack_space > 0)
5262 if (initial_offset_ptr->var)
5264 initial_offset_ptr->var
5265 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
5266 ssize_int (reg_parm_stack_space));
5267 initial_offset_ptr->constant = 0;
5269 else if (initial_offset_ptr->constant < reg_parm_stack_space)
5270 initial_offset_ptr->constant = reg_parm_stack_space;
5273 #endif /* REG_PARM_STACK_SPACE */
5275 arg_size_ptr->var = 0;
5276 arg_size_ptr->constant = 0;
5277 alignment_pad->var = 0;
5278 alignment_pad->constant = 0;
5280 #ifdef ARGS_GROW_DOWNWARD
5281 if (initial_offset_ptr->var)
5283 offset_ptr->constant = 0;
5284 offset_ptr->var = size_binop (MINUS_EXPR, ssize_int (0),
5285 initial_offset_ptr->var);
5287 else
5289 offset_ptr->constant = -initial_offset_ptr->constant;
5290 offset_ptr->var = 0;
5292 if (where_pad != none
5293 && (!host_integerp (sizetree, 1)
5294 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5295 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5296 SUB_PARM_SIZE (*offset_ptr, sizetree);
5297 if (where_pad != downward)
5298 pad_to_arg_alignment (offset_ptr, boundary, alignment_pad);
5299 if (initial_offset_ptr->var)
5300 arg_size_ptr->var = size_binop (MINUS_EXPR,
5301 size_binop (MINUS_EXPR,
5302 ssize_int (0),
5303 initial_offset_ptr->var),
5304 offset_ptr->var);
5306 else
5307 arg_size_ptr->constant = (-initial_offset_ptr->constant
5308 - offset_ptr->constant);
5310 #else /* !ARGS_GROW_DOWNWARD */
5311 if (!in_regs
5312 #ifdef REG_PARM_STACK_SPACE
5313 || REG_PARM_STACK_SPACE (fndecl) > 0
5314 #endif
5316 pad_to_arg_alignment (initial_offset_ptr, boundary, alignment_pad);
5317 *offset_ptr = *initial_offset_ptr;
5319 #ifdef PUSH_ROUNDING
5320 if (passed_mode != BLKmode)
5321 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
5322 #endif
5324 /* Pad_below needs the pre-rounded size to know how much to pad below
5325 so this must be done before rounding up. */
5326 if (where_pad == downward
5327 /* However, BLKmode args passed in regs have their padding done elsewhere.
5328 The stack slot must be able to hold the entire register. */
5329 && !(in_regs && passed_mode == BLKmode))
5330 pad_below (offset_ptr, passed_mode, sizetree);
5332 if (where_pad != none
5333 && (!host_integerp (sizetree, 1)
5334 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5335 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5337 ADD_PARM_SIZE (*arg_size_ptr, sizetree);
5338 #endif /* ARGS_GROW_DOWNWARD */
5341 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
5342 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
5344 static void
5345 pad_to_arg_alignment (offset_ptr, boundary, alignment_pad)
5346 struct args_size *offset_ptr;
5347 int boundary;
5348 struct args_size *alignment_pad;
5350 tree save_var = NULL_TREE;
5351 HOST_WIDE_INT save_constant = 0;
5353 int boundary_in_bytes = boundary / BITS_PER_UNIT;
5355 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5357 save_var = offset_ptr->var;
5358 save_constant = offset_ptr->constant;
5361 alignment_pad->var = NULL_TREE;
5362 alignment_pad->constant = 0;
5364 if (boundary > BITS_PER_UNIT)
5366 if (offset_ptr->var)
5368 offset_ptr->var =
5369 #ifdef ARGS_GROW_DOWNWARD
5370 round_down
5371 #else
5372 round_up
5373 #endif
5374 (ARGS_SIZE_TREE (*offset_ptr),
5375 boundary / BITS_PER_UNIT);
5376 offset_ptr->constant = 0; /*?*/
5377 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5378 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
5379 save_var);
5381 else
5383 offset_ptr->constant =
5384 #ifdef ARGS_GROW_DOWNWARD
5385 FLOOR_ROUND (offset_ptr->constant, boundary_in_bytes);
5386 #else
5387 CEIL_ROUND (offset_ptr->constant, boundary_in_bytes);
5388 #endif
5389 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5390 alignment_pad->constant = offset_ptr->constant - save_constant;
5395 #ifndef ARGS_GROW_DOWNWARD
5396 static void
5397 pad_below (offset_ptr, passed_mode, sizetree)
5398 struct args_size *offset_ptr;
5399 enum machine_mode passed_mode;
5400 tree sizetree;
5402 if (passed_mode != BLKmode)
5404 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
5405 offset_ptr->constant
5406 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
5407 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
5408 - GET_MODE_SIZE (passed_mode));
5410 else
5412 if (TREE_CODE (sizetree) != INTEGER_CST
5413 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
5415 /* Round the size up to multiple of PARM_BOUNDARY bits. */
5416 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5417 /* Add it in. */
5418 ADD_PARM_SIZE (*offset_ptr, s2);
5419 SUB_PARM_SIZE (*offset_ptr, sizetree);
5423 #endif
5425 /* Walk the tree of blocks describing the binding levels within a function
5426 and warn about uninitialized variables.
5427 This is done after calling flow_analysis and before global_alloc
5428 clobbers the pseudo-regs to hard regs. */
5430 void
5431 uninitialized_vars_warning (block)
5432 tree block;
5434 tree decl, sub;
5435 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5437 if (warn_uninitialized
5438 && TREE_CODE (decl) == VAR_DECL
5439 /* These warnings are unreliable for and aggregates
5440 because assigning the fields one by one can fail to convince
5441 flow.c that the entire aggregate was initialized.
5442 Unions are troublesome because members may be shorter. */
5443 && ! AGGREGATE_TYPE_P (TREE_TYPE (decl))
5444 && DECL_RTL (decl) != 0
5445 && GET_CODE (DECL_RTL (decl)) == REG
5446 /* Global optimizations can make it difficult to determine if a
5447 particular variable has been initialized. However, a VAR_DECL
5448 with a nonzero DECL_INITIAL had an initializer, so do not
5449 claim it is potentially uninitialized.
5451 We do not care about the actual value in DECL_INITIAL, so we do
5452 not worry that it may be a dangling pointer. */
5453 && DECL_INITIAL (decl) == NULL_TREE
5454 && regno_uninitialized (REGNO (DECL_RTL (decl))))
5455 warning_with_decl (decl,
5456 "`%s' might be used uninitialized in this function");
5457 if (extra_warnings
5458 && TREE_CODE (decl) == VAR_DECL
5459 && DECL_RTL (decl) != 0
5460 && GET_CODE (DECL_RTL (decl)) == REG
5461 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5462 warning_with_decl (decl,
5463 "variable `%s' might be clobbered by `longjmp' or `vfork'");
5465 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5466 uninitialized_vars_warning (sub);
5469 /* Do the appropriate part of uninitialized_vars_warning
5470 but for arguments instead of local variables. */
5472 void
5473 setjmp_args_warning ()
5475 tree decl;
5476 for (decl = DECL_ARGUMENTS (current_function_decl);
5477 decl; decl = TREE_CHAIN (decl))
5478 if (DECL_RTL (decl) != 0
5479 && GET_CODE (DECL_RTL (decl)) == REG
5480 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5481 warning_with_decl (decl,
5482 "argument `%s' might be clobbered by `longjmp' or `vfork'");
5485 /* If this function call setjmp, put all vars into the stack
5486 unless they were declared `register'. */
5488 void
5489 setjmp_protect (block)
5490 tree block;
5492 tree decl, sub;
5493 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5494 if ((TREE_CODE (decl) == VAR_DECL
5495 || TREE_CODE (decl) == PARM_DECL)
5496 && DECL_RTL (decl) != 0
5497 && (GET_CODE (DECL_RTL (decl)) == REG
5498 || (GET_CODE (DECL_RTL (decl)) == MEM
5499 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5500 /* If this variable came from an inline function, it must be
5501 that its life doesn't overlap the setjmp. If there was a
5502 setjmp in the function, it would already be in memory. We
5503 must exclude such variable because their DECL_RTL might be
5504 set to strange things such as virtual_stack_vars_rtx. */
5505 && ! DECL_FROM_INLINE (decl)
5506 && (
5507 #ifdef NON_SAVING_SETJMP
5508 /* If longjmp doesn't restore the registers,
5509 don't put anything in them. */
5510 NON_SAVING_SETJMP
5512 #endif
5513 ! DECL_REGISTER (decl)))
5514 put_var_into_stack (decl);
5515 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5516 setjmp_protect (sub);
5519 /* Like the previous function, but for args instead of local variables. */
5521 void
5522 setjmp_protect_args ()
5524 tree decl;
5525 for (decl = DECL_ARGUMENTS (current_function_decl);
5526 decl; decl = TREE_CHAIN (decl))
5527 if ((TREE_CODE (decl) == VAR_DECL
5528 || TREE_CODE (decl) == PARM_DECL)
5529 && DECL_RTL (decl) != 0
5530 && (GET_CODE (DECL_RTL (decl)) == REG
5531 || (GET_CODE (DECL_RTL (decl)) == MEM
5532 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5533 && (
5534 /* If longjmp doesn't restore the registers,
5535 don't put anything in them. */
5536 #ifdef NON_SAVING_SETJMP
5537 NON_SAVING_SETJMP
5539 #endif
5540 ! DECL_REGISTER (decl)))
5541 put_var_into_stack (decl);
5544 /* Return the context-pointer register corresponding to DECL,
5545 or 0 if it does not need one. */
5548 lookup_static_chain (decl)
5549 tree decl;
5551 tree context = decl_function_context (decl);
5552 tree link;
5554 if (context == 0
5555 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_NO_STATIC_CHAIN (decl)))
5556 return 0;
5558 /* We treat inline_function_decl as an alias for the current function
5559 because that is the inline function whose vars, types, etc.
5560 are being merged into the current function.
5561 See expand_inline_function. */
5562 if (context == current_function_decl || context == inline_function_decl)
5563 return virtual_stack_vars_rtx;
5565 for (link = context_display; link; link = TREE_CHAIN (link))
5566 if (TREE_PURPOSE (link) == context)
5567 return RTL_EXPR_RTL (TREE_VALUE (link));
5569 abort ();
5572 /* Convert a stack slot address ADDR for variable VAR
5573 (from a containing function)
5574 into an address valid in this function (using a static chain). */
5577 fix_lexical_addr (addr, var)
5578 rtx addr;
5579 tree var;
5581 rtx basereg;
5582 HOST_WIDE_INT displacement;
5583 tree context = decl_function_context (var);
5584 struct function *fp;
5585 rtx base = 0;
5587 /* If this is the present function, we need not do anything. */
5588 if (context == current_function_decl || context == inline_function_decl)
5589 return addr;
5591 fp = find_function_data (context);
5593 if (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == MEM)
5594 addr = XEXP (XEXP (addr, 0), 0);
5596 /* Decode given address as base reg plus displacement. */
5597 if (GET_CODE (addr) == REG)
5598 basereg = addr, displacement = 0;
5599 else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
5600 basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
5601 else
5602 abort ();
5604 /* We accept vars reached via the containing function's
5605 incoming arg pointer and via its stack variables pointer. */
5606 if (basereg == fp->internal_arg_pointer)
5608 /* If reached via arg pointer, get the arg pointer value
5609 out of that function's stack frame.
5611 There are two cases: If a separate ap is needed, allocate a
5612 slot in the outer function for it and dereference it that way.
5613 This is correct even if the real ap is actually a pseudo.
5614 Otherwise, just adjust the offset from the frame pointer to
5615 compensate. */
5617 #ifdef NEED_SEPARATE_AP
5618 rtx addr;
5620 addr = get_arg_pointer_save_area (fp);
5621 addr = fix_lexical_addr (XEXP (addr, 0), var);
5622 addr = memory_address (Pmode, addr);
5624 base = gen_rtx_MEM (Pmode, addr);
5625 set_mem_alias_set (base, get_frame_alias_set ());
5626 base = copy_to_reg (base);
5627 #else
5628 displacement += (FIRST_PARM_OFFSET (context) - STARTING_FRAME_OFFSET);
5629 base = lookup_static_chain (var);
5630 #endif
5633 else if (basereg == virtual_stack_vars_rtx)
5635 /* This is the same code as lookup_static_chain, duplicated here to
5636 avoid an extra call to decl_function_context. */
5637 tree link;
5639 for (link = context_display; link; link = TREE_CHAIN (link))
5640 if (TREE_PURPOSE (link) == context)
5642 base = RTL_EXPR_RTL (TREE_VALUE (link));
5643 break;
5647 if (base == 0)
5648 abort ();
5650 /* Use same offset, relative to appropriate static chain or argument
5651 pointer. */
5652 return plus_constant (base, displacement);
5655 /* Return the address of the trampoline for entering nested fn FUNCTION.
5656 If necessary, allocate a trampoline (in the stack frame)
5657 and emit rtl to initialize its contents (at entry to this function). */
5660 trampoline_address (function)
5661 tree function;
5663 tree link;
5664 tree rtlexp;
5665 rtx tramp;
5666 struct function *fp;
5667 tree fn_context;
5669 /* Find an existing trampoline and return it. */
5670 for (link = trampoline_list; link; link = TREE_CHAIN (link))
5671 if (TREE_PURPOSE (link) == function)
5672 return
5673 adjust_trampoline_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0));
5675 for (fp = outer_function_chain; fp; fp = fp->outer)
5676 for (link = fp->x_trampoline_list; link; link = TREE_CHAIN (link))
5677 if (TREE_PURPOSE (link) == function)
5679 tramp = fix_lexical_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0),
5680 function);
5681 return adjust_trampoline_addr (tramp);
5684 /* None exists; we must make one. */
5686 /* Find the `struct function' for the function containing FUNCTION. */
5687 fp = 0;
5688 fn_context = decl_function_context (function);
5689 if (fn_context != current_function_decl
5690 && fn_context != inline_function_decl)
5691 fp = find_function_data (fn_context);
5693 /* Allocate run-time space for this trampoline
5694 (usually in the defining function's stack frame). */
5695 #ifdef ALLOCATE_TRAMPOLINE
5696 tramp = ALLOCATE_TRAMPOLINE (fp);
5697 #else
5698 /* If rounding needed, allocate extra space
5699 to ensure we have TRAMPOLINE_SIZE bytes left after rounding up. */
5700 #ifdef TRAMPOLINE_ALIGNMENT
5701 #define TRAMPOLINE_REAL_SIZE \
5702 (TRAMPOLINE_SIZE + (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT) - 1)
5703 #else
5704 #define TRAMPOLINE_REAL_SIZE (TRAMPOLINE_SIZE)
5705 #endif
5706 tramp = assign_stack_local_1 (BLKmode, TRAMPOLINE_REAL_SIZE, 0,
5707 fp ? fp : cfun);
5708 #endif
5710 /* Record the trampoline for reuse and note it for later initialization
5711 by expand_function_end. */
5712 if (fp != 0)
5714 rtlexp = make_node (RTL_EXPR);
5715 RTL_EXPR_RTL (rtlexp) = tramp;
5716 fp->x_trampoline_list = tree_cons (function, rtlexp,
5717 fp->x_trampoline_list);
5719 else
5721 /* Make the RTL_EXPR node temporary, not momentary, so that the
5722 trampoline_list doesn't become garbage. */
5723 rtlexp = make_node (RTL_EXPR);
5725 RTL_EXPR_RTL (rtlexp) = tramp;
5726 trampoline_list = tree_cons (function, rtlexp, trampoline_list);
5729 tramp = fix_lexical_addr (XEXP (tramp, 0), function);
5730 return adjust_trampoline_addr (tramp);
5733 /* Given a trampoline address,
5734 round it to multiple of TRAMPOLINE_ALIGNMENT. */
5736 static rtx
5737 round_trampoline_addr (tramp)
5738 rtx tramp;
5740 #ifdef TRAMPOLINE_ALIGNMENT
5741 /* Round address up to desired boundary. */
5742 rtx temp = gen_reg_rtx (Pmode);
5743 rtx addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5744 rtx mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5746 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5747 temp, 0, OPTAB_LIB_WIDEN);
5748 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5749 temp, 0, OPTAB_LIB_WIDEN);
5750 #endif
5751 return tramp;
5754 /* Given a trampoline address, round it then apply any
5755 platform-specific adjustments so that the result can be used for a
5756 function call . */
5758 static rtx
5759 adjust_trampoline_addr (tramp)
5760 rtx tramp;
5762 tramp = round_trampoline_addr (tramp);
5763 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5764 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5765 #endif
5766 return tramp;
5769 /* Put all this function's BLOCK nodes including those that are chained
5770 onto the first block into a vector, and return it.
5771 Also store in each NOTE for the beginning or end of a block
5772 the index of that block in the vector.
5773 The arguments are BLOCK, the chain of top-level blocks of the function,
5774 and INSNS, the insn chain of the function. */
5776 void
5777 identify_blocks ()
5779 int n_blocks;
5780 tree *block_vector, *last_block_vector;
5781 tree *block_stack;
5782 tree block = DECL_INITIAL (current_function_decl);
5784 if (block == 0)
5785 return;
5787 /* Fill the BLOCK_VECTOR with all of the BLOCKs in this function, in
5788 depth-first order. */
5789 block_vector = get_block_vector (block, &n_blocks);
5790 block_stack = (tree *) xmalloc (n_blocks * sizeof (tree));
5792 last_block_vector = identify_blocks_1 (get_insns (),
5793 block_vector + 1,
5794 block_vector + n_blocks,
5795 block_stack);
5797 /* If we didn't use all of the subblocks, we've misplaced block notes. */
5798 /* ??? This appears to happen all the time. Latent bugs elsewhere? */
5799 if (0 && last_block_vector != block_vector + n_blocks)
5800 abort ();
5802 free (block_vector);
5803 free (block_stack);
5806 /* Subroutine of identify_blocks. Do the block substitution on the
5807 insn chain beginning with INSNS. Recurse for CALL_PLACEHOLDER chains.
5809 BLOCK_STACK is pushed and popped for each BLOCK_BEGIN/BLOCK_END pair.
5810 BLOCK_VECTOR is incremented for each block seen. */
5812 static tree *
5813 identify_blocks_1 (insns, block_vector, end_block_vector, orig_block_stack)
5814 rtx insns;
5815 tree *block_vector;
5816 tree *end_block_vector;
5817 tree *orig_block_stack;
5819 rtx insn;
5820 tree *block_stack = orig_block_stack;
5822 for (insn = insns; insn; insn = NEXT_INSN (insn))
5824 if (GET_CODE (insn) == NOTE)
5826 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5828 tree b;
5830 /* If there are more block notes than BLOCKs, something
5831 is badly wrong. */
5832 if (block_vector == end_block_vector)
5833 abort ();
5835 b = *block_vector++;
5836 NOTE_BLOCK (insn) = b;
5837 *block_stack++ = b;
5839 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5841 /* If there are more NOTE_INSN_BLOCK_ENDs than
5842 NOTE_INSN_BLOCK_BEGs, something is badly wrong. */
5843 if (block_stack == orig_block_stack)
5844 abort ();
5846 NOTE_BLOCK (insn) = *--block_stack;
5849 else if (GET_CODE (insn) == CALL_INSN
5850 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5852 rtx cp = PATTERN (insn);
5854 block_vector = identify_blocks_1 (XEXP (cp, 0), block_vector,
5855 end_block_vector, block_stack);
5856 if (XEXP (cp, 1))
5857 block_vector = identify_blocks_1 (XEXP (cp, 1), block_vector,
5858 end_block_vector, block_stack);
5859 if (XEXP (cp, 2))
5860 block_vector = identify_blocks_1 (XEXP (cp, 2), block_vector,
5861 end_block_vector, block_stack);
5865 /* If there are more NOTE_INSN_BLOCK_BEGINs than NOTE_INSN_BLOCK_ENDs,
5866 something is badly wrong. */
5867 if (block_stack != orig_block_stack)
5868 abort ();
5870 return block_vector;
5873 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
5874 and create duplicate blocks. */
5875 /* ??? Need an option to either create block fragments or to create
5876 abstract origin duplicates of a source block. It really depends
5877 on what optimization has been performed. */
5879 void
5880 reorder_blocks ()
5882 tree block = DECL_INITIAL (current_function_decl);
5883 varray_type block_stack;
5885 if (block == NULL_TREE)
5886 return;
5888 VARRAY_TREE_INIT (block_stack, 10, "block_stack");
5890 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
5891 reorder_blocks_0 (block);
5893 /* Prune the old trees away, so that they don't get in the way. */
5894 BLOCK_SUBBLOCKS (block) = NULL_TREE;
5895 BLOCK_CHAIN (block) = NULL_TREE;
5897 /* Recreate the block tree from the note nesting. */
5898 reorder_blocks_1 (get_insns (), block, &block_stack);
5899 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
5901 /* Remove deleted blocks from the block fragment chains. */
5902 reorder_fix_fragments (block);
5905 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
5907 static void
5908 reorder_blocks_0 (block)
5909 tree block;
5911 while (block)
5913 TREE_ASM_WRITTEN (block) = 0;
5914 reorder_blocks_0 (BLOCK_SUBBLOCKS (block));
5915 block = BLOCK_CHAIN (block);
5919 static void
5920 reorder_blocks_1 (insns, current_block, p_block_stack)
5921 rtx insns;
5922 tree current_block;
5923 varray_type *p_block_stack;
5925 rtx insn;
5927 for (insn = insns; insn; insn = NEXT_INSN (insn))
5929 if (GET_CODE (insn) == NOTE)
5931 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5933 tree block = NOTE_BLOCK (insn);
5935 /* If we have seen this block before, that means it now
5936 spans multiple address regions. Create a new fragment. */
5937 if (TREE_ASM_WRITTEN (block))
5939 tree new_block = copy_node (block);
5940 tree origin;
5942 origin = (BLOCK_FRAGMENT_ORIGIN (block)
5943 ? BLOCK_FRAGMENT_ORIGIN (block)
5944 : block);
5945 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
5946 BLOCK_FRAGMENT_CHAIN (new_block)
5947 = BLOCK_FRAGMENT_CHAIN (origin);
5948 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
5950 NOTE_BLOCK (insn) = new_block;
5951 block = new_block;
5954 BLOCK_SUBBLOCKS (block) = 0;
5955 TREE_ASM_WRITTEN (block) = 1;
5956 BLOCK_SUPERCONTEXT (block) = current_block;
5957 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
5958 BLOCK_SUBBLOCKS (current_block) = block;
5959 current_block = block;
5960 VARRAY_PUSH_TREE (*p_block_stack, block);
5962 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5964 NOTE_BLOCK (insn) = VARRAY_TOP_TREE (*p_block_stack);
5965 VARRAY_POP (*p_block_stack);
5966 BLOCK_SUBBLOCKS (current_block)
5967 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
5968 current_block = BLOCK_SUPERCONTEXT (current_block);
5971 else if (GET_CODE (insn) == CALL_INSN
5972 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5974 rtx cp = PATTERN (insn);
5975 reorder_blocks_1 (XEXP (cp, 0), current_block, p_block_stack);
5976 if (XEXP (cp, 1))
5977 reorder_blocks_1 (XEXP (cp, 1), current_block, p_block_stack);
5978 if (XEXP (cp, 2))
5979 reorder_blocks_1 (XEXP (cp, 2), current_block, p_block_stack);
5984 /* Rationalize BLOCK_FRAGMENT_ORIGIN. If an origin block no longer
5985 appears in the block tree, select one of the fragments to become
5986 the new origin block. */
5988 static void
5989 reorder_fix_fragments (block)
5990 tree block;
5992 while (block)
5994 tree dup_origin = BLOCK_FRAGMENT_ORIGIN (block);
5995 tree new_origin = NULL_TREE;
5997 if (dup_origin)
5999 if (! TREE_ASM_WRITTEN (dup_origin))
6001 new_origin = BLOCK_FRAGMENT_CHAIN (dup_origin);
6003 /* Find the first of the remaining fragments. There must
6004 be at least one -- the current block. */
6005 while (! TREE_ASM_WRITTEN (new_origin))
6006 new_origin = BLOCK_FRAGMENT_CHAIN (new_origin);
6007 BLOCK_FRAGMENT_ORIGIN (new_origin) = NULL_TREE;
6010 else if (! dup_origin)
6011 new_origin = block;
6013 /* Re-root the rest of the fragments to the new origin. In the
6014 case that DUP_ORIGIN was null, that means BLOCK was the origin
6015 of a chain of fragments and we want to remove those fragments
6016 that didn't make it to the output. */
6017 if (new_origin)
6019 tree *pp = &BLOCK_FRAGMENT_CHAIN (new_origin);
6020 tree chain = *pp;
6022 while (chain)
6024 if (TREE_ASM_WRITTEN (chain))
6026 BLOCK_FRAGMENT_ORIGIN (chain) = new_origin;
6027 *pp = chain;
6028 pp = &BLOCK_FRAGMENT_CHAIN (chain);
6030 chain = BLOCK_FRAGMENT_CHAIN (chain);
6032 *pp = NULL_TREE;
6035 reorder_fix_fragments (BLOCK_SUBBLOCKS (block));
6036 block = BLOCK_CHAIN (block);
6040 /* Reverse the order of elements in the chain T of blocks,
6041 and return the new head of the chain (old last element). */
6043 static tree
6044 blocks_nreverse (t)
6045 tree t;
6047 tree prev = 0, decl, next;
6048 for (decl = t; decl; decl = next)
6050 next = BLOCK_CHAIN (decl);
6051 BLOCK_CHAIN (decl) = prev;
6052 prev = decl;
6054 return prev;
6057 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
6058 non-NULL, list them all into VECTOR, in a depth-first preorder
6059 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
6060 blocks. */
6062 static int
6063 all_blocks (block, vector)
6064 tree block;
6065 tree *vector;
6067 int n_blocks = 0;
6069 while (block)
6071 TREE_ASM_WRITTEN (block) = 0;
6073 /* Record this block. */
6074 if (vector)
6075 vector[n_blocks] = block;
6077 ++n_blocks;
6079 /* Record the subblocks, and their subblocks... */
6080 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
6081 vector ? vector + n_blocks : 0);
6082 block = BLOCK_CHAIN (block);
6085 return n_blocks;
6088 /* Return a vector containing all the blocks rooted at BLOCK. The
6089 number of elements in the vector is stored in N_BLOCKS_P. The
6090 vector is dynamically allocated; it is the caller's responsibility
6091 to call `free' on the pointer returned. */
6093 static tree *
6094 get_block_vector (block, n_blocks_p)
6095 tree block;
6096 int *n_blocks_p;
6098 tree *block_vector;
6100 *n_blocks_p = all_blocks (block, NULL);
6101 block_vector = (tree *) xmalloc (*n_blocks_p * sizeof (tree));
6102 all_blocks (block, block_vector);
6104 return block_vector;
6107 static int next_block_index = 2;
6109 /* Set BLOCK_NUMBER for all the blocks in FN. */
6111 void
6112 number_blocks (fn)
6113 tree fn;
6115 int i;
6116 int n_blocks;
6117 tree *block_vector;
6119 /* For SDB and XCOFF debugging output, we start numbering the blocks
6120 from 1 within each function, rather than keeping a running
6121 count. */
6122 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
6123 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
6124 next_block_index = 1;
6125 #endif
6127 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
6129 /* The top-level BLOCK isn't numbered at all. */
6130 for (i = 1; i < n_blocks; ++i)
6131 /* We number the blocks from two. */
6132 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
6134 free (block_vector);
6136 return;
6139 /* If VAR is present in a subblock of BLOCK, return the subblock. */
6141 tree
6142 debug_find_var_in_block_tree (var, block)
6143 tree var;
6144 tree block;
6146 tree t;
6148 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
6149 if (t == var)
6150 return block;
6152 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
6154 tree ret = debug_find_var_in_block_tree (var, t);
6155 if (ret)
6156 return ret;
6159 return NULL_TREE;
6162 /* Allocate a function structure and reset its contents to the defaults. */
6164 static void
6165 prepare_function_start ()
6167 cfun = (struct function *) ggc_alloc_cleared (sizeof (struct function));
6169 init_stmt_for_function ();
6170 init_eh_for_function ();
6172 cse_not_expected = ! optimize;
6174 /* Caller save not needed yet. */
6175 caller_save_needed = 0;
6177 /* No stack slots have been made yet. */
6178 stack_slot_list = 0;
6180 current_function_has_nonlocal_label = 0;
6181 current_function_has_nonlocal_goto = 0;
6183 /* There is no stack slot for handling nonlocal gotos. */
6184 nonlocal_goto_handler_slots = 0;
6185 nonlocal_goto_stack_level = 0;
6187 /* No labels have been declared for nonlocal use. */
6188 nonlocal_labels = 0;
6189 nonlocal_goto_handler_labels = 0;
6191 /* No function calls so far in this function. */
6192 function_call_count = 0;
6194 /* No parm regs have been allocated.
6195 (This is important for output_inline_function.) */
6196 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
6198 /* Initialize the RTL mechanism. */
6199 init_emit ();
6201 /* Initialize the queue of pending postincrement and postdecrements,
6202 and some other info in expr.c. */
6203 init_expr ();
6205 /* We haven't done register allocation yet. */
6206 reg_renumber = 0;
6208 init_varasm_status (cfun);
6210 /* Clear out data used for inlining. */
6211 cfun->inlinable = 0;
6212 cfun->original_decl_initial = 0;
6213 cfun->original_arg_vector = 0;
6215 cfun->stack_alignment_needed = STACK_BOUNDARY;
6216 cfun->preferred_stack_boundary = STACK_BOUNDARY;
6218 /* Set if a call to setjmp is seen. */
6219 current_function_calls_setjmp = 0;
6221 /* Set if a call to longjmp is seen. */
6222 current_function_calls_longjmp = 0;
6224 current_function_calls_alloca = 0;
6225 current_function_contains_functions = 0;
6226 current_function_is_leaf = 0;
6227 current_function_nothrow = 0;
6228 current_function_sp_is_unchanging = 0;
6229 current_function_uses_only_leaf_regs = 0;
6230 current_function_has_computed_jump = 0;
6231 current_function_is_thunk = 0;
6233 current_function_returns_pcc_struct = 0;
6234 current_function_returns_struct = 0;
6235 current_function_epilogue_delay_list = 0;
6236 current_function_uses_const_pool = 0;
6237 current_function_uses_pic_offset_table = 0;
6238 current_function_cannot_inline = 0;
6240 /* We have not yet needed to make a label to jump to for tail-recursion. */
6241 tail_recursion_label = 0;
6243 /* We haven't had a need to make a save area for ap yet. */
6244 arg_pointer_save_area = 0;
6246 /* No stack slots allocated yet. */
6247 frame_offset = 0;
6249 /* No SAVE_EXPRs in this function yet. */
6250 save_expr_regs = 0;
6252 /* No RTL_EXPRs in this function yet. */
6253 rtl_expr_chain = 0;
6255 /* Set up to allocate temporaries. */
6256 init_temp_slots ();
6258 /* Indicate that we need to distinguish between the return value of the
6259 present function and the return value of a function being called. */
6260 rtx_equal_function_value_matters = 1;
6262 /* Indicate that we have not instantiated virtual registers yet. */
6263 virtuals_instantiated = 0;
6265 /* Indicate that we want CONCATs now. */
6266 generating_concat_p = 1;
6268 /* Indicate we have no need of a frame pointer yet. */
6269 frame_pointer_needed = 0;
6271 /* By default assume not stdarg. */
6272 current_function_stdarg = 0;
6274 /* We haven't made any trampolines for this function yet. */
6275 trampoline_list = 0;
6277 init_pending_stack_adjust ();
6278 inhibit_defer_pop = 0;
6280 current_function_outgoing_args_size = 0;
6282 current_function_funcdef_no = funcdef_no++;
6284 cfun->arc_profile = profile_arc_flag || flag_test_coverage;
6286 cfun->arc_profile = profile_arc_flag || flag_test_coverage;
6288 cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
6290 (*lang_hooks.function.init) (cfun);
6291 if (init_machine_status)
6292 cfun->machine = (*init_machine_status) ();
6295 /* Initialize the rtl expansion mechanism so that we can do simple things
6296 like generate sequences. This is used to provide a context during global
6297 initialization of some passes. */
6298 void
6299 init_dummy_function_start ()
6301 prepare_function_start ();
6304 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
6305 and initialize static variables for generating RTL for the statements
6306 of the function. */
6308 void
6309 init_function_start (subr, filename, line)
6310 tree subr;
6311 const char *filename;
6312 int line;
6314 prepare_function_start ();
6316 current_function_name = (*lang_hooks.decl_printable_name) (subr, 2);
6317 cfun->decl = subr;
6319 /* Nonzero if this is a nested function that uses a static chain. */
6321 current_function_needs_context
6322 = (decl_function_context (current_function_decl) != 0
6323 && ! DECL_NO_STATIC_CHAIN (current_function_decl));
6325 /* Within function body, compute a type's size as soon it is laid out. */
6326 immediate_size_expand++;
6328 /* Prevent ever trying to delete the first instruction of a function.
6329 Also tell final how to output a linenum before the function prologue.
6330 Note linenums could be missing, e.g. when compiling a Java .class file. */
6331 if (line > 0)
6332 emit_line_note (filename, line);
6334 /* Make sure first insn is a note even if we don't want linenums.
6335 This makes sure the first insn will never be deleted.
6336 Also, final expects a note to appear there. */
6337 emit_note (NULL, NOTE_INSN_DELETED);
6339 /* Set flags used by final.c. */
6340 if (aggregate_value_p (DECL_RESULT (subr)))
6342 #ifdef PCC_STATIC_STRUCT_RETURN
6343 current_function_returns_pcc_struct = 1;
6344 #endif
6345 current_function_returns_struct = 1;
6348 /* Warn if this value is an aggregate type,
6349 regardless of which calling convention we are using for it. */
6350 if (warn_aggregate_return
6351 && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
6352 warning ("function returns an aggregate");
6354 current_function_returns_pointer
6355 = POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
6358 /* Make sure all values used by the optimization passes have sane
6359 defaults. */
6360 void
6361 init_function_for_compilation ()
6363 reg_renumber = 0;
6365 /* No prologue/epilogue insns yet. */
6366 VARRAY_GROW (prologue, 0);
6367 VARRAY_GROW (epilogue, 0);
6368 VARRAY_GROW (sibcall_epilogue, 0);
6371 /* Expand a call to __main at the beginning of a possible main function. */
6373 #if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
6374 #undef HAS_INIT_SECTION
6375 #define HAS_INIT_SECTION
6376 #endif
6378 void
6379 expand_main_function ()
6381 #ifdef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
6382 if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN)
6384 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
6385 rtx tmp, seq;
6387 start_sequence ();
6388 /* Forcibly align the stack. */
6389 #ifdef STACK_GROWS_DOWNWARD
6390 tmp = expand_simple_binop (Pmode, AND, stack_pointer_rtx, GEN_INT(-align),
6391 stack_pointer_rtx, 1, OPTAB_WIDEN);
6392 #else
6393 tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
6394 GEN_INT (align - 1), NULL_RTX, 1, OPTAB_WIDEN);
6395 tmp = expand_simple_binop (Pmode, AND, tmp, GEN_INT (-align),
6396 stack_pointer_rtx, 1, OPTAB_WIDEN);
6397 #endif
6398 if (tmp != stack_pointer_rtx)
6399 emit_move_insn (stack_pointer_rtx, tmp);
6401 /* Enlist allocate_dynamic_stack_space to pick up the pieces. */
6402 tmp = force_reg (Pmode, const0_rtx);
6403 allocate_dynamic_stack_space (tmp, NULL_RTX, BIGGEST_ALIGNMENT);
6404 seq = get_insns ();
6405 end_sequence ();
6407 for (tmp = get_last_insn (); tmp; tmp = PREV_INSN (tmp))
6408 if (NOTE_P (tmp) && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_FUNCTION_BEG)
6409 break;
6410 if (tmp)
6411 emit_insn_before (seq, tmp);
6412 else
6413 emit_insn (seq);
6415 #endif
6417 #ifndef HAS_INIT_SECTION
6418 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, NAME__MAIN), LCT_NORMAL,
6419 VOIDmode, 0);
6420 #endif
6423 /* The PENDING_SIZES represent the sizes of variable-sized types.
6424 Create RTL for the various sizes now (using temporary variables),
6425 so that we can refer to the sizes from the RTL we are generating
6426 for the current function. The PENDING_SIZES are a TREE_LIST. The
6427 TREE_VALUE of each node is a SAVE_EXPR. */
6429 void
6430 expand_pending_sizes (pending_sizes)
6431 tree pending_sizes;
6433 tree tem;
6435 /* Evaluate now the sizes of any types declared among the arguments. */
6436 for (tem = pending_sizes; tem; tem = TREE_CHAIN (tem))
6438 expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode, 0);
6439 /* Flush the queue in case this parameter declaration has
6440 side-effects. */
6441 emit_queue ();
6445 /* Start the RTL for a new function, and set variables used for
6446 emitting RTL.
6447 SUBR is the FUNCTION_DECL node.
6448 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
6449 the function's parameters, which must be run at any return statement. */
6451 void
6452 expand_function_start (subr, parms_have_cleanups)
6453 tree subr;
6454 int parms_have_cleanups;
6456 tree tem;
6457 rtx last_ptr = NULL_RTX;
6459 /* Make sure volatile mem refs aren't considered
6460 valid operands of arithmetic insns. */
6461 init_recog_no_volatile ();
6463 current_function_instrument_entry_exit
6464 = (flag_instrument_function_entry_exit
6465 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6467 current_function_profile
6468 = (profile_flag
6469 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6471 current_function_limit_stack
6472 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
6474 /* If function gets a static chain arg, store it in the stack frame.
6475 Do this first, so it gets the first stack slot offset. */
6476 if (current_function_needs_context)
6478 last_ptr = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
6480 /* Delay copying static chain if it is not a register to avoid
6481 conflicts with regs used for parameters. */
6482 if (! SMALL_REGISTER_CLASSES
6483 || GET_CODE (static_chain_incoming_rtx) == REG)
6484 emit_move_insn (last_ptr, static_chain_incoming_rtx);
6487 /* If the parameters of this function need cleaning up, get a label
6488 for the beginning of the code which executes those cleanups. This must
6489 be done before doing anything with return_label. */
6490 if (parms_have_cleanups)
6491 cleanup_label = gen_label_rtx ();
6492 else
6493 cleanup_label = 0;
6495 /* Make the label for return statements to jump to. Do not special
6496 case machines with special return instructions -- they will be
6497 handled later during jump, ifcvt, or epilogue creation. */
6498 return_label = gen_label_rtx ();
6500 /* Initialize rtx used to return the value. */
6501 /* Do this before assign_parms so that we copy the struct value address
6502 before any library calls that assign parms might generate. */
6504 /* Decide whether to return the value in memory or in a register. */
6505 if (aggregate_value_p (DECL_RESULT (subr)))
6507 /* Returning something that won't go in a register. */
6508 rtx value_address = 0;
6510 #ifdef PCC_STATIC_STRUCT_RETURN
6511 if (current_function_returns_pcc_struct)
6513 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
6514 value_address = assemble_static_space (size);
6516 else
6517 #endif
6519 /* Expect to be passed the address of a place to store the value.
6520 If it is passed as an argument, assign_parms will take care of
6521 it. */
6522 if (struct_value_incoming_rtx)
6524 value_address = gen_reg_rtx (Pmode);
6525 emit_move_insn (value_address, struct_value_incoming_rtx);
6528 if (value_address)
6530 rtx x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), value_address);
6531 set_mem_attributes (x, DECL_RESULT (subr), 1);
6532 SET_DECL_RTL (DECL_RESULT (subr), x);
6535 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
6536 /* If return mode is void, this decl rtl should not be used. */
6537 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
6538 else
6540 /* Compute the return values into a pseudo reg, which we will copy
6541 into the true return register after the cleanups are done. */
6543 /* In order to figure out what mode to use for the pseudo, we
6544 figure out what the mode of the eventual return register will
6545 actually be, and use that. */
6546 rtx hard_reg
6547 = hard_function_value (TREE_TYPE (DECL_RESULT (subr)),
6548 subr, 1);
6550 /* Structures that are returned in registers are not aggregate_value_p,
6551 so we may see a PARALLEL. Don't play pseudo games with this. */
6552 if (! REG_P (hard_reg))
6553 SET_DECL_RTL (DECL_RESULT (subr), hard_reg);
6554 else
6556 /* Create the pseudo. */
6557 SET_DECL_RTL (DECL_RESULT (subr), gen_reg_rtx (GET_MODE (hard_reg)));
6559 /* Needed because we may need to move this to memory
6560 in case it's a named return value whose address is taken. */
6561 DECL_REGISTER (DECL_RESULT (subr)) = 1;
6565 /* Initialize rtx for parameters and local variables.
6566 In some cases this requires emitting insns. */
6568 assign_parms (subr);
6570 /* Copy the static chain now if it wasn't a register. The delay is to
6571 avoid conflicts with the parameter passing registers. */
6573 if (SMALL_REGISTER_CLASSES && current_function_needs_context)
6574 if (GET_CODE (static_chain_incoming_rtx) != REG)
6575 emit_move_insn (last_ptr, static_chain_incoming_rtx);
6577 /* The following was moved from init_function_start.
6578 The move is supposed to make sdb output more accurate. */
6579 /* Indicate the beginning of the function body,
6580 as opposed to parm setup. */
6581 emit_note (NULL, NOTE_INSN_FUNCTION_BEG);
6583 if (GET_CODE (get_last_insn ()) != NOTE)
6584 emit_note (NULL, NOTE_INSN_DELETED);
6585 parm_birth_insn = get_last_insn ();
6587 context_display = 0;
6588 if (current_function_needs_context)
6590 /* Fetch static chain values for containing functions. */
6591 tem = decl_function_context (current_function_decl);
6592 /* Copy the static chain pointer into a pseudo. If we have
6593 small register classes, copy the value from memory if
6594 static_chain_incoming_rtx is a REG. */
6595 if (tem)
6597 /* If the static chain originally came in a register, put it back
6598 there, then move it out in the next insn. The reason for
6599 this peculiar code is to satisfy function integration. */
6600 if (SMALL_REGISTER_CLASSES
6601 && GET_CODE (static_chain_incoming_rtx) == REG)
6602 emit_move_insn (static_chain_incoming_rtx, last_ptr);
6603 last_ptr = copy_to_reg (static_chain_incoming_rtx);
6606 while (tem)
6608 tree rtlexp = make_node (RTL_EXPR);
6610 RTL_EXPR_RTL (rtlexp) = last_ptr;
6611 context_display = tree_cons (tem, rtlexp, context_display);
6612 tem = decl_function_context (tem);
6613 if (tem == 0)
6614 break;
6615 /* Chain thru stack frames, assuming pointer to next lexical frame
6616 is found at the place we always store it. */
6617 #ifdef FRAME_GROWS_DOWNWARD
6618 last_ptr = plus_constant (last_ptr,
6619 -(HOST_WIDE_INT) GET_MODE_SIZE (Pmode));
6620 #endif
6621 last_ptr = gen_rtx_MEM (Pmode, memory_address (Pmode, last_ptr));
6622 set_mem_alias_set (last_ptr, get_frame_alias_set ());
6623 last_ptr = copy_to_reg (last_ptr);
6625 /* If we are not optimizing, ensure that we know that this
6626 piece of context is live over the entire function. */
6627 if (! optimize)
6628 save_expr_regs = gen_rtx_EXPR_LIST (VOIDmode, last_ptr,
6629 save_expr_regs);
6633 if (current_function_instrument_entry_exit)
6635 rtx fun = DECL_RTL (current_function_decl);
6636 if (GET_CODE (fun) == MEM)
6637 fun = XEXP (fun, 0);
6638 else
6639 abort ();
6640 emit_library_call (profile_function_entry_libfunc, LCT_NORMAL, VOIDmode,
6641 2, fun, Pmode,
6642 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6644 hard_frame_pointer_rtx),
6645 Pmode);
6648 if (current_function_profile)
6650 #ifdef PROFILE_HOOK
6651 PROFILE_HOOK (current_function_funcdef_no);
6652 #endif
6655 /* After the display initializations is where the tail-recursion label
6656 should go, if we end up needing one. Ensure we have a NOTE here
6657 since some things (like trampolines) get placed before this. */
6658 tail_recursion_reentry = emit_note (NULL, NOTE_INSN_DELETED);
6660 /* Evaluate now the sizes of any types declared among the arguments. */
6661 expand_pending_sizes (nreverse (get_pending_sizes ()));
6663 /* Make sure there is a line number after the function entry setup code. */
6664 force_next_line_note ();
6667 /* Undo the effects of init_dummy_function_start. */
6668 void
6669 expand_dummy_function_end ()
6671 /* End any sequences that failed to be closed due to syntax errors. */
6672 while (in_sequence_p ())
6673 end_sequence ();
6675 /* Outside function body, can't compute type's actual size
6676 until next function's body starts. */
6678 free_after_parsing (cfun);
6679 free_after_compilation (cfun);
6680 cfun = 0;
6683 /* Call DOIT for each hard register used as a return value from
6684 the current function. */
6686 void
6687 diddle_return_value (doit, arg)
6688 void (*doit) PARAMS ((rtx, void *));
6689 void *arg;
6691 rtx outgoing = current_function_return_rtx;
6693 if (! outgoing)
6694 return;
6696 if (GET_CODE (outgoing) == REG)
6697 (*doit) (outgoing, arg);
6698 else if (GET_CODE (outgoing) == PARALLEL)
6700 int i;
6702 for (i = 0; i < XVECLEN (outgoing, 0); i++)
6704 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
6706 if (GET_CODE (x) == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
6707 (*doit) (x, arg);
6712 static void
6713 do_clobber_return_reg (reg, arg)
6714 rtx reg;
6715 void *arg ATTRIBUTE_UNUSED;
6717 emit_insn (gen_rtx_CLOBBER (VOIDmode, reg));
6720 void
6721 clobber_return_register ()
6723 diddle_return_value (do_clobber_return_reg, NULL);
6725 /* In case we do use pseudo to return value, clobber it too. */
6726 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6728 tree decl_result = DECL_RESULT (current_function_decl);
6729 rtx decl_rtl = DECL_RTL (decl_result);
6730 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
6732 do_clobber_return_reg (decl_rtl, NULL);
6737 static void
6738 do_use_return_reg (reg, arg)
6739 rtx reg;
6740 void *arg ATTRIBUTE_UNUSED;
6742 emit_insn (gen_rtx_USE (VOIDmode, reg));
6745 void
6746 use_return_register ()
6748 diddle_return_value (do_use_return_reg, NULL);
6751 static GTY(()) rtx initial_trampoline;
6753 /* Generate RTL for the end of the current function.
6754 FILENAME and LINE are the current position in the source file.
6756 It is up to language-specific callers to do cleanups for parameters--
6757 or else, supply 1 for END_BINDINGS and we will call expand_end_bindings. */
6759 void
6760 expand_function_end (filename, line, end_bindings)
6761 const char *filename;
6762 int line;
6763 int end_bindings;
6765 tree link;
6766 rtx clobber_after;
6768 finish_expr_for_function ();
6770 /* If arg_pointer_save_area was referenced only from a nested
6771 function, we will not have initialized it yet. Do that now. */
6772 if (arg_pointer_save_area && ! cfun->arg_pointer_save_area_init)
6773 get_arg_pointer_save_area (cfun);
6775 #ifdef NON_SAVING_SETJMP
6776 /* Don't put any variables in registers if we call setjmp
6777 on a machine that fails to restore the registers. */
6778 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
6780 if (DECL_INITIAL (current_function_decl) != error_mark_node)
6781 setjmp_protect (DECL_INITIAL (current_function_decl));
6783 setjmp_protect_args ();
6785 #endif
6787 /* Initialize any trampolines required by this function. */
6788 for (link = trampoline_list; link; link = TREE_CHAIN (link))
6790 tree function = TREE_PURPOSE (link);
6791 rtx context ATTRIBUTE_UNUSED = lookup_static_chain (function);
6792 rtx tramp = RTL_EXPR_RTL (TREE_VALUE (link));
6793 #ifdef TRAMPOLINE_TEMPLATE
6794 rtx blktramp;
6795 #endif
6796 rtx seq;
6798 #ifdef TRAMPOLINE_TEMPLATE
6799 /* First make sure this compilation has a template for
6800 initializing trampolines. */
6801 if (initial_trampoline == 0)
6803 initial_trampoline
6804 = gen_rtx_MEM (BLKmode, assemble_trampoline_template ());
6805 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
6807 #endif
6809 /* Generate insns to initialize the trampoline. */
6810 start_sequence ();
6811 tramp = round_trampoline_addr (XEXP (tramp, 0));
6812 #ifdef TRAMPOLINE_TEMPLATE
6813 blktramp = replace_equiv_address (initial_trampoline, tramp);
6814 emit_block_move (blktramp, initial_trampoline,
6815 GEN_INT (TRAMPOLINE_SIZE));
6816 #endif
6817 INITIALIZE_TRAMPOLINE (tramp, XEXP (DECL_RTL (function), 0), context);
6818 seq = get_insns ();
6819 end_sequence ();
6821 /* Put those insns at entry to the containing function (this one). */
6822 emit_insn_before (seq, tail_recursion_reentry);
6825 /* If we are doing stack checking and this function makes calls,
6826 do a stack probe at the start of the function to ensure we have enough
6827 space for another stack frame. */
6828 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
6830 rtx insn, seq;
6832 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6833 if (GET_CODE (insn) == CALL_INSN)
6835 start_sequence ();
6836 probe_stack_range (STACK_CHECK_PROTECT,
6837 GEN_INT (STACK_CHECK_MAX_FRAME_SIZE));
6838 seq = get_insns ();
6839 end_sequence ();
6840 emit_insn_before (seq, tail_recursion_reentry);
6841 break;
6845 /* Warn about unused parms if extra warnings were specified. */
6846 /* Either ``-W -Wunused'' or ``-Wunused-parameter'' enables this
6847 warning. WARN_UNUSED_PARAMETER is negative when set by
6848 -Wunused. */
6849 if (warn_unused_parameter > 0
6850 || (warn_unused_parameter < 0 && extra_warnings))
6852 tree decl;
6854 for (decl = DECL_ARGUMENTS (current_function_decl);
6855 decl; decl = TREE_CHAIN (decl))
6856 if (! TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
6857 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
6858 warning_with_decl (decl, "unused parameter `%s'");
6861 /* Delete handlers for nonlocal gotos if nothing uses them. */
6862 if (nonlocal_goto_handler_slots != 0
6863 && ! current_function_has_nonlocal_label)
6864 delete_handlers ();
6866 /* End any sequences that failed to be closed due to syntax errors. */
6867 while (in_sequence_p ())
6868 end_sequence ();
6870 /* Outside function body, can't compute type's actual size
6871 until next function's body starts. */
6872 immediate_size_expand--;
6874 clear_pending_stack_adjust ();
6875 do_pending_stack_adjust ();
6877 /* Mark the end of the function body.
6878 If control reaches this insn, the function can drop through
6879 without returning a value. */
6880 emit_note (NULL, NOTE_INSN_FUNCTION_END);
6882 /* Must mark the last line number note in the function, so that the test
6883 coverage code can avoid counting the last line twice. This just tells
6884 the code to ignore the immediately following line note, since there
6885 already exists a copy of this note somewhere above. This line number
6886 note is still needed for debugging though, so we can't delete it. */
6887 if (flag_test_coverage)
6888 emit_note (NULL, NOTE_INSN_REPEATED_LINE_NUMBER);
6890 /* Output a linenumber for the end of the function.
6891 SDB depends on this. */
6892 emit_line_note_force (filename, line);
6894 /* Before the return label (if any), clobber the return
6895 registers so that they are not propagated live to the rest of
6896 the function. This can only happen with functions that drop
6897 through; if there had been a return statement, there would
6898 have either been a return rtx, or a jump to the return label.
6900 We delay actual code generation after the current_function_value_rtx
6901 is computed. */
6902 clobber_after = get_last_insn ();
6904 /* Output the label for the actual return from the function,
6905 if one is expected. This happens either because a function epilogue
6906 is used instead of a return instruction, or because a return was done
6907 with a goto in order to run local cleanups, or because of pcc-style
6908 structure returning. */
6909 if (return_label)
6910 emit_label (return_label);
6912 /* C++ uses this. */
6913 if (end_bindings)
6914 expand_end_bindings (0, 0, 0);
6916 if (current_function_instrument_entry_exit)
6918 rtx fun = DECL_RTL (current_function_decl);
6919 if (GET_CODE (fun) == MEM)
6920 fun = XEXP (fun, 0);
6921 else
6922 abort ();
6923 emit_library_call (profile_function_exit_libfunc, LCT_NORMAL, VOIDmode,
6924 2, fun, Pmode,
6925 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6927 hard_frame_pointer_rtx),
6928 Pmode);
6931 /* Let except.c know where it should emit the call to unregister
6932 the function context for sjlj exceptions. */
6933 if (flag_exceptions && USING_SJLJ_EXCEPTIONS)
6934 sjlj_emit_function_exit_after (get_last_insn ());
6936 /* If we had calls to alloca, and this machine needs
6937 an accurate stack pointer to exit the function,
6938 insert some code to save and restore the stack pointer. */
6939 #ifdef EXIT_IGNORE_STACK
6940 if (! EXIT_IGNORE_STACK)
6941 #endif
6942 if (current_function_calls_alloca)
6944 rtx tem = 0;
6946 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
6947 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
6950 /* If scalar return value was computed in a pseudo-reg, or was a named
6951 return value that got dumped to the stack, copy that to the hard
6952 return register. */
6953 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6955 tree decl_result = DECL_RESULT (current_function_decl);
6956 rtx decl_rtl = DECL_RTL (decl_result);
6958 if (REG_P (decl_rtl)
6959 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
6960 : DECL_REGISTER (decl_result))
6962 rtx real_decl_rtl = current_function_return_rtx;
6964 /* This should be set in assign_parms. */
6965 if (! REG_FUNCTION_VALUE_P (real_decl_rtl))
6966 abort ();
6968 /* If this is a BLKmode structure being returned in registers,
6969 then use the mode computed in expand_return. Note that if
6970 decl_rtl is memory, then its mode may have been changed,
6971 but that current_function_return_rtx has not. */
6972 if (GET_MODE (real_decl_rtl) == BLKmode)
6973 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
6975 /* If a named return value dumped decl_return to memory, then
6976 we may need to re-do the PROMOTE_MODE signed/unsigned
6977 extension. */
6978 if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
6980 int unsignedp = TREE_UNSIGNED (TREE_TYPE (decl_result));
6982 #ifdef PROMOTE_FUNCTION_RETURN
6983 promote_mode (TREE_TYPE (decl_result), GET_MODE (decl_rtl),
6984 &unsignedp, 1);
6985 #endif
6987 convert_move (real_decl_rtl, decl_rtl, unsignedp);
6989 else if (GET_CODE (real_decl_rtl) == PARALLEL)
6990 emit_group_load (real_decl_rtl, decl_rtl,
6991 int_size_in_bytes (TREE_TYPE (decl_result)));
6992 else
6993 emit_move_insn (real_decl_rtl, decl_rtl);
6997 /* If returning a structure, arrange to return the address of the value
6998 in a place where debuggers expect to find it.
7000 If returning a structure PCC style,
7001 the caller also depends on this value.
7002 And current_function_returns_pcc_struct is not necessarily set. */
7003 if (current_function_returns_struct
7004 || current_function_returns_pcc_struct)
7006 rtx value_address
7007 = XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
7008 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
7009 #ifdef FUNCTION_OUTGOING_VALUE
7010 rtx outgoing
7011 = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
7012 current_function_decl);
7013 #else
7014 rtx outgoing
7015 = FUNCTION_VALUE (build_pointer_type (type), current_function_decl);
7016 #endif
7018 /* Mark this as a function return value so integrate will delete the
7019 assignment and USE below when inlining this function. */
7020 REG_FUNCTION_VALUE_P (outgoing) = 1;
7022 #ifdef POINTERS_EXTEND_UNSIGNED
7023 /* The address may be ptr_mode and OUTGOING may be Pmode. */
7024 if (GET_MODE (outgoing) != GET_MODE (value_address))
7025 value_address = convert_memory_address (GET_MODE (outgoing),
7026 value_address);
7027 #endif
7029 emit_move_insn (outgoing, value_address);
7031 /* Show return register used to hold result (in this case the address
7032 of the result. */
7033 current_function_return_rtx = outgoing;
7036 /* If this is an implementation of throw, do what's necessary to
7037 communicate between __builtin_eh_return and the epilogue. */
7038 expand_eh_return ();
7040 /* Emit the actual code to clobber return register. */
7042 rtx seq, after;
7044 start_sequence ();
7045 clobber_return_register ();
7046 seq = get_insns ();
7047 end_sequence ();
7049 after = emit_insn_after (seq, clobber_after);
7051 if (clobber_after != after)
7052 cfun->x_clobber_return_insn = after;
7055 /* ??? This should no longer be necessary since stupid is no longer with
7056 us, but there are some parts of the compiler (eg reload_combine, and
7057 sh mach_dep_reorg) that still try and compute their own lifetime info
7058 instead of using the general framework. */
7059 use_return_register ();
7061 /* Fix up any gotos that jumped out to the outermost
7062 binding level of the function.
7063 Must follow emitting RETURN_LABEL. */
7065 /* If you have any cleanups to do at this point,
7066 and they need to create temporary variables,
7067 then you will lose. */
7068 expand_fixups (get_insns ());
7072 get_arg_pointer_save_area (f)
7073 struct function *f;
7075 rtx ret = f->x_arg_pointer_save_area;
7077 if (! ret)
7079 ret = assign_stack_local_1 (Pmode, GET_MODE_SIZE (Pmode), 0, f);
7080 f->x_arg_pointer_save_area = ret;
7083 if (f == cfun && ! f->arg_pointer_save_area_init)
7085 rtx seq;
7087 /* Save the arg pointer at the beginning of the function. The
7088 generated stack slot may not be a valid memory address, so we
7089 have to check it and fix it if necessary. */
7090 start_sequence ();
7091 emit_move_insn (validize_mem (ret), virtual_incoming_args_rtx);
7092 seq = get_insns ();
7093 end_sequence ();
7095 push_topmost_sequence ();
7096 emit_insn_after (seq, get_insns ());
7097 pop_topmost_sequence ();
7100 return ret;
7103 /* Extend a vector that records the INSN_UIDs of INSNS
7104 (a list of one or more insns). */
7106 static void
7107 record_insns (insns, vecp)
7108 rtx insns;
7109 varray_type *vecp;
7111 int i, len;
7112 rtx tmp;
7114 tmp = insns;
7115 len = 0;
7116 while (tmp != NULL_RTX)
7118 len++;
7119 tmp = NEXT_INSN (tmp);
7122 i = VARRAY_SIZE (*vecp);
7123 VARRAY_GROW (*vecp, i + len);
7124 tmp = insns;
7125 while (tmp != NULL_RTX)
7127 VARRAY_INT (*vecp, i) = INSN_UID (tmp);
7128 i++;
7129 tmp = NEXT_INSN (tmp);
7133 /* Determine how many INSN_UIDs in VEC are part of INSN. Because we can
7134 be running after reorg, SEQUENCE rtl is possible. */
7136 static int
7137 contains (insn, vec)
7138 rtx insn;
7139 varray_type vec;
7141 int i, j;
7143 if (GET_CODE (insn) == INSN
7144 && GET_CODE (PATTERN (insn)) == SEQUENCE)
7146 int count = 0;
7147 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
7148 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7149 if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == VARRAY_INT (vec, j))
7150 count++;
7151 return count;
7153 else
7155 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7156 if (INSN_UID (insn) == VARRAY_INT (vec, j))
7157 return 1;
7159 return 0;
7163 prologue_epilogue_contains (insn)
7164 rtx insn;
7166 if (contains (insn, prologue))
7167 return 1;
7168 if (contains (insn, epilogue))
7169 return 1;
7170 return 0;
7174 sibcall_epilogue_contains (insn)
7175 rtx insn;
7177 if (sibcall_epilogue)
7178 return contains (insn, sibcall_epilogue);
7179 return 0;
7182 #ifdef HAVE_return
7183 /* Insert gen_return at the end of block BB. This also means updating
7184 block_for_insn appropriately. */
7186 static void
7187 emit_return_into_block (bb, line_note)
7188 basic_block bb;
7189 rtx line_note;
7191 rtx p, end;
7193 p = NEXT_INSN (bb->end);
7194 end = emit_jump_insn_after (gen_return (), bb->end);
7195 if (line_note)
7196 emit_line_note_after (NOTE_SOURCE_FILE (line_note),
7197 NOTE_LINE_NUMBER (line_note), PREV_INSN (bb->end));
7199 #endif /* HAVE_return */
7201 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
7203 /* These functions convert the epilogue into a variant that does not modify the
7204 stack pointer. This is used in cases where a function returns an object
7205 whose size is not known until it is computed. The called function leaves the
7206 object on the stack, leaves the stack depressed, and returns a pointer to
7207 the object.
7209 What we need to do is track all modifications and references to the stack
7210 pointer, deleting the modifications and changing the references to point to
7211 the location the stack pointer would have pointed to had the modifications
7212 taken place.
7214 These functions need to be portable so we need to make as few assumptions
7215 about the epilogue as we can. However, the epilogue basically contains
7216 three things: instructions to reset the stack pointer, instructions to
7217 reload registers, possibly including the frame pointer, and an
7218 instruction to return to the caller.
7220 If we can't be sure of what a relevant epilogue insn is doing, we abort.
7221 We also make no attempt to validate the insns we make since if they are
7222 invalid, we probably can't do anything valid. The intent is that these
7223 routines get "smarter" as more and more machines start to use them and
7224 they try operating on different epilogues.
7226 We use the following structure to track what the part of the epilogue that
7227 we've already processed has done. We keep two copies of the SP equivalence,
7228 one for use during the insn we are processing and one for use in the next
7229 insn. The difference is because one part of a PARALLEL may adjust SP
7230 and the other may use it. */
7232 struct epi_info
7234 rtx sp_equiv_reg; /* REG that SP is set from, perhaps SP. */
7235 HOST_WIDE_INT sp_offset; /* Offset from SP_EQUIV_REG of present SP. */
7236 rtx new_sp_equiv_reg; /* REG to be used at end of insn. */
7237 HOST_WIDE_INT new_sp_offset; /* Offset to be used at end of insn. */
7238 rtx equiv_reg_src; /* If nonzero, the value that SP_EQUIV_REG
7239 should be set to once we no longer need
7240 its value. */
7243 static void handle_epilogue_set PARAMS ((rtx, struct epi_info *));
7244 static void emit_equiv_load PARAMS ((struct epi_info *));
7246 /* Modify INSN, a list of one or more insns that is part of the epilogue, to
7247 no modifications to the stack pointer. Return the new list of insns. */
7249 static rtx
7250 keep_stack_depressed (insns)
7251 rtx insns;
7253 int j;
7254 struct epi_info info;
7255 rtx insn, next;
7257 /* If the epilogue is just a single instruction, it ust be OK as is. */
7259 if (NEXT_INSN (insns) == NULL_RTX)
7260 return insns;
7262 /* Otherwise, start a sequence, initialize the information we have, and
7263 process all the insns we were given. */
7264 start_sequence ();
7266 info.sp_equiv_reg = stack_pointer_rtx;
7267 info.sp_offset = 0;
7268 info.equiv_reg_src = 0;
7270 insn = insns;
7271 next = NULL_RTX;
7272 while (insn != NULL_RTX)
7274 next = NEXT_INSN (insn);
7276 if (!INSN_P (insn))
7278 add_insn (insn);
7279 insn = next;
7280 continue;
7283 /* If this insn references the register that SP is equivalent to and
7284 we have a pending load to that register, we must force out the load
7285 first and then indicate we no longer know what SP's equivalent is. */
7286 if (info.equiv_reg_src != 0
7287 && reg_referenced_p (info.sp_equiv_reg, PATTERN (insn)))
7289 emit_equiv_load (&info);
7290 info.sp_equiv_reg = 0;
7293 info.new_sp_equiv_reg = info.sp_equiv_reg;
7294 info.new_sp_offset = info.sp_offset;
7296 /* If this is a (RETURN) and the return address is on the stack,
7297 update the address and change to an indirect jump. */
7298 if (GET_CODE (PATTERN (insn)) == RETURN
7299 || (GET_CODE (PATTERN (insn)) == PARALLEL
7300 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == RETURN))
7302 rtx retaddr = INCOMING_RETURN_ADDR_RTX;
7303 rtx base = 0;
7304 HOST_WIDE_INT offset = 0;
7305 rtx jump_insn, jump_set;
7307 /* If the return address is in a register, we can emit the insn
7308 unchanged. Otherwise, it must be a MEM and we see what the
7309 base register and offset are. In any case, we have to emit any
7310 pending load to the equivalent reg of SP, if any. */
7311 if (GET_CODE (retaddr) == REG)
7313 emit_equiv_load (&info);
7314 add_insn (insn);
7315 insn = next;
7316 continue;
7318 else if (GET_CODE (retaddr) == MEM
7319 && GET_CODE (XEXP (retaddr, 0)) == REG)
7320 base = gen_rtx_REG (Pmode, REGNO (XEXP (retaddr, 0))), offset = 0;
7321 else if (GET_CODE (retaddr) == MEM
7322 && GET_CODE (XEXP (retaddr, 0)) == PLUS
7323 && GET_CODE (XEXP (XEXP (retaddr, 0), 0)) == REG
7324 && GET_CODE (XEXP (XEXP (retaddr, 0), 1)) == CONST_INT)
7326 base = gen_rtx_REG (Pmode, REGNO (XEXP (XEXP (retaddr, 0), 0)));
7327 offset = INTVAL (XEXP (XEXP (retaddr, 0), 1));
7329 else
7330 abort ();
7332 /* If the base of the location containing the return pointer
7333 is SP, we must update it with the replacement address. Otherwise,
7334 just build the necessary MEM. */
7335 retaddr = plus_constant (base, offset);
7336 if (base == stack_pointer_rtx)
7337 retaddr = simplify_replace_rtx (retaddr, stack_pointer_rtx,
7338 plus_constant (info.sp_equiv_reg,
7339 info.sp_offset));
7341 retaddr = gen_rtx_MEM (Pmode, retaddr);
7343 /* If there is a pending load to the equivalent register for SP
7344 and we reference that register, we must load our address into
7345 a scratch register and then do that load. */
7346 if (info.equiv_reg_src
7347 && reg_overlap_mentioned_p (info.equiv_reg_src, retaddr))
7349 unsigned int regno;
7350 rtx reg;
7352 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
7353 if (HARD_REGNO_MODE_OK (regno, Pmode)
7354 && !fixed_regs[regno]
7355 && TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)
7356 && !REGNO_REG_SET_P (EXIT_BLOCK_PTR->global_live_at_start,
7357 regno)
7358 && !refers_to_regno_p (regno,
7359 regno + HARD_REGNO_NREGS (regno,
7360 Pmode),
7361 info.equiv_reg_src, NULL))
7362 break;
7364 if (regno == FIRST_PSEUDO_REGISTER)
7365 abort ();
7367 reg = gen_rtx_REG (Pmode, regno);
7368 emit_move_insn (reg, retaddr);
7369 retaddr = reg;
7372 emit_equiv_load (&info);
7373 jump_insn = emit_jump_insn (gen_indirect_jump (retaddr));
7375 /* Show the SET in the above insn is a RETURN. */
7376 jump_set = single_set (jump_insn);
7377 if (jump_set == 0)
7378 abort ();
7379 else
7380 SET_IS_RETURN_P (jump_set) = 1;
7383 /* If SP is not mentioned in the pattern and its equivalent register, if
7384 any, is not modified, just emit it. Otherwise, if neither is set,
7385 replace the reference to SP and emit the insn. If none of those are
7386 true, handle each SET individually. */
7387 else if (!reg_mentioned_p (stack_pointer_rtx, PATTERN (insn))
7388 && (info.sp_equiv_reg == stack_pointer_rtx
7389 || !reg_set_p (info.sp_equiv_reg, insn)))
7390 add_insn (insn);
7391 else if (! reg_set_p (stack_pointer_rtx, insn)
7392 && (info.sp_equiv_reg == stack_pointer_rtx
7393 || !reg_set_p (info.sp_equiv_reg, insn)))
7395 if (! validate_replace_rtx (stack_pointer_rtx,
7396 plus_constant (info.sp_equiv_reg,
7397 info.sp_offset),
7398 insn))
7399 abort ();
7401 add_insn (insn);
7403 else if (GET_CODE (PATTERN (insn)) == SET)
7404 handle_epilogue_set (PATTERN (insn), &info);
7405 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
7407 for (j = 0; j < XVECLEN (PATTERN (insn), 0); j++)
7408 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET)
7409 handle_epilogue_set (XVECEXP (PATTERN (insn), 0, j), &info);
7411 else
7412 add_insn (insn);
7414 info.sp_equiv_reg = info.new_sp_equiv_reg;
7415 info.sp_offset = info.new_sp_offset;
7417 insn = next;
7420 insns = get_insns ();
7421 end_sequence ();
7422 return insns;
7425 /* SET is a SET from an insn in the epilogue. P is a pointer to the epi_info
7426 structure that contains information about what we've seen so far. We
7427 process this SET by either updating that data or by emitting one or
7428 more insns. */
7430 static void
7431 handle_epilogue_set (set, p)
7432 rtx set;
7433 struct epi_info *p;
7435 /* First handle the case where we are setting SP. Record what it is being
7436 set from. If unknown, abort. */
7437 if (reg_set_p (stack_pointer_rtx, set))
7439 if (SET_DEST (set) != stack_pointer_rtx)
7440 abort ();
7442 if (GET_CODE (SET_SRC (set)) == PLUS
7443 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
7445 p->new_sp_equiv_reg = XEXP (SET_SRC (set), 0);
7446 p->new_sp_offset = INTVAL (XEXP (SET_SRC (set), 1));
7448 else
7449 p->new_sp_equiv_reg = SET_SRC (set), p->new_sp_offset = 0;
7451 /* If we are adjusting SP, we adjust from the old data. */
7452 if (p->new_sp_equiv_reg == stack_pointer_rtx)
7454 p->new_sp_equiv_reg = p->sp_equiv_reg;
7455 p->new_sp_offset += p->sp_offset;
7458 if (p->new_sp_equiv_reg == 0 || GET_CODE (p->new_sp_equiv_reg) != REG)
7459 abort ();
7461 return;
7464 /* Next handle the case where we are setting SP's equivalent register.
7465 If we already have a value to set it to, abort. We could update, but
7466 there seems little point in handling that case. Note that we have
7467 to allow for the case where we are setting the register set in
7468 the previous part of a PARALLEL inside a single insn. But use the
7469 old offset for any updates within this insn. */
7470 else if (p->new_sp_equiv_reg != 0 && reg_set_p (p->new_sp_equiv_reg, set))
7472 if (!rtx_equal_p (p->new_sp_equiv_reg, SET_DEST (set))
7473 || p->equiv_reg_src != 0)
7474 abort ();
7475 else
7476 p->equiv_reg_src
7477 = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7478 plus_constant (p->sp_equiv_reg,
7479 p->sp_offset));
7482 /* Otherwise, replace any references to SP in the insn to its new value
7483 and emit the insn. */
7484 else
7486 SET_SRC (set) = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7487 plus_constant (p->sp_equiv_reg,
7488 p->sp_offset));
7489 SET_DEST (set) = simplify_replace_rtx (SET_DEST (set), stack_pointer_rtx,
7490 plus_constant (p->sp_equiv_reg,
7491 p->sp_offset));
7492 emit_insn (set);
7496 /* Emit an insn to do the load shown in p->equiv_reg_src, if needed. */
7498 static void
7499 emit_equiv_load (p)
7500 struct epi_info *p;
7502 if (p->equiv_reg_src != 0)
7503 emit_move_insn (p->sp_equiv_reg, p->equiv_reg_src);
7505 p->equiv_reg_src = 0;
7507 #endif
7509 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
7510 this into place with notes indicating where the prologue ends and where
7511 the epilogue begins. Update the basic block information when possible. */
7513 void
7514 thread_prologue_and_epilogue_insns (f)
7515 rtx f ATTRIBUTE_UNUSED;
7517 int inserted = 0;
7518 edge e;
7519 #if defined (HAVE_sibcall_epilogue) || defined (HAVE_epilogue) || defined (HAVE_return) || defined (HAVE_prologue)
7520 rtx seq;
7521 #endif
7522 #ifdef HAVE_prologue
7523 rtx prologue_end = NULL_RTX;
7524 #endif
7525 #if defined (HAVE_epilogue) || defined(HAVE_return)
7526 rtx epilogue_end = NULL_RTX;
7527 #endif
7529 #ifdef HAVE_prologue
7530 if (HAVE_prologue)
7532 start_sequence ();
7533 seq = gen_prologue ();
7534 emit_insn (seq);
7536 /* Retain a map of the prologue insns. */
7537 record_insns (seq, &prologue);
7538 prologue_end = emit_note (NULL, NOTE_INSN_PROLOGUE_END);
7540 seq = get_insns ();
7541 end_sequence ();
7543 /* Can't deal with multiple successors of the entry block
7544 at the moment. Function should always have at least one
7545 entry point. */
7546 if (!ENTRY_BLOCK_PTR->succ || ENTRY_BLOCK_PTR->succ->succ_next)
7547 abort ();
7549 insert_insn_on_edge (seq, ENTRY_BLOCK_PTR->succ);
7550 inserted = 1;
7552 #endif
7554 /* If the exit block has no non-fake predecessors, we don't need
7555 an epilogue. */
7556 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7557 if ((e->flags & EDGE_FAKE) == 0)
7558 break;
7559 if (e == NULL)
7560 goto epilogue_done;
7562 #ifdef HAVE_return
7563 if (optimize && HAVE_return)
7565 /* If we're allowed to generate a simple return instruction,
7566 then by definition we don't need a full epilogue. Examine
7567 the block that falls through to EXIT. If it does not
7568 contain any code, examine its predecessors and try to
7569 emit (conditional) return instructions. */
7571 basic_block last;
7572 edge e_next;
7573 rtx label;
7575 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7576 if (e->flags & EDGE_FALLTHRU)
7577 break;
7578 if (e == NULL)
7579 goto epilogue_done;
7580 last = e->src;
7582 /* Verify that there are no active instructions in the last block. */
7583 label = last->end;
7584 while (label && GET_CODE (label) != CODE_LABEL)
7586 if (active_insn_p (label))
7587 break;
7588 label = PREV_INSN (label);
7591 if (last->head == label && GET_CODE (label) == CODE_LABEL)
7593 rtx epilogue_line_note = NULL_RTX;
7595 /* Locate the line number associated with the closing brace,
7596 if we can find one. */
7597 for (seq = get_last_insn ();
7598 seq && ! active_insn_p (seq);
7599 seq = PREV_INSN (seq))
7600 if (GET_CODE (seq) == NOTE && NOTE_LINE_NUMBER (seq) > 0)
7602 epilogue_line_note = seq;
7603 break;
7606 for (e = last->pred; e; e = e_next)
7608 basic_block bb = e->src;
7609 rtx jump;
7611 e_next = e->pred_next;
7612 if (bb == ENTRY_BLOCK_PTR)
7613 continue;
7615 jump = bb->end;
7616 if ((GET_CODE (jump) != JUMP_INSN) || JUMP_LABEL (jump) != label)
7617 continue;
7619 /* If we have an unconditional jump, we can replace that
7620 with a simple return instruction. */
7621 if (simplejump_p (jump))
7623 emit_return_into_block (bb, epilogue_line_note);
7624 delete_insn (jump);
7627 /* If we have a conditional jump, we can try to replace
7628 that with a conditional return instruction. */
7629 else if (condjump_p (jump))
7631 rtx ret, *loc;
7633 ret = SET_SRC (PATTERN (jump));
7634 if (GET_CODE (XEXP (ret, 1)) == LABEL_REF)
7635 loc = &XEXP (ret, 1);
7636 else
7637 loc = &XEXP (ret, 2);
7638 ret = gen_rtx_RETURN (VOIDmode);
7640 if (! validate_change (jump, loc, ret, 0))
7641 continue;
7642 if (JUMP_LABEL (jump))
7643 LABEL_NUSES (JUMP_LABEL (jump))--;
7645 /* If this block has only one successor, it both jumps
7646 and falls through to the fallthru block, so we can't
7647 delete the edge. */
7648 if (bb->succ->succ_next == NULL)
7649 continue;
7651 else
7652 continue;
7654 /* Fix up the CFG for the successful change we just made. */
7655 redirect_edge_succ (e, EXIT_BLOCK_PTR);
7658 /* Emit a return insn for the exit fallthru block. Whether
7659 this is still reachable will be determined later. */
7661 emit_barrier_after (last->end);
7662 emit_return_into_block (last, epilogue_line_note);
7663 epilogue_end = last->end;
7664 last->succ->flags &= ~EDGE_FALLTHRU;
7665 goto epilogue_done;
7668 #endif
7669 #ifdef HAVE_epilogue
7670 if (HAVE_epilogue)
7672 /* Find the edge that falls through to EXIT. Other edges may exist
7673 due to RETURN instructions, but those don't need epilogues.
7674 There really shouldn't be a mixture -- either all should have
7675 been converted or none, however... */
7677 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7678 if (e->flags & EDGE_FALLTHRU)
7679 break;
7680 if (e == NULL)
7681 goto epilogue_done;
7683 start_sequence ();
7684 epilogue_end = emit_note (NULL, NOTE_INSN_EPILOGUE_BEG);
7686 seq = gen_epilogue ();
7688 #ifdef INCOMING_RETURN_ADDR_RTX
7689 /* If this function returns with the stack depressed and we can support
7690 it, massage the epilogue to actually do that. */
7691 if (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
7692 && TYPE_RETURNS_STACK_DEPRESSED (TREE_TYPE (current_function_decl)))
7693 seq = keep_stack_depressed (seq);
7694 #endif
7696 emit_jump_insn (seq);
7698 /* Retain a map of the epilogue insns. */
7699 record_insns (seq, &epilogue);
7701 seq = get_insns ();
7702 end_sequence ();
7704 insert_insn_on_edge (seq, e);
7705 inserted = 1;
7707 #endif
7708 epilogue_done:
7710 if (inserted)
7711 commit_edge_insertions ();
7713 #ifdef HAVE_sibcall_epilogue
7714 /* Emit sibling epilogues before any sibling call sites. */
7715 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7717 basic_block bb = e->src;
7718 rtx insn = bb->end;
7719 rtx i;
7720 rtx newinsn;
7722 if (GET_CODE (insn) != CALL_INSN
7723 || ! SIBLING_CALL_P (insn))
7724 continue;
7726 start_sequence ();
7727 emit_insn (gen_sibcall_epilogue ());
7728 seq = get_insns ();
7729 end_sequence ();
7731 /* Retain a map of the epilogue insns. Used in life analysis to
7732 avoid getting rid of sibcall epilogue insns. Do this before we
7733 actually emit the sequence. */
7734 record_insns (seq, &sibcall_epilogue);
7736 i = PREV_INSN (insn);
7737 newinsn = emit_insn_before (seq, insn);
7739 #endif
7741 #ifdef HAVE_prologue
7742 if (prologue_end)
7744 rtx insn, prev;
7746 /* GDB handles `break f' by setting a breakpoint on the first
7747 line note after the prologue. Which means (1) that if
7748 there are line number notes before where we inserted the
7749 prologue we should move them, and (2) we should generate a
7750 note before the end of the first basic block, if there isn't
7751 one already there.
7753 ??? This behaviour is completely broken when dealing with
7754 multiple entry functions. We simply place the note always
7755 into first basic block and let alternate entry points
7756 to be missed.
7759 for (insn = prologue_end; insn; insn = prev)
7761 prev = PREV_INSN (insn);
7762 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7764 /* Note that we cannot reorder the first insn in the
7765 chain, since rest_of_compilation relies on that
7766 remaining constant. */
7767 if (prev == NULL)
7768 break;
7769 reorder_insns (insn, insn, prologue_end);
7773 /* Find the last line number note in the first block. */
7774 for (insn = ENTRY_BLOCK_PTR->next_bb->end;
7775 insn != prologue_end && insn;
7776 insn = PREV_INSN (insn))
7777 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7778 break;
7780 /* If we didn't find one, make a copy of the first line number
7781 we run across. */
7782 if (! insn)
7784 for (insn = next_active_insn (prologue_end);
7785 insn;
7786 insn = PREV_INSN (insn))
7787 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7789 emit_line_note_after (NOTE_SOURCE_FILE (insn),
7790 NOTE_LINE_NUMBER (insn),
7791 prologue_end);
7792 break;
7796 #endif
7797 #ifdef HAVE_epilogue
7798 if (epilogue_end)
7800 rtx insn, next;
7802 /* Similarly, move any line notes that appear after the epilogue.
7803 There is no need, however, to be quite so anal about the existence
7804 of such a note. */
7805 for (insn = epilogue_end; insn; insn = next)
7807 next = NEXT_INSN (insn);
7808 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7809 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
7812 #endif
7815 /* Reposition the prologue-end and epilogue-begin notes after instruction
7816 scheduling and delayed branch scheduling. */
7818 void
7819 reposition_prologue_and_epilogue_notes (f)
7820 rtx f ATTRIBUTE_UNUSED;
7822 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
7823 rtx insn, last, note;
7824 int len;
7826 if ((len = VARRAY_SIZE (prologue)) > 0)
7828 last = 0, note = 0;
7830 /* Scan from the beginning until we reach the last prologue insn.
7831 We apparently can't depend on basic_block_{head,end} after
7832 reorg has run. */
7833 for (insn = f; insn; insn = NEXT_INSN (insn))
7835 if (GET_CODE (insn) == NOTE)
7837 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
7838 note = insn;
7840 else if (contains (insn, prologue))
7842 last = insn;
7843 if (--len == 0)
7844 break;
7848 if (last)
7850 rtx next;
7852 /* Find the prologue-end note if we haven't already, and
7853 move it to just after the last prologue insn. */
7854 if (note == 0)
7856 for (note = last; (note = NEXT_INSN (note));)
7857 if (GET_CODE (note) == NOTE
7858 && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
7859 break;
7862 next = NEXT_INSN (note);
7864 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
7865 if (GET_CODE (last) == CODE_LABEL)
7866 last = NEXT_INSN (last);
7867 reorder_insns (note, note, last);
7871 if ((len = VARRAY_SIZE (epilogue)) > 0)
7873 last = 0, note = 0;
7875 /* Scan from the end until we reach the first epilogue insn.
7876 We apparently can't depend on basic_block_{head,end} after
7877 reorg has run. */
7878 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
7880 if (GET_CODE (insn) == NOTE)
7882 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
7883 note = insn;
7885 else if (contains (insn, epilogue))
7887 last = insn;
7888 if (--len == 0)
7889 break;
7893 if (last)
7895 /* Find the epilogue-begin note if we haven't already, and
7896 move it to just before the first epilogue insn. */
7897 if (note == 0)
7899 for (note = insn; (note = PREV_INSN (note));)
7900 if (GET_CODE (note) == NOTE
7901 && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
7902 break;
7905 if (PREV_INSN (last) != note)
7906 reorder_insns (note, note, PREV_INSN (last));
7909 #endif /* HAVE_prologue or HAVE_epilogue */
7912 /* Called once, at initialization, to initialize function.c. */
7914 void
7915 init_function_once ()
7917 VARRAY_INT_INIT (prologue, 0, "prologue");
7918 VARRAY_INT_INIT (epilogue, 0, "epilogue");
7919 VARRAY_INT_INIT (sibcall_epilogue, 0, "sibcall_epilogue");
7922 #include "gt-function.h"