s390.c (optimization_options): Set flag_asynchronous_unwind_tables to 1 by default.
[official-gcc.git] / gcc / function.c
blob0c066ed03588974b5be2fe32f8a84cd4a48be143
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 /* Nonzero if this temporary is currently in use. */
185 char in_use;
186 /* Nonzero 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 /* Nonzero 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 static void pad_below PARAMS ((struct args_size *, enum machine_mode,
259 tree));
260 static rtx round_trampoline_addr PARAMS ((rtx));
261 static rtx adjust_trampoline_addr PARAMS ((rtx));
262 static tree *identify_blocks_1 PARAMS ((rtx, tree *, tree *, tree *));
263 static void reorder_blocks_0 PARAMS ((tree));
264 static void reorder_blocks_1 PARAMS ((rtx, tree, varray_type *));
265 static void reorder_fix_fragments PARAMS ((tree));
266 static tree blocks_nreverse PARAMS ((tree));
267 static int all_blocks PARAMS ((tree, tree *));
268 static tree *get_block_vector PARAMS ((tree, int *));
269 extern tree debug_find_var_in_block_tree PARAMS ((tree, tree));
270 /* We always define `record_insns' even if its not used so that we
271 can always export `prologue_epilogue_contains'. */
272 static void record_insns PARAMS ((rtx, varray_type *)) ATTRIBUTE_UNUSED;
273 static int contains PARAMS ((rtx, varray_type));
274 #ifdef HAVE_return
275 static void emit_return_into_block PARAMS ((basic_block, rtx));
276 #endif
277 static void put_addressof_into_stack PARAMS ((rtx, htab_t));
278 static bool purge_addressof_1 PARAMS ((rtx *, rtx, int, int,
279 htab_t));
280 static void purge_single_hard_subreg_set PARAMS ((rtx));
281 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
282 static rtx keep_stack_depressed PARAMS ((rtx));
283 #endif
284 static int is_addressof PARAMS ((rtx *, void *));
285 static hashval_t insns_for_mem_hash PARAMS ((const void *));
286 static int insns_for_mem_comp PARAMS ((const void *, const void *));
287 static int insns_for_mem_walk PARAMS ((rtx *, void *));
288 static void compute_insns_for_mem PARAMS ((rtx, rtx, htab_t));
289 static void prepare_function_start PARAMS ((void));
290 static void do_clobber_return_reg PARAMS ((rtx, void *));
291 static void do_use_return_reg PARAMS ((rtx, void *));
293 /* Pointer to chain of `struct function' for containing functions. */
294 static GTY(()) struct function *outer_function_chain;
296 /* Given a function decl for a containing function,
297 return the `struct function' for it. */
299 struct function *
300 find_function_data (decl)
301 tree decl;
303 struct function *p;
305 for (p = outer_function_chain; p; p = p->outer)
306 if (p->decl == decl)
307 return p;
309 abort ();
312 /* Save the current context for compilation of a nested function.
313 This is called from language-specific code. The caller should use
314 the enter_nested langhook to save any language-specific state,
315 since this function knows only about language-independent
316 variables. */
318 void
319 push_function_context_to (context)
320 tree context;
322 struct function *p;
324 if (context)
326 if (context == current_function_decl)
327 cfun->contains_functions = 1;
328 else
330 struct function *containing = find_function_data (context);
331 containing->contains_functions = 1;
335 if (cfun == 0)
336 init_dummy_function_start ();
337 p = cfun;
339 p->outer = outer_function_chain;
340 outer_function_chain = p;
341 p->fixup_var_refs_queue = 0;
343 (*lang_hooks.function.enter_nested) (p);
345 cfun = 0;
348 void
349 push_function_context ()
351 push_function_context_to (current_function_decl);
354 /* Restore the last saved context, at the end of a nested function.
355 This function is called from language-specific code. */
357 void
358 pop_function_context_from (context)
359 tree context ATTRIBUTE_UNUSED;
361 struct function *p = outer_function_chain;
362 struct var_refs_queue *queue;
364 cfun = p;
365 outer_function_chain = p->outer;
367 current_function_decl = p->decl;
368 reg_renumber = 0;
370 restore_emit_status (p);
372 (*lang_hooks.function.leave_nested) (p);
374 /* Finish doing put_var_into_stack for any of our variables which became
375 addressable during the nested function. If only one entry has to be
376 fixed up, just do that one. Otherwise, first make a list of MEMs that
377 are not to be unshared. */
378 if (p->fixup_var_refs_queue == 0)
380 else if (p->fixup_var_refs_queue->next == 0)
381 fixup_var_refs (p->fixup_var_refs_queue->modified,
382 p->fixup_var_refs_queue->promoted_mode,
383 p->fixup_var_refs_queue->unsignedp,
384 p->fixup_var_refs_queue->modified, 0);
385 else
387 rtx list = 0;
389 for (queue = p->fixup_var_refs_queue; queue; queue = queue->next)
390 list = gen_rtx_EXPR_LIST (VOIDmode, queue->modified, list);
392 for (queue = p->fixup_var_refs_queue; queue; queue = queue->next)
393 fixup_var_refs (queue->modified, queue->promoted_mode,
394 queue->unsignedp, list, 0);
398 p->fixup_var_refs_queue = 0;
400 /* Reset variables that have known state during rtx generation. */
401 rtx_equal_function_value_matters = 1;
402 virtuals_instantiated = 0;
403 generating_concat_p = 1;
406 void
407 pop_function_context ()
409 pop_function_context_from (current_function_decl);
412 /* Clear out all parts of the state in F that can safely be discarded
413 after the function has been parsed, but not compiled, to let
414 garbage collection reclaim the memory. */
416 void
417 free_after_parsing (f)
418 struct function *f;
420 /* f->expr->forced_labels is used by code generation. */
421 /* f->emit->regno_reg_rtx is used by code generation. */
422 /* f->varasm is used by code generation. */
423 /* f->eh->eh_return_stub_label is used by code generation. */
425 (*lang_hooks.function.final) (f);
426 f->stmt = NULL;
429 /* Clear out all parts of the state in F that can safely be discarded
430 after the function has been compiled, to let garbage collection
431 reclaim the memory. */
433 void
434 free_after_compilation (f)
435 struct function *f;
437 f->eh = NULL;
438 f->expr = NULL;
439 f->emit = NULL;
440 f->varasm = NULL;
441 f->machine = NULL;
443 f->x_temp_slots = NULL;
444 f->arg_offset_rtx = NULL;
445 f->return_rtx = NULL;
446 f->internal_arg_pointer = NULL;
447 f->x_nonlocal_labels = NULL;
448 f->x_nonlocal_goto_handler_slots = NULL;
449 f->x_nonlocal_goto_handler_labels = NULL;
450 f->x_nonlocal_goto_stack_level = NULL;
451 f->x_cleanup_label = NULL;
452 f->x_return_label = NULL;
453 f->x_save_expr_regs = NULL;
454 f->x_stack_slot_list = NULL;
455 f->x_rtl_expr_chain = NULL;
456 f->x_tail_recursion_label = NULL;
457 f->x_tail_recursion_reentry = NULL;
458 f->x_arg_pointer_save_area = NULL;
459 f->x_clobber_return_insn = NULL;
460 f->x_context_display = NULL;
461 f->x_trampoline_list = NULL;
462 f->x_parm_birth_insn = NULL;
463 f->x_last_parm_insn = NULL;
464 f->x_parm_reg_stack_loc = NULL;
465 f->fixup_var_refs_queue = NULL;
466 f->original_arg_vector = NULL;
467 f->original_decl_initial = NULL;
468 f->inl_last_parm_insn = NULL;
469 f->epilogue_delay_list = NULL;
472 /* Allocate fixed slots in the stack frame of the current function. */
474 /* Return size needed for stack frame based on slots so far allocated in
475 function F.
476 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
477 the caller may have to do that. */
479 HOST_WIDE_INT
480 get_func_frame_size (f)
481 struct function *f;
483 #ifdef FRAME_GROWS_DOWNWARD
484 return -f->x_frame_offset;
485 #else
486 return f->x_frame_offset;
487 #endif
490 /* Return size needed for stack frame based on slots so far allocated.
491 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
492 the caller may have to do that. */
493 HOST_WIDE_INT
494 get_frame_size ()
496 return get_func_frame_size (cfun);
499 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
500 with machine mode MODE.
502 ALIGN controls the amount of alignment for the address of the slot:
503 0 means according to MODE,
504 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
505 positive specifies alignment boundary in bits.
507 We do not round to stack_boundary here.
509 FUNCTION specifies the function to allocate in. */
511 static rtx
512 assign_stack_local_1 (mode, size, align, function)
513 enum machine_mode mode;
514 HOST_WIDE_INT size;
515 int align;
516 struct function *function;
518 rtx x, addr;
519 int bigend_correction = 0;
520 int alignment;
521 int frame_off, frame_alignment, frame_phase;
523 if (align == 0)
525 tree type;
527 if (mode == BLKmode)
528 alignment = BIGGEST_ALIGNMENT;
529 else
530 alignment = GET_MODE_ALIGNMENT (mode);
532 /* Allow the target to (possibly) increase the alignment of this
533 stack slot. */
534 type = (*lang_hooks.types.type_for_mode) (mode, 0);
535 if (type)
536 alignment = LOCAL_ALIGNMENT (type, alignment);
538 alignment /= BITS_PER_UNIT;
540 else if (align == -1)
542 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
543 size = CEIL_ROUND (size, alignment);
545 else
546 alignment = align / BITS_PER_UNIT;
548 #ifdef FRAME_GROWS_DOWNWARD
549 function->x_frame_offset -= size;
550 #endif
552 /* Ignore alignment we can't do with expected alignment of the boundary. */
553 if (alignment * BITS_PER_UNIT > PREFERRED_STACK_BOUNDARY)
554 alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
556 if (function->stack_alignment_needed < alignment * BITS_PER_UNIT)
557 function->stack_alignment_needed = alignment * BITS_PER_UNIT;
559 /* Calculate how many bytes the start of local variables is off from
560 stack alignment. */
561 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
562 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
563 frame_phase = frame_off ? frame_alignment - frame_off : 0;
565 /* Round frame offset to that alignment.
566 We must be careful here, since FRAME_OFFSET might be negative and
567 division with a negative dividend isn't as well defined as we might
568 like. So we instead assume that ALIGNMENT is a power of two and
569 use logical operations which are unambiguous. */
570 #ifdef FRAME_GROWS_DOWNWARD
571 function->x_frame_offset = FLOOR_ROUND (function->x_frame_offset - frame_phase, alignment) + frame_phase;
572 #else
573 function->x_frame_offset = CEIL_ROUND (function->x_frame_offset - frame_phase, alignment) + frame_phase;
574 #endif
576 /* On a big-endian machine, if we are allocating more space than we will use,
577 use the least significant bytes of those that are allocated. */
578 if (BYTES_BIG_ENDIAN && mode != BLKmode)
579 bigend_correction = size - GET_MODE_SIZE (mode);
581 /* If we have already instantiated virtual registers, return the actual
582 address relative to the frame pointer. */
583 if (function == cfun && virtuals_instantiated)
584 addr = plus_constant (frame_pointer_rtx,
585 (frame_offset + bigend_correction
586 + STARTING_FRAME_OFFSET));
587 else
588 addr = plus_constant (virtual_stack_vars_rtx,
589 function->x_frame_offset + bigend_correction);
591 #ifndef FRAME_GROWS_DOWNWARD
592 function->x_frame_offset += size;
593 #endif
595 x = gen_rtx_MEM (mode, addr);
597 function->x_stack_slot_list
598 = gen_rtx_EXPR_LIST (VOIDmode, x, function->x_stack_slot_list);
600 return x;
603 /* Wrapper around assign_stack_local_1; assign a local stack slot for the
604 current function. */
607 assign_stack_local (mode, size, align)
608 enum machine_mode mode;
609 HOST_WIDE_INT size;
610 int align;
612 return assign_stack_local_1 (mode, size, align, cfun);
615 /* Allocate a temporary stack slot and record it for possible later
616 reuse.
618 MODE is the machine mode to be given to the returned rtx.
620 SIZE is the size in units of the space required. We do no rounding here
621 since assign_stack_local will do any required rounding.
623 KEEP is 1 if this slot is to be retained after a call to
624 free_temp_slots. Automatic variables for a block are allocated
625 with this flag. KEEP is 2 if we allocate a longer term temporary,
626 whose lifetime is controlled by CLEANUP_POINT_EXPRs. KEEP is 3
627 if we are to allocate something at an inner level to be treated as
628 a variable in the block (e.g., a SAVE_EXPR).
630 TYPE is the type that will be used for the stack slot. */
633 assign_stack_temp_for_type (mode, size, keep, type)
634 enum machine_mode mode;
635 HOST_WIDE_INT size;
636 int keep;
637 tree type;
639 unsigned int align;
640 struct temp_slot *p, *best_p = 0;
641 rtx slot;
643 /* If SIZE is -1 it means that somebody tried to allocate a temporary
644 of a variable size. */
645 if (size == -1)
646 abort ();
648 if (mode == BLKmode)
649 align = BIGGEST_ALIGNMENT;
650 else
651 align = GET_MODE_ALIGNMENT (mode);
653 if (! type)
654 type = (*lang_hooks.types.type_for_mode) (mode, 0);
656 if (type)
657 align = LOCAL_ALIGNMENT (type, align);
659 /* Try to find an available, already-allocated temporary of the proper
660 mode which meets the size and alignment requirements. Choose the
661 smallest one with the closest alignment. */
662 for (p = temp_slots; p; p = p->next)
663 if (p->align >= align && p->size >= size && GET_MODE (p->slot) == mode
664 && ! p->in_use
665 && objects_must_conflict_p (p->type, type)
666 && (best_p == 0 || best_p->size > p->size
667 || (best_p->size == p->size && best_p->align > p->align)))
669 if (p->align == align && p->size == size)
671 best_p = 0;
672 break;
674 best_p = p;
677 /* Make our best, if any, the one to use. */
678 if (best_p)
680 /* If there are enough aligned bytes left over, make them into a new
681 temp_slot so that the extra bytes don't get wasted. Do this only
682 for BLKmode slots, so that we can be sure of the alignment. */
683 if (GET_MODE (best_p->slot) == BLKmode)
685 int alignment = best_p->align / BITS_PER_UNIT;
686 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
688 if (best_p->size - rounded_size >= alignment)
690 p = (struct temp_slot *) ggc_alloc (sizeof (struct temp_slot));
691 p->in_use = p->addr_taken = 0;
692 p->size = best_p->size - rounded_size;
693 p->base_offset = best_p->base_offset + rounded_size;
694 p->full_size = best_p->full_size - rounded_size;
695 p->slot = gen_rtx_MEM (BLKmode,
696 plus_constant (XEXP (best_p->slot, 0),
697 rounded_size));
698 p->align = best_p->align;
699 p->address = 0;
700 p->rtl_expr = 0;
701 p->type = best_p->type;
702 p->next = temp_slots;
703 temp_slots = p;
705 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
706 stack_slot_list);
708 best_p->size = rounded_size;
709 best_p->full_size = rounded_size;
713 p = best_p;
716 /* If we still didn't find one, make a new temporary. */
717 if (p == 0)
719 HOST_WIDE_INT frame_offset_old = frame_offset;
721 p = (struct temp_slot *) ggc_alloc (sizeof (struct temp_slot));
723 /* We are passing an explicit alignment request to assign_stack_local.
724 One side effect of that is assign_stack_local will not round SIZE
725 to ensure the frame offset remains suitably aligned.
727 So for requests which depended on the rounding of SIZE, we go ahead
728 and round it now. We also make sure ALIGNMENT is at least
729 BIGGEST_ALIGNMENT. */
730 if (mode == BLKmode && align < BIGGEST_ALIGNMENT)
731 abort ();
732 p->slot = assign_stack_local (mode,
733 (mode == BLKmode
734 ? CEIL_ROUND (size, align / BITS_PER_UNIT)
735 : size),
736 align);
738 p->align = align;
740 /* The following slot size computation is necessary because we don't
741 know the actual size of the temporary slot until assign_stack_local
742 has performed all the frame alignment and size rounding for the
743 requested temporary. Note that extra space added for alignment
744 can be either above or below this stack slot depending on which
745 way the frame grows. We include the extra space if and only if it
746 is above this slot. */
747 #ifdef FRAME_GROWS_DOWNWARD
748 p->size = frame_offset_old - frame_offset;
749 #else
750 p->size = size;
751 #endif
753 /* Now define the fields used by combine_temp_slots. */
754 #ifdef FRAME_GROWS_DOWNWARD
755 p->base_offset = frame_offset;
756 p->full_size = frame_offset_old - frame_offset;
757 #else
758 p->base_offset = frame_offset_old;
759 p->full_size = frame_offset - frame_offset_old;
760 #endif
761 p->address = 0;
762 p->next = temp_slots;
763 temp_slots = p;
766 p->in_use = 1;
767 p->addr_taken = 0;
768 p->rtl_expr = seq_rtl_expr;
769 p->type = type;
771 if (keep == 2)
773 p->level = target_temp_slot_level;
774 p->keep = 0;
776 else if (keep == 3)
778 p->level = var_temp_slot_level;
779 p->keep = 0;
781 else
783 p->level = temp_slot_level;
784 p->keep = keep;
788 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
789 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
790 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
792 /* If we know the alias set for the memory that will be used, use
793 it. If there's no TYPE, then we don't know anything about the
794 alias set for the memory. */
795 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
796 set_mem_align (slot, align);
798 /* If a type is specified, set the relevant flags. */
799 if (type != 0)
801 RTX_UNCHANGING_P (slot) = (lang_hooks.honor_readonly
802 && TYPE_READONLY (type));
803 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
804 MEM_SET_IN_STRUCT_P (slot, AGGREGATE_TYPE_P (type));
807 return slot;
810 /* Allocate a temporary stack slot and record it for possible later
811 reuse. First three arguments are same as in preceding function. */
814 assign_stack_temp (mode, size, keep)
815 enum machine_mode mode;
816 HOST_WIDE_INT size;
817 int keep;
819 return assign_stack_temp_for_type (mode, size, keep, NULL_TREE);
822 /* Assign a temporary.
823 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
824 and so that should be used in error messages. In either case, we
825 allocate of the given type.
826 KEEP is as for assign_stack_temp.
827 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
828 it is 0 if a register is OK.
829 DONT_PROMOTE is 1 if we should not promote values in register
830 to wider modes. */
833 assign_temp (type_or_decl, keep, memory_required, dont_promote)
834 tree type_or_decl;
835 int keep;
836 int memory_required;
837 int dont_promote ATTRIBUTE_UNUSED;
839 tree type, decl;
840 enum machine_mode mode;
841 #ifndef PROMOTE_FOR_CALL_ONLY
842 int unsignedp;
843 #endif
845 if (DECL_P (type_or_decl))
846 decl = type_or_decl, type = TREE_TYPE (decl);
847 else
848 decl = NULL, type = type_or_decl;
850 mode = TYPE_MODE (type);
851 #ifndef PROMOTE_FOR_CALL_ONLY
852 unsignedp = TREE_UNSIGNED (type);
853 #endif
855 if (mode == BLKmode || memory_required)
857 HOST_WIDE_INT size = int_size_in_bytes (type);
858 rtx tmp;
860 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
861 problems with allocating the stack space. */
862 if (size == 0)
863 size = 1;
865 /* Unfortunately, we don't yet know how to allocate variable-sized
866 temporaries. However, sometimes we have a fixed upper limit on
867 the size (which is stored in TYPE_ARRAY_MAX_SIZE) and can use that
868 instead. This is the case for Chill variable-sized strings. */
869 if (size == -1 && TREE_CODE (type) == ARRAY_TYPE
870 && TYPE_ARRAY_MAX_SIZE (type) != NULL_TREE
871 && host_integerp (TYPE_ARRAY_MAX_SIZE (type), 1))
872 size = tree_low_cst (TYPE_ARRAY_MAX_SIZE (type), 1);
874 /* The size of the temporary may be too large to fit into an integer. */
875 /* ??? Not sure this should happen except for user silliness, so limit
876 this to things that aren't compiler-generated temporaries. The
877 rest of the time we'll abort in assign_stack_temp_for_type. */
878 if (decl && size == -1
879 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
881 error_with_decl (decl, "size of variable `%s' is too large");
882 size = 1;
885 tmp = assign_stack_temp_for_type (mode, size, keep, type);
886 return tmp;
889 #ifndef PROMOTE_FOR_CALL_ONLY
890 if (! dont_promote)
891 mode = promote_mode (type, mode, &unsignedp, 0);
892 #endif
894 return gen_reg_rtx (mode);
897 /* Combine temporary stack slots which are adjacent on the stack.
899 This allows for better use of already allocated stack space. This is only
900 done for BLKmode slots because we can be sure that we won't have alignment
901 problems in this case. */
903 void
904 combine_temp_slots ()
906 struct temp_slot *p, *q;
907 struct temp_slot *prev_p, *prev_q;
908 int num_slots;
910 /* We can't combine slots, because the information about which slot
911 is in which alias set will be lost. */
912 if (flag_strict_aliasing)
913 return;
915 /* If there are a lot of temp slots, don't do anything unless
916 high levels of optimization. */
917 if (! flag_expensive_optimizations)
918 for (p = temp_slots, num_slots = 0; p; p = p->next, num_slots++)
919 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
920 return;
922 for (p = temp_slots, prev_p = 0; p; p = prev_p ? prev_p->next : temp_slots)
924 int delete_p = 0;
926 if (! p->in_use && GET_MODE (p->slot) == BLKmode)
927 for (q = p->next, prev_q = p; q; q = prev_q->next)
929 int delete_q = 0;
930 if (! q->in_use && GET_MODE (q->slot) == BLKmode)
932 if (p->base_offset + p->full_size == q->base_offset)
934 /* Q comes after P; combine Q into P. */
935 p->size += q->size;
936 p->full_size += q->full_size;
937 delete_q = 1;
939 else if (q->base_offset + q->full_size == p->base_offset)
941 /* P comes after Q; combine P into Q. */
942 q->size += p->size;
943 q->full_size += p->full_size;
944 delete_p = 1;
945 break;
948 /* Either delete Q or advance past it. */
949 if (delete_q)
950 prev_q->next = q->next;
951 else
952 prev_q = q;
954 /* Either delete P or advance past it. */
955 if (delete_p)
957 if (prev_p)
958 prev_p->next = p->next;
959 else
960 temp_slots = p->next;
962 else
963 prev_p = p;
967 /* Find the temp slot corresponding to the object at address X. */
969 static struct temp_slot *
970 find_temp_slot_from_address (x)
971 rtx x;
973 struct temp_slot *p;
974 rtx next;
976 for (p = temp_slots; p; p = p->next)
978 if (! p->in_use)
979 continue;
981 else if (XEXP (p->slot, 0) == x
982 || p->address == x
983 || (GET_CODE (x) == PLUS
984 && XEXP (x, 0) == virtual_stack_vars_rtx
985 && GET_CODE (XEXP (x, 1)) == CONST_INT
986 && INTVAL (XEXP (x, 1)) >= p->base_offset
987 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size))
988 return p;
990 else if (p->address != 0 && GET_CODE (p->address) == EXPR_LIST)
991 for (next = p->address; next; next = XEXP (next, 1))
992 if (XEXP (next, 0) == x)
993 return p;
996 /* If we have a sum involving a register, see if it points to a temp
997 slot. */
998 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == REG
999 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
1000 return p;
1001 else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG
1002 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
1003 return p;
1005 return 0;
1008 /* Indicate that NEW is an alternate way of referring to the temp slot
1009 that previously was known by OLD. */
1011 void
1012 update_temp_slot_address (old, new)
1013 rtx old, new;
1015 struct temp_slot *p;
1017 if (rtx_equal_p (old, new))
1018 return;
1020 p = find_temp_slot_from_address (old);
1022 /* If we didn't find one, see if both OLD is a PLUS. If so, and NEW
1023 is a register, see if one operand of the PLUS is a temporary
1024 location. If so, NEW points into it. Otherwise, if both OLD and
1025 NEW are a PLUS and if there is a register in common between them.
1026 If so, try a recursive call on those values. */
1027 if (p == 0)
1029 if (GET_CODE (old) != PLUS)
1030 return;
1032 if (GET_CODE (new) == REG)
1034 update_temp_slot_address (XEXP (old, 0), new);
1035 update_temp_slot_address (XEXP (old, 1), new);
1036 return;
1038 else if (GET_CODE (new) != PLUS)
1039 return;
1041 if (rtx_equal_p (XEXP (old, 0), XEXP (new, 0)))
1042 update_temp_slot_address (XEXP (old, 1), XEXP (new, 1));
1043 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 0)))
1044 update_temp_slot_address (XEXP (old, 0), XEXP (new, 1));
1045 else if (rtx_equal_p (XEXP (old, 0), XEXP (new, 1)))
1046 update_temp_slot_address (XEXP (old, 1), XEXP (new, 0));
1047 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 1)))
1048 update_temp_slot_address (XEXP (old, 0), XEXP (new, 0));
1050 return;
1053 /* Otherwise add an alias for the temp's address. */
1054 else if (p->address == 0)
1055 p->address = new;
1056 else
1058 if (GET_CODE (p->address) != EXPR_LIST)
1059 p->address = gen_rtx_EXPR_LIST (VOIDmode, p->address, NULL_RTX);
1061 p->address = gen_rtx_EXPR_LIST (VOIDmode, new, p->address);
1065 /* If X could be a reference to a temporary slot, mark the fact that its
1066 address was taken. */
1068 void
1069 mark_temp_addr_taken (x)
1070 rtx x;
1072 struct temp_slot *p;
1074 if (x == 0)
1075 return;
1077 /* If X is not in memory or is at a constant address, it cannot be in
1078 a temporary slot. */
1079 if (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1080 return;
1082 p = find_temp_slot_from_address (XEXP (x, 0));
1083 if (p != 0)
1084 p->addr_taken = 1;
1087 /* If X could be a reference to a temporary slot, mark that slot as
1088 belonging to the to one level higher than the current level. If X
1089 matched one of our slots, just mark that one. Otherwise, we can't
1090 easily predict which it is, so upgrade all of them. Kept slots
1091 need not be touched.
1093 This is called when an ({...}) construct occurs and a statement
1094 returns a value in memory. */
1096 void
1097 preserve_temp_slots (x)
1098 rtx x;
1100 struct temp_slot *p = 0;
1102 /* If there is no result, we still might have some objects whose address
1103 were taken, so we need to make sure they stay around. */
1104 if (x == 0)
1106 for (p = temp_slots; p; p = p->next)
1107 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1108 p->level--;
1110 return;
1113 /* If X is a register that is being used as a pointer, see if we have
1114 a temporary slot we know it points to. To be consistent with
1115 the code below, we really should preserve all non-kept slots
1116 if we can't find a match, but that seems to be much too costly. */
1117 if (GET_CODE (x) == REG && REG_POINTER (x))
1118 p = find_temp_slot_from_address (x);
1120 /* If X is not in memory or is at a constant address, it cannot be in
1121 a temporary slot, but it can contain something whose address was
1122 taken. */
1123 if (p == 0 && (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0))))
1125 for (p = temp_slots; p; p = p->next)
1126 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1127 p->level--;
1129 return;
1132 /* First see if we can find a match. */
1133 if (p == 0)
1134 p = find_temp_slot_from_address (XEXP (x, 0));
1136 if (p != 0)
1138 /* Move everything at our level whose address was taken to our new
1139 level in case we used its address. */
1140 struct temp_slot *q;
1142 if (p->level == temp_slot_level)
1144 for (q = temp_slots; q; q = q->next)
1145 if (q != p && q->addr_taken && q->level == p->level)
1146 q->level--;
1148 p->level--;
1149 p->addr_taken = 0;
1151 return;
1154 /* Otherwise, preserve all non-kept slots at this level. */
1155 for (p = temp_slots; p; p = p->next)
1156 if (p->in_use && p->level == temp_slot_level && ! p->keep)
1157 p->level--;
1160 /* X is the result of an RTL_EXPR. If it is a temporary slot associated
1161 with that RTL_EXPR, promote it into a temporary slot at the present
1162 level so it will not be freed when we free slots made in the
1163 RTL_EXPR. */
1165 void
1166 preserve_rtl_expr_result (x)
1167 rtx x;
1169 struct temp_slot *p;
1171 /* If X is not in memory or is at a constant address, it cannot be in
1172 a temporary slot. */
1173 if (x == 0 || GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1174 return;
1176 /* If we can find a match, move it to our level unless it is already at
1177 an upper level. */
1178 p = find_temp_slot_from_address (XEXP (x, 0));
1179 if (p != 0)
1181 p->level = MIN (p->level, temp_slot_level);
1182 p->rtl_expr = 0;
1185 return;
1188 /* Free all temporaries used so far. This is normally called at the end
1189 of generating code for a statement. Don't free any temporaries
1190 currently in use for an RTL_EXPR that hasn't yet been emitted.
1191 We could eventually do better than this since it can be reused while
1192 generating the same RTL_EXPR, but this is complex and probably not
1193 worthwhile. */
1195 void
1196 free_temp_slots ()
1198 struct temp_slot *p;
1200 for (p = temp_slots; p; p = p->next)
1201 if (p->in_use && p->level == temp_slot_level && ! p->keep
1202 && p->rtl_expr == 0)
1203 p->in_use = 0;
1205 combine_temp_slots ();
1208 /* Free all temporary slots used in T, an RTL_EXPR node. */
1210 void
1211 free_temps_for_rtl_expr (t)
1212 tree t;
1214 struct temp_slot *p;
1216 for (p = temp_slots; p; p = p->next)
1217 if (p->rtl_expr == t)
1219 /* If this slot is below the current TEMP_SLOT_LEVEL, then it
1220 needs to be preserved. This can happen if a temporary in
1221 the RTL_EXPR was addressed; preserve_temp_slots will move
1222 the temporary into a higher level. */
1223 if (temp_slot_level <= p->level)
1224 p->in_use = 0;
1225 else
1226 p->rtl_expr = NULL_TREE;
1229 combine_temp_slots ();
1232 /* Mark all temporaries ever allocated in this function as not suitable
1233 for reuse until the current level is exited. */
1235 void
1236 mark_all_temps_used ()
1238 struct temp_slot *p;
1240 for (p = temp_slots; p; p = p->next)
1242 p->in_use = p->keep = 1;
1243 p->level = MIN (p->level, temp_slot_level);
1247 /* Push deeper into the nesting level for stack temporaries. */
1249 void
1250 push_temp_slots ()
1252 temp_slot_level++;
1255 /* Likewise, but save the new level as the place to allocate variables
1256 for blocks. */
1258 #if 0
1259 void
1260 push_temp_slots_for_block ()
1262 push_temp_slots ();
1264 var_temp_slot_level = temp_slot_level;
1267 /* Likewise, but save the new level as the place to allocate temporaries
1268 for TARGET_EXPRs. */
1270 void
1271 push_temp_slots_for_target ()
1273 push_temp_slots ();
1275 target_temp_slot_level = temp_slot_level;
1278 /* Set and get the value of target_temp_slot_level. The only
1279 permitted use of these functions is to save and restore this value. */
1282 get_target_temp_slot_level ()
1284 return target_temp_slot_level;
1287 void
1288 set_target_temp_slot_level (level)
1289 int level;
1291 target_temp_slot_level = level;
1293 #endif
1295 /* Pop a temporary nesting level. All slots in use in the current level
1296 are freed. */
1298 void
1299 pop_temp_slots ()
1301 struct temp_slot *p;
1303 for (p = temp_slots; p; p = p->next)
1304 if (p->in_use && p->level == temp_slot_level && p->rtl_expr == 0)
1305 p->in_use = 0;
1307 combine_temp_slots ();
1309 temp_slot_level--;
1312 /* Initialize temporary slots. */
1314 void
1315 init_temp_slots ()
1317 /* We have not allocated any temporaries yet. */
1318 temp_slots = 0;
1319 temp_slot_level = 0;
1320 var_temp_slot_level = 0;
1321 target_temp_slot_level = 0;
1324 /* Retroactively move an auto variable from a register to a stack slot.
1325 This is done when an address-reference to the variable is seen. */
1327 void
1328 put_var_into_stack (decl)
1329 tree decl;
1331 rtx reg;
1332 enum machine_mode promoted_mode, decl_mode;
1333 struct function *function = 0;
1334 tree context;
1335 int can_use_addressof;
1336 int volatilep = TREE_CODE (decl) != SAVE_EXPR && TREE_THIS_VOLATILE (decl);
1337 int usedp = (TREE_USED (decl)
1338 || (TREE_CODE (decl) != SAVE_EXPR && DECL_INITIAL (decl) != 0));
1340 context = decl_function_context (decl);
1342 /* Get the current rtl used for this object and its original mode. */
1343 reg = (TREE_CODE (decl) == SAVE_EXPR
1344 ? SAVE_EXPR_RTL (decl)
1345 : DECL_RTL_IF_SET (decl));
1347 /* No need to do anything if decl has no rtx yet
1348 since in that case caller is setting TREE_ADDRESSABLE
1349 and a stack slot will be assigned when the rtl is made. */
1350 if (reg == 0)
1351 return;
1353 /* Get the declared mode for this object. */
1354 decl_mode = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
1355 : DECL_MODE (decl));
1356 /* Get the mode it's actually stored in. */
1357 promoted_mode = GET_MODE (reg);
1359 /* If this variable comes from an outer function, find that
1360 function's saved context. Don't use find_function_data here,
1361 because it might not be in any active function.
1362 FIXME: Is that really supposed to happen?
1363 It does in ObjC at least. */
1364 if (context != current_function_decl && context != inline_function_decl)
1365 for (function = outer_function_chain; function; function = function->outer)
1366 if (function->decl == context)
1367 break;
1369 /* If this is a variable-size object with a pseudo to address it,
1370 put that pseudo into the stack, if the var is nonlocal. */
1371 if (TREE_CODE (decl) != SAVE_EXPR && DECL_NONLOCAL (decl)
1372 && GET_CODE (reg) == MEM
1373 && GET_CODE (XEXP (reg, 0)) == REG
1374 && REGNO (XEXP (reg, 0)) > LAST_VIRTUAL_REGISTER)
1376 reg = XEXP (reg, 0);
1377 decl_mode = promoted_mode = GET_MODE (reg);
1380 can_use_addressof
1381 = (function == 0
1382 && optimize > 0
1383 /* FIXME make it work for promoted modes too */
1384 && decl_mode == promoted_mode
1385 #ifdef NON_SAVING_SETJMP
1386 && ! (NON_SAVING_SETJMP && current_function_calls_setjmp)
1387 #endif
1390 /* If we can't use ADDRESSOF, make sure we see through one we already
1391 generated. */
1392 if (! can_use_addressof && GET_CODE (reg) == MEM
1393 && GET_CODE (XEXP (reg, 0)) == ADDRESSOF)
1394 reg = XEXP (XEXP (reg, 0), 0);
1396 /* Now we should have a value that resides in one or more pseudo regs. */
1398 if (GET_CODE (reg) == REG)
1400 /* If this variable lives in the current function and we don't need
1401 to put things in the stack for the sake of setjmp, try to keep it
1402 in a register until we know we actually need the address. */
1403 if (can_use_addressof)
1404 gen_mem_addressof (reg, decl);
1405 else
1406 put_reg_into_stack (function, reg, TREE_TYPE (decl), promoted_mode,
1407 decl_mode, volatilep, 0, usedp, 0);
1409 else if (GET_CODE (reg) == CONCAT)
1411 /* A CONCAT contains two pseudos; put them both in the stack.
1412 We do it so they end up consecutive.
1413 We fixup references to the parts only after we fixup references
1414 to the whole CONCAT, lest we do double fixups for the latter
1415 references. */
1416 enum machine_mode part_mode = GET_MODE (XEXP (reg, 0));
1417 tree part_type = (*lang_hooks.types.type_for_mode) (part_mode, 0);
1418 rtx lopart = XEXP (reg, 0);
1419 rtx hipart = XEXP (reg, 1);
1420 #ifdef FRAME_GROWS_DOWNWARD
1421 /* Since part 0 should have a lower address, do it second. */
1422 put_reg_into_stack (function, hipart, part_type, part_mode,
1423 part_mode, volatilep, 0, 0, 0);
1424 put_reg_into_stack (function, lopart, part_type, part_mode,
1425 part_mode, volatilep, 0, 0, 0);
1426 #else
1427 put_reg_into_stack (function, lopart, part_type, part_mode,
1428 part_mode, volatilep, 0, 0, 0);
1429 put_reg_into_stack (function, hipart, part_type, part_mode,
1430 part_mode, volatilep, 0, 0, 0);
1431 #endif
1433 /* Change the CONCAT into a combined MEM for both parts. */
1434 PUT_CODE (reg, MEM);
1435 MEM_ATTRS (reg) = 0;
1437 /* set_mem_attributes uses DECL_RTL to avoid re-generating of
1438 already computed alias sets. Here we want to re-generate. */
1439 if (DECL_P (decl))
1440 SET_DECL_RTL (decl, NULL);
1441 set_mem_attributes (reg, decl, 1);
1442 if (DECL_P (decl))
1443 SET_DECL_RTL (decl, reg);
1445 /* The two parts are in memory order already.
1446 Use the lower parts address as ours. */
1447 XEXP (reg, 0) = XEXP (XEXP (reg, 0), 0);
1448 /* Prevent sharing of rtl that might lose. */
1449 if (GET_CODE (XEXP (reg, 0)) == PLUS)
1450 XEXP (reg, 0) = copy_rtx (XEXP (reg, 0));
1451 if (usedp)
1453 schedule_fixup_var_refs (function, reg, TREE_TYPE (decl),
1454 promoted_mode, 0);
1455 schedule_fixup_var_refs (function, lopart, part_type, part_mode, 0);
1456 schedule_fixup_var_refs (function, hipart, part_type, part_mode, 0);
1459 else
1460 return;
1463 /* Subroutine of put_var_into_stack. This puts a single pseudo reg REG
1464 into the stack frame of FUNCTION (0 means the current function).
1465 DECL_MODE is the machine mode of the user-level data type.
1466 PROMOTED_MODE is the machine mode of the register.
1467 VOLATILE_P is nonzero if this is for a "volatile" decl.
1468 USED_P is nonzero if this reg might have already been used in an insn. */
1470 static void
1471 put_reg_into_stack (function, reg, type, promoted_mode, decl_mode, volatile_p,
1472 original_regno, used_p, ht)
1473 struct function *function;
1474 rtx reg;
1475 tree type;
1476 enum machine_mode promoted_mode, decl_mode;
1477 int volatile_p;
1478 unsigned int original_regno;
1479 int used_p;
1480 htab_t ht;
1482 struct function *func = function ? function : cfun;
1483 rtx new = 0;
1484 unsigned int regno = original_regno;
1486 if (regno == 0)
1487 regno = REGNO (reg);
1489 if (regno < func->x_max_parm_reg)
1490 new = func->x_parm_reg_stack_loc[regno];
1492 if (new == 0)
1493 new = assign_stack_local_1 (decl_mode, GET_MODE_SIZE (decl_mode), 0, func);
1495 PUT_CODE (reg, MEM);
1496 PUT_MODE (reg, decl_mode);
1497 XEXP (reg, 0) = XEXP (new, 0);
1498 MEM_ATTRS (reg) = 0;
1499 /* `volatil' bit means one thing for MEMs, another entirely for REGs. */
1500 MEM_VOLATILE_P (reg) = volatile_p;
1502 /* If this is a memory ref that contains aggregate components,
1503 mark it as such for cse and loop optimize. If we are reusing a
1504 previously generated stack slot, then we need to copy the bit in
1505 case it was set for other reasons. For instance, it is set for
1506 __builtin_va_alist. */
1507 if (type)
1509 MEM_SET_IN_STRUCT_P (reg,
1510 AGGREGATE_TYPE_P (type) || MEM_IN_STRUCT_P (new));
1511 set_mem_alias_set (reg, get_alias_set (type));
1514 if (used_p)
1515 schedule_fixup_var_refs (function, reg, type, promoted_mode, ht);
1518 /* Make sure that all refs to the variable, previously made
1519 when it was a register, are fixed up to be valid again.
1520 See function above for meaning of arguments. */
1522 static void
1523 schedule_fixup_var_refs (function, reg, type, promoted_mode, ht)
1524 struct function *function;
1525 rtx reg;
1526 tree type;
1527 enum machine_mode promoted_mode;
1528 htab_t ht;
1530 int unsigned_p = type ? TREE_UNSIGNED (type) : 0;
1532 if (function != 0)
1534 struct var_refs_queue *temp;
1536 temp
1537 = (struct var_refs_queue *) ggc_alloc (sizeof (struct var_refs_queue));
1538 temp->modified = reg;
1539 temp->promoted_mode = promoted_mode;
1540 temp->unsignedp = unsigned_p;
1541 temp->next = function->fixup_var_refs_queue;
1542 function->fixup_var_refs_queue = temp;
1544 else
1545 /* Variable is local; fix it up now. */
1546 fixup_var_refs (reg, promoted_mode, unsigned_p, reg, ht);
1549 static void
1550 fixup_var_refs (var, promoted_mode, unsignedp, may_share, ht)
1551 rtx var;
1552 enum machine_mode promoted_mode;
1553 int unsignedp;
1554 htab_t ht;
1555 rtx may_share;
1557 tree pending;
1558 rtx first_insn = get_insns ();
1559 struct sequence_stack *stack = seq_stack;
1560 tree rtl_exps = rtl_expr_chain;
1562 /* If there's a hash table, it must record all uses of VAR. */
1563 if (ht)
1565 if (stack != 0)
1566 abort ();
1567 fixup_var_refs_insns_with_hash (ht, var, promoted_mode, unsignedp,
1568 may_share);
1569 return;
1572 fixup_var_refs_insns (first_insn, var, promoted_mode, unsignedp,
1573 stack == 0, may_share);
1575 /* Scan all pending sequences too. */
1576 for (; stack; stack = stack->next)
1578 push_to_full_sequence (stack->first, stack->last);
1579 fixup_var_refs_insns (stack->first, var, promoted_mode, unsignedp,
1580 stack->next != 0, may_share);
1581 /* Update remembered end of sequence
1582 in case we added an insn at the end. */
1583 stack->last = get_last_insn ();
1584 end_sequence ();
1587 /* Scan all waiting RTL_EXPRs too. */
1588 for (pending = rtl_exps; pending; pending = TREE_CHAIN (pending))
1590 rtx seq = RTL_EXPR_SEQUENCE (TREE_VALUE (pending));
1591 if (seq != const0_rtx && seq != 0)
1593 push_to_sequence (seq);
1594 fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0,
1595 may_share);
1596 end_sequence ();
1601 /* REPLACEMENTS is a pointer to a list of the struct fixup_replacement and X is
1602 some part of an insn. Return a struct fixup_replacement whose OLD
1603 value is equal to X. Allocate a new structure if no such entry exists. */
1605 static struct fixup_replacement *
1606 find_fixup_replacement (replacements, x)
1607 struct fixup_replacement **replacements;
1608 rtx x;
1610 struct fixup_replacement *p;
1612 /* See if we have already replaced this. */
1613 for (p = *replacements; p != 0 && ! rtx_equal_p (p->old, x); p = p->next)
1616 if (p == 0)
1618 p = (struct fixup_replacement *) xmalloc (sizeof (struct fixup_replacement));
1619 p->old = x;
1620 p->new = 0;
1621 p->next = *replacements;
1622 *replacements = p;
1625 return p;
1628 /* Scan the insn-chain starting with INSN for refs to VAR and fix them
1629 up. TOPLEVEL is nonzero if this chain is the main chain of insns
1630 for the current function. MAY_SHARE is either a MEM that is not
1631 to be unshared or a list of them. */
1633 static void
1634 fixup_var_refs_insns (insn, var, promoted_mode, unsignedp, toplevel, may_share)
1635 rtx insn;
1636 rtx var;
1637 enum machine_mode promoted_mode;
1638 int unsignedp;
1639 int toplevel;
1640 rtx may_share;
1642 while (insn)
1644 /* fixup_var_refs_insn might modify insn, so save its next
1645 pointer now. */
1646 rtx next = NEXT_INSN (insn);
1648 /* CALL_PLACEHOLDERs are special; we have to switch into each of
1649 the three sequences they (potentially) contain, and process
1650 them recursively. The CALL_INSN itself is not interesting. */
1652 if (GET_CODE (insn) == CALL_INSN
1653 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1655 int i;
1657 /* Look at the Normal call, sibling call and tail recursion
1658 sequences attached to the CALL_PLACEHOLDER. */
1659 for (i = 0; i < 3; i++)
1661 rtx seq = XEXP (PATTERN (insn), i);
1662 if (seq)
1664 push_to_sequence (seq);
1665 fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0,
1666 may_share);
1667 XEXP (PATTERN (insn), i) = get_insns ();
1668 end_sequence ();
1673 else if (INSN_P (insn))
1674 fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, toplevel,
1675 may_share);
1677 insn = next;
1681 /* Look up the insns which reference VAR in HT and fix them up. Other
1682 arguments are the same as fixup_var_refs_insns.
1684 N.B. No need for special processing of CALL_PLACEHOLDERs here,
1685 because the hash table will point straight to the interesting insn
1686 (inside the CALL_PLACEHOLDER). */
1688 static void
1689 fixup_var_refs_insns_with_hash (ht, var, promoted_mode, unsignedp, may_share)
1690 htab_t ht;
1691 rtx var;
1692 enum machine_mode promoted_mode;
1693 int unsignedp;
1694 rtx may_share;
1696 struct insns_for_mem_entry tmp;
1697 struct insns_for_mem_entry *ime;
1698 rtx insn_list;
1700 tmp.key = var;
1701 ime = (struct insns_for_mem_entry *) htab_find (ht, &tmp);
1702 for (insn_list = ime->insns; insn_list != 0; insn_list = XEXP (insn_list, 1))
1703 if (INSN_P (XEXP (insn_list, 0)))
1704 fixup_var_refs_insn (XEXP (insn_list, 0), var, promoted_mode,
1705 unsignedp, 1, may_share);
1709 /* Per-insn processing by fixup_var_refs_insns(_with_hash). INSN is
1710 the insn under examination, VAR is the variable to fix up
1711 references to, PROMOTED_MODE and UNSIGNEDP describe VAR, and
1712 TOPLEVEL is nonzero if this is the main insn chain for this
1713 function. */
1715 static void
1716 fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, toplevel, no_share)
1717 rtx insn;
1718 rtx var;
1719 enum machine_mode promoted_mode;
1720 int unsignedp;
1721 int toplevel;
1722 rtx no_share;
1724 rtx call_dest = 0;
1725 rtx set, prev, prev_set;
1726 rtx note;
1728 /* Remember the notes in case we delete the insn. */
1729 note = REG_NOTES (insn);
1731 /* If this is a CLOBBER of VAR, delete it.
1733 If it has a REG_LIBCALL note, delete the REG_LIBCALL
1734 and REG_RETVAL notes too. */
1735 if (GET_CODE (PATTERN (insn)) == CLOBBER
1736 && (XEXP (PATTERN (insn), 0) == var
1737 || (GET_CODE (XEXP (PATTERN (insn), 0)) == CONCAT
1738 && (XEXP (XEXP (PATTERN (insn), 0), 0) == var
1739 || XEXP (XEXP (PATTERN (insn), 0), 1) == var))))
1741 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0)
1742 /* The REG_LIBCALL note will go away since we are going to
1743 turn INSN into a NOTE, so just delete the
1744 corresponding REG_RETVAL note. */
1745 remove_note (XEXP (note, 0),
1746 find_reg_note (XEXP (note, 0), REG_RETVAL,
1747 NULL_RTX));
1749 delete_insn (insn);
1752 /* The insn to load VAR from a home in the arglist
1753 is now a no-op. When we see it, just delete it.
1754 Similarly if this is storing VAR from a register from which
1755 it was loaded in the previous insn. This will occur
1756 when an ADDRESSOF was made for an arglist slot. */
1757 else if (toplevel
1758 && (set = single_set (insn)) != 0
1759 && SET_DEST (set) == var
1760 /* If this represents the result of an insn group,
1761 don't delete the insn. */
1762 && find_reg_note (insn, REG_RETVAL, NULL_RTX) == 0
1763 && (rtx_equal_p (SET_SRC (set), var)
1764 || (GET_CODE (SET_SRC (set)) == REG
1765 && (prev = prev_nonnote_insn (insn)) != 0
1766 && (prev_set = single_set (prev)) != 0
1767 && SET_DEST (prev_set) == SET_SRC (set)
1768 && rtx_equal_p (SET_SRC (prev_set), var))))
1770 delete_insn (insn);
1772 else
1774 struct fixup_replacement *replacements = 0;
1775 rtx next_insn = NEXT_INSN (insn);
1777 if (SMALL_REGISTER_CLASSES)
1779 /* If the insn that copies the results of a CALL_INSN
1780 into a pseudo now references VAR, we have to use an
1781 intermediate pseudo since we want the life of the
1782 return value register to be only a single insn.
1784 If we don't use an intermediate pseudo, such things as
1785 address computations to make the address of VAR valid
1786 if it is not can be placed between the CALL_INSN and INSN.
1788 To make sure this doesn't happen, we record the destination
1789 of the CALL_INSN and see if the next insn uses both that
1790 and VAR. */
1792 if (call_dest != 0 && GET_CODE (insn) == INSN
1793 && reg_mentioned_p (var, PATTERN (insn))
1794 && reg_mentioned_p (call_dest, PATTERN (insn)))
1796 rtx temp = gen_reg_rtx (GET_MODE (call_dest));
1798 emit_insn_before (gen_move_insn (temp, call_dest), insn);
1800 PATTERN (insn) = replace_rtx (PATTERN (insn),
1801 call_dest, temp);
1804 if (GET_CODE (insn) == CALL_INSN
1805 && GET_CODE (PATTERN (insn)) == SET)
1806 call_dest = SET_DEST (PATTERN (insn));
1807 else if (GET_CODE (insn) == CALL_INSN
1808 && GET_CODE (PATTERN (insn)) == PARALLEL
1809 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1810 call_dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1811 else
1812 call_dest = 0;
1815 /* See if we have to do anything to INSN now that VAR is in
1816 memory. If it needs to be loaded into a pseudo, use a single
1817 pseudo for the entire insn in case there is a MATCH_DUP
1818 between two operands. We pass a pointer to the head of
1819 a list of struct fixup_replacements. If fixup_var_refs_1
1820 needs to allocate pseudos or replacement MEMs (for SUBREGs),
1821 it will record them in this list.
1823 If it allocated a pseudo for any replacement, we copy into
1824 it here. */
1826 fixup_var_refs_1 (var, promoted_mode, &PATTERN (insn), insn,
1827 &replacements, no_share);
1829 /* If this is last_parm_insn, and any instructions were output
1830 after it to fix it up, then we must set last_parm_insn to
1831 the last such instruction emitted. */
1832 if (insn == last_parm_insn)
1833 last_parm_insn = PREV_INSN (next_insn);
1835 while (replacements)
1837 struct fixup_replacement *next;
1839 if (GET_CODE (replacements->new) == REG)
1841 rtx insert_before;
1842 rtx seq;
1844 /* OLD might be a (subreg (mem)). */
1845 if (GET_CODE (replacements->old) == SUBREG)
1846 replacements->old
1847 = fixup_memory_subreg (replacements->old, insn,
1848 promoted_mode, 0);
1849 else
1850 replacements->old
1851 = fixup_stack_1 (replacements->old, insn);
1853 insert_before = insn;
1855 /* If we are changing the mode, do a conversion.
1856 This might be wasteful, but combine.c will
1857 eliminate much of the waste. */
1859 if (GET_MODE (replacements->new)
1860 != GET_MODE (replacements->old))
1862 start_sequence ();
1863 convert_move (replacements->new,
1864 replacements->old, unsignedp);
1865 seq = get_insns ();
1866 end_sequence ();
1868 else
1869 seq = gen_move_insn (replacements->new,
1870 replacements->old);
1872 emit_insn_before (seq, insert_before);
1875 next = replacements->next;
1876 free (replacements);
1877 replacements = next;
1881 /* Also fix up any invalid exprs in the REG_NOTES of this insn.
1882 But don't touch other insns referred to by reg-notes;
1883 we will get them elsewhere. */
1884 while (note)
1886 if (GET_CODE (note) != INSN_LIST)
1887 XEXP (note, 0)
1888 = walk_fixup_memory_subreg (XEXP (note, 0), insn,
1889 promoted_mode, 1);
1890 note = XEXP (note, 1);
1894 /* VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
1895 See if the rtx expression at *LOC in INSN needs to be changed.
1897 REPLACEMENTS is a pointer to a list head that starts out zero, but may
1898 contain a list of original rtx's and replacements. If we find that we need
1899 to modify this insn by replacing a memory reference with a pseudo or by
1900 making a new MEM to implement a SUBREG, we consult that list to see if
1901 we have already chosen a replacement. If none has already been allocated,
1902 we allocate it and update the list. fixup_var_refs_insn will copy VAR
1903 or the SUBREG, as appropriate, to the pseudo. */
1905 static void
1906 fixup_var_refs_1 (var, promoted_mode, loc, insn, replacements, no_share)
1907 rtx var;
1908 enum machine_mode promoted_mode;
1909 rtx *loc;
1910 rtx insn;
1911 struct fixup_replacement **replacements;
1912 rtx no_share;
1914 int i;
1915 rtx x = *loc;
1916 RTX_CODE code = GET_CODE (x);
1917 const char *fmt;
1918 rtx tem, tem1;
1919 struct fixup_replacement *replacement;
1921 switch (code)
1923 case ADDRESSOF:
1924 if (XEXP (x, 0) == var)
1926 /* Prevent sharing of rtl that might lose. */
1927 rtx sub = copy_rtx (XEXP (var, 0));
1929 if (! validate_change (insn, loc, sub, 0))
1931 rtx y = gen_reg_rtx (GET_MODE (sub));
1932 rtx seq, new_insn;
1934 /* We should be able to replace with a register or all is lost.
1935 Note that we can't use validate_change to verify this, since
1936 we're not caring for replacing all dups simultaneously. */
1937 if (! validate_replace_rtx (*loc, y, insn))
1938 abort ();
1940 /* Careful! First try to recognize a direct move of the
1941 value, mimicking how things are done in gen_reload wrt
1942 PLUS. Consider what happens when insn is a conditional
1943 move instruction and addsi3 clobbers flags. */
1945 start_sequence ();
1946 new_insn = emit_insn (gen_rtx_SET (VOIDmode, y, sub));
1947 seq = get_insns ();
1948 end_sequence ();
1950 if (recog_memoized (new_insn) < 0)
1952 /* That failed. Fall back on force_operand and hope. */
1954 start_sequence ();
1955 sub = force_operand (sub, y);
1956 if (sub != y)
1957 emit_insn (gen_move_insn (y, sub));
1958 seq = get_insns ();
1959 end_sequence ();
1962 #ifdef HAVE_cc0
1963 /* Don't separate setter from user. */
1964 if (PREV_INSN (insn) && sets_cc0_p (PREV_INSN (insn)))
1965 insn = PREV_INSN (insn);
1966 #endif
1968 emit_insn_before (seq, insn);
1971 return;
1973 case MEM:
1974 if (var == x)
1976 /* If we already have a replacement, use it. Otherwise,
1977 try to fix up this address in case it is invalid. */
1979 replacement = find_fixup_replacement (replacements, var);
1980 if (replacement->new)
1982 *loc = replacement->new;
1983 return;
1986 *loc = replacement->new = x = fixup_stack_1 (x, insn);
1988 /* Unless we are forcing memory to register or we changed the mode,
1989 we can leave things the way they are if the insn is valid. */
1991 INSN_CODE (insn) = -1;
1992 if (! flag_force_mem && GET_MODE (x) == promoted_mode
1993 && recog_memoized (insn) >= 0)
1994 return;
1996 *loc = replacement->new = gen_reg_rtx (promoted_mode);
1997 return;
2000 /* If X contains VAR, we need to unshare it here so that we update
2001 each occurrence separately. But all identical MEMs in one insn
2002 must be replaced with the same rtx because of the possibility of
2003 MATCH_DUPs. */
2005 if (reg_mentioned_p (var, x))
2007 replacement = find_fixup_replacement (replacements, x);
2008 if (replacement->new == 0)
2009 replacement->new = copy_most_rtx (x, no_share);
2011 *loc = x = replacement->new;
2012 code = GET_CODE (x);
2014 break;
2016 case REG:
2017 case CC0:
2018 case PC:
2019 case CONST_INT:
2020 case CONST:
2021 case SYMBOL_REF:
2022 case LABEL_REF:
2023 case CONST_DOUBLE:
2024 case CONST_VECTOR:
2025 return;
2027 case SIGN_EXTRACT:
2028 case ZERO_EXTRACT:
2029 /* Note that in some cases those types of expressions are altered
2030 by optimize_bit_field, and do not survive to get here. */
2031 if (XEXP (x, 0) == var
2032 || (GET_CODE (XEXP (x, 0)) == SUBREG
2033 && SUBREG_REG (XEXP (x, 0)) == var))
2035 /* Get TEM as a valid MEM in the mode presently in the insn.
2037 We don't worry about the possibility of MATCH_DUP here; it
2038 is highly unlikely and would be tricky to handle. */
2040 tem = XEXP (x, 0);
2041 if (GET_CODE (tem) == SUBREG)
2043 if (GET_MODE_BITSIZE (GET_MODE (tem))
2044 > GET_MODE_BITSIZE (GET_MODE (var)))
2046 replacement = find_fixup_replacement (replacements, var);
2047 if (replacement->new == 0)
2048 replacement->new = gen_reg_rtx (GET_MODE (var));
2049 SUBREG_REG (tem) = replacement->new;
2051 /* The following code works only if we have a MEM, so we
2052 need to handle the subreg here. We directly substitute
2053 it assuming that a subreg must be OK here. We already
2054 scheduled a replacement to copy the mem into the
2055 subreg. */
2056 XEXP (x, 0) = tem;
2057 return;
2059 else
2060 tem = fixup_memory_subreg (tem, insn, promoted_mode, 0);
2062 else
2063 tem = fixup_stack_1 (tem, insn);
2065 /* Unless we want to load from memory, get TEM into the proper mode
2066 for an extract from memory. This can only be done if the
2067 extract is at a constant position and length. */
2069 if (! flag_force_mem && GET_CODE (XEXP (x, 1)) == CONST_INT
2070 && GET_CODE (XEXP (x, 2)) == CONST_INT
2071 && ! mode_dependent_address_p (XEXP (tem, 0))
2072 && ! MEM_VOLATILE_P (tem))
2074 enum machine_mode wanted_mode = VOIDmode;
2075 enum machine_mode is_mode = GET_MODE (tem);
2076 HOST_WIDE_INT pos = INTVAL (XEXP (x, 2));
2078 if (GET_CODE (x) == ZERO_EXTRACT)
2080 enum machine_mode new_mode
2081 = mode_for_extraction (EP_extzv, 1);
2082 if (new_mode != MAX_MACHINE_MODE)
2083 wanted_mode = new_mode;
2085 else if (GET_CODE (x) == SIGN_EXTRACT)
2087 enum machine_mode new_mode
2088 = mode_for_extraction (EP_extv, 1);
2089 if (new_mode != MAX_MACHINE_MODE)
2090 wanted_mode = new_mode;
2093 /* If we have a narrower mode, we can do something. */
2094 if (wanted_mode != VOIDmode
2095 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2097 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2098 rtx old_pos = XEXP (x, 2);
2099 rtx newmem;
2101 /* If the bytes and bits are counted differently, we
2102 must adjust the offset. */
2103 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2104 offset = (GET_MODE_SIZE (is_mode)
2105 - GET_MODE_SIZE (wanted_mode) - offset);
2107 pos %= GET_MODE_BITSIZE (wanted_mode);
2109 newmem = adjust_address_nv (tem, wanted_mode, offset);
2111 /* Make the change and see if the insn remains valid. */
2112 INSN_CODE (insn) = -1;
2113 XEXP (x, 0) = newmem;
2114 XEXP (x, 2) = GEN_INT (pos);
2116 if (recog_memoized (insn) >= 0)
2117 return;
2119 /* Otherwise, restore old position. XEXP (x, 0) will be
2120 restored later. */
2121 XEXP (x, 2) = old_pos;
2125 /* If we get here, the bitfield extract insn can't accept a memory
2126 reference. Copy the input into a register. */
2128 tem1 = gen_reg_rtx (GET_MODE (tem));
2129 emit_insn_before (gen_move_insn (tem1, tem), insn);
2130 XEXP (x, 0) = tem1;
2131 return;
2133 break;
2135 case SUBREG:
2136 if (SUBREG_REG (x) == var)
2138 /* If this is a special SUBREG made because VAR was promoted
2139 from a wider mode, replace it with VAR and call ourself
2140 recursively, this time saying that the object previously
2141 had its current mode (by virtue of the SUBREG). */
2143 if (SUBREG_PROMOTED_VAR_P (x))
2145 *loc = var;
2146 fixup_var_refs_1 (var, GET_MODE (var), loc, insn, replacements,
2147 no_share);
2148 return;
2151 /* If this SUBREG makes VAR wider, it has become a paradoxical
2152 SUBREG with VAR in memory, but these aren't allowed at this
2153 stage of the compilation. So load VAR into a pseudo and take
2154 a SUBREG of that pseudo. */
2155 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (var)))
2157 replacement = find_fixup_replacement (replacements, var);
2158 if (replacement->new == 0)
2159 replacement->new = gen_reg_rtx (promoted_mode);
2160 SUBREG_REG (x) = replacement->new;
2161 return;
2164 /* See if we have already found a replacement for this SUBREG.
2165 If so, use it. Otherwise, make a MEM and see if the insn
2166 is recognized. If not, or if we should force MEM into a register,
2167 make a pseudo for this SUBREG. */
2168 replacement = find_fixup_replacement (replacements, x);
2169 if (replacement->new)
2171 *loc = replacement->new;
2172 return;
2175 replacement->new = *loc = fixup_memory_subreg (x, insn,
2176 promoted_mode, 0);
2178 INSN_CODE (insn) = -1;
2179 if (! flag_force_mem && recog_memoized (insn) >= 0)
2180 return;
2182 *loc = replacement->new = gen_reg_rtx (GET_MODE (x));
2183 return;
2185 break;
2187 case SET:
2188 /* First do special simplification of bit-field references. */
2189 if (GET_CODE (SET_DEST (x)) == SIGN_EXTRACT
2190 || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2191 optimize_bit_field (x, insn, 0);
2192 if (GET_CODE (SET_SRC (x)) == SIGN_EXTRACT
2193 || GET_CODE (SET_SRC (x)) == ZERO_EXTRACT)
2194 optimize_bit_field (x, insn, 0);
2196 /* For a paradoxical SUBREG inside a ZERO_EXTRACT, load the object
2197 into a register and then store it back out. */
2198 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2199 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG
2200 && SUBREG_REG (XEXP (SET_DEST (x), 0)) == var
2201 && (GET_MODE_SIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2202 > GET_MODE_SIZE (GET_MODE (var))))
2204 replacement = find_fixup_replacement (replacements, var);
2205 if (replacement->new == 0)
2206 replacement->new = gen_reg_rtx (GET_MODE (var));
2208 SUBREG_REG (XEXP (SET_DEST (x), 0)) = replacement->new;
2209 emit_insn_after (gen_move_insn (var, replacement->new), insn);
2212 /* If SET_DEST is now a paradoxical SUBREG, put the result of this
2213 insn into a pseudo and store the low part of the pseudo into VAR. */
2214 if (GET_CODE (SET_DEST (x)) == SUBREG
2215 && SUBREG_REG (SET_DEST (x)) == var
2216 && (GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
2217 > GET_MODE_SIZE (GET_MODE (var))))
2219 SET_DEST (x) = tem = gen_reg_rtx (GET_MODE (SET_DEST (x)));
2220 emit_insn_after (gen_move_insn (var, gen_lowpart (GET_MODE (var),
2221 tem)),
2222 insn);
2223 break;
2227 rtx dest = SET_DEST (x);
2228 rtx src = SET_SRC (x);
2229 rtx outerdest = dest;
2231 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2232 || GET_CODE (dest) == SIGN_EXTRACT
2233 || GET_CODE (dest) == ZERO_EXTRACT)
2234 dest = XEXP (dest, 0);
2236 if (GET_CODE (src) == SUBREG)
2237 src = SUBREG_REG (src);
2239 /* If VAR does not appear at the top level of the SET
2240 just scan the lower levels of the tree. */
2242 if (src != var && dest != var)
2243 break;
2245 /* We will need to rerecognize this insn. */
2246 INSN_CODE (insn) = -1;
2248 if (GET_CODE (outerdest) == ZERO_EXTRACT && dest == var
2249 && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
2251 /* Since this case will return, ensure we fixup all the
2252 operands here. */
2253 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 1),
2254 insn, replacements, no_share);
2255 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 2),
2256 insn, replacements, no_share);
2257 fixup_var_refs_1 (var, promoted_mode, &SET_SRC (x),
2258 insn, replacements, no_share);
2260 tem = XEXP (outerdest, 0);
2262 /* Clean up (SUBREG:SI (MEM:mode ...) 0)
2263 that may appear inside a ZERO_EXTRACT.
2264 This was legitimate when the MEM was a REG. */
2265 if (GET_CODE (tem) == SUBREG
2266 && SUBREG_REG (tem) == var)
2267 tem = fixup_memory_subreg (tem, insn, promoted_mode, 0);
2268 else
2269 tem = fixup_stack_1 (tem, insn);
2271 if (GET_CODE (XEXP (outerdest, 1)) == CONST_INT
2272 && GET_CODE (XEXP (outerdest, 2)) == CONST_INT
2273 && ! mode_dependent_address_p (XEXP (tem, 0))
2274 && ! MEM_VOLATILE_P (tem))
2276 enum machine_mode wanted_mode;
2277 enum machine_mode is_mode = GET_MODE (tem);
2278 HOST_WIDE_INT pos = INTVAL (XEXP (outerdest, 2));
2280 wanted_mode = mode_for_extraction (EP_insv, 0);
2282 /* If we have a narrower mode, we can do something. */
2283 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2285 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2286 rtx old_pos = XEXP (outerdest, 2);
2287 rtx newmem;
2289 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2290 offset = (GET_MODE_SIZE (is_mode)
2291 - GET_MODE_SIZE (wanted_mode) - offset);
2293 pos %= GET_MODE_BITSIZE (wanted_mode);
2295 newmem = adjust_address_nv (tem, wanted_mode, offset);
2297 /* Make the change and see if the insn remains valid. */
2298 INSN_CODE (insn) = -1;
2299 XEXP (outerdest, 0) = newmem;
2300 XEXP (outerdest, 2) = GEN_INT (pos);
2302 if (recog_memoized (insn) >= 0)
2303 return;
2305 /* Otherwise, restore old position. XEXP (x, 0) will be
2306 restored later. */
2307 XEXP (outerdest, 2) = old_pos;
2311 /* If we get here, the bit-field store doesn't allow memory
2312 or isn't located at a constant position. Load the value into
2313 a register, do the store, and put it back into memory. */
2315 tem1 = gen_reg_rtx (GET_MODE (tem));
2316 emit_insn_before (gen_move_insn (tem1, tem), insn);
2317 emit_insn_after (gen_move_insn (tem, tem1), insn);
2318 XEXP (outerdest, 0) = tem1;
2319 return;
2322 /* STRICT_LOW_PART is a no-op on memory references
2323 and it can cause combinations to be unrecognizable,
2324 so eliminate it. */
2326 if (dest == var && GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2327 SET_DEST (x) = XEXP (SET_DEST (x), 0);
2329 /* A valid insn to copy VAR into or out of a register
2330 must be left alone, to avoid an infinite loop here.
2331 If the reference to VAR is by a subreg, fix that up,
2332 since SUBREG is not valid for a memref.
2333 Also fix up the address of the stack slot.
2335 Note that we must not try to recognize the insn until
2336 after we know that we have valid addresses and no
2337 (subreg (mem ...) ...) constructs, since these interfere
2338 with determining the validity of the insn. */
2340 if ((SET_SRC (x) == var
2341 || (GET_CODE (SET_SRC (x)) == SUBREG
2342 && SUBREG_REG (SET_SRC (x)) == var))
2343 && (GET_CODE (SET_DEST (x)) == REG
2344 || (GET_CODE (SET_DEST (x)) == SUBREG
2345 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG))
2346 && GET_MODE (var) == promoted_mode
2347 && x == single_set (insn))
2349 rtx pat, last;
2351 if (GET_CODE (SET_SRC (x)) == SUBREG
2352 && (GET_MODE_SIZE (GET_MODE (SET_SRC (x)))
2353 > GET_MODE_SIZE (GET_MODE (var))))
2355 /* This (subreg VAR) is now a paradoxical subreg. We need
2356 to replace VAR instead of the subreg. */
2357 replacement = find_fixup_replacement (replacements, var);
2358 if (replacement->new == NULL_RTX)
2359 replacement->new = gen_reg_rtx (GET_MODE (var));
2360 SUBREG_REG (SET_SRC (x)) = replacement->new;
2362 else
2364 replacement = find_fixup_replacement (replacements, SET_SRC (x));
2365 if (replacement->new)
2366 SET_SRC (x) = replacement->new;
2367 else if (GET_CODE (SET_SRC (x)) == SUBREG)
2368 SET_SRC (x) = replacement->new
2369 = fixup_memory_subreg (SET_SRC (x), insn, promoted_mode,
2371 else
2372 SET_SRC (x) = replacement->new
2373 = fixup_stack_1 (SET_SRC (x), insn);
2376 if (recog_memoized (insn) >= 0)
2377 return;
2379 /* INSN is not valid, but we know that we want to
2380 copy SET_SRC (x) to SET_DEST (x) in some way. So
2381 we generate the move and see whether it requires more
2382 than one insn. If it does, we emit those insns and
2383 delete INSN. Otherwise, we an just replace the pattern
2384 of INSN; we have already verified above that INSN has
2385 no other function that to do X. */
2387 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2388 if (NEXT_INSN (pat) != NULL_RTX)
2390 last = emit_insn_before (pat, insn);
2392 /* INSN might have REG_RETVAL or other important notes, so
2393 we need to store the pattern of the last insn in the
2394 sequence into INSN similarly to the normal case. LAST
2395 should not have REG_NOTES, but we allow them if INSN has
2396 no REG_NOTES. */
2397 if (REG_NOTES (last) && REG_NOTES (insn))
2398 abort ();
2399 if (REG_NOTES (last))
2400 REG_NOTES (insn) = REG_NOTES (last);
2401 PATTERN (insn) = PATTERN (last);
2403 delete_insn (last);
2405 else
2406 PATTERN (insn) = PATTERN (pat);
2408 return;
2411 if ((SET_DEST (x) == var
2412 || (GET_CODE (SET_DEST (x)) == SUBREG
2413 && SUBREG_REG (SET_DEST (x)) == var))
2414 && (GET_CODE (SET_SRC (x)) == REG
2415 || (GET_CODE (SET_SRC (x)) == SUBREG
2416 && GET_CODE (SUBREG_REG (SET_SRC (x))) == REG))
2417 && GET_MODE (var) == promoted_mode
2418 && x == single_set (insn))
2420 rtx pat, last;
2422 if (GET_CODE (SET_DEST (x)) == SUBREG)
2423 SET_DEST (x) = fixup_memory_subreg (SET_DEST (x), insn,
2424 promoted_mode, 0);
2425 else
2426 SET_DEST (x) = fixup_stack_1 (SET_DEST (x), insn);
2428 if (recog_memoized (insn) >= 0)
2429 return;
2431 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2432 if (NEXT_INSN (pat) != NULL_RTX)
2434 last = emit_insn_before (pat, insn);
2436 /* INSN might have REG_RETVAL or other important notes, so
2437 we need to store the pattern of the last insn in the
2438 sequence into INSN similarly to the normal case. LAST
2439 should not have REG_NOTES, but we allow them if INSN has
2440 no REG_NOTES. */
2441 if (REG_NOTES (last) && REG_NOTES (insn))
2442 abort ();
2443 if (REG_NOTES (last))
2444 REG_NOTES (insn) = REG_NOTES (last);
2445 PATTERN (insn) = PATTERN (last);
2447 delete_insn (last);
2449 else
2450 PATTERN (insn) = PATTERN (pat);
2452 return;
2455 /* Otherwise, storing into VAR must be handled specially
2456 by storing into a temporary and copying that into VAR
2457 with a new insn after this one. Note that this case
2458 will be used when storing into a promoted scalar since
2459 the insn will now have different modes on the input
2460 and output and hence will be invalid (except for the case
2461 of setting it to a constant, which does not need any
2462 change if it is valid). We generate extra code in that case,
2463 but combine.c will eliminate it. */
2465 if (dest == var)
2467 rtx temp;
2468 rtx fixeddest = SET_DEST (x);
2469 enum machine_mode temp_mode;
2471 /* STRICT_LOW_PART can be discarded, around a MEM. */
2472 if (GET_CODE (fixeddest) == STRICT_LOW_PART)
2473 fixeddest = XEXP (fixeddest, 0);
2474 /* Convert (SUBREG (MEM)) to a MEM in a changed mode. */
2475 if (GET_CODE (fixeddest) == SUBREG)
2477 fixeddest = fixup_memory_subreg (fixeddest, insn,
2478 promoted_mode, 0);
2479 temp_mode = GET_MODE (fixeddest);
2481 else
2483 fixeddest = fixup_stack_1 (fixeddest, insn);
2484 temp_mode = promoted_mode;
2487 temp = gen_reg_rtx (temp_mode);
2489 emit_insn_after (gen_move_insn (fixeddest,
2490 gen_lowpart (GET_MODE (fixeddest),
2491 temp)),
2492 insn);
2494 SET_DEST (x) = temp;
2498 default:
2499 break;
2502 /* Nothing special about this RTX; fix its operands. */
2504 fmt = GET_RTX_FORMAT (code);
2505 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2507 if (fmt[i] == 'e')
2508 fixup_var_refs_1 (var, promoted_mode, &XEXP (x, i), insn, replacements,
2509 no_share);
2510 else if (fmt[i] == 'E')
2512 int j;
2513 for (j = 0; j < XVECLEN (x, i); j++)
2514 fixup_var_refs_1 (var, promoted_mode, &XVECEXP (x, i, j),
2515 insn, replacements, no_share);
2520 /* Previously, X had the form (SUBREG:m1 (REG:PROMOTED_MODE ...)).
2521 The REG was placed on the stack, so X now has the form (SUBREG:m1
2522 (MEM:m2 ...)).
2524 Return an rtx (MEM:m1 newaddr) which is equivalent. If any insns
2525 must be emitted to compute NEWADDR, put them before INSN.
2527 UNCRITICAL nonzero means accept paradoxical subregs.
2528 This is used for subregs found inside REG_NOTES. */
2530 static rtx
2531 fixup_memory_subreg (x, insn, promoted_mode, uncritical)
2532 rtx x;
2533 rtx insn;
2534 enum machine_mode promoted_mode;
2535 int uncritical;
2537 int offset;
2538 rtx mem = SUBREG_REG (x);
2539 rtx addr = XEXP (mem, 0);
2540 enum machine_mode mode = GET_MODE (x);
2541 rtx result, seq;
2543 /* Paradoxical SUBREGs are usually invalid during RTL generation. */
2544 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (mem)) && ! uncritical)
2545 abort ();
2547 offset = SUBREG_BYTE (x);
2548 if (BYTES_BIG_ENDIAN)
2549 /* If the PROMOTED_MODE is wider than the mode of the MEM, adjust
2550 the offset so that it points to the right location within the
2551 MEM. */
2552 offset -= (GET_MODE_SIZE (promoted_mode) - GET_MODE_SIZE (GET_MODE (mem)));
2554 if (!flag_force_addr
2555 && memory_address_p (mode, plus_constant (addr, offset)))
2556 /* Shortcut if no insns need be emitted. */
2557 return adjust_address (mem, mode, offset);
2559 start_sequence ();
2560 result = adjust_address (mem, mode, offset);
2561 seq = get_insns ();
2562 end_sequence ();
2564 emit_insn_before (seq, insn);
2565 return result;
2568 /* Do fixup_memory_subreg on all (SUBREG (MEM ...) ...) contained in X.
2569 Replace subexpressions of X in place.
2570 If X itself is a (SUBREG (MEM ...) ...), return the replacement expression.
2571 Otherwise return X, with its contents possibly altered.
2573 INSN, PROMOTED_MODE and UNCRITICAL are as for
2574 fixup_memory_subreg. */
2576 static rtx
2577 walk_fixup_memory_subreg (x, insn, promoted_mode, uncritical)
2578 rtx x;
2579 rtx insn;
2580 enum machine_mode promoted_mode;
2581 int uncritical;
2583 enum rtx_code code;
2584 const char *fmt;
2585 int i;
2587 if (x == 0)
2588 return 0;
2590 code = GET_CODE (x);
2592 if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
2593 return fixup_memory_subreg (x, insn, promoted_mode, uncritical);
2595 /* Nothing special about this RTX; fix its operands. */
2597 fmt = GET_RTX_FORMAT (code);
2598 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2600 if (fmt[i] == 'e')
2601 XEXP (x, i) = walk_fixup_memory_subreg (XEXP (x, i), insn,
2602 promoted_mode, uncritical);
2603 else if (fmt[i] == 'E')
2605 int j;
2606 for (j = 0; j < XVECLEN (x, i); j++)
2607 XVECEXP (x, i, j)
2608 = walk_fixup_memory_subreg (XVECEXP (x, i, j), insn,
2609 promoted_mode, uncritical);
2612 return x;
2615 /* For each memory ref within X, if it refers to a stack slot
2616 with an out of range displacement, put the address in a temp register
2617 (emitting new insns before INSN to load these registers)
2618 and alter the memory ref to use that register.
2619 Replace each such MEM rtx with a copy, to avoid clobberage. */
2621 static rtx
2622 fixup_stack_1 (x, insn)
2623 rtx x;
2624 rtx insn;
2626 int i;
2627 RTX_CODE code = GET_CODE (x);
2628 const char *fmt;
2630 if (code == MEM)
2632 rtx ad = XEXP (x, 0);
2633 /* If we have address of a stack slot but it's not valid
2634 (displacement is too large), compute the sum in a register. */
2635 if (GET_CODE (ad) == PLUS
2636 && GET_CODE (XEXP (ad, 0)) == REG
2637 && ((REGNO (XEXP (ad, 0)) >= FIRST_VIRTUAL_REGISTER
2638 && REGNO (XEXP (ad, 0)) <= LAST_VIRTUAL_REGISTER)
2639 || REGNO (XEXP (ad, 0)) == FRAME_POINTER_REGNUM
2640 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2641 || REGNO (XEXP (ad, 0)) == HARD_FRAME_POINTER_REGNUM
2642 #endif
2643 || REGNO (XEXP (ad, 0)) == STACK_POINTER_REGNUM
2644 || REGNO (XEXP (ad, 0)) == ARG_POINTER_REGNUM
2645 || XEXP (ad, 0) == current_function_internal_arg_pointer)
2646 && GET_CODE (XEXP (ad, 1)) == CONST_INT)
2648 rtx temp, seq;
2649 if (memory_address_p (GET_MODE (x), ad))
2650 return x;
2652 start_sequence ();
2653 temp = copy_to_reg (ad);
2654 seq = get_insns ();
2655 end_sequence ();
2656 emit_insn_before (seq, insn);
2657 return replace_equiv_address (x, temp);
2659 return x;
2662 fmt = GET_RTX_FORMAT (code);
2663 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2665 if (fmt[i] == 'e')
2666 XEXP (x, i) = fixup_stack_1 (XEXP (x, i), insn);
2667 else if (fmt[i] == 'E')
2669 int j;
2670 for (j = 0; j < XVECLEN (x, i); j++)
2671 XVECEXP (x, i, j) = fixup_stack_1 (XVECEXP (x, i, j), insn);
2674 return x;
2677 /* Optimization: a bit-field instruction whose field
2678 happens to be a byte or halfword in memory
2679 can be changed to a move instruction.
2681 We call here when INSN is an insn to examine or store into a bit-field.
2682 BODY is the SET-rtx to be altered.
2684 EQUIV_MEM is the table `reg_equiv_mem' if that is available; else 0.
2685 (Currently this is called only from function.c, and EQUIV_MEM
2686 is always 0.) */
2688 static void
2689 optimize_bit_field (body, insn, equiv_mem)
2690 rtx body;
2691 rtx insn;
2692 rtx *equiv_mem;
2694 rtx bitfield;
2695 int destflag;
2696 rtx seq = 0;
2697 enum machine_mode mode;
2699 if (GET_CODE (SET_DEST (body)) == SIGN_EXTRACT
2700 || GET_CODE (SET_DEST (body)) == ZERO_EXTRACT)
2701 bitfield = SET_DEST (body), destflag = 1;
2702 else
2703 bitfield = SET_SRC (body), destflag = 0;
2705 /* First check that the field being stored has constant size and position
2706 and is in fact a byte or halfword suitably aligned. */
2708 if (GET_CODE (XEXP (bitfield, 1)) == CONST_INT
2709 && GET_CODE (XEXP (bitfield, 2)) == CONST_INT
2710 && ((mode = mode_for_size (INTVAL (XEXP (bitfield, 1)), MODE_INT, 1))
2711 != BLKmode)
2712 && INTVAL (XEXP (bitfield, 2)) % INTVAL (XEXP (bitfield, 1)) == 0)
2714 rtx memref = 0;
2716 /* Now check that the containing word is memory, not a register,
2717 and that it is safe to change the machine mode. */
2719 if (GET_CODE (XEXP (bitfield, 0)) == MEM)
2720 memref = XEXP (bitfield, 0);
2721 else if (GET_CODE (XEXP (bitfield, 0)) == REG
2722 && equiv_mem != 0)
2723 memref = equiv_mem[REGNO (XEXP (bitfield, 0))];
2724 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2725 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == MEM)
2726 memref = SUBREG_REG (XEXP (bitfield, 0));
2727 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2728 && equiv_mem != 0
2729 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == REG)
2730 memref = equiv_mem[REGNO (SUBREG_REG (XEXP (bitfield, 0)))];
2732 if (memref
2733 && ! mode_dependent_address_p (XEXP (memref, 0))
2734 && ! MEM_VOLATILE_P (memref))
2736 /* Now adjust the address, first for any subreg'ing
2737 that we are now getting rid of,
2738 and then for which byte of the word is wanted. */
2740 HOST_WIDE_INT offset = INTVAL (XEXP (bitfield, 2));
2741 rtx insns;
2743 /* Adjust OFFSET to count bits from low-address byte. */
2744 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
2745 offset = (GET_MODE_BITSIZE (GET_MODE (XEXP (bitfield, 0)))
2746 - offset - INTVAL (XEXP (bitfield, 1)));
2748 /* Adjust OFFSET to count bytes from low-address byte. */
2749 offset /= BITS_PER_UNIT;
2750 if (GET_CODE (XEXP (bitfield, 0)) == SUBREG)
2752 offset += (SUBREG_BYTE (XEXP (bitfield, 0))
2753 / UNITS_PER_WORD) * UNITS_PER_WORD;
2754 if (BYTES_BIG_ENDIAN)
2755 offset -= (MIN (UNITS_PER_WORD,
2756 GET_MODE_SIZE (GET_MODE (XEXP (bitfield, 0))))
2757 - MIN (UNITS_PER_WORD,
2758 GET_MODE_SIZE (GET_MODE (memref))));
2761 start_sequence ();
2762 memref = adjust_address (memref, mode, offset);
2763 insns = get_insns ();
2764 end_sequence ();
2765 emit_insn_before (insns, insn);
2767 /* Store this memory reference where
2768 we found the bit field reference. */
2770 if (destflag)
2772 validate_change (insn, &SET_DEST (body), memref, 1);
2773 if (! CONSTANT_ADDRESS_P (SET_SRC (body)))
2775 rtx src = SET_SRC (body);
2776 while (GET_CODE (src) == SUBREG
2777 && SUBREG_BYTE (src) == 0)
2778 src = SUBREG_REG (src);
2779 if (GET_MODE (src) != GET_MODE (memref))
2780 src = gen_lowpart (GET_MODE (memref), SET_SRC (body));
2781 validate_change (insn, &SET_SRC (body), src, 1);
2783 else if (GET_MODE (SET_SRC (body)) != VOIDmode
2784 && GET_MODE (SET_SRC (body)) != GET_MODE (memref))
2785 /* This shouldn't happen because anything that didn't have
2786 one of these modes should have got converted explicitly
2787 and then referenced through a subreg.
2788 This is so because the original bit-field was
2789 handled by agg_mode and so its tree structure had
2790 the same mode that memref now has. */
2791 abort ();
2793 else
2795 rtx dest = SET_DEST (body);
2797 while (GET_CODE (dest) == SUBREG
2798 && SUBREG_BYTE (dest) == 0
2799 && (GET_MODE_CLASS (GET_MODE (dest))
2800 == GET_MODE_CLASS (GET_MODE (SUBREG_REG (dest))))
2801 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
2802 <= UNITS_PER_WORD))
2803 dest = SUBREG_REG (dest);
2805 validate_change (insn, &SET_DEST (body), dest, 1);
2807 if (GET_MODE (dest) == GET_MODE (memref))
2808 validate_change (insn, &SET_SRC (body), memref, 1);
2809 else
2811 /* Convert the mem ref to the destination mode. */
2812 rtx newreg = gen_reg_rtx (GET_MODE (dest));
2814 start_sequence ();
2815 convert_move (newreg, memref,
2816 GET_CODE (SET_SRC (body)) == ZERO_EXTRACT);
2817 seq = get_insns ();
2818 end_sequence ();
2820 validate_change (insn, &SET_SRC (body), newreg, 1);
2824 /* See if we can convert this extraction or insertion into
2825 a simple move insn. We might not be able to do so if this
2826 was, for example, part of a PARALLEL.
2828 If we succeed, write out any needed conversions. If we fail,
2829 it is hard to guess why we failed, so don't do anything
2830 special; just let the optimization be suppressed. */
2832 if (apply_change_group () && seq)
2833 emit_insn_before (seq, insn);
2838 /* These routines are responsible for converting virtual register references
2839 to the actual hard register references once RTL generation is complete.
2841 The following four variables are used for communication between the
2842 routines. They contain the offsets of the virtual registers from their
2843 respective hard registers. */
2845 static int in_arg_offset;
2846 static int var_offset;
2847 static int dynamic_offset;
2848 static int out_arg_offset;
2849 static int cfa_offset;
2851 /* In most machines, the stack pointer register is equivalent to the bottom
2852 of the stack. */
2854 #ifndef STACK_POINTER_OFFSET
2855 #define STACK_POINTER_OFFSET 0
2856 #endif
2858 /* If not defined, pick an appropriate default for the offset of dynamically
2859 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
2860 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
2862 #ifndef STACK_DYNAMIC_OFFSET
2864 /* The bottom of the stack points to the actual arguments. If
2865 REG_PARM_STACK_SPACE is defined, this includes the space for the register
2866 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
2867 stack space for register parameters is not pushed by the caller, but
2868 rather part of the fixed stack areas and hence not included in
2869 `current_function_outgoing_args_size'. Nevertheless, we must allow
2870 for it when allocating stack dynamic objects. */
2872 #if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
2873 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2874 ((ACCUMULATE_OUTGOING_ARGS \
2875 ? (current_function_outgoing_args_size + REG_PARM_STACK_SPACE (FNDECL)) : 0)\
2876 + (STACK_POINTER_OFFSET)) \
2878 #else
2879 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2880 ((ACCUMULATE_OUTGOING_ARGS ? current_function_outgoing_args_size : 0) \
2881 + (STACK_POINTER_OFFSET))
2882 #endif
2883 #endif
2885 /* On most machines, the CFA coincides with the first incoming parm. */
2887 #ifndef ARG_POINTER_CFA_OFFSET
2888 #define ARG_POINTER_CFA_OFFSET(FNDECL) FIRST_PARM_OFFSET (FNDECL)
2889 #endif
2891 /* Build up a (MEM (ADDRESSOF (REG))) rtx for a register REG that just had its
2892 address taken. DECL is the decl or SAVE_EXPR for the object stored in the
2893 register, for later use if we do need to force REG into the stack. REG is
2894 overwritten by the MEM like in put_reg_into_stack. */
2897 gen_mem_addressof (reg, decl)
2898 rtx reg;
2899 tree decl;
2901 rtx r = gen_rtx_ADDRESSOF (Pmode, gen_reg_rtx (GET_MODE (reg)),
2902 REGNO (reg), decl);
2904 /* Calculate this before we start messing with decl's RTL. */
2905 HOST_WIDE_INT set = decl ? get_alias_set (decl) : 0;
2907 /* If the original REG was a user-variable, then so is the REG whose
2908 address is being taken. Likewise for unchanging. */
2909 REG_USERVAR_P (XEXP (r, 0)) = REG_USERVAR_P (reg);
2910 RTX_UNCHANGING_P (XEXP (r, 0)) = RTX_UNCHANGING_P (reg);
2912 PUT_CODE (reg, MEM);
2913 MEM_ATTRS (reg) = 0;
2914 XEXP (reg, 0) = r;
2916 if (decl)
2918 tree type = TREE_TYPE (decl);
2919 enum machine_mode decl_mode
2920 = (DECL_P (decl) ? DECL_MODE (decl) : TYPE_MODE (TREE_TYPE (decl)));
2921 rtx decl_rtl = (TREE_CODE (decl) == SAVE_EXPR ? SAVE_EXPR_RTL (decl)
2922 : DECL_RTL_IF_SET (decl));
2924 PUT_MODE (reg, decl_mode);
2926 /* Clear DECL_RTL momentarily so functions below will work
2927 properly, then set it again. */
2928 if (DECL_P (decl) && decl_rtl == reg)
2929 SET_DECL_RTL (decl, 0);
2931 set_mem_attributes (reg, decl, 1);
2932 set_mem_alias_set (reg, set);
2934 if (DECL_P (decl) && decl_rtl == reg)
2935 SET_DECL_RTL (decl, reg);
2937 if (TREE_USED (decl) || (DECL_P (decl) && DECL_INITIAL (decl) != 0))
2938 fixup_var_refs (reg, GET_MODE (reg), TREE_UNSIGNED (type), reg, 0);
2940 else
2941 fixup_var_refs (reg, GET_MODE (reg), 0, reg, 0);
2943 return reg;
2946 /* If DECL has an RTL that is an ADDRESSOF rtx, put it into the stack. */
2948 void
2949 flush_addressof (decl)
2950 tree decl;
2952 if ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL)
2953 && DECL_RTL (decl) != 0
2954 && GET_CODE (DECL_RTL (decl)) == MEM
2955 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF
2956 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == REG)
2957 put_addressof_into_stack (XEXP (DECL_RTL (decl), 0), 0);
2960 /* Force the register pointed to by R, an ADDRESSOF rtx, into the stack. */
2962 static void
2963 put_addressof_into_stack (r, ht)
2964 rtx r;
2965 htab_t ht;
2967 tree decl, type;
2968 int volatile_p, used_p;
2970 rtx reg = XEXP (r, 0);
2972 if (GET_CODE (reg) != REG)
2973 abort ();
2975 decl = ADDRESSOF_DECL (r);
2976 if (decl)
2978 type = TREE_TYPE (decl);
2979 volatile_p = (TREE_CODE (decl) != SAVE_EXPR
2980 && TREE_THIS_VOLATILE (decl));
2981 used_p = (TREE_USED (decl)
2982 || (DECL_P (decl) && DECL_INITIAL (decl) != 0));
2984 else
2986 type = NULL_TREE;
2987 volatile_p = 0;
2988 used_p = 1;
2991 put_reg_into_stack (0, reg, type, GET_MODE (reg), GET_MODE (reg),
2992 volatile_p, ADDRESSOF_REGNO (r), used_p, ht);
2995 /* List of replacements made below in purge_addressof_1 when creating
2996 bitfield insertions. */
2997 static rtx purge_bitfield_addressof_replacements;
2999 /* List of replacements made below in purge_addressof_1 for patterns
3000 (MEM (ADDRESSOF (REG ...))). The key of the list entry is the
3001 corresponding (ADDRESSOF (REG ...)) and value is a substitution for
3002 the all pattern. List PURGE_BITFIELD_ADDRESSOF_REPLACEMENTS is not
3003 enough in complex cases, e.g. when some field values can be
3004 extracted by usage MEM with narrower mode. */
3005 static rtx purge_addressof_replacements;
3007 /* Helper function for purge_addressof. See if the rtx expression at *LOC
3008 in INSN needs to be changed. If FORCE, always put any ADDRESSOFs into
3009 the stack. If the function returns FALSE then the replacement could not
3010 be made. */
3012 static bool
3013 purge_addressof_1 (loc, insn, force, store, ht)
3014 rtx *loc;
3015 rtx insn;
3016 int force, store;
3017 htab_t ht;
3019 rtx x;
3020 RTX_CODE code;
3021 int i, j;
3022 const char *fmt;
3023 bool result = true;
3025 /* Re-start here to avoid recursion in common cases. */
3026 restart:
3028 x = *loc;
3029 if (x == 0)
3030 return true;
3032 code = GET_CODE (x);
3034 /* If we don't return in any of the cases below, we will recurse inside
3035 the RTX, which will normally result in any ADDRESSOF being forced into
3036 memory. */
3037 if (code == SET)
3039 result = purge_addressof_1 (&SET_DEST (x), insn, force, 1, ht);
3040 result &= purge_addressof_1 (&SET_SRC (x), insn, force, 0, ht);
3041 return result;
3043 else if (code == ADDRESSOF)
3045 rtx sub, insns;
3047 if (GET_CODE (XEXP (x, 0)) != MEM)
3049 put_addressof_into_stack (x, ht);
3050 return true;
3053 /* We must create a copy of the rtx because it was created by
3054 overwriting a REG rtx which is always shared. */
3055 sub = copy_rtx (XEXP (XEXP (x, 0), 0));
3056 if (validate_change (insn, loc, sub, 0)
3057 || validate_replace_rtx (x, sub, insn))
3058 return true;
3060 start_sequence ();
3061 sub = force_operand (sub, NULL_RTX);
3062 if (! validate_change (insn, loc, sub, 0)
3063 && ! validate_replace_rtx (x, sub, insn))
3064 abort ();
3066 insns = get_insns ();
3067 end_sequence ();
3068 emit_insn_before (insns, insn);
3069 return true;
3072 else if (code == MEM && GET_CODE (XEXP (x, 0)) == ADDRESSOF && ! force)
3074 rtx sub = XEXP (XEXP (x, 0), 0);
3076 if (GET_CODE (sub) == MEM)
3077 sub = adjust_address_nv (sub, GET_MODE (x), 0);
3078 else if (GET_CODE (sub) == REG
3079 && (MEM_VOLATILE_P (x) || GET_MODE (x) == BLKmode))
3081 else if (GET_CODE (sub) == REG && GET_MODE (x) != GET_MODE (sub))
3083 int size_x, size_sub;
3085 if (!insn)
3087 /* When processing REG_NOTES look at the list of
3088 replacements done on the insn to find the register that X
3089 was replaced by. */
3090 rtx tem;
3092 for (tem = purge_bitfield_addressof_replacements;
3093 tem != NULL_RTX;
3094 tem = XEXP (XEXP (tem, 1), 1))
3095 if (rtx_equal_p (x, XEXP (tem, 0)))
3097 *loc = XEXP (XEXP (tem, 1), 0);
3098 return true;
3101 /* See comment for purge_addressof_replacements. */
3102 for (tem = purge_addressof_replacements;
3103 tem != NULL_RTX;
3104 tem = XEXP (XEXP (tem, 1), 1))
3105 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3107 rtx z = XEXP (XEXP (tem, 1), 0);
3109 if (GET_MODE (x) == GET_MODE (z)
3110 || (GET_CODE (XEXP (XEXP (tem, 1), 0)) != REG
3111 && GET_CODE (XEXP (XEXP (tem, 1), 0)) != SUBREG))
3112 abort ();
3114 /* It can happen that the note may speak of things
3115 in a wider (or just different) mode than the
3116 code did. This is especially true of
3117 REG_RETVAL. */
3119 if (GET_CODE (z) == SUBREG && SUBREG_BYTE (z) == 0)
3120 z = SUBREG_REG (z);
3122 if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3123 && (GET_MODE_SIZE (GET_MODE (x))
3124 > GET_MODE_SIZE (GET_MODE (z))))
3126 /* This can occur as a result in invalid
3127 pointer casts, e.g. float f; ...
3128 *(long long int *)&f.
3129 ??? We could emit a warning here, but
3130 without a line number that wouldn't be
3131 very helpful. */
3132 z = gen_rtx_SUBREG (GET_MODE (x), z, 0);
3134 else
3135 z = gen_lowpart (GET_MODE (x), z);
3137 *loc = z;
3138 return true;
3141 /* Sometimes we may not be able to find the replacement. For
3142 example when the original insn was a MEM in a wider mode,
3143 and the note is part of a sign extension of a narrowed
3144 version of that MEM. Gcc testcase compile/990829-1.c can
3145 generate an example of this situation. Rather than complain
3146 we return false, which will prompt our caller to remove the
3147 offending note. */
3148 return false;
3151 size_x = GET_MODE_BITSIZE (GET_MODE (x));
3152 size_sub = GET_MODE_BITSIZE (GET_MODE (sub));
3154 /* Don't even consider working with paradoxical subregs,
3155 or the moral equivalent seen here. */
3156 if (size_x <= size_sub
3157 && int_mode_for_mode (GET_MODE (sub)) != BLKmode)
3159 /* Do a bitfield insertion to mirror what would happen
3160 in memory. */
3162 rtx val, seq;
3164 if (store)
3166 rtx p = PREV_INSN (insn);
3168 start_sequence ();
3169 val = gen_reg_rtx (GET_MODE (x));
3170 if (! validate_change (insn, loc, val, 0))
3172 /* Discard the current sequence and put the
3173 ADDRESSOF on stack. */
3174 end_sequence ();
3175 goto give_up;
3177 seq = get_insns ();
3178 end_sequence ();
3179 emit_insn_before (seq, insn);
3180 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3181 insn, ht);
3183 start_sequence ();
3184 store_bit_field (sub, size_x, 0, GET_MODE (x),
3185 val, GET_MODE_SIZE (GET_MODE (sub)));
3187 /* Make sure to unshare any shared rtl that store_bit_field
3188 might have created. */
3189 unshare_all_rtl_again (get_insns ());
3191 seq = get_insns ();
3192 end_sequence ();
3193 p = emit_insn_after (seq, insn);
3194 if (NEXT_INSN (insn))
3195 compute_insns_for_mem (NEXT_INSN (insn),
3196 p ? NEXT_INSN (p) : NULL_RTX,
3197 ht);
3199 else
3201 rtx p = PREV_INSN (insn);
3203 start_sequence ();
3204 val = extract_bit_field (sub, size_x, 0, 1, NULL_RTX,
3205 GET_MODE (x), GET_MODE (x),
3206 GET_MODE_SIZE (GET_MODE (sub)));
3208 if (! validate_change (insn, loc, val, 0))
3210 /* Discard the current sequence and put the
3211 ADDRESSOF on stack. */
3212 end_sequence ();
3213 goto give_up;
3216 seq = get_insns ();
3217 end_sequence ();
3218 emit_insn_before (seq, insn);
3219 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3220 insn, ht);
3223 /* Remember the replacement so that the same one can be done
3224 on the REG_NOTES. */
3225 purge_bitfield_addressof_replacements
3226 = gen_rtx_EXPR_LIST (VOIDmode, x,
3227 gen_rtx_EXPR_LIST
3228 (VOIDmode, val,
3229 purge_bitfield_addressof_replacements));
3231 /* We replaced with a reg -- all done. */
3232 return true;
3236 else if (validate_change (insn, loc, sub, 0))
3238 /* Remember the replacement so that the same one can be done
3239 on the REG_NOTES. */
3240 if (GET_CODE (sub) == REG || GET_CODE (sub) == SUBREG)
3242 rtx tem;
3244 for (tem = purge_addressof_replacements;
3245 tem != NULL_RTX;
3246 tem = XEXP (XEXP (tem, 1), 1))
3247 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3249 XEXP (XEXP (tem, 1), 0) = sub;
3250 return true;
3252 purge_addressof_replacements
3253 = gen_rtx (EXPR_LIST, VOIDmode, XEXP (x, 0),
3254 gen_rtx_EXPR_LIST (VOIDmode, sub,
3255 purge_addressof_replacements));
3256 return true;
3258 goto restart;
3262 give_up:
3263 /* Scan all subexpressions. */
3264 fmt = GET_RTX_FORMAT (code);
3265 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3267 if (*fmt == 'e')
3268 result &= purge_addressof_1 (&XEXP (x, i), insn, force, 0, ht);
3269 else if (*fmt == 'E')
3270 for (j = 0; j < XVECLEN (x, i); j++)
3271 result &= purge_addressof_1 (&XVECEXP (x, i, j), insn, force, 0, ht);
3274 return result;
3277 /* Return a hash value for K, a REG. */
3279 static hashval_t
3280 insns_for_mem_hash (k)
3281 const void * k;
3283 /* Use the address of the key for the hash value. */
3284 struct insns_for_mem_entry *m = (struct insns_for_mem_entry *) k;
3285 return htab_hash_pointer (m->key);
3288 /* Return nonzero if K1 and K2 (two REGs) are the same. */
3290 static int
3291 insns_for_mem_comp (k1, k2)
3292 const void * k1;
3293 const void * k2;
3295 struct insns_for_mem_entry *m1 = (struct insns_for_mem_entry *) k1;
3296 struct insns_for_mem_entry *m2 = (struct insns_for_mem_entry *) k2;
3297 return m1->key == m2->key;
3300 struct insns_for_mem_walk_info
3302 /* The hash table that we are using to record which INSNs use which
3303 MEMs. */
3304 htab_t ht;
3306 /* The INSN we are currently processing. */
3307 rtx insn;
3309 /* Zero if we are walking to find ADDRESSOFs, one if we are walking
3310 to find the insns that use the REGs in the ADDRESSOFs. */
3311 int pass;
3314 /* Called from compute_insns_for_mem via for_each_rtx. If R is a REG
3315 that might be used in an ADDRESSOF expression, record this INSN in
3316 the hash table given by DATA (which is really a pointer to an
3317 insns_for_mem_walk_info structure). */
3319 static int
3320 insns_for_mem_walk (r, data)
3321 rtx *r;
3322 void *data;
3324 struct insns_for_mem_walk_info *ifmwi
3325 = (struct insns_for_mem_walk_info *) data;
3326 struct insns_for_mem_entry tmp;
3327 tmp.insns = NULL_RTX;
3329 if (ifmwi->pass == 0 && *r && GET_CODE (*r) == ADDRESSOF
3330 && GET_CODE (XEXP (*r, 0)) == REG)
3332 PTR *e;
3333 tmp.key = XEXP (*r, 0);
3334 e = htab_find_slot (ifmwi->ht, &tmp, INSERT);
3335 if (*e == NULL)
3337 *e = ggc_alloc (sizeof (tmp));
3338 memcpy (*e, &tmp, sizeof (tmp));
3341 else if (ifmwi->pass == 1 && *r && GET_CODE (*r) == REG)
3343 struct insns_for_mem_entry *ifme;
3344 tmp.key = *r;
3345 ifme = (struct insns_for_mem_entry *) htab_find (ifmwi->ht, &tmp);
3347 /* If we have not already recorded this INSN, do so now. Since
3348 we process the INSNs in order, we know that if we have
3349 recorded it it must be at the front of the list. */
3350 if (ifme && (!ifme->insns || XEXP (ifme->insns, 0) != ifmwi->insn))
3351 ifme->insns = gen_rtx_EXPR_LIST (VOIDmode, ifmwi->insn,
3352 ifme->insns);
3355 return 0;
3358 /* Walk the INSNS, until we reach LAST_INSN, recording which INSNs use
3359 which REGs in HT. */
3361 static void
3362 compute_insns_for_mem (insns, last_insn, ht)
3363 rtx insns;
3364 rtx last_insn;
3365 htab_t ht;
3367 rtx insn;
3368 struct insns_for_mem_walk_info ifmwi;
3369 ifmwi.ht = ht;
3371 for (ifmwi.pass = 0; ifmwi.pass < 2; ++ifmwi.pass)
3372 for (insn = insns; insn != last_insn; insn = NEXT_INSN (insn))
3373 if (INSN_P (insn))
3375 ifmwi.insn = insn;
3376 for_each_rtx (&insn, insns_for_mem_walk, &ifmwi);
3380 /* Helper function for purge_addressof called through for_each_rtx.
3381 Returns true iff the rtl is an ADDRESSOF. */
3383 static int
3384 is_addressof (rtl, data)
3385 rtx *rtl;
3386 void *data ATTRIBUTE_UNUSED;
3388 return GET_CODE (*rtl) == ADDRESSOF;
3391 /* Eliminate all occurrences of ADDRESSOF from INSNS. Elide any remaining
3392 (MEM (ADDRESSOF)) patterns, and force any needed registers into the
3393 stack. */
3395 void
3396 purge_addressof (insns)
3397 rtx insns;
3399 rtx insn;
3400 htab_t ht;
3402 /* When we actually purge ADDRESSOFs, we turn REGs into MEMs. That
3403 requires a fixup pass over the instruction stream to correct
3404 INSNs that depended on the REG being a REG, and not a MEM. But,
3405 these fixup passes are slow. Furthermore, most MEMs are not
3406 mentioned in very many instructions. So, we speed up the process
3407 by pre-calculating which REGs occur in which INSNs; that allows
3408 us to perform the fixup passes much more quickly. */
3409 ht = htab_create_ggc (1000, insns_for_mem_hash, insns_for_mem_comp, NULL);
3410 compute_insns_for_mem (insns, NULL_RTX, ht);
3412 for (insn = insns; insn; insn = NEXT_INSN (insn))
3413 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3414 || GET_CODE (insn) == CALL_INSN)
3416 if (! purge_addressof_1 (&PATTERN (insn), insn,
3417 asm_noperands (PATTERN (insn)) > 0, 0, ht))
3418 /* If we could not replace the ADDRESSOFs in the insn,
3419 something is wrong. */
3420 abort ();
3422 if (! purge_addressof_1 (&REG_NOTES (insn), NULL_RTX, 0, 0, ht))
3424 /* If we could not replace the ADDRESSOFs in the insn's notes,
3425 we can just remove the offending notes instead. */
3426 rtx note;
3428 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3430 /* If we find a REG_RETVAL note then the insn is a libcall.
3431 Such insns must have REG_EQUAL notes as well, in order
3432 for later passes of the compiler to work. So it is not
3433 safe to delete the notes here, and instead we abort. */
3434 if (REG_NOTE_KIND (note) == REG_RETVAL)
3435 abort ();
3436 if (for_each_rtx (&note, is_addressof, NULL))
3437 remove_note (insn, note);
3442 /* Clean up. */
3443 purge_bitfield_addressof_replacements = 0;
3444 purge_addressof_replacements = 0;
3446 /* REGs are shared. purge_addressof will destructively replace a REG
3447 with a MEM, which creates shared MEMs.
3449 Unfortunately, the children of put_reg_into_stack assume that MEMs
3450 referring to the same stack slot are shared (fixup_var_refs and
3451 the associated hash table code).
3453 So, we have to do another unsharing pass after we have flushed any
3454 REGs that had their address taken into the stack.
3456 It may be worth tracking whether or not we converted any REGs into
3457 MEMs to avoid this overhead when it is not needed. */
3458 unshare_all_rtl_again (get_insns ());
3461 /* Convert a SET of a hard subreg to a set of the appropriate hard
3462 register. A subroutine of purge_hard_subreg_sets. */
3464 static void
3465 purge_single_hard_subreg_set (pattern)
3466 rtx pattern;
3468 rtx reg = SET_DEST (pattern);
3469 enum machine_mode mode = GET_MODE (SET_DEST (pattern));
3470 int offset = 0;
3472 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG
3473 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
3475 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
3476 GET_MODE (SUBREG_REG (reg)),
3477 SUBREG_BYTE (reg),
3478 GET_MODE (reg));
3479 reg = SUBREG_REG (reg);
3483 if (GET_CODE (reg) == REG && REGNO (reg) < FIRST_PSEUDO_REGISTER)
3485 reg = gen_rtx_REG (mode, REGNO (reg) + offset);
3486 SET_DEST (pattern) = reg;
3490 /* Eliminate all occurrences of SETs of hard subregs from INSNS. The
3491 only such SETs that we expect to see are those left in because
3492 integrate can't handle sets of parts of a return value register.
3494 We don't use alter_subreg because we only want to eliminate subregs
3495 of hard registers. */
3497 void
3498 purge_hard_subreg_sets (insn)
3499 rtx insn;
3501 for (; insn; insn = NEXT_INSN (insn))
3503 if (INSN_P (insn))
3505 rtx pattern = PATTERN (insn);
3506 switch (GET_CODE (pattern))
3508 case SET:
3509 if (GET_CODE (SET_DEST (pattern)) == SUBREG)
3510 purge_single_hard_subreg_set (pattern);
3511 break;
3512 case PARALLEL:
3514 int j;
3515 for (j = XVECLEN (pattern, 0) - 1; j >= 0; j--)
3517 rtx inner_pattern = XVECEXP (pattern, 0, j);
3518 if (GET_CODE (inner_pattern) == SET
3519 && GET_CODE (SET_DEST (inner_pattern)) == SUBREG)
3520 purge_single_hard_subreg_set (inner_pattern);
3523 break;
3524 default:
3525 break;
3531 /* Pass through the INSNS of function FNDECL and convert virtual register
3532 references to hard register references. */
3534 void
3535 instantiate_virtual_regs (fndecl, insns)
3536 tree fndecl;
3537 rtx insns;
3539 rtx insn;
3540 unsigned int i;
3542 /* Compute the offsets to use for this function. */
3543 in_arg_offset = FIRST_PARM_OFFSET (fndecl);
3544 var_offset = STARTING_FRAME_OFFSET;
3545 dynamic_offset = STACK_DYNAMIC_OFFSET (fndecl);
3546 out_arg_offset = STACK_POINTER_OFFSET;
3547 cfa_offset = ARG_POINTER_CFA_OFFSET (fndecl);
3549 /* Scan all variables and parameters of this function. For each that is
3550 in memory, instantiate all virtual registers if the result is a valid
3551 address. If not, we do it later. That will handle most uses of virtual
3552 regs on many machines. */
3553 instantiate_decls (fndecl, 1);
3555 /* Initialize recognition, indicating that volatile is OK. */
3556 init_recog ();
3558 /* Scan through all the insns, instantiating every virtual register still
3559 present. */
3560 for (insn = insns; insn; insn = NEXT_INSN (insn))
3561 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3562 || GET_CODE (insn) == CALL_INSN)
3564 instantiate_virtual_regs_1 (&PATTERN (insn), insn, 1);
3565 instantiate_virtual_regs_1 (&REG_NOTES (insn), NULL_RTX, 0);
3566 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
3567 if (GET_CODE (insn) == CALL_INSN)
3568 instantiate_virtual_regs_1 (&CALL_INSN_FUNCTION_USAGE (insn),
3569 NULL_RTX, 0);
3572 /* Instantiate the stack slots for the parm registers, for later use in
3573 addressof elimination. */
3574 for (i = 0; i < max_parm_reg; ++i)
3575 if (parm_reg_stack_loc[i])
3576 instantiate_virtual_regs_1 (&parm_reg_stack_loc[i], NULL_RTX, 0);
3578 /* Now instantiate the remaining register equivalences for debugging info.
3579 These will not be valid addresses. */
3580 instantiate_decls (fndecl, 0);
3582 /* Indicate that, from now on, assign_stack_local should use
3583 frame_pointer_rtx. */
3584 virtuals_instantiated = 1;
3587 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
3588 all virtual registers in their DECL_RTL's.
3590 If VALID_ONLY, do this only if the resulting address is still valid.
3591 Otherwise, always do it. */
3593 static void
3594 instantiate_decls (fndecl, valid_only)
3595 tree fndecl;
3596 int valid_only;
3598 tree decl;
3600 /* Process all parameters of the function. */
3601 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
3603 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
3604 HOST_WIDE_INT size_rtl;
3606 instantiate_decl (DECL_RTL (decl), size, valid_only);
3608 /* If the parameter was promoted, then the incoming RTL mode may be
3609 larger than the declared type size. We must use the larger of
3610 the two sizes. */
3611 size_rtl = GET_MODE_SIZE (GET_MODE (DECL_INCOMING_RTL (decl)));
3612 size = MAX (size_rtl, size);
3613 instantiate_decl (DECL_INCOMING_RTL (decl), size, valid_only);
3616 /* Now process all variables defined in the function or its subblocks. */
3617 instantiate_decls_1 (DECL_INITIAL (fndecl), valid_only);
3620 /* Subroutine of instantiate_decls: Process all decls in the given
3621 BLOCK node and all its subblocks. */
3623 static void
3624 instantiate_decls_1 (let, valid_only)
3625 tree let;
3626 int valid_only;
3628 tree t;
3630 for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
3631 if (DECL_RTL_SET_P (t))
3632 instantiate_decl (DECL_RTL (t),
3633 int_size_in_bytes (TREE_TYPE (t)),
3634 valid_only);
3636 /* Process all subblocks. */
3637 for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
3638 instantiate_decls_1 (t, valid_only);
3641 /* Subroutine of the preceding procedures: Given RTL representing a
3642 decl and the size of the object, do any instantiation required.
3644 If VALID_ONLY is nonzero, it means that the RTL should only be
3645 changed if the new address is valid. */
3647 static void
3648 instantiate_decl (x, size, valid_only)
3649 rtx x;
3650 HOST_WIDE_INT size;
3651 int valid_only;
3653 enum machine_mode mode;
3654 rtx addr;
3656 /* If this is not a MEM, no need to do anything. Similarly if the
3657 address is a constant or a register that is not a virtual register. */
3659 if (x == 0 || GET_CODE (x) != MEM)
3660 return;
3662 addr = XEXP (x, 0);
3663 if (CONSTANT_P (addr)
3664 || (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == REG)
3665 || (GET_CODE (addr) == REG
3666 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
3667 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
3668 return;
3670 /* If we should only do this if the address is valid, copy the address.
3671 We need to do this so we can undo any changes that might make the
3672 address invalid. This copy is unfortunate, but probably can't be
3673 avoided. */
3675 if (valid_only)
3676 addr = copy_rtx (addr);
3678 instantiate_virtual_regs_1 (&addr, NULL_RTX, 0);
3680 if (valid_only && size >= 0)
3682 unsigned HOST_WIDE_INT decl_size = size;
3684 /* Now verify that the resulting address is valid for every integer or
3685 floating-point mode up to and including SIZE bytes long. We do this
3686 since the object might be accessed in any mode and frame addresses
3687 are shared. */
3689 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3690 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3691 mode = GET_MODE_WIDER_MODE (mode))
3692 if (! memory_address_p (mode, addr))
3693 return;
3695 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
3696 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3697 mode = GET_MODE_WIDER_MODE (mode))
3698 if (! memory_address_p (mode, addr))
3699 return;
3702 /* Put back the address now that we have updated it and we either know
3703 it is valid or we don't care whether it is valid. */
3705 XEXP (x, 0) = addr;
3708 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
3709 is a virtual register, return the equivalent hard register and set the
3710 offset indirectly through the pointer. Otherwise, return 0. */
3712 static rtx
3713 instantiate_new_reg (x, poffset)
3714 rtx x;
3715 HOST_WIDE_INT *poffset;
3717 rtx new;
3718 HOST_WIDE_INT offset;
3720 if (x == virtual_incoming_args_rtx)
3721 new = arg_pointer_rtx, offset = in_arg_offset;
3722 else if (x == virtual_stack_vars_rtx)
3723 new = frame_pointer_rtx, offset = var_offset;
3724 else if (x == virtual_stack_dynamic_rtx)
3725 new = stack_pointer_rtx, offset = dynamic_offset;
3726 else if (x == virtual_outgoing_args_rtx)
3727 new = stack_pointer_rtx, offset = out_arg_offset;
3728 else if (x == virtual_cfa_rtx)
3729 new = arg_pointer_rtx, offset = cfa_offset;
3730 else
3731 return 0;
3733 *poffset = offset;
3734 return new;
3737 /* Given a pointer to a piece of rtx and an optional pointer to the
3738 containing object, instantiate any virtual registers present in it.
3740 If EXTRA_INSNS, we always do the replacement and generate
3741 any extra insns before OBJECT. If it zero, we do nothing if replacement
3742 is not valid.
3744 Return 1 if we either had nothing to do or if we were able to do the
3745 needed replacement. Return 0 otherwise; we only return zero if
3746 EXTRA_INSNS is zero.
3748 We first try some simple transformations to avoid the creation of extra
3749 pseudos. */
3751 static int
3752 instantiate_virtual_regs_1 (loc, object, extra_insns)
3753 rtx *loc;
3754 rtx object;
3755 int extra_insns;
3757 rtx x;
3758 RTX_CODE code;
3759 rtx new = 0;
3760 HOST_WIDE_INT offset = 0;
3761 rtx temp;
3762 rtx seq;
3763 int i, j;
3764 const char *fmt;
3766 /* Re-start here to avoid recursion in common cases. */
3767 restart:
3769 x = *loc;
3770 if (x == 0)
3771 return 1;
3773 code = GET_CODE (x);
3775 /* Check for some special cases. */
3776 switch (code)
3778 case CONST_INT:
3779 case CONST_DOUBLE:
3780 case CONST_VECTOR:
3781 case CONST:
3782 case SYMBOL_REF:
3783 case CODE_LABEL:
3784 case PC:
3785 case CC0:
3786 case ASM_INPUT:
3787 case ADDR_VEC:
3788 case ADDR_DIFF_VEC:
3789 case RETURN:
3790 return 1;
3792 case SET:
3793 /* We are allowed to set the virtual registers. This means that
3794 the actual register should receive the source minus the
3795 appropriate offset. This is used, for example, in the handling
3796 of non-local gotos. */
3797 if ((new = instantiate_new_reg (SET_DEST (x), &offset)) != 0)
3799 rtx src = SET_SRC (x);
3801 /* We are setting the register, not using it, so the relevant
3802 offset is the negative of the offset to use were we using
3803 the register. */
3804 offset = - offset;
3805 instantiate_virtual_regs_1 (&src, NULL_RTX, 0);
3807 /* The only valid sources here are PLUS or REG. Just do
3808 the simplest possible thing to handle them. */
3809 if (GET_CODE (src) != REG && GET_CODE (src) != PLUS)
3810 abort ();
3812 start_sequence ();
3813 if (GET_CODE (src) != REG)
3814 temp = force_operand (src, NULL_RTX);
3815 else
3816 temp = src;
3817 temp = force_operand (plus_constant (temp, offset), NULL_RTX);
3818 seq = get_insns ();
3819 end_sequence ();
3821 emit_insn_before (seq, object);
3822 SET_DEST (x) = new;
3824 if (! validate_change (object, &SET_SRC (x), temp, 0)
3825 || ! extra_insns)
3826 abort ();
3828 return 1;
3831 instantiate_virtual_regs_1 (&SET_DEST (x), object, extra_insns);
3832 loc = &SET_SRC (x);
3833 goto restart;
3835 case PLUS:
3836 /* Handle special case of virtual register plus constant. */
3837 if (CONSTANT_P (XEXP (x, 1)))
3839 rtx old, new_offset;
3841 /* Check for (plus (plus VIRT foo) (const_int)) first. */
3842 if (GET_CODE (XEXP (x, 0)) == PLUS)
3844 if ((new = instantiate_new_reg (XEXP (XEXP (x, 0), 0), &offset)))
3846 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 1), object,
3847 extra_insns);
3848 new = gen_rtx_PLUS (Pmode, new, XEXP (XEXP (x, 0), 1));
3850 else
3852 loc = &XEXP (x, 0);
3853 goto restart;
3857 #ifdef POINTERS_EXTEND_UNSIGNED
3858 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
3859 we can commute the PLUS and SUBREG because pointers into the
3860 frame are well-behaved. */
3861 else if (GET_CODE (XEXP (x, 0)) == SUBREG && GET_MODE (x) == ptr_mode
3862 && GET_CODE (XEXP (x, 1)) == CONST_INT
3863 && 0 != (new
3864 = instantiate_new_reg (SUBREG_REG (XEXP (x, 0)),
3865 &offset))
3866 && validate_change (object, loc,
3867 plus_constant (gen_lowpart (ptr_mode,
3868 new),
3869 offset
3870 + INTVAL (XEXP (x, 1))),
3872 return 1;
3873 #endif
3874 else if ((new = instantiate_new_reg (XEXP (x, 0), &offset)) == 0)
3876 /* We know the second operand is a constant. Unless the
3877 first operand is a REG (which has been already checked),
3878 it needs to be checked. */
3879 if (GET_CODE (XEXP (x, 0)) != REG)
3881 loc = &XEXP (x, 0);
3882 goto restart;
3884 return 1;
3887 new_offset = plus_constant (XEXP (x, 1), offset);
3889 /* If the new constant is zero, try to replace the sum with just
3890 the register. */
3891 if (new_offset == const0_rtx
3892 && validate_change (object, loc, new, 0))
3893 return 1;
3895 /* Next try to replace the register and new offset.
3896 There are two changes to validate here and we can't assume that
3897 in the case of old offset equals new just changing the register
3898 will yield a valid insn. In the interests of a little efficiency,
3899 however, we only call validate change once (we don't queue up the
3900 changes and then call apply_change_group). */
3902 old = XEXP (x, 0);
3903 if (offset == 0
3904 ? ! validate_change (object, &XEXP (x, 0), new, 0)
3905 : (XEXP (x, 0) = new,
3906 ! validate_change (object, &XEXP (x, 1), new_offset, 0)))
3908 if (! extra_insns)
3910 XEXP (x, 0) = old;
3911 return 0;
3914 /* Otherwise copy the new constant into a register and replace
3915 constant with that register. */
3916 temp = gen_reg_rtx (Pmode);
3917 XEXP (x, 0) = new;
3918 if (validate_change (object, &XEXP (x, 1), temp, 0))
3919 emit_insn_before (gen_move_insn (temp, new_offset), object);
3920 else
3922 /* If that didn't work, replace this expression with a
3923 register containing the sum. */
3925 XEXP (x, 0) = old;
3926 new = gen_rtx_PLUS (Pmode, new, new_offset);
3928 start_sequence ();
3929 temp = force_operand (new, NULL_RTX);
3930 seq = get_insns ();
3931 end_sequence ();
3933 emit_insn_before (seq, object);
3934 if (! validate_change (object, loc, temp, 0)
3935 && ! validate_replace_rtx (x, temp, object))
3936 abort ();
3940 return 1;
3943 /* Fall through to generic two-operand expression case. */
3944 case EXPR_LIST:
3945 case CALL:
3946 case COMPARE:
3947 case MINUS:
3948 case MULT:
3949 case DIV: case UDIV:
3950 case MOD: case UMOD:
3951 case AND: case IOR: case XOR:
3952 case ROTATERT: case ROTATE:
3953 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
3954 case NE: case EQ:
3955 case GE: case GT: case GEU: case GTU:
3956 case LE: case LT: case LEU: case LTU:
3957 if (XEXP (x, 1) && ! CONSTANT_P (XEXP (x, 1)))
3958 instantiate_virtual_regs_1 (&XEXP (x, 1), object, extra_insns);
3959 loc = &XEXP (x, 0);
3960 goto restart;
3962 case MEM:
3963 /* Most cases of MEM that convert to valid addresses have already been
3964 handled by our scan of decls. The only special handling we
3965 need here is to make a copy of the rtx to ensure it isn't being
3966 shared if we have to change it to a pseudo.
3968 If the rtx is a simple reference to an address via a virtual register,
3969 it can potentially be shared. In such cases, first try to make it
3970 a valid address, which can also be shared. Otherwise, copy it and
3971 proceed normally.
3973 First check for common cases that need no processing. These are
3974 usually due to instantiation already being done on a previous instance
3975 of a shared rtx. */
3977 temp = XEXP (x, 0);
3978 if (CONSTANT_ADDRESS_P (temp)
3979 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3980 || temp == arg_pointer_rtx
3981 #endif
3982 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3983 || temp == hard_frame_pointer_rtx
3984 #endif
3985 || temp == frame_pointer_rtx)
3986 return 1;
3988 if (GET_CODE (temp) == PLUS
3989 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3990 && (XEXP (temp, 0) == frame_pointer_rtx
3991 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3992 || XEXP (temp, 0) == hard_frame_pointer_rtx
3993 #endif
3994 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3995 || XEXP (temp, 0) == arg_pointer_rtx
3996 #endif
3998 return 1;
4000 if (temp == virtual_stack_vars_rtx
4001 || temp == virtual_incoming_args_rtx
4002 || (GET_CODE (temp) == PLUS
4003 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
4004 && (XEXP (temp, 0) == virtual_stack_vars_rtx
4005 || XEXP (temp, 0) == virtual_incoming_args_rtx)))
4007 /* This MEM may be shared. If the substitution can be done without
4008 the need to generate new pseudos, we want to do it in place
4009 so all copies of the shared rtx benefit. The call below will
4010 only make substitutions if the resulting address is still
4011 valid.
4013 Note that we cannot pass X as the object in the recursive call
4014 since the insn being processed may not allow all valid
4015 addresses. However, if we were not passed on object, we can
4016 only modify X without copying it if X will have a valid
4017 address.
4019 ??? Also note that this can still lose if OBJECT is an insn that
4020 has less restrictions on an address that some other insn.
4021 In that case, we will modify the shared address. This case
4022 doesn't seem very likely, though. One case where this could
4023 happen is in the case of a USE or CLOBBER reference, but we
4024 take care of that below. */
4026 if (instantiate_virtual_regs_1 (&XEXP (x, 0),
4027 object ? object : x, 0))
4028 return 1;
4030 /* Otherwise make a copy and process that copy. We copy the entire
4031 RTL expression since it might be a PLUS which could also be
4032 shared. */
4033 *loc = x = copy_rtx (x);
4036 /* Fall through to generic unary operation case. */
4037 case PREFETCH:
4038 case SUBREG:
4039 case STRICT_LOW_PART:
4040 case NEG: case NOT:
4041 case PRE_DEC: case PRE_INC: case POST_DEC: case POST_INC:
4042 case SIGN_EXTEND: case ZERO_EXTEND:
4043 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
4044 case FLOAT: case FIX:
4045 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
4046 case ABS:
4047 case SQRT:
4048 case FFS:
4049 /* These case either have just one operand or we know that we need not
4050 check the rest of the operands. */
4051 loc = &XEXP (x, 0);
4052 goto restart;
4054 case USE:
4055 case CLOBBER:
4056 /* If the operand is a MEM, see if the change is a valid MEM. If not,
4057 go ahead and make the invalid one, but do it to a copy. For a REG,
4058 just make the recursive call, since there's no chance of a problem. */
4060 if ((GET_CODE (XEXP (x, 0)) == MEM
4061 && instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), XEXP (x, 0),
4063 || (GET_CODE (XEXP (x, 0)) == REG
4064 && instantiate_virtual_regs_1 (&XEXP (x, 0), object, 0)))
4065 return 1;
4067 XEXP (x, 0) = copy_rtx (XEXP (x, 0));
4068 loc = &XEXP (x, 0);
4069 goto restart;
4071 case REG:
4072 /* Try to replace with a PLUS. If that doesn't work, compute the sum
4073 in front of this insn and substitute the temporary. */
4074 if ((new = instantiate_new_reg (x, &offset)) != 0)
4076 temp = plus_constant (new, offset);
4077 if (!validate_change (object, loc, temp, 0))
4079 if (! extra_insns)
4080 return 0;
4082 start_sequence ();
4083 temp = force_operand (temp, NULL_RTX);
4084 seq = get_insns ();
4085 end_sequence ();
4087 emit_insn_before (seq, object);
4088 if (! validate_change (object, loc, temp, 0)
4089 && ! validate_replace_rtx (x, temp, object))
4090 abort ();
4094 return 1;
4096 case ADDRESSOF:
4097 if (GET_CODE (XEXP (x, 0)) == REG)
4098 return 1;
4100 else if (GET_CODE (XEXP (x, 0)) == MEM)
4102 /* If we have a (addressof (mem ..)), do any instantiation inside
4103 since we know we'll be making the inside valid when we finally
4104 remove the ADDRESSOF. */
4105 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), NULL_RTX, 0);
4106 return 1;
4108 break;
4110 default:
4111 break;
4114 /* Scan all subexpressions. */
4115 fmt = GET_RTX_FORMAT (code);
4116 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
4117 if (*fmt == 'e')
4119 if (!instantiate_virtual_regs_1 (&XEXP (x, i), object, extra_insns))
4120 return 0;
4122 else if (*fmt == 'E')
4123 for (j = 0; j < XVECLEN (x, i); j++)
4124 if (! instantiate_virtual_regs_1 (&XVECEXP (x, i, j), object,
4125 extra_insns))
4126 return 0;
4128 return 1;
4131 /* Optimization: assuming this function does not receive nonlocal gotos,
4132 delete the handlers for such, as well as the insns to establish
4133 and disestablish them. */
4135 static void
4136 delete_handlers ()
4138 rtx insn;
4139 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4141 /* Delete the handler by turning off the flag that would
4142 prevent jump_optimize from deleting it.
4143 Also permit deletion of the nonlocal labels themselves
4144 if nothing local refers to them. */
4145 if (GET_CODE (insn) == CODE_LABEL)
4147 tree t, last_t;
4149 LABEL_PRESERVE_P (insn) = 0;
4151 /* Remove it from the nonlocal_label list, to avoid confusing
4152 flow. */
4153 for (t = nonlocal_labels, last_t = 0; t;
4154 last_t = t, t = TREE_CHAIN (t))
4155 if (DECL_RTL (TREE_VALUE (t)) == insn)
4156 break;
4157 if (t)
4159 if (! last_t)
4160 nonlocal_labels = TREE_CHAIN (nonlocal_labels);
4161 else
4162 TREE_CHAIN (last_t) = TREE_CHAIN (t);
4165 if (GET_CODE (insn) == INSN)
4167 int can_delete = 0;
4168 rtx t;
4169 for (t = nonlocal_goto_handler_slots; t != 0; t = XEXP (t, 1))
4170 if (reg_mentioned_p (t, PATTERN (insn)))
4172 can_delete = 1;
4173 break;
4175 if (can_delete
4176 || (nonlocal_goto_stack_level != 0
4177 && reg_mentioned_p (nonlocal_goto_stack_level,
4178 PATTERN (insn))))
4179 delete_related_insns (insn);
4185 max_parm_reg_num ()
4187 return max_parm_reg;
4190 /* Return the first insn following those generated by `assign_parms'. */
4193 get_first_nonparm_insn ()
4195 if (last_parm_insn)
4196 return NEXT_INSN (last_parm_insn);
4197 return get_insns ();
4200 /* Return the first NOTE_INSN_BLOCK_BEG note in the function.
4201 Crash if there is none. */
4204 get_first_block_beg ()
4206 rtx searcher;
4207 rtx insn = get_first_nonparm_insn ();
4209 for (searcher = insn; searcher; searcher = NEXT_INSN (searcher))
4210 if (GET_CODE (searcher) == NOTE
4211 && NOTE_LINE_NUMBER (searcher) == NOTE_INSN_BLOCK_BEG)
4212 return searcher;
4214 abort (); /* Invalid call to this function. (See comments above.) */
4215 return NULL_RTX;
4218 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
4219 This means a type for which function calls must pass an address to the
4220 function or get an address back from the function.
4221 EXP may be a type node or an expression (whose type is tested). */
4224 aggregate_value_p (exp)
4225 tree exp;
4227 int i, regno, nregs;
4228 rtx reg;
4230 tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
4232 if (TREE_CODE (type) == VOID_TYPE)
4233 return 0;
4234 if (RETURN_IN_MEMORY (type))
4235 return 1;
4236 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
4237 and thus can't be returned in registers. */
4238 if (TREE_ADDRESSABLE (type))
4239 return 1;
4240 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
4241 return 1;
4242 /* Make sure we have suitable call-clobbered regs to return
4243 the value in; if not, we must return it in memory. */
4244 reg = hard_function_value (type, 0, 0);
4246 /* If we have something other than a REG (e.g. a PARALLEL), then assume
4247 it is OK. */
4248 if (GET_CODE (reg) != REG)
4249 return 0;
4251 regno = REGNO (reg);
4252 nregs = HARD_REGNO_NREGS (regno, TYPE_MODE (type));
4253 for (i = 0; i < nregs; i++)
4254 if (! call_used_regs[regno + i])
4255 return 1;
4256 return 0;
4259 /* Assign RTL expressions to the function's parameters.
4260 This may involve copying them into registers and using
4261 those registers as the RTL for them. */
4263 void
4264 assign_parms (fndecl)
4265 tree fndecl;
4267 tree parm;
4268 rtx entry_parm = 0;
4269 rtx stack_parm = 0;
4270 CUMULATIVE_ARGS args_so_far;
4271 enum machine_mode promoted_mode, passed_mode;
4272 enum machine_mode nominal_mode, promoted_nominal_mode;
4273 int unsignedp;
4274 /* Total space needed so far for args on the stack,
4275 given as a constant and a tree-expression. */
4276 struct args_size stack_args_size;
4277 tree fntype = TREE_TYPE (fndecl);
4278 tree fnargs = DECL_ARGUMENTS (fndecl);
4279 /* This is used for the arg pointer when referring to stack args. */
4280 rtx internal_arg_pointer;
4281 /* This is a dummy PARM_DECL that we used for the function result if
4282 the function returns a structure. */
4283 tree function_result_decl = 0;
4284 #ifdef SETUP_INCOMING_VARARGS
4285 int varargs_setup = 0;
4286 #endif
4287 rtx conversion_insns = 0;
4288 struct args_size alignment_pad;
4290 /* Nonzero if function takes extra anonymous args.
4291 This means the last named arg must be on the stack
4292 right before the anonymous ones. */
4293 int stdarg
4294 = (TYPE_ARG_TYPES (fntype) != 0
4295 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4296 != void_type_node));
4298 current_function_stdarg = stdarg;
4300 /* If the reg that the virtual arg pointer will be translated into is
4301 not a fixed reg or is the stack pointer, make a copy of the virtual
4302 arg pointer, and address parms via the copy. The frame pointer is
4303 considered fixed even though it is not marked as such.
4305 The second time through, simply use ap to avoid generating rtx. */
4307 if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
4308 || ! (fixed_regs[ARG_POINTER_REGNUM]
4309 || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM)))
4310 internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
4311 else
4312 internal_arg_pointer = virtual_incoming_args_rtx;
4313 current_function_internal_arg_pointer = internal_arg_pointer;
4315 stack_args_size.constant = 0;
4316 stack_args_size.var = 0;
4318 /* If struct value address is treated as the first argument, make it so. */
4319 if (aggregate_value_p (DECL_RESULT (fndecl))
4320 && ! current_function_returns_pcc_struct
4321 && struct_value_incoming_rtx == 0)
4323 tree type = build_pointer_type (TREE_TYPE (fntype));
4325 function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
4327 DECL_ARG_TYPE (function_result_decl) = type;
4328 TREE_CHAIN (function_result_decl) = fnargs;
4329 fnargs = function_result_decl;
4332 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
4333 parm_reg_stack_loc = (rtx *) ggc_alloc_cleared (max_parm_reg * sizeof (rtx));
4335 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
4336 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far, fntype, NULL_RTX);
4337 #else
4338 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, 0);
4339 #endif
4341 /* We haven't yet found an argument that we must push and pretend the
4342 caller did. */
4343 current_function_pretend_args_size = 0;
4345 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
4347 struct args_size stack_offset;
4348 struct args_size arg_size;
4349 int passed_pointer = 0;
4350 int did_conversion = 0;
4351 tree passed_type = DECL_ARG_TYPE (parm);
4352 tree nominal_type = TREE_TYPE (parm);
4353 int pretend_named;
4354 int last_named = 0, named_arg;
4356 /* Set LAST_NAMED if this is last named arg before last
4357 anonymous args. */
4358 if (stdarg)
4360 tree tem;
4362 for (tem = TREE_CHAIN (parm); tem; tem = TREE_CHAIN (tem))
4363 if (DECL_NAME (tem))
4364 break;
4366 if (tem == 0)
4367 last_named = 1;
4369 /* Set NAMED_ARG if this arg should be treated as a named arg. For
4370 most machines, if this is a varargs/stdarg function, then we treat
4371 the last named arg as if it were anonymous too. */
4372 named_arg = STRICT_ARGUMENT_NAMING ? 1 : ! last_named;
4374 if (TREE_TYPE (parm) == error_mark_node
4375 /* This can happen after weird syntax errors
4376 or if an enum type is defined among the parms. */
4377 || TREE_CODE (parm) != PARM_DECL
4378 || passed_type == NULL)
4380 SET_DECL_RTL (parm, gen_rtx_MEM (BLKmode, const0_rtx));
4381 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4382 TREE_USED (parm) = 1;
4383 continue;
4386 /* Find mode of arg as it is passed, and mode of arg
4387 as it should be during execution of this function. */
4388 passed_mode = TYPE_MODE (passed_type);
4389 nominal_mode = TYPE_MODE (nominal_type);
4391 /* If the parm's mode is VOID, its value doesn't matter,
4392 and avoid the usual things like emit_move_insn that could crash. */
4393 if (nominal_mode == VOIDmode)
4395 SET_DECL_RTL (parm, const0_rtx);
4396 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4397 continue;
4400 /* If the parm is to be passed as a transparent union, use the
4401 type of the first field for the tests below. We have already
4402 verified that the modes are the same. */
4403 if (DECL_TRANSPARENT_UNION (parm)
4404 || (TREE_CODE (passed_type) == UNION_TYPE
4405 && TYPE_TRANSPARENT_UNION (passed_type)))
4406 passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
4408 /* See if this arg was passed by invisible reference. It is if
4409 it is an object whose size depends on the contents of the
4410 object itself or if the machine requires these objects be passed
4411 that way. */
4413 if ((TREE_CODE (TYPE_SIZE (passed_type)) != INTEGER_CST
4414 && contains_placeholder_p (TYPE_SIZE (passed_type)))
4415 || TREE_ADDRESSABLE (passed_type)
4416 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
4417 || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, passed_mode,
4418 passed_type, named_arg)
4419 #endif
4422 passed_type = nominal_type = build_pointer_type (passed_type);
4423 passed_pointer = 1;
4424 passed_mode = nominal_mode = Pmode;
4426 /* See if the frontend wants to pass this by invisible reference. */
4427 else if (passed_type != nominal_type
4428 && POINTER_TYPE_P (passed_type)
4429 && TREE_TYPE (passed_type) == nominal_type)
4431 nominal_type = passed_type;
4432 passed_pointer = 1;
4433 passed_mode = nominal_mode = Pmode;
4436 promoted_mode = passed_mode;
4438 #ifdef PROMOTE_FUNCTION_ARGS
4439 /* Compute the mode in which the arg is actually extended to. */
4440 unsignedp = TREE_UNSIGNED (passed_type);
4441 promoted_mode = promote_mode (passed_type, promoted_mode, &unsignedp, 1);
4442 #endif
4444 /* Let machine desc say which reg (if any) the parm arrives in.
4445 0 means it arrives on the stack. */
4446 #ifdef FUNCTION_INCOMING_ARG
4447 entry_parm = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4448 passed_type, named_arg);
4449 #else
4450 entry_parm = FUNCTION_ARG (args_so_far, promoted_mode,
4451 passed_type, named_arg);
4452 #endif
4454 if (entry_parm == 0)
4455 promoted_mode = passed_mode;
4457 #ifdef SETUP_INCOMING_VARARGS
4458 /* If this is the last named parameter, do any required setup for
4459 varargs or stdargs. We need to know about the case of this being an
4460 addressable type, in which case we skip the registers it
4461 would have arrived in.
4463 For stdargs, LAST_NAMED will be set for two parameters, the one that
4464 is actually the last named, and the dummy parameter. We only
4465 want to do this action once.
4467 Also, indicate when RTL generation is to be suppressed. */
4468 if (last_named && !varargs_setup)
4470 SETUP_INCOMING_VARARGS (args_so_far, promoted_mode, passed_type,
4471 current_function_pretend_args_size, 0);
4472 varargs_setup = 1;
4474 #endif
4476 /* Determine parm's home in the stack,
4477 in case it arrives in the stack or we should pretend it did.
4479 Compute the stack position and rtx where the argument arrives
4480 and its size.
4482 There is one complexity here: If this was a parameter that would
4483 have been passed in registers, but wasn't only because it is
4484 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
4485 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
4486 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of
4487 0 as it was the previous time. */
4489 pretend_named = named_arg || PRETEND_OUTGOING_VARARGS_NAMED;
4490 locate_and_pad_parm (promoted_mode, passed_type,
4491 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4493 #else
4494 #ifdef FUNCTION_INCOMING_ARG
4495 FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4496 passed_type,
4497 pretend_named) != 0,
4498 #else
4499 FUNCTION_ARG (args_so_far, promoted_mode,
4500 passed_type,
4501 pretend_named) != 0,
4502 #endif
4503 #endif
4504 fndecl, &stack_args_size, &stack_offset, &arg_size,
4505 &alignment_pad);
4508 rtx offset_rtx = ARGS_SIZE_RTX (stack_offset);
4510 if (offset_rtx == const0_rtx)
4511 stack_parm = gen_rtx_MEM (promoted_mode, internal_arg_pointer);
4512 else
4513 stack_parm = gen_rtx_MEM (promoted_mode,
4514 gen_rtx_PLUS (Pmode,
4515 internal_arg_pointer,
4516 offset_rtx));
4518 set_mem_attributes (stack_parm, parm, 1);
4521 /* If this parameter was passed both in registers and in the stack,
4522 use the copy on the stack. */
4523 if (MUST_PASS_IN_STACK (promoted_mode, passed_type))
4524 entry_parm = 0;
4526 #ifdef FUNCTION_ARG_PARTIAL_NREGS
4527 /* If this parm was passed part in regs and part in memory,
4528 pretend it arrived entirely in memory
4529 by pushing the register-part onto the stack.
4531 In the special case of a DImode or DFmode that is split,
4532 we could put it together in a pseudoreg directly,
4533 but for now that's not worth bothering with. */
4535 if (entry_parm)
4537 int nregs = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, promoted_mode,
4538 passed_type, named_arg);
4540 if (nregs > 0)
4542 current_function_pretend_args_size
4543 = (((nregs * UNITS_PER_WORD) + (PARM_BOUNDARY / BITS_PER_UNIT) - 1)
4544 / (PARM_BOUNDARY / BITS_PER_UNIT)
4545 * (PARM_BOUNDARY / BITS_PER_UNIT));
4547 /* Handle calls that pass values in multiple non-contiguous
4548 locations. The Irix 6 ABI has examples of this. */
4549 if (GET_CODE (entry_parm) == PARALLEL)
4550 emit_group_store (validize_mem (stack_parm), entry_parm,
4551 int_size_in_bytes (TREE_TYPE (parm)));
4553 else
4554 move_block_from_reg (REGNO (entry_parm),
4555 validize_mem (stack_parm), nregs,
4556 int_size_in_bytes (TREE_TYPE (parm)));
4558 entry_parm = stack_parm;
4561 #endif
4563 /* If we didn't decide this parm came in a register,
4564 by default it came on the stack. */
4565 if (entry_parm == 0)
4566 entry_parm = stack_parm;
4568 /* Record permanently how this parm was passed. */
4569 DECL_INCOMING_RTL (parm) = entry_parm;
4571 /* If there is actually space on the stack for this parm,
4572 count it in stack_args_size; otherwise set stack_parm to 0
4573 to indicate there is no preallocated stack slot for the parm. */
4575 if (entry_parm == stack_parm
4576 || (GET_CODE (entry_parm) == PARALLEL
4577 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
4578 #if defined (REG_PARM_STACK_SPACE) && ! defined (MAYBE_REG_PARM_STACK_SPACE)
4579 /* On some machines, even if a parm value arrives in a register
4580 there is still an (uninitialized) stack slot allocated for it.
4582 ??? When MAYBE_REG_PARM_STACK_SPACE is defined, we can't tell
4583 whether this parameter already has a stack slot allocated,
4584 because an arg block exists only if current_function_args_size
4585 is larger than some threshold, and we haven't calculated that
4586 yet. So, for now, we just assume that stack slots never exist
4587 in this case. */
4588 || REG_PARM_STACK_SPACE (fndecl) > 0
4589 #endif
4592 stack_args_size.constant += arg_size.constant;
4593 if (arg_size.var)
4594 ADD_PARM_SIZE (stack_args_size, arg_size.var);
4596 else
4597 /* No stack slot was pushed for this parm. */
4598 stack_parm = 0;
4600 /* Update info on where next arg arrives in registers. */
4602 FUNCTION_ARG_ADVANCE (args_so_far, promoted_mode,
4603 passed_type, named_arg);
4605 /* If we can't trust the parm stack slot to be aligned enough
4606 for its ultimate type, don't use that slot after entry.
4607 We'll make another stack slot, if we need one. */
4609 unsigned int thisparm_boundary
4610 = FUNCTION_ARG_BOUNDARY (promoted_mode, passed_type);
4612 if (GET_MODE_ALIGNMENT (nominal_mode) > thisparm_boundary)
4613 stack_parm = 0;
4616 /* If parm was passed in memory, and we need to convert it on entry,
4617 don't store it back in that same slot. */
4618 if (entry_parm != 0
4619 && nominal_mode != BLKmode && nominal_mode != passed_mode)
4620 stack_parm = 0;
4622 /* When an argument is passed in multiple locations, we can't
4623 make use of this information, but we can save some copying if
4624 the whole argument is passed in a single register. */
4625 if (GET_CODE (entry_parm) == PARALLEL
4626 && nominal_mode != BLKmode && passed_mode != BLKmode)
4628 int i, len = XVECLEN (entry_parm, 0);
4630 for (i = 0; i < len; i++)
4631 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
4632 && GET_CODE (XEXP (XVECEXP (entry_parm, 0, i), 0)) == REG
4633 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
4634 == passed_mode)
4635 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
4637 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
4638 DECL_INCOMING_RTL (parm) = entry_parm;
4639 break;
4643 /* ENTRY_PARM is an RTX for the parameter as it arrives,
4644 in the mode in which it arrives.
4645 STACK_PARM is an RTX for a stack slot where the parameter can live
4646 during the function (in case we want to put it there).
4647 STACK_PARM is 0 if no stack slot was pushed for it.
4649 Now output code if necessary to convert ENTRY_PARM to
4650 the type in which this function declares it,
4651 and store that result in an appropriate place,
4652 which may be a pseudo reg, may be STACK_PARM,
4653 or may be a local stack slot if STACK_PARM is 0.
4655 Set DECL_RTL to that place. */
4657 if (nominal_mode == BLKmode || GET_CODE (entry_parm) == PARALLEL)
4659 /* If a BLKmode arrives in registers, copy it to a stack slot.
4660 Handle calls that pass values in multiple non-contiguous
4661 locations. The Irix 6 ABI has examples of this. */
4662 if (GET_CODE (entry_parm) == REG
4663 || GET_CODE (entry_parm) == PARALLEL)
4665 int size_stored
4666 = CEIL_ROUND (int_size_in_bytes (TREE_TYPE (parm)),
4667 UNITS_PER_WORD);
4669 /* Note that we will be storing an integral number of words.
4670 So we have to be careful to ensure that we allocate an
4671 integral number of words. We do this below in the
4672 assign_stack_local if space was not allocated in the argument
4673 list. If it was, this will not work if PARM_BOUNDARY is not
4674 a multiple of BITS_PER_WORD. It isn't clear how to fix this
4675 if it becomes a problem. */
4677 if (stack_parm == 0)
4679 stack_parm
4680 = assign_stack_local (GET_MODE (entry_parm),
4681 size_stored, 0);
4682 set_mem_attributes (stack_parm, parm, 1);
4685 else if (PARM_BOUNDARY % BITS_PER_WORD != 0)
4686 abort ();
4688 /* Handle calls that pass values in multiple non-contiguous
4689 locations. The Irix 6 ABI has examples of this. */
4690 if (GET_CODE (entry_parm) == PARALLEL)
4691 emit_group_store (validize_mem (stack_parm), entry_parm,
4692 int_size_in_bytes (TREE_TYPE (parm)));
4693 else
4694 move_block_from_reg (REGNO (entry_parm),
4695 validize_mem (stack_parm),
4696 size_stored / UNITS_PER_WORD,
4697 int_size_in_bytes (TREE_TYPE (parm)));
4699 SET_DECL_RTL (parm, stack_parm);
4701 else if (! ((! optimize
4702 && ! DECL_REGISTER (parm))
4703 || TREE_SIDE_EFFECTS (parm)
4704 /* If -ffloat-store specified, don't put explicit
4705 float variables into registers. */
4706 || (flag_float_store
4707 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
4708 /* Always assign pseudo to structure return or item passed
4709 by invisible reference. */
4710 || passed_pointer || parm == function_result_decl)
4712 /* Store the parm in a pseudoregister during the function, but we
4713 may need to do it in a wider mode. */
4715 rtx parmreg;
4716 unsigned int regno, regnoi = 0, regnor = 0;
4718 unsignedp = TREE_UNSIGNED (TREE_TYPE (parm));
4720 promoted_nominal_mode
4721 = promote_mode (TREE_TYPE (parm), nominal_mode, &unsignedp, 0);
4723 parmreg = gen_reg_rtx (promoted_nominal_mode);
4724 mark_user_reg (parmreg);
4726 /* If this was an item that we received a pointer to, set DECL_RTL
4727 appropriately. */
4728 if (passed_pointer)
4730 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (passed_type)),
4731 parmreg);
4732 set_mem_attributes (x, parm, 1);
4733 SET_DECL_RTL (parm, x);
4735 else
4737 SET_DECL_RTL (parm, parmreg);
4738 maybe_set_unchanging (DECL_RTL (parm), parm);
4741 /* Copy the value into the register. */
4742 if (nominal_mode != passed_mode
4743 || promoted_nominal_mode != promoted_mode)
4745 int save_tree_used;
4746 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
4747 mode, by the caller. We now have to convert it to
4748 NOMINAL_MODE, if different. However, PARMREG may be in
4749 a different mode than NOMINAL_MODE if it is being stored
4750 promoted.
4752 If ENTRY_PARM is a hard register, it might be in a register
4753 not valid for operating in its mode (e.g., an odd-numbered
4754 register for a DFmode). In that case, moves are the only
4755 thing valid, so we can't do a convert from there. This
4756 occurs when the calling sequence allow such misaligned
4757 usages.
4759 In addition, the conversion may involve a call, which could
4760 clobber parameters which haven't been copied to pseudo
4761 registers yet. Therefore, we must first copy the parm to
4762 a pseudo reg here, and save the conversion until after all
4763 parameters have been moved. */
4765 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4767 emit_move_insn (tempreg, validize_mem (entry_parm));
4769 push_to_sequence (conversion_insns);
4770 tempreg = convert_to_mode (nominal_mode, tempreg, unsignedp);
4772 if (GET_CODE (tempreg) == SUBREG
4773 && GET_MODE (tempreg) == nominal_mode
4774 && GET_CODE (SUBREG_REG (tempreg)) == REG
4775 && nominal_mode == passed_mode
4776 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (entry_parm)
4777 && GET_MODE_SIZE (GET_MODE (tempreg))
4778 < GET_MODE_SIZE (GET_MODE (entry_parm)))
4780 /* The argument is already sign/zero extended, so note it
4781 into the subreg. */
4782 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
4783 SUBREG_PROMOTED_UNSIGNED_SET (tempreg, unsignedp);
4786 /* TREE_USED gets set erroneously during expand_assignment. */
4787 save_tree_used = TREE_USED (parm);
4788 expand_assignment (parm,
4789 make_tree (nominal_type, tempreg), 0, 0);
4790 TREE_USED (parm) = save_tree_used;
4791 conversion_insns = get_insns ();
4792 did_conversion = 1;
4793 end_sequence ();
4795 else
4796 emit_move_insn (parmreg, validize_mem (entry_parm));
4798 /* If we were passed a pointer but the actual value
4799 can safely live in a register, put it in one. */
4800 if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
4801 /* If by-reference argument was promoted, demote it. */
4802 && (TYPE_MODE (TREE_TYPE (parm)) != GET_MODE (DECL_RTL (parm))
4803 || ! ((! optimize
4804 && ! DECL_REGISTER (parm))
4805 || TREE_SIDE_EFFECTS (parm)
4806 /* If -ffloat-store specified, don't put explicit
4807 float variables into registers. */
4808 || (flag_float_store
4809 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))))
4811 /* We can't use nominal_mode, because it will have been set to
4812 Pmode above. We must use the actual mode of the parm. */
4813 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
4814 mark_user_reg (parmreg);
4815 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
4817 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
4818 int unsigned_p = TREE_UNSIGNED (TREE_TYPE (parm));
4819 push_to_sequence (conversion_insns);
4820 emit_move_insn (tempreg, DECL_RTL (parm));
4821 SET_DECL_RTL (parm,
4822 convert_to_mode (GET_MODE (parmreg),
4823 tempreg,
4824 unsigned_p));
4825 emit_move_insn (parmreg, DECL_RTL (parm));
4826 conversion_insns = get_insns();
4827 did_conversion = 1;
4828 end_sequence ();
4830 else
4831 emit_move_insn (parmreg, DECL_RTL (parm));
4832 SET_DECL_RTL (parm, parmreg);
4833 /* STACK_PARM is the pointer, not the parm, and PARMREG is
4834 now the parm. */
4835 stack_parm = 0;
4837 #ifdef FUNCTION_ARG_CALLEE_COPIES
4838 /* If we are passed an arg by reference and it is our responsibility
4839 to make a copy, do it now.
4840 PASSED_TYPE and PASSED mode now refer to the pointer, not the
4841 original argument, so we must recreate them in the call to
4842 FUNCTION_ARG_CALLEE_COPIES. */
4843 /* ??? Later add code to handle the case that if the argument isn't
4844 modified, don't do the copy. */
4846 else if (passed_pointer
4847 && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
4848 TYPE_MODE (DECL_ARG_TYPE (parm)),
4849 DECL_ARG_TYPE (parm),
4850 named_arg)
4851 && ! TREE_ADDRESSABLE (DECL_ARG_TYPE (parm)))
4853 rtx copy;
4854 tree type = DECL_ARG_TYPE (parm);
4856 /* This sequence may involve a library call perhaps clobbering
4857 registers that haven't been copied to pseudos yet. */
4859 push_to_sequence (conversion_insns);
4861 if (!COMPLETE_TYPE_P (type)
4862 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4863 /* This is a variable sized object. */
4864 copy = gen_rtx_MEM (BLKmode,
4865 allocate_dynamic_stack_space
4866 (expr_size (parm), NULL_RTX,
4867 TYPE_ALIGN (type)));
4868 else
4869 copy = assign_stack_temp (TYPE_MODE (type),
4870 int_size_in_bytes (type), 1);
4871 set_mem_attributes (copy, parm, 1);
4873 store_expr (parm, copy, 0);
4874 emit_move_insn (parmreg, XEXP (copy, 0));
4875 conversion_insns = get_insns ();
4876 did_conversion = 1;
4877 end_sequence ();
4879 #endif /* FUNCTION_ARG_CALLEE_COPIES */
4881 /* In any case, record the parm's desired stack location
4882 in case we later discover it must live in the stack.
4884 If it is a COMPLEX value, store the stack location for both
4885 halves. */
4887 if (GET_CODE (parmreg) == CONCAT)
4888 regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
4889 else
4890 regno = REGNO (parmreg);
4892 if (regno >= max_parm_reg)
4894 rtx *new;
4895 int old_max_parm_reg = max_parm_reg;
4897 /* It's slow to expand this one register at a time,
4898 but it's also rare and we need max_parm_reg to be
4899 precisely correct. */
4900 max_parm_reg = regno + 1;
4901 new = (rtx *) ggc_realloc (parm_reg_stack_loc,
4902 max_parm_reg * sizeof (rtx));
4903 memset ((char *) (new + old_max_parm_reg), 0,
4904 (max_parm_reg - old_max_parm_reg) * sizeof (rtx));
4905 parm_reg_stack_loc = new;
4908 if (GET_CODE (parmreg) == CONCAT)
4910 enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
4912 regnor = REGNO (gen_realpart (submode, parmreg));
4913 regnoi = REGNO (gen_imagpart (submode, parmreg));
4915 if (stack_parm != 0)
4917 parm_reg_stack_loc[regnor]
4918 = gen_realpart (submode, stack_parm);
4919 parm_reg_stack_loc[regnoi]
4920 = gen_imagpart (submode, stack_parm);
4922 else
4924 parm_reg_stack_loc[regnor] = 0;
4925 parm_reg_stack_loc[regnoi] = 0;
4928 else
4929 parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
4931 /* Mark the register as eliminable if we did no conversion
4932 and it was copied from memory at a fixed offset,
4933 and the arg pointer was not copied to a pseudo-reg.
4934 If the arg pointer is a pseudo reg or the offset formed
4935 an invalid address, such memory-equivalences
4936 as we make here would screw up life analysis for it. */
4937 if (nominal_mode == passed_mode
4938 && ! did_conversion
4939 && stack_parm != 0
4940 && GET_CODE (stack_parm) == MEM
4941 && stack_offset.var == 0
4942 && reg_mentioned_p (virtual_incoming_args_rtx,
4943 XEXP (stack_parm, 0)))
4945 rtx linsn = get_last_insn ();
4946 rtx sinsn, set;
4948 /* Mark complex types separately. */
4949 if (GET_CODE (parmreg) == CONCAT)
4950 /* Scan backwards for the set of the real and
4951 imaginary parts. */
4952 for (sinsn = linsn; sinsn != 0;
4953 sinsn = prev_nonnote_insn (sinsn))
4955 set = single_set (sinsn);
4956 if (set != 0
4957 && SET_DEST (set) == regno_reg_rtx [regnoi])
4958 REG_NOTES (sinsn)
4959 = gen_rtx_EXPR_LIST (REG_EQUIV,
4960 parm_reg_stack_loc[regnoi],
4961 REG_NOTES (sinsn));
4962 else if (set != 0
4963 && SET_DEST (set) == regno_reg_rtx [regnor])
4964 REG_NOTES (sinsn)
4965 = gen_rtx_EXPR_LIST (REG_EQUIV,
4966 parm_reg_stack_loc[regnor],
4967 REG_NOTES (sinsn));
4969 else if ((set = single_set (linsn)) != 0
4970 && SET_DEST (set) == parmreg)
4971 REG_NOTES (linsn)
4972 = gen_rtx_EXPR_LIST (REG_EQUIV,
4973 stack_parm, REG_NOTES (linsn));
4976 /* For pointer data type, suggest pointer register. */
4977 if (POINTER_TYPE_P (TREE_TYPE (parm)))
4978 mark_reg_pointer (parmreg,
4979 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4981 /* If something wants our address, try to use ADDRESSOF. */
4982 if (TREE_ADDRESSABLE (parm))
4984 /* If we end up putting something into the stack,
4985 fixup_var_refs_insns will need to make a pass over
4986 all the instructions. It looks through the pending
4987 sequences -- but it can't see the ones in the
4988 CONVERSION_INSNS, if they're not on the sequence
4989 stack. So, we go back to that sequence, just so that
4990 the fixups will happen. */
4991 push_to_sequence (conversion_insns);
4992 put_var_into_stack (parm);
4993 conversion_insns = get_insns ();
4994 end_sequence ();
4997 else
4999 /* Value must be stored in the stack slot STACK_PARM
5000 during function execution. */
5002 if (promoted_mode != nominal_mode)
5004 /* Conversion is required. */
5005 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
5007 emit_move_insn (tempreg, validize_mem (entry_parm));
5009 push_to_sequence (conversion_insns);
5010 entry_parm = convert_to_mode (nominal_mode, tempreg,
5011 TREE_UNSIGNED (TREE_TYPE (parm)));
5012 if (stack_parm)
5013 /* ??? This may need a big-endian conversion on sparc64. */
5014 stack_parm = adjust_address (stack_parm, nominal_mode, 0);
5016 conversion_insns = get_insns ();
5017 did_conversion = 1;
5018 end_sequence ();
5021 if (entry_parm != stack_parm)
5023 if (stack_parm == 0)
5025 stack_parm
5026 = assign_stack_local (GET_MODE (entry_parm),
5027 GET_MODE_SIZE (GET_MODE (entry_parm)), 0);
5028 set_mem_attributes (stack_parm, parm, 1);
5031 if (promoted_mode != nominal_mode)
5033 push_to_sequence (conversion_insns);
5034 emit_move_insn (validize_mem (stack_parm),
5035 validize_mem (entry_parm));
5036 conversion_insns = get_insns ();
5037 end_sequence ();
5039 else
5040 emit_move_insn (validize_mem (stack_parm),
5041 validize_mem (entry_parm));
5044 SET_DECL_RTL (parm, stack_parm);
5047 /* If this "parameter" was the place where we are receiving the
5048 function's incoming structure pointer, set up the result. */
5049 if (parm == function_result_decl)
5051 tree result = DECL_RESULT (fndecl);
5052 rtx addr = DECL_RTL (parm);
5053 rtx x;
5055 #ifdef POINTERS_EXTEND_UNSIGNED
5056 if (GET_MODE (addr) != Pmode)
5057 addr = convert_memory_address (Pmode, addr);
5058 #endif
5060 x = gen_rtx_MEM (DECL_MODE (result), addr);
5061 set_mem_attributes (x, result, 1);
5062 SET_DECL_RTL (result, x);
5065 if (GET_CODE (DECL_RTL (parm)) == REG)
5066 REGNO_DECL (REGNO (DECL_RTL (parm))) = parm;
5067 else if (GET_CODE (DECL_RTL (parm)) == CONCAT)
5069 REGNO_DECL (REGNO (XEXP (DECL_RTL (parm), 0))) = parm;
5070 REGNO_DECL (REGNO (XEXP (DECL_RTL (parm), 1))) = parm;
5075 /* Output all parameter conversion instructions (possibly including calls)
5076 now that all parameters have been copied out of hard registers. */
5077 emit_insn (conversion_insns);
5079 last_parm_insn = get_last_insn ();
5081 current_function_args_size = stack_args_size.constant;
5083 /* Adjust function incoming argument size for alignment and
5084 minimum length. */
5086 #ifdef REG_PARM_STACK_SPACE
5087 #ifndef MAYBE_REG_PARM_STACK_SPACE
5088 current_function_args_size = MAX (current_function_args_size,
5089 REG_PARM_STACK_SPACE (fndecl));
5090 #endif
5091 #endif
5093 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
5095 current_function_args_size
5096 = ((current_function_args_size + STACK_BYTES - 1)
5097 / STACK_BYTES) * STACK_BYTES;
5099 #ifdef ARGS_GROW_DOWNWARD
5100 current_function_arg_offset_rtx
5101 = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
5102 : expand_expr (size_diffop (stack_args_size.var,
5103 size_int (-stack_args_size.constant)),
5104 NULL_RTX, VOIDmode, 0));
5105 #else
5106 current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
5107 #endif
5109 /* See how many bytes, if any, of its args a function should try to pop
5110 on return. */
5112 current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
5113 current_function_args_size);
5115 /* For stdarg.h function, save info about
5116 regs and stack space used by the named args. */
5118 current_function_args_info = args_so_far;
5120 /* Set the rtx used for the function return value. Put this in its
5121 own variable so any optimizers that need this information don't have
5122 to include tree.h. Do this here so it gets done when an inlined
5123 function gets output. */
5125 current_function_return_rtx
5126 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
5127 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
5129 /* If scalar return value was computed in a pseudo-reg, or was a named
5130 return value that got dumped to the stack, copy that to the hard
5131 return register. */
5132 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
5134 tree decl_result = DECL_RESULT (fndecl);
5135 rtx decl_rtl = DECL_RTL (decl_result);
5137 if (REG_P (decl_rtl)
5138 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5139 : DECL_REGISTER (decl_result))
5141 rtx real_decl_rtl;
5143 #ifdef FUNCTION_OUTGOING_VALUE
5144 real_decl_rtl = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl_result),
5145 fndecl);
5146 #else
5147 real_decl_rtl = FUNCTION_VALUE (TREE_TYPE (decl_result),
5148 fndecl);
5149 #endif
5150 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
5151 /* The delay slot scheduler assumes that current_function_return_rtx
5152 holds the hard register containing the return value, not a
5153 temporary pseudo. */
5154 current_function_return_rtx = real_decl_rtl;
5159 /* Indicate whether REGNO is an incoming argument to the current function
5160 that was promoted to a wider mode. If so, return the RTX for the
5161 register (to get its mode). PMODE and PUNSIGNEDP are set to the mode
5162 that REGNO is promoted from and whether the promotion was signed or
5163 unsigned. */
5165 #ifdef PROMOTE_FUNCTION_ARGS
5168 promoted_input_arg (regno, pmode, punsignedp)
5169 unsigned int regno;
5170 enum machine_mode *pmode;
5171 int *punsignedp;
5173 tree arg;
5175 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
5176 arg = TREE_CHAIN (arg))
5177 if (GET_CODE (DECL_INCOMING_RTL (arg)) == REG
5178 && REGNO (DECL_INCOMING_RTL (arg)) == regno
5179 && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
5181 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
5182 int unsignedp = TREE_UNSIGNED (TREE_TYPE (arg));
5184 mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
5185 if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
5186 && mode != DECL_MODE (arg))
5188 *pmode = DECL_MODE (arg);
5189 *punsignedp = unsignedp;
5190 return DECL_INCOMING_RTL (arg);
5194 return 0;
5197 #endif
5199 /* Compute the size and offset from the start of the stacked arguments for a
5200 parm passed in mode PASSED_MODE and with type TYPE.
5202 INITIAL_OFFSET_PTR points to the current offset into the stacked
5203 arguments.
5205 The starting offset and size for this parm are returned in *OFFSET_PTR
5206 and *ARG_SIZE_PTR, respectively.
5208 IN_REGS is nonzero if the argument will be passed in registers. It will
5209 never be set if REG_PARM_STACK_SPACE is not defined.
5211 FNDECL is the function in which the argument was defined.
5213 There are two types of rounding that are done. The first, controlled by
5214 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
5215 list to be aligned to the specific boundary (in bits). This rounding
5216 affects the initial and starting offsets, but not the argument size.
5218 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
5219 optionally rounds the size of the parm to PARM_BOUNDARY. The
5220 initial offset is not affected by this rounding, while the size always
5221 is and the starting offset may be. */
5223 /* offset_ptr will be negative for ARGS_GROW_DOWNWARD case;
5224 initial_offset_ptr is positive because locate_and_pad_parm's
5225 callers pass in the total size of args so far as
5226 initial_offset_ptr. arg_size_ptr is always positive. */
5228 void
5229 locate_and_pad_parm (passed_mode, type, in_regs, fndecl,
5230 initial_offset_ptr, offset_ptr, arg_size_ptr,
5231 alignment_pad)
5232 enum machine_mode passed_mode;
5233 tree type;
5234 int in_regs ATTRIBUTE_UNUSED;
5235 tree fndecl ATTRIBUTE_UNUSED;
5236 struct args_size *initial_offset_ptr;
5237 struct args_size *offset_ptr;
5238 struct args_size *arg_size_ptr;
5239 struct args_size *alignment_pad;
5242 tree sizetree
5243 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
5244 enum direction where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
5245 int boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
5246 #ifdef ARGS_GROW_DOWNWARD
5247 tree s2 = sizetree;
5248 #endif
5250 #ifdef REG_PARM_STACK_SPACE
5251 /* If we have found a stack parm before we reach the end of the
5252 area reserved for registers, skip that area. */
5253 if (! in_regs)
5255 int reg_parm_stack_space = 0;
5257 #ifdef MAYBE_REG_PARM_STACK_SPACE
5258 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
5259 #else
5260 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
5261 #endif
5262 if (reg_parm_stack_space > 0)
5264 if (initial_offset_ptr->var)
5266 initial_offset_ptr->var
5267 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
5268 ssize_int (reg_parm_stack_space));
5269 initial_offset_ptr->constant = 0;
5271 else if (initial_offset_ptr->constant < reg_parm_stack_space)
5272 initial_offset_ptr->constant = reg_parm_stack_space;
5275 #endif /* REG_PARM_STACK_SPACE */
5277 arg_size_ptr->var = 0;
5278 arg_size_ptr->constant = 0;
5279 alignment_pad->var = 0;
5280 alignment_pad->constant = 0;
5282 #ifdef ARGS_GROW_DOWNWARD
5283 if (initial_offset_ptr->var)
5285 offset_ptr->constant = 0;
5286 offset_ptr->var = size_binop (MINUS_EXPR, ssize_int (0),
5287 initial_offset_ptr->var);
5289 else
5291 offset_ptr->constant = -initial_offset_ptr->constant;
5292 offset_ptr->var = 0;
5295 if (where_pad != none
5296 && (!host_integerp (sizetree, 1)
5297 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5298 s2 = round_up (s2, PARM_BOUNDARY / BITS_PER_UNIT);
5299 SUB_PARM_SIZE (*offset_ptr, s2);
5301 if (!in_regs
5302 #ifdef REG_PARM_STACK_SPACE
5303 || REG_PARM_STACK_SPACE (fndecl) > 0
5304 #endif
5306 pad_to_arg_alignment (offset_ptr, boundary, alignment_pad);
5308 if (initial_offset_ptr->var)
5309 arg_size_ptr->var = size_binop (MINUS_EXPR,
5310 size_binop (MINUS_EXPR,
5311 ssize_int (0),
5312 initial_offset_ptr->var),
5313 offset_ptr->var);
5315 else
5316 arg_size_ptr->constant = (-initial_offset_ptr->constant
5317 - offset_ptr->constant);
5319 /* Pad_below needs the pre-rounded size to know how much to pad below.
5320 We only pad parameters which are not in registers as they have their
5321 padding done elsewhere. */
5322 if (where_pad == downward
5323 && !in_regs)
5324 pad_below (offset_ptr, passed_mode, sizetree);
5326 #else /* !ARGS_GROW_DOWNWARD */
5327 if (!in_regs
5328 #ifdef REG_PARM_STACK_SPACE
5329 || REG_PARM_STACK_SPACE (fndecl) > 0
5330 #endif
5332 pad_to_arg_alignment (initial_offset_ptr, boundary, alignment_pad);
5333 *offset_ptr = *initial_offset_ptr;
5335 #ifdef PUSH_ROUNDING
5336 if (passed_mode != BLKmode)
5337 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
5338 #endif
5340 /* Pad_below needs the pre-rounded size to know how much to pad below
5341 so this must be done before rounding up. */
5342 if (where_pad == downward
5343 /* However, BLKmode args passed in regs have their padding done elsewhere.
5344 The stack slot must be able to hold the entire register. */
5345 && !(in_regs && passed_mode == BLKmode))
5346 pad_below (offset_ptr, passed_mode, sizetree);
5348 if (where_pad != none
5349 && (!host_integerp (sizetree, 1)
5350 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5351 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5353 ADD_PARM_SIZE (*arg_size_ptr, sizetree);
5354 #endif /* ARGS_GROW_DOWNWARD */
5357 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
5358 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
5360 static void
5361 pad_to_arg_alignment (offset_ptr, boundary, alignment_pad)
5362 struct args_size *offset_ptr;
5363 int boundary;
5364 struct args_size *alignment_pad;
5366 tree save_var = NULL_TREE;
5367 HOST_WIDE_INT save_constant = 0;
5369 int boundary_in_bytes = boundary / BITS_PER_UNIT;
5371 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5373 save_var = offset_ptr->var;
5374 save_constant = offset_ptr->constant;
5377 alignment_pad->var = NULL_TREE;
5378 alignment_pad->constant = 0;
5380 if (boundary > BITS_PER_UNIT)
5382 if (offset_ptr->var)
5384 offset_ptr->var =
5385 #ifdef ARGS_GROW_DOWNWARD
5386 round_down
5387 #else
5388 round_up
5389 #endif
5390 (ARGS_SIZE_TREE (*offset_ptr),
5391 boundary / BITS_PER_UNIT);
5392 offset_ptr->constant = 0; /*?*/
5393 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5394 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
5395 save_var);
5397 else
5399 offset_ptr->constant =
5400 #ifdef ARGS_GROW_DOWNWARD
5401 FLOOR_ROUND (offset_ptr->constant, boundary_in_bytes);
5402 #else
5403 CEIL_ROUND (offset_ptr->constant, boundary_in_bytes);
5404 #endif
5405 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5406 alignment_pad->constant = offset_ptr->constant - save_constant;
5411 static void
5412 pad_below (offset_ptr, passed_mode, sizetree)
5413 struct args_size *offset_ptr;
5414 enum machine_mode passed_mode;
5415 tree sizetree;
5417 if (passed_mode != BLKmode)
5419 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
5420 offset_ptr->constant
5421 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
5422 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
5423 - GET_MODE_SIZE (passed_mode));
5425 else
5427 if (TREE_CODE (sizetree) != INTEGER_CST
5428 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
5430 /* Round the size up to multiple of PARM_BOUNDARY bits. */
5431 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5432 /* Add it in. */
5433 ADD_PARM_SIZE (*offset_ptr, s2);
5434 SUB_PARM_SIZE (*offset_ptr, sizetree);
5439 /* Walk the tree of blocks describing the binding levels within a function
5440 and warn about uninitialized variables.
5441 This is done after calling flow_analysis and before global_alloc
5442 clobbers the pseudo-regs to hard regs. */
5444 void
5445 uninitialized_vars_warning (block)
5446 tree block;
5448 tree decl, sub;
5449 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5451 if (warn_uninitialized
5452 && TREE_CODE (decl) == VAR_DECL
5453 /* These warnings are unreliable for and aggregates
5454 because assigning the fields one by one can fail to convince
5455 flow.c that the entire aggregate was initialized.
5456 Unions are troublesome because members may be shorter. */
5457 && ! AGGREGATE_TYPE_P (TREE_TYPE (decl))
5458 && DECL_RTL (decl) != 0
5459 && GET_CODE (DECL_RTL (decl)) == REG
5460 /* Global optimizations can make it difficult to determine if a
5461 particular variable has been initialized. However, a VAR_DECL
5462 with a nonzero DECL_INITIAL had an initializer, so do not
5463 claim it is potentially uninitialized.
5465 We do not care about the actual value in DECL_INITIAL, so we do
5466 not worry that it may be a dangling pointer. */
5467 && DECL_INITIAL (decl) == NULL_TREE
5468 && regno_uninitialized (REGNO (DECL_RTL (decl))))
5469 warning_with_decl (decl,
5470 "`%s' might be used uninitialized in this function");
5471 if (extra_warnings
5472 && TREE_CODE (decl) == VAR_DECL
5473 && DECL_RTL (decl) != 0
5474 && GET_CODE (DECL_RTL (decl)) == REG
5475 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5476 warning_with_decl (decl,
5477 "variable `%s' might be clobbered by `longjmp' or `vfork'");
5479 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5480 uninitialized_vars_warning (sub);
5483 /* Do the appropriate part of uninitialized_vars_warning
5484 but for arguments instead of local variables. */
5486 void
5487 setjmp_args_warning ()
5489 tree decl;
5490 for (decl = DECL_ARGUMENTS (current_function_decl);
5491 decl; decl = TREE_CHAIN (decl))
5492 if (DECL_RTL (decl) != 0
5493 && GET_CODE (DECL_RTL (decl)) == REG
5494 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5495 warning_with_decl (decl,
5496 "argument `%s' might be clobbered by `longjmp' or `vfork'");
5499 /* If this function call setjmp, put all vars into the stack
5500 unless they were declared `register'. */
5502 void
5503 setjmp_protect (block)
5504 tree block;
5506 tree decl, sub;
5507 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5508 if ((TREE_CODE (decl) == VAR_DECL
5509 || TREE_CODE (decl) == PARM_DECL)
5510 && DECL_RTL (decl) != 0
5511 && (GET_CODE (DECL_RTL (decl)) == REG
5512 || (GET_CODE (DECL_RTL (decl)) == MEM
5513 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5514 /* If this variable came from an inline function, it must be
5515 that its life doesn't overlap the setjmp. If there was a
5516 setjmp in the function, it would already be in memory. We
5517 must exclude such variable because their DECL_RTL might be
5518 set to strange things such as virtual_stack_vars_rtx. */
5519 && ! DECL_FROM_INLINE (decl)
5520 && (
5521 #ifdef NON_SAVING_SETJMP
5522 /* If longjmp doesn't restore the registers,
5523 don't put anything in them. */
5524 NON_SAVING_SETJMP
5526 #endif
5527 ! DECL_REGISTER (decl)))
5528 put_var_into_stack (decl);
5529 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5530 setjmp_protect (sub);
5533 /* Like the previous function, but for args instead of local variables. */
5535 void
5536 setjmp_protect_args ()
5538 tree decl;
5539 for (decl = DECL_ARGUMENTS (current_function_decl);
5540 decl; decl = TREE_CHAIN (decl))
5541 if ((TREE_CODE (decl) == VAR_DECL
5542 || TREE_CODE (decl) == PARM_DECL)
5543 && DECL_RTL (decl) != 0
5544 && (GET_CODE (DECL_RTL (decl)) == REG
5545 || (GET_CODE (DECL_RTL (decl)) == MEM
5546 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5547 && (
5548 /* If longjmp doesn't restore the registers,
5549 don't put anything in them. */
5550 #ifdef NON_SAVING_SETJMP
5551 NON_SAVING_SETJMP
5553 #endif
5554 ! DECL_REGISTER (decl)))
5555 put_var_into_stack (decl);
5558 /* Return the context-pointer register corresponding to DECL,
5559 or 0 if it does not need one. */
5562 lookup_static_chain (decl)
5563 tree decl;
5565 tree context = decl_function_context (decl);
5566 tree link;
5568 if (context == 0
5569 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_NO_STATIC_CHAIN (decl)))
5570 return 0;
5572 /* We treat inline_function_decl as an alias for the current function
5573 because that is the inline function whose vars, types, etc.
5574 are being merged into the current function.
5575 See expand_inline_function. */
5576 if (context == current_function_decl || context == inline_function_decl)
5577 return virtual_stack_vars_rtx;
5579 for (link = context_display; link; link = TREE_CHAIN (link))
5580 if (TREE_PURPOSE (link) == context)
5581 return RTL_EXPR_RTL (TREE_VALUE (link));
5583 abort ();
5586 /* Convert a stack slot address ADDR for variable VAR
5587 (from a containing function)
5588 into an address valid in this function (using a static chain). */
5591 fix_lexical_addr (addr, var)
5592 rtx addr;
5593 tree var;
5595 rtx basereg;
5596 HOST_WIDE_INT displacement;
5597 tree context = decl_function_context (var);
5598 struct function *fp;
5599 rtx base = 0;
5601 /* If this is the present function, we need not do anything. */
5602 if (context == current_function_decl || context == inline_function_decl)
5603 return addr;
5605 fp = find_function_data (context);
5607 if (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == MEM)
5608 addr = XEXP (XEXP (addr, 0), 0);
5610 /* Decode given address as base reg plus displacement. */
5611 if (GET_CODE (addr) == REG)
5612 basereg = addr, displacement = 0;
5613 else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
5614 basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
5615 else
5616 abort ();
5618 /* We accept vars reached via the containing function's
5619 incoming arg pointer and via its stack variables pointer. */
5620 if (basereg == fp->internal_arg_pointer)
5622 /* If reached via arg pointer, get the arg pointer value
5623 out of that function's stack frame.
5625 There are two cases: If a separate ap is needed, allocate a
5626 slot in the outer function for it and dereference it that way.
5627 This is correct even if the real ap is actually a pseudo.
5628 Otherwise, just adjust the offset from the frame pointer to
5629 compensate. */
5631 #ifdef NEED_SEPARATE_AP
5632 rtx addr;
5634 addr = get_arg_pointer_save_area (fp);
5635 addr = fix_lexical_addr (XEXP (addr, 0), var);
5636 addr = memory_address (Pmode, addr);
5638 base = gen_rtx_MEM (Pmode, addr);
5639 set_mem_alias_set (base, get_frame_alias_set ());
5640 base = copy_to_reg (base);
5641 #else
5642 displacement += (FIRST_PARM_OFFSET (context) - STARTING_FRAME_OFFSET);
5643 base = lookup_static_chain (var);
5644 #endif
5647 else if (basereg == virtual_stack_vars_rtx)
5649 /* This is the same code as lookup_static_chain, duplicated here to
5650 avoid an extra call to decl_function_context. */
5651 tree link;
5653 for (link = context_display; link; link = TREE_CHAIN (link))
5654 if (TREE_PURPOSE (link) == context)
5656 base = RTL_EXPR_RTL (TREE_VALUE (link));
5657 break;
5661 if (base == 0)
5662 abort ();
5664 /* Use same offset, relative to appropriate static chain or argument
5665 pointer. */
5666 return plus_constant (base, displacement);
5669 /* Return the address of the trampoline for entering nested fn FUNCTION.
5670 If necessary, allocate a trampoline (in the stack frame)
5671 and emit rtl to initialize its contents (at entry to this function). */
5674 trampoline_address (function)
5675 tree function;
5677 tree link;
5678 tree rtlexp;
5679 rtx tramp;
5680 struct function *fp;
5681 tree fn_context;
5683 /* Find an existing trampoline and return it. */
5684 for (link = trampoline_list; link; link = TREE_CHAIN (link))
5685 if (TREE_PURPOSE (link) == function)
5686 return
5687 adjust_trampoline_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0));
5689 for (fp = outer_function_chain; fp; fp = fp->outer)
5690 for (link = fp->x_trampoline_list; link; link = TREE_CHAIN (link))
5691 if (TREE_PURPOSE (link) == function)
5693 tramp = fix_lexical_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0),
5694 function);
5695 return adjust_trampoline_addr (tramp);
5698 /* None exists; we must make one. */
5700 /* Find the `struct function' for the function containing FUNCTION. */
5701 fp = 0;
5702 fn_context = decl_function_context (function);
5703 if (fn_context != current_function_decl
5704 && fn_context != inline_function_decl)
5705 fp = find_function_data (fn_context);
5707 /* Allocate run-time space for this trampoline
5708 (usually in the defining function's stack frame). */
5709 #ifdef ALLOCATE_TRAMPOLINE
5710 tramp = ALLOCATE_TRAMPOLINE (fp);
5711 #else
5712 /* If rounding needed, allocate extra space
5713 to ensure we have TRAMPOLINE_SIZE bytes left after rounding up. */
5714 #define TRAMPOLINE_REAL_SIZE \
5715 (TRAMPOLINE_SIZE + (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT) - 1)
5716 tramp = assign_stack_local_1 (BLKmode, TRAMPOLINE_REAL_SIZE, 0,
5717 fp ? fp : cfun);
5718 #endif
5720 /* Record the trampoline for reuse and note it for later initialization
5721 by expand_function_end. */
5722 if (fp != 0)
5724 rtlexp = make_node (RTL_EXPR);
5725 RTL_EXPR_RTL (rtlexp) = tramp;
5726 fp->x_trampoline_list = tree_cons (function, rtlexp,
5727 fp->x_trampoline_list);
5729 else
5731 /* Make the RTL_EXPR node temporary, not momentary, so that the
5732 trampoline_list doesn't become garbage. */
5733 rtlexp = make_node (RTL_EXPR);
5735 RTL_EXPR_RTL (rtlexp) = tramp;
5736 trampoline_list = tree_cons (function, rtlexp, trampoline_list);
5739 tramp = fix_lexical_addr (XEXP (tramp, 0), function);
5740 return adjust_trampoline_addr (tramp);
5743 /* Given a trampoline address,
5744 round it to multiple of TRAMPOLINE_ALIGNMENT. */
5746 static rtx
5747 round_trampoline_addr (tramp)
5748 rtx tramp;
5750 /* Round address up to desired boundary. */
5751 rtx temp = gen_reg_rtx (Pmode);
5752 rtx addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5753 rtx mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5755 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5756 temp, 0, OPTAB_LIB_WIDEN);
5757 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5758 temp, 0, OPTAB_LIB_WIDEN);
5760 return tramp;
5763 /* Given a trampoline address, round it then apply any
5764 platform-specific adjustments so that the result can be used for a
5765 function call . */
5767 static rtx
5768 adjust_trampoline_addr (tramp)
5769 rtx tramp;
5771 tramp = round_trampoline_addr (tramp);
5772 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5773 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5774 #endif
5775 return tramp;
5778 /* Put all this function's BLOCK nodes including those that are chained
5779 onto the first block into a vector, and return it.
5780 Also store in each NOTE for the beginning or end of a block
5781 the index of that block in the vector.
5782 The arguments are BLOCK, the chain of top-level blocks of the function,
5783 and INSNS, the insn chain of the function. */
5785 void
5786 identify_blocks ()
5788 int n_blocks;
5789 tree *block_vector, *last_block_vector;
5790 tree *block_stack;
5791 tree block = DECL_INITIAL (current_function_decl);
5793 if (block == 0)
5794 return;
5796 /* Fill the BLOCK_VECTOR with all of the BLOCKs in this function, in
5797 depth-first order. */
5798 block_vector = get_block_vector (block, &n_blocks);
5799 block_stack = (tree *) xmalloc (n_blocks * sizeof (tree));
5801 last_block_vector = identify_blocks_1 (get_insns (),
5802 block_vector + 1,
5803 block_vector + n_blocks,
5804 block_stack);
5806 /* If we didn't use all of the subblocks, we've misplaced block notes. */
5807 /* ??? This appears to happen all the time. Latent bugs elsewhere? */
5808 if (0 && last_block_vector != block_vector + n_blocks)
5809 abort ();
5811 free (block_vector);
5812 free (block_stack);
5815 /* Subroutine of identify_blocks. Do the block substitution on the
5816 insn chain beginning with INSNS. Recurse for CALL_PLACEHOLDER chains.
5818 BLOCK_STACK is pushed and popped for each BLOCK_BEGIN/BLOCK_END pair.
5819 BLOCK_VECTOR is incremented for each block seen. */
5821 static tree *
5822 identify_blocks_1 (insns, block_vector, end_block_vector, orig_block_stack)
5823 rtx insns;
5824 tree *block_vector;
5825 tree *end_block_vector;
5826 tree *orig_block_stack;
5828 rtx insn;
5829 tree *block_stack = orig_block_stack;
5831 for (insn = insns; insn; insn = NEXT_INSN (insn))
5833 if (GET_CODE (insn) == NOTE)
5835 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5837 tree b;
5839 /* If there are more block notes than BLOCKs, something
5840 is badly wrong. */
5841 if (block_vector == end_block_vector)
5842 abort ();
5844 b = *block_vector++;
5845 NOTE_BLOCK (insn) = b;
5846 *block_stack++ = b;
5848 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5850 /* If there are more NOTE_INSN_BLOCK_ENDs than
5851 NOTE_INSN_BLOCK_BEGs, something is badly wrong. */
5852 if (block_stack == orig_block_stack)
5853 abort ();
5855 NOTE_BLOCK (insn) = *--block_stack;
5858 else if (GET_CODE (insn) == CALL_INSN
5859 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5861 rtx cp = PATTERN (insn);
5863 block_vector = identify_blocks_1 (XEXP (cp, 0), block_vector,
5864 end_block_vector, block_stack);
5865 if (XEXP (cp, 1))
5866 block_vector = identify_blocks_1 (XEXP (cp, 1), block_vector,
5867 end_block_vector, block_stack);
5868 if (XEXP (cp, 2))
5869 block_vector = identify_blocks_1 (XEXP (cp, 2), block_vector,
5870 end_block_vector, block_stack);
5874 /* If there are more NOTE_INSN_BLOCK_BEGINs than NOTE_INSN_BLOCK_ENDs,
5875 something is badly wrong. */
5876 if (block_stack != orig_block_stack)
5877 abort ();
5879 return block_vector;
5882 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
5883 and create duplicate blocks. */
5884 /* ??? Need an option to either create block fragments or to create
5885 abstract origin duplicates of a source block. It really depends
5886 on what optimization has been performed. */
5888 void
5889 reorder_blocks ()
5891 tree block = DECL_INITIAL (current_function_decl);
5892 varray_type block_stack;
5894 if (block == NULL_TREE)
5895 return;
5897 VARRAY_TREE_INIT (block_stack, 10, "block_stack");
5899 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
5900 reorder_blocks_0 (block);
5902 /* Prune the old trees away, so that they don't get in the way. */
5903 BLOCK_SUBBLOCKS (block) = NULL_TREE;
5904 BLOCK_CHAIN (block) = NULL_TREE;
5906 /* Recreate the block tree from the note nesting. */
5907 reorder_blocks_1 (get_insns (), block, &block_stack);
5908 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
5910 /* Remove deleted blocks from the block fragment chains. */
5911 reorder_fix_fragments (block);
5914 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
5916 static void
5917 reorder_blocks_0 (block)
5918 tree block;
5920 while (block)
5922 TREE_ASM_WRITTEN (block) = 0;
5923 reorder_blocks_0 (BLOCK_SUBBLOCKS (block));
5924 block = BLOCK_CHAIN (block);
5928 static void
5929 reorder_blocks_1 (insns, current_block, p_block_stack)
5930 rtx insns;
5931 tree current_block;
5932 varray_type *p_block_stack;
5934 rtx insn;
5936 for (insn = insns; insn; insn = NEXT_INSN (insn))
5938 if (GET_CODE (insn) == NOTE)
5940 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5942 tree block = NOTE_BLOCK (insn);
5944 /* If we have seen this block before, that means it now
5945 spans multiple address regions. Create a new fragment. */
5946 if (TREE_ASM_WRITTEN (block))
5948 tree new_block = copy_node (block);
5949 tree origin;
5951 origin = (BLOCK_FRAGMENT_ORIGIN (block)
5952 ? BLOCK_FRAGMENT_ORIGIN (block)
5953 : block);
5954 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
5955 BLOCK_FRAGMENT_CHAIN (new_block)
5956 = BLOCK_FRAGMENT_CHAIN (origin);
5957 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
5959 NOTE_BLOCK (insn) = new_block;
5960 block = new_block;
5963 BLOCK_SUBBLOCKS (block) = 0;
5964 TREE_ASM_WRITTEN (block) = 1;
5965 BLOCK_SUPERCONTEXT (block) = current_block;
5966 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
5967 BLOCK_SUBBLOCKS (current_block) = block;
5968 current_block = block;
5969 VARRAY_PUSH_TREE (*p_block_stack, block);
5971 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5973 NOTE_BLOCK (insn) = VARRAY_TOP_TREE (*p_block_stack);
5974 VARRAY_POP (*p_block_stack);
5975 BLOCK_SUBBLOCKS (current_block)
5976 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
5977 current_block = BLOCK_SUPERCONTEXT (current_block);
5980 else if (GET_CODE (insn) == CALL_INSN
5981 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5983 rtx cp = PATTERN (insn);
5984 reorder_blocks_1 (XEXP (cp, 0), current_block, p_block_stack);
5985 if (XEXP (cp, 1))
5986 reorder_blocks_1 (XEXP (cp, 1), current_block, p_block_stack);
5987 if (XEXP (cp, 2))
5988 reorder_blocks_1 (XEXP (cp, 2), current_block, p_block_stack);
5993 /* Rationalize BLOCK_FRAGMENT_ORIGIN. If an origin block no longer
5994 appears in the block tree, select one of the fragments to become
5995 the new origin block. */
5997 static void
5998 reorder_fix_fragments (block)
5999 tree block;
6001 while (block)
6003 tree dup_origin = BLOCK_FRAGMENT_ORIGIN (block);
6004 tree new_origin = NULL_TREE;
6006 if (dup_origin)
6008 if (! TREE_ASM_WRITTEN (dup_origin))
6010 new_origin = BLOCK_FRAGMENT_CHAIN (dup_origin);
6012 /* Find the first of the remaining fragments. There must
6013 be at least one -- the current block. */
6014 while (! TREE_ASM_WRITTEN (new_origin))
6015 new_origin = BLOCK_FRAGMENT_CHAIN (new_origin);
6016 BLOCK_FRAGMENT_ORIGIN (new_origin) = NULL_TREE;
6019 else if (! dup_origin)
6020 new_origin = block;
6022 /* Re-root the rest of the fragments to the new origin. In the
6023 case that DUP_ORIGIN was null, that means BLOCK was the origin
6024 of a chain of fragments and we want to remove those fragments
6025 that didn't make it to the output. */
6026 if (new_origin)
6028 tree *pp = &BLOCK_FRAGMENT_CHAIN (new_origin);
6029 tree chain = *pp;
6031 while (chain)
6033 if (TREE_ASM_WRITTEN (chain))
6035 BLOCK_FRAGMENT_ORIGIN (chain) = new_origin;
6036 *pp = chain;
6037 pp = &BLOCK_FRAGMENT_CHAIN (chain);
6039 chain = BLOCK_FRAGMENT_CHAIN (chain);
6041 *pp = NULL_TREE;
6044 reorder_fix_fragments (BLOCK_SUBBLOCKS (block));
6045 block = BLOCK_CHAIN (block);
6049 /* Reverse the order of elements in the chain T of blocks,
6050 and return the new head of the chain (old last element). */
6052 static tree
6053 blocks_nreverse (t)
6054 tree t;
6056 tree prev = 0, decl, next;
6057 for (decl = t; decl; decl = next)
6059 next = BLOCK_CHAIN (decl);
6060 BLOCK_CHAIN (decl) = prev;
6061 prev = decl;
6063 return prev;
6066 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
6067 non-NULL, list them all into VECTOR, in a depth-first preorder
6068 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
6069 blocks. */
6071 static int
6072 all_blocks (block, vector)
6073 tree block;
6074 tree *vector;
6076 int n_blocks = 0;
6078 while (block)
6080 TREE_ASM_WRITTEN (block) = 0;
6082 /* Record this block. */
6083 if (vector)
6084 vector[n_blocks] = block;
6086 ++n_blocks;
6088 /* Record the subblocks, and their subblocks... */
6089 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
6090 vector ? vector + n_blocks : 0);
6091 block = BLOCK_CHAIN (block);
6094 return n_blocks;
6097 /* Return a vector containing all the blocks rooted at BLOCK. The
6098 number of elements in the vector is stored in N_BLOCKS_P. The
6099 vector is dynamically allocated; it is the caller's responsibility
6100 to call `free' on the pointer returned. */
6102 static tree *
6103 get_block_vector (block, n_blocks_p)
6104 tree block;
6105 int *n_blocks_p;
6107 tree *block_vector;
6109 *n_blocks_p = all_blocks (block, NULL);
6110 block_vector = (tree *) xmalloc (*n_blocks_p * sizeof (tree));
6111 all_blocks (block, block_vector);
6113 return block_vector;
6116 static int next_block_index = 2;
6118 /* Set BLOCK_NUMBER for all the blocks in FN. */
6120 void
6121 number_blocks (fn)
6122 tree fn;
6124 int i;
6125 int n_blocks;
6126 tree *block_vector;
6128 /* For SDB and XCOFF debugging output, we start numbering the blocks
6129 from 1 within each function, rather than keeping a running
6130 count. */
6131 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
6132 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
6133 next_block_index = 1;
6134 #endif
6136 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
6138 /* The top-level BLOCK isn't numbered at all. */
6139 for (i = 1; i < n_blocks; ++i)
6140 /* We number the blocks from two. */
6141 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
6143 free (block_vector);
6145 return;
6148 /* If VAR is present in a subblock of BLOCK, return the subblock. */
6150 tree
6151 debug_find_var_in_block_tree (var, block)
6152 tree var;
6153 tree block;
6155 tree t;
6157 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
6158 if (t == var)
6159 return block;
6161 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
6163 tree ret = debug_find_var_in_block_tree (var, t);
6164 if (ret)
6165 return ret;
6168 return NULL_TREE;
6171 /* Allocate a function structure and reset its contents to the defaults. */
6173 static void
6174 prepare_function_start ()
6176 cfun = (struct function *) ggc_alloc_cleared (sizeof (struct function));
6178 init_stmt_for_function ();
6179 init_eh_for_function ();
6181 cse_not_expected = ! optimize;
6183 /* Caller save not needed yet. */
6184 caller_save_needed = 0;
6186 /* No stack slots have been made yet. */
6187 stack_slot_list = 0;
6189 current_function_has_nonlocal_label = 0;
6190 current_function_has_nonlocal_goto = 0;
6192 /* There is no stack slot for handling nonlocal gotos. */
6193 nonlocal_goto_handler_slots = 0;
6194 nonlocal_goto_stack_level = 0;
6196 /* No labels have been declared for nonlocal use. */
6197 nonlocal_labels = 0;
6198 nonlocal_goto_handler_labels = 0;
6200 /* No function calls so far in this function. */
6201 function_call_count = 0;
6203 /* No parm regs have been allocated.
6204 (This is important for output_inline_function.) */
6205 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
6207 /* Initialize the RTL mechanism. */
6208 init_emit ();
6210 /* Initialize the queue of pending postincrement and postdecrements,
6211 and some other info in expr.c. */
6212 init_expr ();
6214 /* We haven't done register allocation yet. */
6215 reg_renumber = 0;
6217 init_varasm_status (cfun);
6219 /* Clear out data used for inlining. */
6220 cfun->inlinable = 0;
6221 cfun->original_decl_initial = 0;
6222 cfun->original_arg_vector = 0;
6224 cfun->stack_alignment_needed = STACK_BOUNDARY;
6225 cfun->preferred_stack_boundary = STACK_BOUNDARY;
6227 /* Set if a call to setjmp is seen. */
6228 current_function_calls_setjmp = 0;
6230 /* Set if a call to longjmp is seen. */
6231 current_function_calls_longjmp = 0;
6233 current_function_calls_alloca = 0;
6234 current_function_contains_functions = 0;
6235 current_function_is_leaf = 0;
6236 current_function_nothrow = 0;
6237 current_function_sp_is_unchanging = 0;
6238 current_function_uses_only_leaf_regs = 0;
6239 current_function_has_computed_jump = 0;
6240 current_function_is_thunk = 0;
6242 current_function_returns_pcc_struct = 0;
6243 current_function_returns_struct = 0;
6244 current_function_epilogue_delay_list = 0;
6245 current_function_uses_const_pool = 0;
6246 current_function_uses_pic_offset_table = 0;
6247 current_function_cannot_inline = 0;
6249 /* We have not yet needed to make a label to jump to for tail-recursion. */
6250 tail_recursion_label = 0;
6252 /* We haven't had a need to make a save area for ap yet. */
6253 arg_pointer_save_area = 0;
6255 /* No stack slots allocated yet. */
6256 frame_offset = 0;
6258 /* No SAVE_EXPRs in this function yet. */
6259 save_expr_regs = 0;
6261 /* No RTL_EXPRs in this function yet. */
6262 rtl_expr_chain = 0;
6264 /* Set up to allocate temporaries. */
6265 init_temp_slots ();
6267 /* Indicate that we need to distinguish between the return value of the
6268 present function and the return value of a function being called. */
6269 rtx_equal_function_value_matters = 1;
6271 /* Indicate that we have not instantiated virtual registers yet. */
6272 virtuals_instantiated = 0;
6274 /* Indicate that we want CONCATs now. */
6275 generating_concat_p = 1;
6277 /* Indicate we have no need of a frame pointer yet. */
6278 frame_pointer_needed = 0;
6280 /* By default assume not stdarg. */
6281 current_function_stdarg = 0;
6283 /* We haven't made any trampolines for this function yet. */
6284 trampoline_list = 0;
6286 init_pending_stack_adjust ();
6287 inhibit_defer_pop = 0;
6289 current_function_outgoing_args_size = 0;
6291 current_function_funcdef_no = funcdef_no++;
6293 cfun->arc_profile = profile_arc_flag || flag_test_coverage;
6295 cfun->arc_profile = profile_arc_flag || flag_test_coverage;
6297 cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
6299 cfun->max_jumptable_ents = 0;
6301 (*lang_hooks.function.init) (cfun);
6302 if (init_machine_status)
6303 cfun->machine = (*init_machine_status) ();
6306 /* Initialize the rtl expansion mechanism so that we can do simple things
6307 like generate sequences. This is used to provide a context during global
6308 initialization of some passes. */
6309 void
6310 init_dummy_function_start ()
6312 prepare_function_start ();
6315 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
6316 and initialize static variables for generating RTL for the statements
6317 of the function. */
6319 void
6320 init_function_start (subr, filename, line)
6321 tree subr;
6322 const char *filename;
6323 int line;
6325 prepare_function_start ();
6327 current_function_name = (*lang_hooks.decl_printable_name) (subr, 2);
6328 cfun->decl = subr;
6330 /* Nonzero if this is a nested function that uses a static chain. */
6332 current_function_needs_context
6333 = (decl_function_context (current_function_decl) != 0
6334 && ! DECL_NO_STATIC_CHAIN (current_function_decl));
6336 /* Within function body, compute a type's size as soon it is laid out. */
6337 immediate_size_expand++;
6339 /* Prevent ever trying to delete the first instruction of a function.
6340 Also tell final how to output a linenum before the function prologue.
6341 Note linenums could be missing, e.g. when compiling a Java .class file. */
6342 if (line > 0)
6343 emit_line_note (filename, line);
6345 /* Make sure first insn is a note even if we don't want linenums.
6346 This makes sure the first insn will never be deleted.
6347 Also, final expects a note to appear there. */
6348 emit_note (NULL, NOTE_INSN_DELETED);
6350 /* Set flags used by final.c. */
6351 if (aggregate_value_p (DECL_RESULT (subr)))
6353 #ifdef PCC_STATIC_STRUCT_RETURN
6354 current_function_returns_pcc_struct = 1;
6355 #endif
6356 current_function_returns_struct = 1;
6359 /* Warn if this value is an aggregate type,
6360 regardless of which calling convention we are using for it. */
6361 if (warn_aggregate_return
6362 && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
6363 warning ("function returns an aggregate");
6365 current_function_returns_pointer
6366 = POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
6369 /* Make sure all values used by the optimization passes have sane
6370 defaults. */
6371 void
6372 init_function_for_compilation ()
6374 reg_renumber = 0;
6376 /* No prologue/epilogue insns yet. */
6377 VARRAY_GROW (prologue, 0);
6378 VARRAY_GROW (epilogue, 0);
6379 VARRAY_GROW (sibcall_epilogue, 0);
6382 /* Expand a call to __main at the beginning of a possible main function. */
6384 #if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
6385 #undef HAS_INIT_SECTION
6386 #define HAS_INIT_SECTION
6387 #endif
6389 void
6390 expand_main_function ()
6392 #ifdef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
6393 if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN)
6395 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
6396 rtx tmp, seq;
6398 start_sequence ();
6399 /* Forcibly align the stack. */
6400 #ifdef STACK_GROWS_DOWNWARD
6401 tmp = expand_simple_binop (Pmode, AND, stack_pointer_rtx, GEN_INT(-align),
6402 stack_pointer_rtx, 1, OPTAB_WIDEN);
6403 #else
6404 tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
6405 GEN_INT (align - 1), NULL_RTX, 1, OPTAB_WIDEN);
6406 tmp = expand_simple_binop (Pmode, AND, tmp, GEN_INT (-align),
6407 stack_pointer_rtx, 1, OPTAB_WIDEN);
6408 #endif
6409 if (tmp != stack_pointer_rtx)
6410 emit_move_insn (stack_pointer_rtx, tmp);
6412 /* Enlist allocate_dynamic_stack_space to pick up the pieces. */
6413 tmp = force_reg (Pmode, const0_rtx);
6414 allocate_dynamic_stack_space (tmp, NULL_RTX, BIGGEST_ALIGNMENT);
6415 seq = get_insns ();
6416 end_sequence ();
6418 for (tmp = get_last_insn (); tmp; tmp = PREV_INSN (tmp))
6419 if (NOTE_P (tmp) && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_FUNCTION_BEG)
6420 break;
6421 if (tmp)
6422 emit_insn_before (seq, tmp);
6423 else
6424 emit_insn (seq);
6426 #endif
6428 #ifndef HAS_INIT_SECTION
6429 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, NAME__MAIN), LCT_NORMAL,
6430 VOIDmode, 0);
6431 #endif
6434 /* The PENDING_SIZES represent the sizes of variable-sized types.
6435 Create RTL for the various sizes now (using temporary variables),
6436 so that we can refer to the sizes from the RTL we are generating
6437 for the current function. The PENDING_SIZES are a TREE_LIST. The
6438 TREE_VALUE of each node is a SAVE_EXPR. */
6440 void
6441 expand_pending_sizes (pending_sizes)
6442 tree pending_sizes;
6444 tree tem;
6446 /* Evaluate now the sizes of any types declared among the arguments. */
6447 for (tem = pending_sizes; tem; tem = TREE_CHAIN (tem))
6449 expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode, 0);
6450 /* Flush the queue in case this parameter declaration has
6451 side-effects. */
6452 emit_queue ();
6456 /* Start the RTL for a new function, and set variables used for
6457 emitting RTL.
6458 SUBR is the FUNCTION_DECL node.
6459 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
6460 the function's parameters, which must be run at any return statement. */
6462 void
6463 expand_function_start (subr, parms_have_cleanups)
6464 tree subr;
6465 int parms_have_cleanups;
6467 tree tem;
6468 rtx last_ptr = NULL_RTX;
6470 /* Make sure volatile mem refs aren't considered
6471 valid operands of arithmetic insns. */
6472 init_recog_no_volatile ();
6474 current_function_instrument_entry_exit
6475 = (flag_instrument_function_entry_exit
6476 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6478 current_function_profile
6479 = (profile_flag
6480 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6482 current_function_limit_stack
6483 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
6485 /* If function gets a static chain arg, store it in the stack frame.
6486 Do this first, so it gets the first stack slot offset. */
6487 if (current_function_needs_context)
6489 last_ptr = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
6491 /* Delay copying static chain if it is not a register to avoid
6492 conflicts with regs used for parameters. */
6493 if (! SMALL_REGISTER_CLASSES
6494 || GET_CODE (static_chain_incoming_rtx) == REG)
6495 emit_move_insn (last_ptr, static_chain_incoming_rtx);
6498 /* If the parameters of this function need cleaning up, get a label
6499 for the beginning of the code which executes those cleanups. This must
6500 be done before doing anything with return_label. */
6501 if (parms_have_cleanups)
6502 cleanup_label = gen_label_rtx ();
6503 else
6504 cleanup_label = 0;
6506 /* Make the label for return statements to jump to. Do not special
6507 case machines with special return instructions -- they will be
6508 handled later during jump, ifcvt, or epilogue creation. */
6509 return_label = gen_label_rtx ();
6511 /* Initialize rtx used to return the value. */
6512 /* Do this before assign_parms so that we copy the struct value address
6513 before any library calls that assign parms might generate. */
6515 /* Decide whether to return the value in memory or in a register. */
6516 if (aggregate_value_p (DECL_RESULT (subr)))
6518 /* Returning something that won't go in a register. */
6519 rtx value_address = 0;
6521 #ifdef PCC_STATIC_STRUCT_RETURN
6522 if (current_function_returns_pcc_struct)
6524 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
6525 value_address = assemble_static_space (size);
6527 else
6528 #endif
6530 /* Expect to be passed the address of a place to store the value.
6531 If it is passed as an argument, assign_parms will take care of
6532 it. */
6533 if (struct_value_incoming_rtx)
6535 value_address = gen_reg_rtx (Pmode);
6536 emit_move_insn (value_address, struct_value_incoming_rtx);
6539 if (value_address)
6541 rtx x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), value_address);
6542 set_mem_attributes (x, DECL_RESULT (subr), 1);
6543 SET_DECL_RTL (DECL_RESULT (subr), x);
6546 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
6547 /* If return mode is void, this decl rtl should not be used. */
6548 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
6549 else
6551 /* Compute the return values into a pseudo reg, which we will copy
6552 into the true return register after the cleanups are done. */
6554 /* In order to figure out what mode to use for the pseudo, we
6555 figure out what the mode of the eventual return register will
6556 actually be, and use that. */
6557 rtx hard_reg
6558 = hard_function_value (TREE_TYPE (DECL_RESULT (subr)),
6559 subr, 1);
6561 /* Structures that are returned in registers are not aggregate_value_p,
6562 so we may see a PARALLEL. Don't play pseudo games with this. */
6563 if (! REG_P (hard_reg))
6564 SET_DECL_RTL (DECL_RESULT (subr), hard_reg);
6565 else
6567 /* Create the pseudo. */
6568 SET_DECL_RTL (DECL_RESULT (subr), gen_reg_rtx (GET_MODE (hard_reg)));
6570 /* Needed because we may need to move this to memory
6571 in case it's a named return value whose address is taken. */
6572 DECL_REGISTER (DECL_RESULT (subr)) = 1;
6576 /* Initialize rtx for parameters and local variables.
6577 In some cases this requires emitting insns. */
6579 assign_parms (subr);
6581 /* Copy the static chain now if it wasn't a register. The delay is to
6582 avoid conflicts with the parameter passing registers. */
6584 if (SMALL_REGISTER_CLASSES && current_function_needs_context)
6585 if (GET_CODE (static_chain_incoming_rtx) != REG)
6586 emit_move_insn (last_ptr, static_chain_incoming_rtx);
6588 /* The following was moved from init_function_start.
6589 The move is supposed to make sdb output more accurate. */
6590 /* Indicate the beginning of the function body,
6591 as opposed to parm setup. */
6592 emit_note (NULL, NOTE_INSN_FUNCTION_BEG);
6594 if (GET_CODE (get_last_insn ()) != NOTE)
6595 emit_note (NULL, NOTE_INSN_DELETED);
6596 parm_birth_insn = get_last_insn ();
6598 context_display = 0;
6599 if (current_function_needs_context)
6601 /* Fetch static chain values for containing functions. */
6602 tem = decl_function_context (current_function_decl);
6603 /* Copy the static chain pointer into a pseudo. If we have
6604 small register classes, copy the value from memory if
6605 static_chain_incoming_rtx is a REG. */
6606 if (tem)
6608 /* If the static chain originally came in a register, put it back
6609 there, then move it out in the next insn. The reason for
6610 this peculiar code is to satisfy function integration. */
6611 if (SMALL_REGISTER_CLASSES
6612 && GET_CODE (static_chain_incoming_rtx) == REG)
6613 emit_move_insn (static_chain_incoming_rtx, last_ptr);
6614 last_ptr = copy_to_reg (static_chain_incoming_rtx);
6617 while (tem)
6619 tree rtlexp = make_node (RTL_EXPR);
6621 RTL_EXPR_RTL (rtlexp) = last_ptr;
6622 context_display = tree_cons (tem, rtlexp, context_display);
6623 tem = decl_function_context (tem);
6624 if (tem == 0)
6625 break;
6626 /* Chain thru stack frames, assuming pointer to next lexical frame
6627 is found at the place we always store it. */
6628 #ifdef FRAME_GROWS_DOWNWARD
6629 last_ptr = plus_constant (last_ptr,
6630 -(HOST_WIDE_INT) GET_MODE_SIZE (Pmode));
6631 #endif
6632 last_ptr = gen_rtx_MEM (Pmode, memory_address (Pmode, last_ptr));
6633 set_mem_alias_set (last_ptr, get_frame_alias_set ());
6634 last_ptr = copy_to_reg (last_ptr);
6636 /* If we are not optimizing, ensure that we know that this
6637 piece of context is live over the entire function. */
6638 if (! optimize)
6639 save_expr_regs = gen_rtx_EXPR_LIST (VOIDmode, last_ptr,
6640 save_expr_regs);
6644 if (current_function_instrument_entry_exit)
6646 rtx fun = DECL_RTL (current_function_decl);
6647 if (GET_CODE (fun) == MEM)
6648 fun = XEXP (fun, 0);
6649 else
6650 abort ();
6651 emit_library_call (profile_function_entry_libfunc, LCT_NORMAL, VOIDmode,
6652 2, fun, Pmode,
6653 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6655 hard_frame_pointer_rtx),
6656 Pmode);
6659 if (current_function_profile)
6661 #ifdef PROFILE_HOOK
6662 PROFILE_HOOK (current_function_funcdef_no);
6663 #endif
6666 /* After the display initializations is where the tail-recursion label
6667 should go, if we end up needing one. Ensure we have a NOTE here
6668 since some things (like trampolines) get placed before this. */
6669 tail_recursion_reentry = emit_note (NULL, NOTE_INSN_DELETED);
6671 /* Evaluate now the sizes of any types declared among the arguments. */
6672 expand_pending_sizes (nreverse (get_pending_sizes ()));
6674 /* Make sure there is a line number after the function entry setup code. */
6675 force_next_line_note ();
6678 /* Undo the effects of init_dummy_function_start. */
6679 void
6680 expand_dummy_function_end ()
6682 /* End any sequences that failed to be closed due to syntax errors. */
6683 while (in_sequence_p ())
6684 end_sequence ();
6686 /* Outside function body, can't compute type's actual size
6687 until next function's body starts. */
6689 free_after_parsing (cfun);
6690 free_after_compilation (cfun);
6691 cfun = 0;
6694 /* Call DOIT for each hard register used as a return value from
6695 the current function. */
6697 void
6698 diddle_return_value (doit, arg)
6699 void (*doit) PARAMS ((rtx, void *));
6700 void *arg;
6702 rtx outgoing = current_function_return_rtx;
6704 if (! outgoing)
6705 return;
6707 if (GET_CODE (outgoing) == REG)
6708 (*doit) (outgoing, arg);
6709 else if (GET_CODE (outgoing) == PARALLEL)
6711 int i;
6713 for (i = 0; i < XVECLEN (outgoing, 0); i++)
6715 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
6717 if (GET_CODE (x) == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
6718 (*doit) (x, arg);
6723 static void
6724 do_clobber_return_reg (reg, arg)
6725 rtx reg;
6726 void *arg ATTRIBUTE_UNUSED;
6728 emit_insn (gen_rtx_CLOBBER (VOIDmode, reg));
6731 void
6732 clobber_return_register ()
6734 diddle_return_value (do_clobber_return_reg, NULL);
6736 /* In case we do use pseudo to return value, clobber it too. */
6737 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6739 tree decl_result = DECL_RESULT (current_function_decl);
6740 rtx decl_rtl = DECL_RTL (decl_result);
6741 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
6743 do_clobber_return_reg (decl_rtl, NULL);
6748 static void
6749 do_use_return_reg (reg, arg)
6750 rtx reg;
6751 void *arg ATTRIBUTE_UNUSED;
6753 emit_insn (gen_rtx_USE (VOIDmode, reg));
6756 void
6757 use_return_register ()
6759 diddle_return_value (do_use_return_reg, NULL);
6762 static GTY(()) rtx initial_trampoline;
6764 /* Generate RTL for the end of the current function.
6765 FILENAME and LINE are the current position in the source file.
6767 It is up to language-specific callers to do cleanups for parameters--
6768 or else, supply 1 for END_BINDINGS and we will call expand_end_bindings. */
6770 void
6771 expand_function_end (filename, line, end_bindings)
6772 const char *filename;
6773 int line;
6774 int end_bindings;
6776 tree link;
6777 rtx clobber_after;
6779 finish_expr_for_function ();
6781 /* If arg_pointer_save_area was referenced only from a nested
6782 function, we will not have initialized it yet. Do that now. */
6783 if (arg_pointer_save_area && ! cfun->arg_pointer_save_area_init)
6784 get_arg_pointer_save_area (cfun);
6786 #ifdef NON_SAVING_SETJMP
6787 /* Don't put any variables in registers if we call setjmp
6788 on a machine that fails to restore the registers. */
6789 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
6791 if (DECL_INITIAL (current_function_decl) != error_mark_node)
6792 setjmp_protect (DECL_INITIAL (current_function_decl));
6794 setjmp_protect_args ();
6796 #endif
6798 /* Initialize any trampolines required by this function. */
6799 for (link = trampoline_list; link; link = TREE_CHAIN (link))
6801 tree function = TREE_PURPOSE (link);
6802 rtx context ATTRIBUTE_UNUSED = lookup_static_chain (function);
6803 rtx tramp = RTL_EXPR_RTL (TREE_VALUE (link));
6804 #ifdef TRAMPOLINE_TEMPLATE
6805 rtx blktramp;
6806 #endif
6807 rtx seq;
6809 #ifdef TRAMPOLINE_TEMPLATE
6810 /* First make sure this compilation has a template for
6811 initializing trampolines. */
6812 if (initial_trampoline == 0)
6814 initial_trampoline
6815 = gen_rtx_MEM (BLKmode, assemble_trampoline_template ());
6816 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
6818 #endif
6820 /* Generate insns to initialize the trampoline. */
6821 start_sequence ();
6822 tramp = round_trampoline_addr (XEXP (tramp, 0));
6823 #ifdef TRAMPOLINE_TEMPLATE
6824 blktramp = replace_equiv_address (initial_trampoline, tramp);
6825 emit_block_move (blktramp, initial_trampoline,
6826 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
6827 #endif
6828 INITIALIZE_TRAMPOLINE (tramp, XEXP (DECL_RTL (function), 0), context);
6829 seq = get_insns ();
6830 end_sequence ();
6832 /* Put those insns at entry to the containing function (this one). */
6833 emit_insn_before (seq, tail_recursion_reentry);
6836 /* If we are doing stack checking and this function makes calls,
6837 do a stack probe at the start of the function to ensure we have enough
6838 space for another stack frame. */
6839 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
6841 rtx insn, seq;
6843 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6844 if (GET_CODE (insn) == CALL_INSN)
6846 start_sequence ();
6847 probe_stack_range (STACK_CHECK_PROTECT,
6848 GEN_INT (STACK_CHECK_MAX_FRAME_SIZE));
6849 seq = get_insns ();
6850 end_sequence ();
6851 emit_insn_before (seq, tail_recursion_reentry);
6852 break;
6856 /* Warn about unused parms if extra warnings were specified. */
6857 /* Either ``-W -Wunused'' or ``-Wunused-parameter'' enables this
6858 warning. WARN_UNUSED_PARAMETER is negative when set by
6859 -Wunused. */
6860 if (warn_unused_parameter > 0
6861 || (warn_unused_parameter < 0 && extra_warnings))
6863 tree decl;
6865 for (decl = DECL_ARGUMENTS (current_function_decl);
6866 decl; decl = TREE_CHAIN (decl))
6867 if (! TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
6868 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
6869 warning_with_decl (decl, "unused parameter `%s'");
6872 /* Delete handlers for nonlocal gotos if nothing uses them. */
6873 if (nonlocal_goto_handler_slots != 0
6874 && ! current_function_has_nonlocal_label)
6875 delete_handlers ();
6877 /* End any sequences that failed to be closed due to syntax errors. */
6878 while (in_sequence_p ())
6879 end_sequence ();
6881 /* Outside function body, can't compute type's actual size
6882 until next function's body starts. */
6883 immediate_size_expand--;
6885 clear_pending_stack_adjust ();
6886 do_pending_stack_adjust ();
6888 /* Mark the end of the function body.
6889 If control reaches this insn, the function can drop through
6890 without returning a value. */
6891 emit_note (NULL, NOTE_INSN_FUNCTION_END);
6893 /* Must mark the last line number note in the function, so that the test
6894 coverage code can avoid counting the last line twice. This just tells
6895 the code to ignore the immediately following line note, since there
6896 already exists a copy of this note somewhere above. This line number
6897 note is still needed for debugging though, so we can't delete it. */
6898 if (flag_test_coverage)
6899 emit_note (NULL, NOTE_INSN_REPEATED_LINE_NUMBER);
6901 /* Output a linenumber for the end of the function.
6902 SDB depends on this. */
6903 emit_line_note_force (filename, line);
6905 /* Before the return label (if any), clobber the return
6906 registers so that they are not propagated live to the rest of
6907 the function. This can only happen with functions that drop
6908 through; if there had been a return statement, there would
6909 have either been a return rtx, or a jump to the return label.
6911 We delay actual code generation after the current_function_value_rtx
6912 is computed. */
6913 clobber_after = get_last_insn ();
6915 /* Output the label for the actual return from the function,
6916 if one is expected. This happens either because a function epilogue
6917 is used instead of a return instruction, or because a return was done
6918 with a goto in order to run local cleanups, or because of pcc-style
6919 structure returning. */
6920 if (return_label)
6921 emit_label (return_label);
6923 /* C++ uses this. */
6924 if (end_bindings)
6925 expand_end_bindings (0, 0, 0);
6927 if (current_function_instrument_entry_exit)
6929 rtx fun = DECL_RTL (current_function_decl);
6930 if (GET_CODE (fun) == MEM)
6931 fun = XEXP (fun, 0);
6932 else
6933 abort ();
6934 emit_library_call (profile_function_exit_libfunc, LCT_NORMAL, VOIDmode,
6935 2, fun, Pmode,
6936 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6938 hard_frame_pointer_rtx),
6939 Pmode);
6942 /* Let except.c know where it should emit the call to unregister
6943 the function context for sjlj exceptions. */
6944 if (flag_exceptions && USING_SJLJ_EXCEPTIONS)
6945 sjlj_emit_function_exit_after (get_last_insn ());
6947 /* If we had calls to alloca, and this machine needs
6948 an accurate stack pointer to exit the function,
6949 insert some code to save and restore the stack pointer. */
6950 #ifdef EXIT_IGNORE_STACK
6951 if (! EXIT_IGNORE_STACK)
6952 #endif
6953 if (current_function_calls_alloca)
6955 rtx tem = 0;
6957 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
6958 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
6961 /* If scalar return value was computed in a pseudo-reg, or was a named
6962 return value that got dumped to the stack, copy that to the hard
6963 return register. */
6964 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6966 tree decl_result = DECL_RESULT (current_function_decl);
6967 rtx decl_rtl = DECL_RTL (decl_result);
6969 if (REG_P (decl_rtl)
6970 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
6971 : DECL_REGISTER (decl_result))
6973 rtx real_decl_rtl = current_function_return_rtx;
6975 /* This should be set in assign_parms. */
6976 if (! REG_FUNCTION_VALUE_P (real_decl_rtl))
6977 abort ();
6979 /* If this is a BLKmode structure being returned in registers,
6980 then use the mode computed in expand_return. Note that if
6981 decl_rtl is memory, then its mode may have been changed,
6982 but that current_function_return_rtx has not. */
6983 if (GET_MODE (real_decl_rtl) == BLKmode)
6984 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
6986 /* If a named return value dumped decl_return to memory, then
6987 we may need to re-do the PROMOTE_MODE signed/unsigned
6988 extension. */
6989 if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
6991 int unsignedp = TREE_UNSIGNED (TREE_TYPE (decl_result));
6993 #ifdef PROMOTE_FUNCTION_RETURN
6994 promote_mode (TREE_TYPE (decl_result), GET_MODE (decl_rtl),
6995 &unsignedp, 1);
6996 #endif
6998 convert_move (real_decl_rtl, decl_rtl, unsignedp);
7000 else if (GET_CODE (real_decl_rtl) == PARALLEL)
7001 emit_group_load (real_decl_rtl, decl_rtl,
7002 int_size_in_bytes (TREE_TYPE (decl_result)));
7003 else
7004 emit_move_insn (real_decl_rtl, decl_rtl);
7008 /* If returning a structure, arrange to return the address of the value
7009 in a place where debuggers expect to find it.
7011 If returning a structure PCC style,
7012 the caller also depends on this value.
7013 And current_function_returns_pcc_struct is not necessarily set. */
7014 if (current_function_returns_struct
7015 || current_function_returns_pcc_struct)
7017 rtx value_address
7018 = XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
7019 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
7020 #ifdef FUNCTION_OUTGOING_VALUE
7021 rtx outgoing
7022 = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
7023 current_function_decl);
7024 #else
7025 rtx outgoing
7026 = FUNCTION_VALUE (build_pointer_type (type), current_function_decl);
7027 #endif
7029 /* Mark this as a function return value so integrate will delete the
7030 assignment and USE below when inlining this function. */
7031 REG_FUNCTION_VALUE_P (outgoing) = 1;
7033 #ifdef POINTERS_EXTEND_UNSIGNED
7034 /* The address may be ptr_mode and OUTGOING may be Pmode. */
7035 if (GET_MODE (outgoing) != GET_MODE (value_address))
7036 value_address = convert_memory_address (GET_MODE (outgoing),
7037 value_address);
7038 #endif
7040 emit_move_insn (outgoing, value_address);
7042 /* Show return register used to hold result (in this case the address
7043 of the result. */
7044 current_function_return_rtx = outgoing;
7047 /* If this is an implementation of throw, do what's necessary to
7048 communicate between __builtin_eh_return and the epilogue. */
7049 expand_eh_return ();
7051 /* Emit the actual code to clobber return register. */
7053 rtx seq, after;
7055 start_sequence ();
7056 clobber_return_register ();
7057 seq = get_insns ();
7058 end_sequence ();
7060 after = emit_insn_after (seq, clobber_after);
7062 if (clobber_after != after)
7063 cfun->x_clobber_return_insn = after;
7066 /* ??? This should no longer be necessary since stupid is no longer with
7067 us, but there are some parts of the compiler (eg reload_combine, and
7068 sh mach_dep_reorg) that still try and compute their own lifetime info
7069 instead of using the general framework. */
7070 use_return_register ();
7072 /* Fix up any gotos that jumped out to the outermost
7073 binding level of the function.
7074 Must follow emitting RETURN_LABEL. */
7076 /* If you have any cleanups to do at this point,
7077 and they need to create temporary variables,
7078 then you will lose. */
7079 expand_fixups (get_insns ());
7083 get_arg_pointer_save_area (f)
7084 struct function *f;
7086 rtx ret = f->x_arg_pointer_save_area;
7088 if (! ret)
7090 ret = assign_stack_local_1 (Pmode, GET_MODE_SIZE (Pmode), 0, f);
7091 f->x_arg_pointer_save_area = ret;
7094 if (f == cfun && ! f->arg_pointer_save_area_init)
7096 rtx seq;
7098 /* Save the arg pointer at the beginning of the function. The
7099 generated stack slot may not be a valid memory address, so we
7100 have to check it and fix it if necessary. */
7101 start_sequence ();
7102 emit_move_insn (validize_mem (ret), virtual_incoming_args_rtx);
7103 seq = get_insns ();
7104 end_sequence ();
7106 push_topmost_sequence ();
7107 emit_insn_after (seq, get_insns ());
7108 pop_topmost_sequence ();
7111 return ret;
7114 /* Extend a vector that records the INSN_UIDs of INSNS
7115 (a list of one or more insns). */
7117 static void
7118 record_insns (insns, vecp)
7119 rtx insns;
7120 varray_type *vecp;
7122 int i, len;
7123 rtx tmp;
7125 tmp = insns;
7126 len = 0;
7127 while (tmp != NULL_RTX)
7129 len++;
7130 tmp = NEXT_INSN (tmp);
7133 i = VARRAY_SIZE (*vecp);
7134 VARRAY_GROW (*vecp, i + len);
7135 tmp = insns;
7136 while (tmp != NULL_RTX)
7138 VARRAY_INT (*vecp, i) = INSN_UID (tmp);
7139 i++;
7140 tmp = NEXT_INSN (tmp);
7144 /* Determine how many INSN_UIDs in VEC are part of INSN. Because we can
7145 be running after reorg, SEQUENCE rtl is possible. */
7147 static int
7148 contains (insn, vec)
7149 rtx insn;
7150 varray_type vec;
7152 int i, j;
7154 if (GET_CODE (insn) == INSN
7155 && GET_CODE (PATTERN (insn)) == SEQUENCE)
7157 int count = 0;
7158 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
7159 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7160 if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == VARRAY_INT (vec, j))
7161 count++;
7162 return count;
7164 else
7166 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7167 if (INSN_UID (insn) == VARRAY_INT (vec, j))
7168 return 1;
7170 return 0;
7174 prologue_epilogue_contains (insn)
7175 rtx insn;
7177 if (contains (insn, prologue))
7178 return 1;
7179 if (contains (insn, epilogue))
7180 return 1;
7181 return 0;
7185 sibcall_epilogue_contains (insn)
7186 rtx insn;
7188 if (sibcall_epilogue)
7189 return contains (insn, sibcall_epilogue);
7190 return 0;
7193 #ifdef HAVE_return
7194 /* Insert gen_return at the end of block BB. This also means updating
7195 block_for_insn appropriately. */
7197 static void
7198 emit_return_into_block (bb, line_note)
7199 basic_block bb;
7200 rtx line_note;
7202 rtx p, end;
7204 p = NEXT_INSN (bb->end);
7205 end = emit_jump_insn_after (gen_return (), bb->end);
7206 if (line_note)
7207 emit_line_note_after (NOTE_SOURCE_FILE (line_note),
7208 NOTE_LINE_NUMBER (line_note), PREV_INSN (bb->end));
7210 #endif /* HAVE_return */
7212 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
7214 /* These functions convert the epilogue into a variant that does not modify the
7215 stack pointer. This is used in cases where a function returns an object
7216 whose size is not known until it is computed. The called function leaves the
7217 object on the stack, leaves the stack depressed, and returns a pointer to
7218 the object.
7220 What we need to do is track all modifications and references to the stack
7221 pointer, deleting the modifications and changing the references to point to
7222 the location the stack pointer would have pointed to had the modifications
7223 taken place.
7225 These functions need to be portable so we need to make as few assumptions
7226 about the epilogue as we can. However, the epilogue basically contains
7227 three things: instructions to reset the stack pointer, instructions to
7228 reload registers, possibly including the frame pointer, and an
7229 instruction to return to the caller.
7231 If we can't be sure of what a relevant epilogue insn is doing, we abort.
7232 We also make no attempt to validate the insns we make since if they are
7233 invalid, we probably can't do anything valid. The intent is that these
7234 routines get "smarter" as more and more machines start to use them and
7235 they try operating on different epilogues.
7237 We use the following structure to track what the part of the epilogue that
7238 we've already processed has done. We keep two copies of the SP equivalence,
7239 one for use during the insn we are processing and one for use in the next
7240 insn. The difference is because one part of a PARALLEL may adjust SP
7241 and the other may use it. */
7243 struct epi_info
7245 rtx sp_equiv_reg; /* REG that SP is set from, perhaps SP. */
7246 HOST_WIDE_INT sp_offset; /* Offset from SP_EQUIV_REG of present SP. */
7247 rtx new_sp_equiv_reg; /* REG to be used at end of insn. */
7248 HOST_WIDE_INT new_sp_offset; /* Offset to be used at end of insn. */
7249 rtx equiv_reg_src; /* If nonzero, the value that SP_EQUIV_REG
7250 should be set to once we no longer need
7251 its value. */
7254 static void handle_epilogue_set PARAMS ((rtx, struct epi_info *));
7255 static void emit_equiv_load PARAMS ((struct epi_info *));
7257 /* Modify INSN, a list of one or more insns that is part of the epilogue, to
7258 no modifications to the stack pointer. Return the new list of insns. */
7260 static rtx
7261 keep_stack_depressed (insns)
7262 rtx insns;
7264 int j;
7265 struct epi_info info;
7266 rtx insn, next;
7268 /* If the epilogue is just a single instruction, it ust be OK as is. */
7270 if (NEXT_INSN (insns) == NULL_RTX)
7271 return insns;
7273 /* Otherwise, start a sequence, initialize the information we have, and
7274 process all the insns we were given. */
7275 start_sequence ();
7277 info.sp_equiv_reg = stack_pointer_rtx;
7278 info.sp_offset = 0;
7279 info.equiv_reg_src = 0;
7281 insn = insns;
7282 next = NULL_RTX;
7283 while (insn != NULL_RTX)
7285 next = NEXT_INSN (insn);
7287 if (!INSN_P (insn))
7289 add_insn (insn);
7290 insn = next;
7291 continue;
7294 /* If this insn references the register that SP is equivalent to and
7295 we have a pending load to that register, we must force out the load
7296 first and then indicate we no longer know what SP's equivalent is. */
7297 if (info.equiv_reg_src != 0
7298 && reg_referenced_p (info.sp_equiv_reg, PATTERN (insn)))
7300 emit_equiv_load (&info);
7301 info.sp_equiv_reg = 0;
7304 info.new_sp_equiv_reg = info.sp_equiv_reg;
7305 info.new_sp_offset = info.sp_offset;
7307 /* If this is a (RETURN) and the return address is on the stack,
7308 update the address and change to an indirect jump. */
7309 if (GET_CODE (PATTERN (insn)) == RETURN
7310 || (GET_CODE (PATTERN (insn)) == PARALLEL
7311 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == RETURN))
7313 rtx retaddr = INCOMING_RETURN_ADDR_RTX;
7314 rtx base = 0;
7315 HOST_WIDE_INT offset = 0;
7316 rtx jump_insn, jump_set;
7318 /* If the return address is in a register, we can emit the insn
7319 unchanged. Otherwise, it must be a MEM and we see what the
7320 base register and offset are. In any case, we have to emit any
7321 pending load to the equivalent reg of SP, if any. */
7322 if (GET_CODE (retaddr) == REG)
7324 emit_equiv_load (&info);
7325 add_insn (insn);
7326 insn = next;
7327 continue;
7329 else if (GET_CODE (retaddr) == MEM
7330 && GET_CODE (XEXP (retaddr, 0)) == REG)
7331 base = gen_rtx_REG (Pmode, REGNO (XEXP (retaddr, 0))), offset = 0;
7332 else if (GET_CODE (retaddr) == MEM
7333 && GET_CODE (XEXP (retaddr, 0)) == PLUS
7334 && GET_CODE (XEXP (XEXP (retaddr, 0), 0)) == REG
7335 && GET_CODE (XEXP (XEXP (retaddr, 0), 1)) == CONST_INT)
7337 base = gen_rtx_REG (Pmode, REGNO (XEXP (XEXP (retaddr, 0), 0)));
7338 offset = INTVAL (XEXP (XEXP (retaddr, 0), 1));
7340 else
7341 abort ();
7343 /* If the base of the location containing the return pointer
7344 is SP, we must update it with the replacement address. Otherwise,
7345 just build the necessary MEM. */
7346 retaddr = plus_constant (base, offset);
7347 if (base == stack_pointer_rtx)
7348 retaddr = simplify_replace_rtx (retaddr, stack_pointer_rtx,
7349 plus_constant (info.sp_equiv_reg,
7350 info.sp_offset));
7352 retaddr = gen_rtx_MEM (Pmode, retaddr);
7354 /* If there is a pending load to the equivalent register for SP
7355 and we reference that register, we must load our address into
7356 a scratch register and then do that load. */
7357 if (info.equiv_reg_src
7358 && reg_overlap_mentioned_p (info.equiv_reg_src, retaddr))
7360 unsigned int regno;
7361 rtx reg;
7363 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
7364 if (HARD_REGNO_MODE_OK (regno, Pmode)
7365 && !fixed_regs[regno]
7366 && TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)
7367 && !REGNO_REG_SET_P (EXIT_BLOCK_PTR->global_live_at_start,
7368 regno)
7369 && !refers_to_regno_p (regno,
7370 regno + HARD_REGNO_NREGS (regno,
7371 Pmode),
7372 info.equiv_reg_src, NULL))
7373 break;
7375 if (regno == FIRST_PSEUDO_REGISTER)
7376 abort ();
7378 reg = gen_rtx_REG (Pmode, regno);
7379 emit_move_insn (reg, retaddr);
7380 retaddr = reg;
7383 emit_equiv_load (&info);
7384 jump_insn = emit_jump_insn (gen_indirect_jump (retaddr));
7386 /* Show the SET in the above insn is a RETURN. */
7387 jump_set = single_set (jump_insn);
7388 if (jump_set == 0)
7389 abort ();
7390 else
7391 SET_IS_RETURN_P (jump_set) = 1;
7394 /* If SP is not mentioned in the pattern and its equivalent register, if
7395 any, is not modified, just emit it. Otherwise, if neither is set,
7396 replace the reference to SP and emit the insn. If none of those are
7397 true, handle each SET individually. */
7398 else if (!reg_mentioned_p (stack_pointer_rtx, PATTERN (insn))
7399 && (info.sp_equiv_reg == stack_pointer_rtx
7400 || !reg_set_p (info.sp_equiv_reg, insn)))
7401 add_insn (insn);
7402 else if (! reg_set_p (stack_pointer_rtx, insn)
7403 && (info.sp_equiv_reg == stack_pointer_rtx
7404 || !reg_set_p (info.sp_equiv_reg, insn)))
7406 if (! validate_replace_rtx (stack_pointer_rtx,
7407 plus_constant (info.sp_equiv_reg,
7408 info.sp_offset),
7409 insn))
7410 abort ();
7412 add_insn (insn);
7414 else if (GET_CODE (PATTERN (insn)) == SET)
7415 handle_epilogue_set (PATTERN (insn), &info);
7416 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
7418 for (j = 0; j < XVECLEN (PATTERN (insn), 0); j++)
7419 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET)
7420 handle_epilogue_set (XVECEXP (PATTERN (insn), 0, j), &info);
7422 else
7423 add_insn (insn);
7425 info.sp_equiv_reg = info.new_sp_equiv_reg;
7426 info.sp_offset = info.new_sp_offset;
7428 insn = next;
7431 insns = get_insns ();
7432 end_sequence ();
7433 return insns;
7436 /* SET is a SET from an insn in the epilogue. P is a pointer to the epi_info
7437 structure that contains information about what we've seen so far. We
7438 process this SET by either updating that data or by emitting one or
7439 more insns. */
7441 static void
7442 handle_epilogue_set (set, p)
7443 rtx set;
7444 struct epi_info *p;
7446 /* First handle the case where we are setting SP. Record what it is being
7447 set from. If unknown, abort. */
7448 if (reg_set_p (stack_pointer_rtx, set))
7450 if (SET_DEST (set) != stack_pointer_rtx)
7451 abort ();
7453 if (GET_CODE (SET_SRC (set)) == PLUS
7454 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
7456 p->new_sp_equiv_reg = XEXP (SET_SRC (set), 0);
7457 p->new_sp_offset = INTVAL (XEXP (SET_SRC (set), 1));
7459 else
7460 p->new_sp_equiv_reg = SET_SRC (set), p->new_sp_offset = 0;
7462 /* If we are adjusting SP, we adjust from the old data. */
7463 if (p->new_sp_equiv_reg == stack_pointer_rtx)
7465 p->new_sp_equiv_reg = p->sp_equiv_reg;
7466 p->new_sp_offset += p->sp_offset;
7469 if (p->new_sp_equiv_reg == 0 || GET_CODE (p->new_sp_equiv_reg) != REG)
7470 abort ();
7472 return;
7475 /* Next handle the case where we are setting SP's equivalent register.
7476 If we already have a value to set it to, abort. We could update, but
7477 there seems little point in handling that case. Note that we have
7478 to allow for the case where we are setting the register set in
7479 the previous part of a PARALLEL inside a single insn. But use the
7480 old offset for any updates within this insn. */
7481 else if (p->new_sp_equiv_reg != 0 && reg_set_p (p->new_sp_equiv_reg, set))
7483 if (!rtx_equal_p (p->new_sp_equiv_reg, SET_DEST (set))
7484 || p->equiv_reg_src != 0)
7485 abort ();
7486 else
7487 p->equiv_reg_src
7488 = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7489 plus_constant (p->sp_equiv_reg,
7490 p->sp_offset));
7493 /* Otherwise, replace any references to SP in the insn to its new value
7494 and emit the insn. */
7495 else
7497 SET_SRC (set) = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7498 plus_constant (p->sp_equiv_reg,
7499 p->sp_offset));
7500 SET_DEST (set) = simplify_replace_rtx (SET_DEST (set), stack_pointer_rtx,
7501 plus_constant (p->sp_equiv_reg,
7502 p->sp_offset));
7503 emit_insn (set);
7507 /* Emit an insn to do the load shown in p->equiv_reg_src, if needed. */
7509 static void
7510 emit_equiv_load (p)
7511 struct epi_info *p;
7513 if (p->equiv_reg_src != 0)
7514 emit_move_insn (p->sp_equiv_reg, p->equiv_reg_src);
7516 p->equiv_reg_src = 0;
7518 #endif
7520 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
7521 this into place with notes indicating where the prologue ends and where
7522 the epilogue begins. Update the basic block information when possible. */
7524 void
7525 thread_prologue_and_epilogue_insns (f)
7526 rtx f ATTRIBUTE_UNUSED;
7528 int inserted = 0;
7529 edge e;
7530 #if defined (HAVE_sibcall_epilogue) || defined (HAVE_epilogue) || defined (HAVE_return) || defined (HAVE_prologue)
7531 rtx seq;
7532 #endif
7533 #ifdef HAVE_prologue
7534 rtx prologue_end = NULL_RTX;
7535 #endif
7536 #if defined (HAVE_epilogue) || defined(HAVE_return)
7537 rtx epilogue_end = NULL_RTX;
7538 #endif
7540 #ifdef HAVE_prologue
7541 if (HAVE_prologue)
7543 start_sequence ();
7544 seq = gen_prologue ();
7545 emit_insn (seq);
7547 /* Retain a map of the prologue insns. */
7548 record_insns (seq, &prologue);
7549 prologue_end = emit_note (NULL, NOTE_INSN_PROLOGUE_END);
7551 seq = get_insns ();
7552 end_sequence ();
7554 /* Can't deal with multiple successors of the entry block
7555 at the moment. Function should always have at least one
7556 entry point. */
7557 if (!ENTRY_BLOCK_PTR->succ || ENTRY_BLOCK_PTR->succ->succ_next)
7558 abort ();
7560 insert_insn_on_edge (seq, ENTRY_BLOCK_PTR->succ);
7561 inserted = 1;
7563 #endif
7565 /* If the exit block has no non-fake predecessors, we don't need
7566 an epilogue. */
7567 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7568 if ((e->flags & EDGE_FAKE) == 0)
7569 break;
7570 if (e == NULL)
7571 goto epilogue_done;
7573 #ifdef HAVE_return
7574 if (optimize && HAVE_return)
7576 /* If we're allowed to generate a simple return instruction,
7577 then by definition we don't need a full epilogue. Examine
7578 the block that falls through to EXIT. If it does not
7579 contain any code, examine its predecessors and try to
7580 emit (conditional) return instructions. */
7582 basic_block last;
7583 edge e_next;
7584 rtx label;
7586 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7587 if (e->flags & EDGE_FALLTHRU)
7588 break;
7589 if (e == NULL)
7590 goto epilogue_done;
7591 last = e->src;
7593 /* Verify that there are no active instructions in the last block. */
7594 label = last->end;
7595 while (label && GET_CODE (label) != CODE_LABEL)
7597 if (active_insn_p (label))
7598 break;
7599 label = PREV_INSN (label);
7602 if (last->head == label && GET_CODE (label) == CODE_LABEL)
7604 rtx epilogue_line_note = NULL_RTX;
7606 /* Locate the line number associated with the closing brace,
7607 if we can find one. */
7608 for (seq = get_last_insn ();
7609 seq && ! active_insn_p (seq);
7610 seq = PREV_INSN (seq))
7611 if (GET_CODE (seq) == NOTE && NOTE_LINE_NUMBER (seq) > 0)
7613 epilogue_line_note = seq;
7614 break;
7617 for (e = last->pred; e; e = e_next)
7619 basic_block bb = e->src;
7620 rtx jump;
7622 e_next = e->pred_next;
7623 if (bb == ENTRY_BLOCK_PTR)
7624 continue;
7626 jump = bb->end;
7627 if ((GET_CODE (jump) != JUMP_INSN) || JUMP_LABEL (jump) != label)
7628 continue;
7630 /* If we have an unconditional jump, we can replace that
7631 with a simple return instruction. */
7632 if (simplejump_p (jump))
7634 emit_return_into_block (bb, epilogue_line_note);
7635 delete_insn (jump);
7638 /* If we have a conditional jump, we can try to replace
7639 that with a conditional return instruction. */
7640 else if (condjump_p (jump))
7642 rtx ret, *loc;
7644 ret = SET_SRC (PATTERN (jump));
7645 if (GET_CODE (XEXP (ret, 1)) == LABEL_REF)
7646 loc = &XEXP (ret, 1);
7647 else
7648 loc = &XEXP (ret, 2);
7649 ret = gen_rtx_RETURN (VOIDmode);
7651 if (! validate_change (jump, loc, ret, 0))
7652 continue;
7653 if (JUMP_LABEL (jump))
7654 LABEL_NUSES (JUMP_LABEL (jump))--;
7656 /* If this block has only one successor, it both jumps
7657 and falls through to the fallthru block, so we can't
7658 delete the edge. */
7659 if (bb->succ->succ_next == NULL)
7660 continue;
7662 else
7663 continue;
7665 /* Fix up the CFG for the successful change we just made. */
7666 redirect_edge_succ (e, EXIT_BLOCK_PTR);
7669 /* Emit a return insn for the exit fallthru block. Whether
7670 this is still reachable will be determined later. */
7672 emit_barrier_after (last->end);
7673 emit_return_into_block (last, epilogue_line_note);
7674 epilogue_end = last->end;
7675 last->succ->flags &= ~EDGE_FALLTHRU;
7676 goto epilogue_done;
7679 #endif
7680 #ifdef HAVE_epilogue
7681 if (HAVE_epilogue)
7683 /* Find the edge that falls through to EXIT. Other edges may exist
7684 due to RETURN instructions, but those don't need epilogues.
7685 There really shouldn't be a mixture -- either all should have
7686 been converted or none, however... */
7688 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7689 if (e->flags & EDGE_FALLTHRU)
7690 break;
7691 if (e == NULL)
7692 goto epilogue_done;
7694 start_sequence ();
7695 epilogue_end = emit_note (NULL, NOTE_INSN_EPILOGUE_BEG);
7697 seq = gen_epilogue ();
7699 #ifdef INCOMING_RETURN_ADDR_RTX
7700 /* If this function returns with the stack depressed and we can support
7701 it, massage the epilogue to actually do that. */
7702 if (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
7703 && TYPE_RETURNS_STACK_DEPRESSED (TREE_TYPE (current_function_decl)))
7704 seq = keep_stack_depressed (seq);
7705 #endif
7707 emit_jump_insn (seq);
7709 /* Retain a map of the epilogue insns. */
7710 record_insns (seq, &epilogue);
7712 seq = get_insns ();
7713 end_sequence ();
7715 insert_insn_on_edge (seq, e);
7716 inserted = 1;
7718 #endif
7719 epilogue_done:
7721 if (inserted)
7722 commit_edge_insertions ();
7724 #ifdef HAVE_sibcall_epilogue
7725 /* Emit sibling epilogues before any sibling call sites. */
7726 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7728 basic_block bb = e->src;
7729 rtx insn = bb->end;
7730 rtx i;
7731 rtx newinsn;
7733 if (GET_CODE (insn) != CALL_INSN
7734 || ! SIBLING_CALL_P (insn))
7735 continue;
7737 start_sequence ();
7738 emit_insn (gen_sibcall_epilogue ());
7739 seq = get_insns ();
7740 end_sequence ();
7742 /* Retain a map of the epilogue insns. Used in life analysis to
7743 avoid getting rid of sibcall epilogue insns. Do this before we
7744 actually emit the sequence. */
7745 record_insns (seq, &sibcall_epilogue);
7747 i = PREV_INSN (insn);
7748 newinsn = emit_insn_before (seq, insn);
7750 #endif
7752 #ifdef HAVE_prologue
7753 if (prologue_end)
7755 rtx insn, prev;
7757 /* GDB handles `break f' by setting a breakpoint on the first
7758 line note after the prologue. Which means (1) that if
7759 there are line number notes before where we inserted the
7760 prologue we should move them, and (2) we should generate a
7761 note before the end of the first basic block, if there isn't
7762 one already there.
7764 ??? This behavior is completely broken when dealing with
7765 multiple entry functions. We simply place the note always
7766 into first basic block and let alternate entry points
7767 to be missed.
7770 for (insn = prologue_end; insn; insn = prev)
7772 prev = PREV_INSN (insn);
7773 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7775 /* Note that we cannot reorder the first insn in the
7776 chain, since rest_of_compilation relies on that
7777 remaining constant. */
7778 if (prev == NULL)
7779 break;
7780 reorder_insns (insn, insn, prologue_end);
7784 /* Find the last line number note in the first block. */
7785 for (insn = ENTRY_BLOCK_PTR->next_bb->end;
7786 insn != prologue_end && insn;
7787 insn = PREV_INSN (insn))
7788 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7789 break;
7791 /* If we didn't find one, make a copy of the first line number
7792 we run across. */
7793 if (! insn)
7795 for (insn = next_active_insn (prologue_end);
7796 insn;
7797 insn = PREV_INSN (insn))
7798 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7800 emit_line_note_after (NOTE_SOURCE_FILE (insn),
7801 NOTE_LINE_NUMBER (insn),
7802 prologue_end);
7803 break;
7807 #endif
7808 #ifdef HAVE_epilogue
7809 if (epilogue_end)
7811 rtx insn, next;
7813 /* Similarly, move any line notes that appear after the epilogue.
7814 There is no need, however, to be quite so anal about the existence
7815 of such a note. */
7816 for (insn = epilogue_end; insn; insn = next)
7818 next = NEXT_INSN (insn);
7819 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7820 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
7823 #endif
7826 /* Reposition the prologue-end and epilogue-begin notes after instruction
7827 scheduling and delayed branch scheduling. */
7829 void
7830 reposition_prologue_and_epilogue_notes (f)
7831 rtx f ATTRIBUTE_UNUSED;
7833 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
7834 rtx insn, last, note;
7835 int len;
7837 if ((len = VARRAY_SIZE (prologue)) > 0)
7839 last = 0, note = 0;
7841 /* Scan from the beginning until we reach the last prologue insn.
7842 We apparently can't depend on basic_block_{head,end} after
7843 reorg has run. */
7844 for (insn = f; insn; insn = NEXT_INSN (insn))
7846 if (GET_CODE (insn) == NOTE)
7848 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
7849 note = insn;
7851 else if (contains (insn, prologue))
7853 last = insn;
7854 if (--len == 0)
7855 break;
7859 if (last)
7861 rtx next;
7863 /* Find the prologue-end note if we haven't already, and
7864 move it to just after the last prologue insn. */
7865 if (note == 0)
7867 for (note = last; (note = NEXT_INSN (note));)
7868 if (GET_CODE (note) == NOTE
7869 && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
7870 break;
7873 next = NEXT_INSN (note);
7875 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
7876 if (GET_CODE (last) == CODE_LABEL)
7877 last = NEXT_INSN (last);
7878 reorder_insns (note, note, last);
7882 if ((len = VARRAY_SIZE (epilogue)) > 0)
7884 last = 0, note = 0;
7886 /* Scan from the end until we reach the first epilogue insn.
7887 We apparently can't depend on basic_block_{head,end} after
7888 reorg has run. */
7889 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
7891 if (GET_CODE (insn) == NOTE)
7893 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
7894 note = insn;
7896 else if (contains (insn, epilogue))
7898 last = insn;
7899 if (--len == 0)
7900 break;
7904 if (last)
7906 /* Find the epilogue-begin note if we haven't already, and
7907 move it to just before the first epilogue insn. */
7908 if (note == 0)
7910 for (note = insn; (note = PREV_INSN (note));)
7911 if (GET_CODE (note) == NOTE
7912 && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
7913 break;
7916 if (PREV_INSN (last) != note)
7917 reorder_insns (note, note, PREV_INSN (last));
7920 #endif /* HAVE_prologue or HAVE_epilogue */
7923 /* Called once, at initialization, to initialize function.c. */
7925 void
7926 init_function_once ()
7928 VARRAY_INT_INIT (prologue, 0, "prologue");
7929 VARRAY_INT_INIT (epilogue, 0, "epilogue");
7930 VARRAY_INT_INIT (sibcall_epilogue, 0, "sibcall_epilogue");
7933 #include "gt-function.h"