* config/alpha/alpha.c: Follow spelling conventions.
[official-gcc.git] / gcc / function.c
blobedc158152d97da9361f1ecac1bc5ba771dc700e3
1 /* Expands front end tree to back end RTL for GNU C-Compiler
2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This file handles the generation of rtl code from tree structure
23 at the level of the function as a whole.
24 It creates the rtl expressions for parameters and auto variables
25 and has full responsibility for allocating stack slots.
27 `expand_function_start' is called at the beginning of a function,
28 before the function body is parsed, and `expand_function_end' is
29 called after parsing the body.
31 Call `assign_stack_local' to allocate a stack slot for a local variable.
32 This is usually done during the RTL generation for the function body,
33 but it can also be done in the reload pass when a pseudo-register does
34 not get a hard register.
36 Call `put_var_into_stack' when you learn, belatedly, that a variable
37 previously given a pseudo-register must in fact go in the stack.
38 This function changes the DECL_RTL to be a stack slot instead of a reg
39 then scans all the RTL instructions so far generated to correct them. */
41 #include "config.h"
42 #include "system.h"
43 #include "rtl.h"
44 #include "tree.h"
45 #include "flags.h"
46 #include "except.h"
47 #include "function.h"
48 #include "expr.h"
49 #include "libfuncs.h"
50 #include "regs.h"
51 #include "hard-reg-set.h"
52 #include "insn-config.h"
53 #include "recog.h"
54 #include "output.h"
55 #include "basic-block.h"
56 #include "toplev.h"
57 #include "hashtab.h"
58 #include "ggc.h"
59 #include "tm_p.h"
60 #include "integrate.h"
61 #include "langhooks.h"
63 #ifndef TRAMPOLINE_ALIGNMENT
64 #define TRAMPOLINE_ALIGNMENT FUNCTION_BOUNDARY
65 #endif
67 #ifndef LOCAL_ALIGNMENT
68 #define LOCAL_ALIGNMENT(TYPE, ALIGNMENT) ALIGNMENT
69 #endif
71 /* Some systems use __main in a way incompatible with its use in gcc, in these
72 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
73 give the same symbol without quotes for an alternative entry point. You
74 must define both, or neither. */
75 #ifndef NAME__MAIN
76 #define NAME__MAIN "__main"
77 #endif
79 /* Round a value to the lowest integer less than it that is a multiple of
80 the required alignment. Avoid using division in case the value is
81 negative. Assume the alignment is a power of two. */
82 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
84 /* Similar, but round to the next highest integer that meets the
85 alignment. */
86 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
88 /* NEED_SEPARATE_AP means that we cannot derive ap from the value of fp
89 during rtl generation. If they are different register numbers, this is
90 always true. It may also be true if
91 FIRST_PARM_OFFSET - STARTING_FRAME_OFFSET is not a constant during rtl
92 generation. See fix_lexical_addr for details. */
94 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
95 #define NEED_SEPARATE_AP
96 #endif
98 /* Nonzero if function being compiled doesn't contain any calls
99 (ignoring the prologue and epilogue). This is set prior to
100 local register allocation and is valid for the remaining
101 compiler passes. */
102 int current_function_is_leaf;
104 /* Nonzero if function being compiled doesn't contain any instructions
105 that can throw an exception. This is set prior to final. */
107 int current_function_nothrow;
109 /* Nonzero if function being compiled doesn't modify the stack pointer
110 (ignoring the prologue and epilogue). This is only valid after
111 life_analysis has run. */
112 int current_function_sp_is_unchanging;
114 /* Nonzero if the function being compiled is a leaf function which only
115 uses leaf registers. This is valid after reload (specifically after
116 sched2) and is useful only if the port defines LEAF_REGISTERS. */
117 int current_function_uses_only_leaf_regs;
119 /* Nonzero once virtual register instantiation has been done.
120 assign_stack_local uses frame_pointer_rtx when this is nonzero.
121 calls.c:emit_library_call_value_1 uses it to set up
122 post-instantiation libcalls. */
123 int virtuals_instantiated;
125 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
126 static int funcdef_no;
128 /* These variables hold pointers to functions to create and destroy
129 target specific, per-function data structures. */
130 struct machine_function * (*init_machine_status) PARAMS ((void));
132 /* The FUNCTION_DECL for an inline function currently being expanded. */
133 tree inline_function_decl;
135 /* The currently compiled function. */
136 struct function *cfun = 0;
138 /* These arrays record the INSN_UIDs of the prologue and epilogue insns. */
139 static GTY(()) varray_type prologue;
140 static GTY(()) varray_type epilogue;
142 /* Array of INSN_UIDs to hold the INSN_UIDs for each sibcall epilogue
143 in this function. */
144 static GTY(()) varray_type sibcall_epilogue;
146 /* In order to evaluate some expressions, such as function calls returning
147 structures in memory, we need to temporarily allocate stack locations.
148 We record each allocated temporary in the following structure.
150 Associated with each temporary slot is a nesting level. When we pop up
151 one level, all temporaries associated with the previous level are freed.
152 Normally, all temporaries are freed after the execution of the statement
153 in which they were created. However, if we are inside a ({...}) grouping,
154 the result may be in a temporary and hence must be preserved. If the
155 result could be in a temporary, we preserve it if we can determine which
156 one it is in. If we cannot determine which temporary may contain the
157 result, all temporaries are preserved. A temporary is preserved by
158 pretending it was allocated at the previous nesting level.
160 Automatic variables are also assigned temporary slots, at the nesting
161 level where they are defined. They are marked a "kept" so that
162 free_temp_slots will not free them. */
164 struct temp_slot GTY(())
166 /* Points to next temporary slot. */
167 struct temp_slot *next;
168 /* The rtx to used to reference the slot. */
169 rtx slot;
170 /* The rtx used to represent the address if not the address of the
171 slot above. May be an EXPR_LIST if multiple addresses exist. */
172 rtx address;
173 /* The alignment (in bits) of the slot. */
174 unsigned int align;
175 /* The size, in units, of the slot. */
176 HOST_WIDE_INT size;
177 /* The type of the object in the slot, or zero if it doesn't correspond
178 to a type. We use this to determine whether a slot can be reused.
179 It can be reused if objects of the type of the new slot will always
180 conflict with objects of the type of the old slot. */
181 tree type;
182 /* The value of `sequence_rtl_expr' when this temporary is allocated. */
183 tree rtl_expr;
184 /* Non-zero if this temporary is currently in use. */
185 char in_use;
186 /* Non-zero if this temporary has its address taken. */
187 char addr_taken;
188 /* Nesting level at which this slot is being used. */
189 int level;
190 /* Non-zero if this should survive a call to free_temp_slots. */
191 int keep;
192 /* The offset of the slot from the frame_pointer, including extra space
193 for alignment. This info is for combine_temp_slots. */
194 HOST_WIDE_INT base_offset;
195 /* The size of the slot, including extra space for alignment. This
196 info is for combine_temp_slots. */
197 HOST_WIDE_INT full_size;
200 /* This structure is used to record MEMs or pseudos used to replace VAR, any
201 SUBREGs of VAR, and any MEMs containing VAR as an address. We need to
202 maintain this list in case two operands of an insn were required to match;
203 in that case we must ensure we use the same replacement. */
205 struct fixup_replacement GTY(())
207 rtx old;
208 rtx new;
209 struct fixup_replacement *next;
212 struct insns_for_mem_entry
214 /* A MEM. */
215 rtx key;
216 /* These are the INSNs which reference the MEM. */
217 rtx insns;
220 /* Forward declarations. */
222 static rtx assign_stack_local_1 PARAMS ((enum machine_mode, HOST_WIDE_INT,
223 int, struct function *));
224 static struct temp_slot *find_temp_slot_from_address PARAMS ((rtx));
225 static void put_reg_into_stack PARAMS ((struct function *, rtx, tree,
226 enum machine_mode, enum machine_mode,
227 int, unsigned int, int,
228 htab_t));
229 static void schedule_fixup_var_refs PARAMS ((struct function *, rtx, tree,
230 enum machine_mode,
231 htab_t));
232 static void fixup_var_refs PARAMS ((rtx, enum machine_mode, int, rtx,
233 htab_t));
234 static struct fixup_replacement
235 *find_fixup_replacement PARAMS ((struct fixup_replacement **, rtx));
236 static void fixup_var_refs_insns PARAMS ((rtx, rtx, enum machine_mode,
237 int, int, rtx));
238 static void fixup_var_refs_insns_with_hash
239 PARAMS ((htab_t, rtx,
240 enum machine_mode, int, rtx));
241 static void fixup_var_refs_insn PARAMS ((rtx, rtx, enum machine_mode,
242 int, int, rtx));
243 static void fixup_var_refs_1 PARAMS ((rtx, enum machine_mode, rtx *, rtx,
244 struct fixup_replacement **, rtx));
245 static rtx fixup_memory_subreg PARAMS ((rtx, rtx, enum machine_mode, int));
246 static rtx walk_fixup_memory_subreg PARAMS ((rtx, rtx, enum machine_mode,
247 int));
248 static rtx fixup_stack_1 PARAMS ((rtx, rtx));
249 static void optimize_bit_field PARAMS ((rtx, rtx, rtx *));
250 static void instantiate_decls PARAMS ((tree, int));
251 static void instantiate_decls_1 PARAMS ((tree, int));
252 static void instantiate_decl PARAMS ((rtx, HOST_WIDE_INT, int));
253 static rtx instantiate_new_reg PARAMS ((rtx, HOST_WIDE_INT *));
254 static int instantiate_virtual_regs_1 PARAMS ((rtx *, rtx, int));
255 static void delete_handlers PARAMS ((void));
256 static void pad_to_arg_alignment PARAMS ((struct args_size *, int,
257 struct args_size *));
258 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) = TYPE_READONLY (type);
802 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
803 MEM_SET_IN_STRUCT_P (slot, AGGREGATE_TYPE_P (type));
806 return slot;
809 /* Allocate a temporary stack slot and record it for possible later
810 reuse. First three arguments are same as in preceding function. */
813 assign_stack_temp (mode, size, keep)
814 enum machine_mode mode;
815 HOST_WIDE_INT size;
816 int keep;
818 return assign_stack_temp_for_type (mode, size, keep, NULL_TREE);
821 /* Assign a temporary.
822 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
823 and so that should be used in error messages. In either case, we
824 allocate of the given type.
825 KEEP is as for assign_stack_temp.
826 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
827 it is 0 if a register is OK.
828 DONT_PROMOTE is 1 if we should not promote values in register
829 to wider modes. */
832 assign_temp (type_or_decl, keep, memory_required, dont_promote)
833 tree type_or_decl;
834 int keep;
835 int memory_required;
836 int dont_promote ATTRIBUTE_UNUSED;
838 tree type, decl;
839 enum machine_mode mode;
840 #ifndef PROMOTE_FOR_CALL_ONLY
841 int unsignedp;
842 #endif
844 if (DECL_P (type_or_decl))
845 decl = type_or_decl, type = TREE_TYPE (decl);
846 else
847 decl = NULL, type = type_or_decl;
849 mode = TYPE_MODE (type);
850 #ifndef PROMOTE_FOR_CALL_ONLY
851 unsignedp = TREE_UNSIGNED (type);
852 #endif
854 if (mode == BLKmode || memory_required)
856 HOST_WIDE_INT size = int_size_in_bytes (type);
857 rtx tmp;
859 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
860 problems with allocating the stack space. */
861 if (size == 0)
862 size = 1;
864 /* Unfortunately, we don't yet know how to allocate variable-sized
865 temporaries. However, sometimes we have a fixed upper limit on
866 the size (which is stored in TYPE_ARRAY_MAX_SIZE) and can use that
867 instead. This is the case for Chill variable-sized strings. */
868 if (size == -1 && TREE_CODE (type) == ARRAY_TYPE
869 && TYPE_ARRAY_MAX_SIZE (type) != NULL_TREE
870 && host_integerp (TYPE_ARRAY_MAX_SIZE (type), 1))
871 size = tree_low_cst (TYPE_ARRAY_MAX_SIZE (type), 1);
873 /* The size of the temporary may be too large to fit into an integer. */
874 /* ??? Not sure this should happen except for user silliness, so limit
875 this to things that aren't compiler-generated temporaries. The
876 rest of the time we'll abort in assign_stack_temp_for_type. */
877 if (decl && size == -1
878 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
880 error_with_decl (decl, "size of variable `%s' is too large");
881 size = 1;
884 tmp = assign_stack_temp_for_type (mode, size, keep, type);
885 return tmp;
888 #ifndef PROMOTE_FOR_CALL_ONLY
889 if (! dont_promote)
890 mode = promote_mode (type, mode, &unsignedp, 0);
891 #endif
893 return gen_reg_rtx (mode);
896 /* Combine temporary stack slots which are adjacent on the stack.
898 This allows for better use of already allocated stack space. This is only
899 done for BLKmode slots because we can be sure that we won't have alignment
900 problems in this case. */
902 void
903 combine_temp_slots ()
905 struct temp_slot *p, *q;
906 struct temp_slot *prev_p, *prev_q;
907 int num_slots;
909 /* We can't combine slots, because the information about which slot
910 is in which alias set will be lost. */
911 if (flag_strict_aliasing)
912 return;
914 /* If there are a lot of temp slots, don't do anything unless
915 high levels of optimization. */
916 if (! flag_expensive_optimizations)
917 for (p = temp_slots, num_slots = 0; p; p = p->next, num_slots++)
918 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
919 return;
921 for (p = temp_slots, prev_p = 0; p; p = prev_p ? prev_p->next : temp_slots)
923 int delete_p = 0;
925 if (! p->in_use && GET_MODE (p->slot) == BLKmode)
926 for (q = p->next, prev_q = p; q; q = prev_q->next)
928 int delete_q = 0;
929 if (! q->in_use && GET_MODE (q->slot) == BLKmode)
931 if (p->base_offset + p->full_size == q->base_offset)
933 /* Q comes after P; combine Q into P. */
934 p->size += q->size;
935 p->full_size += q->full_size;
936 delete_q = 1;
938 else if (q->base_offset + q->full_size == p->base_offset)
940 /* P comes after Q; combine P into Q. */
941 q->size += p->size;
942 q->full_size += p->full_size;
943 delete_p = 1;
944 break;
947 /* Either delete Q or advance past it. */
948 if (delete_q)
949 prev_q->next = q->next;
950 else
951 prev_q = q;
953 /* Either delete P or advance past it. */
954 if (delete_p)
956 if (prev_p)
957 prev_p->next = p->next;
958 else
959 temp_slots = p->next;
961 else
962 prev_p = p;
966 /* Find the temp slot corresponding to the object at address X. */
968 static struct temp_slot *
969 find_temp_slot_from_address (x)
970 rtx x;
972 struct temp_slot *p;
973 rtx next;
975 for (p = temp_slots; p; p = p->next)
977 if (! p->in_use)
978 continue;
980 else if (XEXP (p->slot, 0) == x
981 || p->address == x
982 || (GET_CODE (x) == PLUS
983 && XEXP (x, 0) == virtual_stack_vars_rtx
984 && GET_CODE (XEXP (x, 1)) == CONST_INT
985 && INTVAL (XEXP (x, 1)) >= p->base_offset
986 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size))
987 return p;
989 else if (p->address != 0 && GET_CODE (p->address) == EXPR_LIST)
990 for (next = p->address; next; next = XEXP (next, 1))
991 if (XEXP (next, 0) == x)
992 return p;
995 /* If we have a sum involving a register, see if it points to a temp
996 slot. */
997 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == REG
998 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
999 return p;
1000 else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG
1001 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
1002 return p;
1004 return 0;
1007 /* Indicate that NEW is an alternate way of referring to the temp slot
1008 that previously was known by OLD. */
1010 void
1011 update_temp_slot_address (old, new)
1012 rtx old, new;
1014 struct temp_slot *p;
1016 if (rtx_equal_p (old, new))
1017 return;
1019 p = find_temp_slot_from_address (old);
1021 /* If we didn't find one, see if both OLD is a PLUS. If so, and NEW
1022 is a register, see if one operand of the PLUS is a temporary
1023 location. If so, NEW points into it. Otherwise, if both OLD and
1024 NEW are a PLUS and if there is a register in common between them.
1025 If so, try a recursive call on those values. */
1026 if (p == 0)
1028 if (GET_CODE (old) != PLUS)
1029 return;
1031 if (GET_CODE (new) == REG)
1033 update_temp_slot_address (XEXP (old, 0), new);
1034 update_temp_slot_address (XEXP (old, 1), new);
1035 return;
1037 else if (GET_CODE (new) != PLUS)
1038 return;
1040 if (rtx_equal_p (XEXP (old, 0), XEXP (new, 0)))
1041 update_temp_slot_address (XEXP (old, 1), XEXP (new, 1));
1042 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 0)))
1043 update_temp_slot_address (XEXP (old, 0), XEXP (new, 1));
1044 else if (rtx_equal_p (XEXP (old, 0), XEXP (new, 1)))
1045 update_temp_slot_address (XEXP (old, 1), XEXP (new, 0));
1046 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 1)))
1047 update_temp_slot_address (XEXP (old, 0), XEXP (new, 0));
1049 return;
1052 /* Otherwise add an alias for the temp's address. */
1053 else if (p->address == 0)
1054 p->address = new;
1055 else
1057 if (GET_CODE (p->address) != EXPR_LIST)
1058 p->address = gen_rtx_EXPR_LIST (VOIDmode, p->address, NULL_RTX);
1060 p->address = gen_rtx_EXPR_LIST (VOIDmode, new, p->address);
1064 /* If X could be a reference to a temporary slot, mark the fact that its
1065 address was taken. */
1067 void
1068 mark_temp_addr_taken (x)
1069 rtx x;
1071 struct temp_slot *p;
1073 if (x == 0)
1074 return;
1076 /* If X is not in memory or is at a constant address, it cannot be in
1077 a temporary slot. */
1078 if (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1079 return;
1081 p = find_temp_slot_from_address (XEXP (x, 0));
1082 if (p != 0)
1083 p->addr_taken = 1;
1086 /* If X could be a reference to a temporary slot, mark that slot as
1087 belonging to the to one level higher than the current level. If X
1088 matched one of our slots, just mark that one. Otherwise, we can't
1089 easily predict which it is, so upgrade all of them. Kept slots
1090 need not be touched.
1092 This is called when an ({...}) construct occurs and a statement
1093 returns a value in memory. */
1095 void
1096 preserve_temp_slots (x)
1097 rtx x;
1099 struct temp_slot *p = 0;
1101 /* If there is no result, we still might have some objects whose address
1102 were taken, so we need to make sure they stay around. */
1103 if (x == 0)
1105 for (p = temp_slots; p; p = p->next)
1106 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1107 p->level--;
1109 return;
1112 /* If X is a register that is being used as a pointer, see if we have
1113 a temporary slot we know it points to. To be consistent with
1114 the code below, we really should preserve all non-kept slots
1115 if we can't find a match, but that seems to be much too costly. */
1116 if (GET_CODE (x) == REG && REG_POINTER (x))
1117 p = find_temp_slot_from_address (x);
1119 /* If X is not in memory or is at a constant address, it cannot be in
1120 a temporary slot, but it can contain something whose address was
1121 taken. */
1122 if (p == 0 && (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0))))
1124 for (p = temp_slots; p; p = p->next)
1125 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1126 p->level--;
1128 return;
1131 /* First see if we can find a match. */
1132 if (p == 0)
1133 p = find_temp_slot_from_address (XEXP (x, 0));
1135 if (p != 0)
1137 /* Move everything at our level whose address was taken to our new
1138 level in case we used its address. */
1139 struct temp_slot *q;
1141 if (p->level == temp_slot_level)
1143 for (q = temp_slots; q; q = q->next)
1144 if (q != p && q->addr_taken && q->level == p->level)
1145 q->level--;
1147 p->level--;
1148 p->addr_taken = 0;
1150 return;
1153 /* Otherwise, preserve all non-kept slots at this level. */
1154 for (p = temp_slots; p; p = p->next)
1155 if (p->in_use && p->level == temp_slot_level && ! p->keep)
1156 p->level--;
1159 /* X is the result of an RTL_EXPR. If it is a temporary slot associated
1160 with that RTL_EXPR, promote it into a temporary slot at the present
1161 level so it will not be freed when we free slots made in the
1162 RTL_EXPR. */
1164 void
1165 preserve_rtl_expr_result (x)
1166 rtx x;
1168 struct temp_slot *p;
1170 /* If X is not in memory or is at a constant address, it cannot be in
1171 a temporary slot. */
1172 if (x == 0 || GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1173 return;
1175 /* If we can find a match, move it to our level unless it is already at
1176 an upper level. */
1177 p = find_temp_slot_from_address (XEXP (x, 0));
1178 if (p != 0)
1180 p->level = MIN (p->level, temp_slot_level);
1181 p->rtl_expr = 0;
1184 return;
1187 /* Free all temporaries used so far. This is normally called at the end
1188 of generating code for a statement. Don't free any temporaries
1189 currently in use for an RTL_EXPR that hasn't yet been emitted.
1190 We could eventually do better than this since it can be reused while
1191 generating the same RTL_EXPR, but this is complex and probably not
1192 worthwhile. */
1194 void
1195 free_temp_slots ()
1197 struct temp_slot *p;
1199 for (p = temp_slots; p; p = p->next)
1200 if (p->in_use && p->level == temp_slot_level && ! p->keep
1201 && p->rtl_expr == 0)
1202 p->in_use = 0;
1204 combine_temp_slots ();
1207 /* Free all temporary slots used in T, an RTL_EXPR node. */
1209 void
1210 free_temps_for_rtl_expr (t)
1211 tree t;
1213 struct temp_slot *p;
1215 for (p = temp_slots; p; p = p->next)
1216 if (p->rtl_expr == t)
1218 /* If this slot is below the current TEMP_SLOT_LEVEL, then it
1219 needs to be preserved. This can happen if a temporary in
1220 the RTL_EXPR was addressed; preserve_temp_slots will move
1221 the temporary into a higher level. */
1222 if (temp_slot_level <= p->level)
1223 p->in_use = 0;
1224 else
1225 p->rtl_expr = NULL_TREE;
1228 combine_temp_slots ();
1231 /* Mark all temporaries ever allocated in this function as not suitable
1232 for reuse until the current level is exited. */
1234 void
1235 mark_all_temps_used ()
1237 struct temp_slot *p;
1239 for (p = temp_slots; p; p = p->next)
1241 p->in_use = p->keep = 1;
1242 p->level = MIN (p->level, temp_slot_level);
1246 /* Push deeper into the nesting level for stack temporaries. */
1248 void
1249 push_temp_slots ()
1251 temp_slot_level++;
1254 /* Likewise, but save the new level as the place to allocate variables
1255 for blocks. */
1257 #if 0
1258 void
1259 push_temp_slots_for_block ()
1261 push_temp_slots ();
1263 var_temp_slot_level = temp_slot_level;
1266 /* Likewise, but save the new level as the place to allocate temporaries
1267 for TARGET_EXPRs. */
1269 void
1270 push_temp_slots_for_target ()
1272 push_temp_slots ();
1274 target_temp_slot_level = temp_slot_level;
1277 /* Set and get the value of target_temp_slot_level. The only
1278 permitted use of these functions is to save and restore this value. */
1281 get_target_temp_slot_level ()
1283 return target_temp_slot_level;
1286 void
1287 set_target_temp_slot_level (level)
1288 int level;
1290 target_temp_slot_level = level;
1292 #endif
1294 /* Pop a temporary nesting level. All slots in use in the current level
1295 are freed. */
1297 void
1298 pop_temp_slots ()
1300 struct temp_slot *p;
1302 for (p = temp_slots; p; p = p->next)
1303 if (p->in_use && p->level == temp_slot_level && p->rtl_expr == 0)
1304 p->in_use = 0;
1306 combine_temp_slots ();
1308 temp_slot_level--;
1311 /* Initialize temporary slots. */
1313 void
1314 init_temp_slots ()
1316 /* We have not allocated any temporaries yet. */
1317 temp_slots = 0;
1318 temp_slot_level = 0;
1319 var_temp_slot_level = 0;
1320 target_temp_slot_level = 0;
1323 /* Retroactively move an auto variable from a register to a stack slot.
1324 This is done when an address-reference to the variable is seen. */
1326 void
1327 put_var_into_stack (decl)
1328 tree decl;
1330 rtx reg;
1331 enum machine_mode promoted_mode, decl_mode;
1332 struct function *function = 0;
1333 tree context;
1334 int can_use_addressof;
1335 int volatilep = TREE_CODE (decl) != SAVE_EXPR && TREE_THIS_VOLATILE (decl);
1336 int usedp = (TREE_USED (decl)
1337 || (TREE_CODE (decl) != SAVE_EXPR && DECL_INITIAL (decl) != 0));
1339 context = decl_function_context (decl);
1341 /* Get the current rtl used for this object and its original mode. */
1342 reg = (TREE_CODE (decl) == SAVE_EXPR
1343 ? SAVE_EXPR_RTL (decl)
1344 : DECL_RTL_IF_SET (decl));
1346 /* No need to do anything if decl has no rtx yet
1347 since in that case caller is setting TREE_ADDRESSABLE
1348 and a stack slot will be assigned when the rtl is made. */
1349 if (reg == 0)
1350 return;
1352 /* Get the declared mode for this object. */
1353 decl_mode = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
1354 : DECL_MODE (decl));
1355 /* Get the mode it's actually stored in. */
1356 promoted_mode = GET_MODE (reg);
1358 /* If this variable comes from an outer function, find that
1359 function's saved context. Don't use find_function_data here,
1360 because it might not be in any active function.
1361 FIXME: Is that really supposed to happen?
1362 It does in ObjC at least. */
1363 if (context != current_function_decl && context != inline_function_decl)
1364 for (function = outer_function_chain; function; function = function->outer)
1365 if (function->decl == context)
1366 break;
1368 /* If this is a variable-size object with a pseudo to address it,
1369 put that pseudo into the stack, if the var is nonlocal. */
1370 if (TREE_CODE (decl) != SAVE_EXPR && DECL_NONLOCAL (decl)
1371 && GET_CODE (reg) == MEM
1372 && GET_CODE (XEXP (reg, 0)) == REG
1373 && REGNO (XEXP (reg, 0)) > LAST_VIRTUAL_REGISTER)
1375 reg = XEXP (reg, 0);
1376 decl_mode = promoted_mode = GET_MODE (reg);
1379 can_use_addressof
1380 = (function == 0
1381 && optimize > 0
1382 /* FIXME make it work for promoted modes too */
1383 && decl_mode == promoted_mode
1384 #ifdef NON_SAVING_SETJMP
1385 && ! (NON_SAVING_SETJMP && current_function_calls_setjmp)
1386 #endif
1389 /* If we can't use ADDRESSOF, make sure we see through one we already
1390 generated. */
1391 if (! can_use_addressof && GET_CODE (reg) == MEM
1392 && GET_CODE (XEXP (reg, 0)) == ADDRESSOF)
1393 reg = XEXP (XEXP (reg, 0), 0);
1395 /* Now we should have a value that resides in one or more pseudo regs. */
1397 if (GET_CODE (reg) == REG)
1399 /* If this variable lives in the current function and we don't need
1400 to put things in the stack for the sake of setjmp, try to keep it
1401 in a register until we know we actually need the address. */
1402 if (can_use_addressof)
1403 gen_mem_addressof (reg, decl);
1404 else
1405 put_reg_into_stack (function, reg, TREE_TYPE (decl), promoted_mode,
1406 decl_mode, volatilep, 0, usedp, 0);
1408 else if (GET_CODE (reg) == CONCAT)
1410 /* A CONCAT contains two pseudos; put them both in the stack.
1411 We do it so they end up consecutive.
1412 We fixup references to the parts only after we fixup references
1413 to the whole CONCAT, lest we do double fixups for the latter
1414 references. */
1415 enum machine_mode part_mode = GET_MODE (XEXP (reg, 0));
1416 tree part_type = (*lang_hooks.types.type_for_mode) (part_mode, 0);
1417 rtx lopart = XEXP (reg, 0);
1418 rtx hipart = XEXP (reg, 1);
1419 #ifdef FRAME_GROWS_DOWNWARD
1420 /* Since part 0 should have a lower address, do it second. */
1421 put_reg_into_stack (function, hipart, part_type, part_mode,
1422 part_mode, volatilep, 0, 0, 0);
1423 put_reg_into_stack (function, lopart, part_type, part_mode,
1424 part_mode, volatilep, 0, 0, 0);
1425 #else
1426 put_reg_into_stack (function, lopart, part_type, part_mode,
1427 part_mode, volatilep, 0, 0, 0);
1428 put_reg_into_stack (function, hipart, part_type, part_mode,
1429 part_mode, volatilep, 0, 0, 0);
1430 #endif
1432 /* Change the CONCAT into a combined MEM for both parts. */
1433 PUT_CODE (reg, MEM);
1434 MEM_ATTRS (reg) = 0;
1436 /* set_mem_attributes uses DECL_RTL to avoid re-generating of
1437 already computed alias sets. Here we want to re-generate. */
1438 if (DECL_P (decl))
1439 SET_DECL_RTL (decl, NULL);
1440 set_mem_attributes (reg, decl, 1);
1441 if (DECL_P (decl))
1442 SET_DECL_RTL (decl, reg);
1444 /* The two parts are in memory order already.
1445 Use the lower parts address as ours. */
1446 XEXP (reg, 0) = XEXP (XEXP (reg, 0), 0);
1447 /* Prevent sharing of rtl that might lose. */
1448 if (GET_CODE (XEXP (reg, 0)) == PLUS)
1449 XEXP (reg, 0) = copy_rtx (XEXP (reg, 0));
1450 if (usedp)
1452 schedule_fixup_var_refs (function, reg, TREE_TYPE (decl),
1453 promoted_mode, 0);
1454 schedule_fixup_var_refs (function, lopart, part_type, part_mode, 0);
1455 schedule_fixup_var_refs (function, hipart, part_type, part_mode, 0);
1458 else
1459 return;
1462 /* Subroutine of put_var_into_stack. This puts a single pseudo reg REG
1463 into the stack frame of FUNCTION (0 means the current function).
1464 DECL_MODE is the machine mode of the user-level data type.
1465 PROMOTED_MODE is the machine mode of the register.
1466 VOLATILE_P is nonzero if this is for a "volatile" decl.
1467 USED_P is nonzero if this reg might have already been used in an insn. */
1469 static void
1470 put_reg_into_stack (function, reg, type, promoted_mode, decl_mode, volatile_p,
1471 original_regno, used_p, ht)
1472 struct function *function;
1473 rtx reg;
1474 tree type;
1475 enum machine_mode promoted_mode, decl_mode;
1476 int volatile_p;
1477 unsigned int original_regno;
1478 int used_p;
1479 htab_t ht;
1481 struct function *func = function ? function : cfun;
1482 rtx new = 0;
1483 unsigned int regno = original_regno;
1485 if (regno == 0)
1486 regno = REGNO (reg);
1488 if (regno < func->x_max_parm_reg)
1489 new = func->x_parm_reg_stack_loc[regno];
1491 if (new == 0)
1492 new = assign_stack_local_1 (decl_mode, GET_MODE_SIZE (decl_mode), 0, func);
1494 PUT_CODE (reg, MEM);
1495 PUT_MODE (reg, decl_mode);
1496 XEXP (reg, 0) = XEXP (new, 0);
1497 MEM_ATTRS (reg) = 0;
1498 /* `volatil' bit means one thing for MEMs, another entirely for REGs. */
1499 MEM_VOLATILE_P (reg) = volatile_p;
1501 /* If this is a memory ref that contains aggregate components,
1502 mark it as such for cse and loop optimize. If we are reusing a
1503 previously generated stack slot, then we need to copy the bit in
1504 case it was set for other reasons. For instance, it is set for
1505 __builtin_va_alist. */
1506 if (type)
1508 MEM_SET_IN_STRUCT_P (reg,
1509 AGGREGATE_TYPE_P (type) || MEM_IN_STRUCT_P (new));
1510 set_mem_alias_set (reg, get_alias_set (type));
1513 if (used_p)
1514 schedule_fixup_var_refs (function, reg, type, promoted_mode, ht);
1517 /* Make sure that all refs to the variable, previously made
1518 when it was a register, are fixed up to be valid again.
1519 See function above for meaning of arguments. */
1521 static void
1522 schedule_fixup_var_refs (function, reg, type, promoted_mode, ht)
1523 struct function *function;
1524 rtx reg;
1525 tree type;
1526 enum machine_mode promoted_mode;
1527 htab_t ht;
1529 int unsigned_p = type ? TREE_UNSIGNED (type) : 0;
1531 if (function != 0)
1533 struct var_refs_queue *temp;
1535 temp
1536 = (struct var_refs_queue *) ggc_alloc (sizeof (struct var_refs_queue));
1537 temp->modified = reg;
1538 temp->promoted_mode = promoted_mode;
1539 temp->unsignedp = unsigned_p;
1540 temp->next = function->fixup_var_refs_queue;
1541 function->fixup_var_refs_queue = temp;
1543 else
1544 /* Variable is local; fix it up now. */
1545 fixup_var_refs (reg, promoted_mode, unsigned_p, reg, ht);
1548 static void
1549 fixup_var_refs (var, promoted_mode, unsignedp, may_share, ht)
1550 rtx var;
1551 enum machine_mode promoted_mode;
1552 int unsignedp;
1553 htab_t ht;
1554 rtx may_share;
1556 tree pending;
1557 rtx first_insn = get_insns ();
1558 struct sequence_stack *stack = seq_stack;
1559 tree rtl_exps = rtl_expr_chain;
1561 /* If there's a hash table, it must record all uses of VAR. */
1562 if (ht)
1564 if (stack != 0)
1565 abort ();
1566 fixup_var_refs_insns_with_hash (ht, var, promoted_mode, unsignedp,
1567 may_share);
1568 return;
1571 fixup_var_refs_insns (first_insn, var, promoted_mode, unsignedp,
1572 stack == 0, may_share);
1574 /* Scan all pending sequences too. */
1575 for (; stack; stack = stack->next)
1577 push_to_full_sequence (stack->first, stack->last);
1578 fixup_var_refs_insns (stack->first, var, promoted_mode, unsignedp,
1579 stack->next != 0, may_share);
1580 /* Update remembered end of sequence
1581 in case we added an insn at the end. */
1582 stack->last = get_last_insn ();
1583 end_sequence ();
1586 /* Scan all waiting RTL_EXPRs too. */
1587 for (pending = rtl_exps; pending; pending = TREE_CHAIN (pending))
1589 rtx seq = RTL_EXPR_SEQUENCE (TREE_VALUE (pending));
1590 if (seq != const0_rtx && seq != 0)
1592 push_to_sequence (seq);
1593 fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0,
1594 may_share);
1595 end_sequence ();
1600 /* REPLACEMENTS is a pointer to a list of the struct fixup_replacement and X is
1601 some part of an insn. Return a struct fixup_replacement whose OLD
1602 value is equal to X. Allocate a new structure if no such entry exists. */
1604 static struct fixup_replacement *
1605 find_fixup_replacement (replacements, x)
1606 struct fixup_replacement **replacements;
1607 rtx x;
1609 struct fixup_replacement *p;
1611 /* See if we have already replaced this. */
1612 for (p = *replacements; p != 0 && ! rtx_equal_p (p->old, x); p = p->next)
1615 if (p == 0)
1617 p = (struct fixup_replacement *) xmalloc (sizeof (struct fixup_replacement));
1618 p->old = x;
1619 p->new = 0;
1620 p->next = *replacements;
1621 *replacements = p;
1624 return p;
1627 /* Scan the insn-chain starting with INSN for refs to VAR and fix them
1628 up. TOPLEVEL is nonzero if this chain is the main chain of insns
1629 for the current function. MAY_SHARE is either a MEM that is not
1630 to be unshared or a list of them. */
1632 static void
1633 fixup_var_refs_insns (insn, var, promoted_mode, unsignedp, toplevel, may_share)
1634 rtx insn;
1635 rtx var;
1636 enum machine_mode promoted_mode;
1637 int unsignedp;
1638 int toplevel;
1639 rtx may_share;
1641 while (insn)
1643 /* fixup_var_refs_insn might modify insn, so save its next
1644 pointer now. */
1645 rtx next = NEXT_INSN (insn);
1647 /* CALL_PLACEHOLDERs are special; we have to switch into each of
1648 the three sequences they (potentially) contain, and process
1649 them recursively. The CALL_INSN itself is not interesting. */
1651 if (GET_CODE (insn) == CALL_INSN
1652 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1654 int i;
1656 /* Look at the Normal call, sibling call and tail recursion
1657 sequences attached to the CALL_PLACEHOLDER. */
1658 for (i = 0; i < 3; i++)
1660 rtx seq = XEXP (PATTERN (insn), i);
1661 if (seq)
1663 push_to_sequence (seq);
1664 fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0,
1665 may_share);
1666 XEXP (PATTERN (insn), i) = get_insns ();
1667 end_sequence ();
1672 else if (INSN_P (insn))
1673 fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, toplevel,
1674 may_share);
1676 insn = next;
1680 /* Look up the insns which reference VAR in HT and fix them up. Other
1681 arguments are the same as fixup_var_refs_insns.
1683 N.B. No need for special processing of CALL_PLACEHOLDERs here,
1684 because the hash table will point straight to the interesting insn
1685 (inside the CALL_PLACEHOLDER). */
1687 static void
1688 fixup_var_refs_insns_with_hash (ht, var, promoted_mode, unsignedp, may_share)
1689 htab_t ht;
1690 rtx var;
1691 enum machine_mode promoted_mode;
1692 int unsignedp;
1693 rtx may_share;
1695 struct insns_for_mem_entry tmp;
1696 struct insns_for_mem_entry *ime;
1697 rtx insn_list;
1699 tmp.key = var;
1700 ime = (struct insns_for_mem_entry *) htab_find (ht, &tmp);
1701 for (insn_list = ime->insns; insn_list != 0; insn_list = XEXP (insn_list, 1))
1702 if (INSN_P (XEXP (insn_list, 0)))
1703 fixup_var_refs_insn (XEXP (insn_list, 0), var, promoted_mode,
1704 unsignedp, 1, may_share);
1708 /* Per-insn processing by fixup_var_refs_insns(_with_hash). INSN is
1709 the insn under examination, VAR is the variable to fix up
1710 references to, PROMOTED_MODE and UNSIGNEDP describe VAR, and
1711 TOPLEVEL is nonzero if this is the main insn chain for this
1712 function. */
1714 static void
1715 fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, toplevel, no_share)
1716 rtx insn;
1717 rtx var;
1718 enum machine_mode promoted_mode;
1719 int unsignedp;
1720 int toplevel;
1721 rtx no_share;
1723 rtx call_dest = 0;
1724 rtx set, prev, prev_set;
1725 rtx note;
1727 /* Remember the notes in case we delete the insn. */
1728 note = REG_NOTES (insn);
1730 /* If this is a CLOBBER of VAR, delete it.
1732 If it has a REG_LIBCALL note, delete the REG_LIBCALL
1733 and REG_RETVAL notes too. */
1734 if (GET_CODE (PATTERN (insn)) == CLOBBER
1735 && (XEXP (PATTERN (insn), 0) == var
1736 || (GET_CODE (XEXP (PATTERN (insn), 0)) == CONCAT
1737 && (XEXP (XEXP (PATTERN (insn), 0), 0) == var
1738 || XEXP (XEXP (PATTERN (insn), 0), 1) == var))))
1740 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0)
1741 /* The REG_LIBCALL note will go away since we are going to
1742 turn INSN into a NOTE, so just delete the
1743 corresponding REG_RETVAL note. */
1744 remove_note (XEXP (note, 0),
1745 find_reg_note (XEXP (note, 0), REG_RETVAL,
1746 NULL_RTX));
1748 delete_insn (insn);
1751 /* The insn to load VAR from a home in the arglist
1752 is now a no-op. When we see it, just delete it.
1753 Similarly if this is storing VAR from a register from which
1754 it was loaded in the previous insn. This will occur
1755 when an ADDRESSOF was made for an arglist slot. */
1756 else if (toplevel
1757 && (set = single_set (insn)) != 0
1758 && SET_DEST (set) == var
1759 /* If this represents the result of an insn group,
1760 don't delete the insn. */
1761 && find_reg_note (insn, REG_RETVAL, NULL_RTX) == 0
1762 && (rtx_equal_p (SET_SRC (set), var)
1763 || (GET_CODE (SET_SRC (set)) == REG
1764 && (prev = prev_nonnote_insn (insn)) != 0
1765 && (prev_set = single_set (prev)) != 0
1766 && SET_DEST (prev_set) == SET_SRC (set)
1767 && rtx_equal_p (SET_SRC (prev_set), var))))
1769 delete_insn (insn);
1771 else
1773 struct fixup_replacement *replacements = 0;
1774 rtx next_insn = NEXT_INSN (insn);
1776 if (SMALL_REGISTER_CLASSES)
1778 /* If the insn that copies the results of a CALL_INSN
1779 into a pseudo now references VAR, we have to use an
1780 intermediate pseudo since we want the life of the
1781 return value register to be only a single insn.
1783 If we don't use an intermediate pseudo, such things as
1784 address computations to make the address of VAR valid
1785 if it is not can be placed between the CALL_INSN and INSN.
1787 To make sure this doesn't happen, we record the destination
1788 of the CALL_INSN and see if the next insn uses both that
1789 and VAR. */
1791 if (call_dest != 0 && GET_CODE (insn) == INSN
1792 && reg_mentioned_p (var, PATTERN (insn))
1793 && reg_mentioned_p (call_dest, PATTERN (insn)))
1795 rtx temp = gen_reg_rtx (GET_MODE (call_dest));
1797 emit_insn_before (gen_move_insn (temp, call_dest), insn);
1799 PATTERN (insn) = replace_rtx (PATTERN (insn),
1800 call_dest, temp);
1803 if (GET_CODE (insn) == CALL_INSN
1804 && GET_CODE (PATTERN (insn)) == SET)
1805 call_dest = SET_DEST (PATTERN (insn));
1806 else if (GET_CODE (insn) == CALL_INSN
1807 && GET_CODE (PATTERN (insn)) == PARALLEL
1808 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1809 call_dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1810 else
1811 call_dest = 0;
1814 /* See if we have to do anything to INSN now that VAR is in
1815 memory. If it needs to be loaded into a pseudo, use a single
1816 pseudo for the entire insn in case there is a MATCH_DUP
1817 between two operands. We pass a pointer to the head of
1818 a list of struct fixup_replacements. If fixup_var_refs_1
1819 needs to allocate pseudos or replacement MEMs (for SUBREGs),
1820 it will record them in this list.
1822 If it allocated a pseudo for any replacement, we copy into
1823 it here. */
1825 fixup_var_refs_1 (var, promoted_mode, &PATTERN (insn), insn,
1826 &replacements, no_share);
1828 /* If this is last_parm_insn, and any instructions were output
1829 after it to fix it up, then we must set last_parm_insn to
1830 the last such instruction emitted. */
1831 if (insn == last_parm_insn)
1832 last_parm_insn = PREV_INSN (next_insn);
1834 while (replacements)
1836 struct fixup_replacement *next;
1838 if (GET_CODE (replacements->new) == REG)
1840 rtx insert_before;
1841 rtx seq;
1843 /* OLD might be a (subreg (mem)). */
1844 if (GET_CODE (replacements->old) == SUBREG)
1845 replacements->old
1846 = fixup_memory_subreg (replacements->old, insn,
1847 promoted_mode, 0);
1848 else
1849 replacements->old
1850 = fixup_stack_1 (replacements->old, insn);
1852 insert_before = insn;
1854 /* If we are changing the mode, do a conversion.
1855 This might be wasteful, but combine.c will
1856 eliminate much of the waste. */
1858 if (GET_MODE (replacements->new)
1859 != GET_MODE (replacements->old))
1861 start_sequence ();
1862 convert_move (replacements->new,
1863 replacements->old, unsignedp);
1864 seq = get_insns ();
1865 end_sequence ();
1867 else
1868 seq = gen_move_insn (replacements->new,
1869 replacements->old);
1871 emit_insn_before (seq, insert_before);
1874 next = replacements->next;
1875 free (replacements);
1876 replacements = next;
1880 /* Also fix up any invalid exprs in the REG_NOTES of this insn.
1881 But don't touch other insns referred to by reg-notes;
1882 we will get them elsewhere. */
1883 while (note)
1885 if (GET_CODE (note) != INSN_LIST)
1886 XEXP (note, 0)
1887 = walk_fixup_memory_subreg (XEXP (note, 0), insn,
1888 promoted_mode, 1);
1889 note = XEXP (note, 1);
1893 /* VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
1894 See if the rtx expression at *LOC in INSN needs to be changed.
1896 REPLACEMENTS is a pointer to a list head that starts out zero, but may
1897 contain a list of original rtx's and replacements. If we find that we need
1898 to modify this insn by replacing a memory reference with a pseudo or by
1899 making a new MEM to implement a SUBREG, we consult that list to see if
1900 we have already chosen a replacement. If none has already been allocated,
1901 we allocate it and update the list. fixup_var_refs_insn will copy VAR
1902 or the SUBREG, as appropriate, to the pseudo. */
1904 static void
1905 fixup_var_refs_1 (var, promoted_mode, loc, insn, replacements, no_share)
1906 rtx var;
1907 enum machine_mode promoted_mode;
1908 rtx *loc;
1909 rtx insn;
1910 struct fixup_replacement **replacements;
1911 rtx no_share;
1913 int i;
1914 rtx x = *loc;
1915 RTX_CODE code = GET_CODE (x);
1916 const char *fmt;
1917 rtx tem, tem1;
1918 struct fixup_replacement *replacement;
1920 switch (code)
1922 case ADDRESSOF:
1923 if (XEXP (x, 0) == var)
1925 /* Prevent sharing of rtl that might lose. */
1926 rtx sub = copy_rtx (XEXP (var, 0));
1928 if (! validate_change (insn, loc, sub, 0))
1930 rtx y = gen_reg_rtx (GET_MODE (sub));
1931 rtx seq, new_insn;
1933 /* We should be able to replace with a register or all is lost.
1934 Note that we can't use validate_change to verify this, since
1935 we're not caring for replacing all dups simultaneously. */
1936 if (! validate_replace_rtx (*loc, y, insn))
1937 abort ();
1939 /* Careful! First try to recognize a direct move of the
1940 value, mimicking how things are done in gen_reload wrt
1941 PLUS. Consider what happens when insn is a conditional
1942 move instruction and addsi3 clobbers flags. */
1944 start_sequence ();
1945 new_insn = emit_insn (gen_rtx_SET (VOIDmode, y, sub));
1946 seq = get_insns ();
1947 end_sequence ();
1949 if (recog_memoized (new_insn) < 0)
1951 /* That failed. Fall back on force_operand and hope. */
1953 start_sequence ();
1954 sub = force_operand (sub, y);
1955 if (sub != y)
1956 emit_insn (gen_move_insn (y, sub));
1957 seq = get_insns ();
1958 end_sequence ();
1961 #ifdef HAVE_cc0
1962 /* Don't separate setter from user. */
1963 if (PREV_INSN (insn) && sets_cc0_p (PREV_INSN (insn)))
1964 insn = PREV_INSN (insn);
1965 #endif
1967 emit_insn_before (seq, insn);
1970 return;
1972 case MEM:
1973 if (var == x)
1975 /* If we already have a replacement, use it. Otherwise,
1976 try to fix up this address in case it is invalid. */
1978 replacement = find_fixup_replacement (replacements, var);
1979 if (replacement->new)
1981 *loc = replacement->new;
1982 return;
1985 *loc = replacement->new = x = fixup_stack_1 (x, insn);
1987 /* Unless we are forcing memory to register or we changed the mode,
1988 we can leave things the way they are if the insn is valid. */
1990 INSN_CODE (insn) = -1;
1991 if (! flag_force_mem && GET_MODE (x) == promoted_mode
1992 && recog_memoized (insn) >= 0)
1993 return;
1995 *loc = replacement->new = gen_reg_rtx (promoted_mode);
1996 return;
1999 /* If X contains VAR, we need to unshare it here so that we update
2000 each occurrence separately. But all identical MEMs in one insn
2001 must be replaced with the same rtx because of the possibility of
2002 MATCH_DUPs. */
2004 if (reg_mentioned_p (var, x))
2006 replacement = find_fixup_replacement (replacements, x);
2007 if (replacement->new == 0)
2008 replacement->new = copy_most_rtx (x, no_share);
2010 *loc = x = replacement->new;
2011 code = GET_CODE (x);
2013 break;
2015 case REG:
2016 case CC0:
2017 case PC:
2018 case CONST_INT:
2019 case CONST:
2020 case SYMBOL_REF:
2021 case LABEL_REF:
2022 case CONST_DOUBLE:
2023 case CONST_VECTOR:
2024 return;
2026 case SIGN_EXTRACT:
2027 case ZERO_EXTRACT:
2028 /* Note that in some cases those types of expressions are altered
2029 by optimize_bit_field, and do not survive to get here. */
2030 if (XEXP (x, 0) == var
2031 || (GET_CODE (XEXP (x, 0)) == SUBREG
2032 && SUBREG_REG (XEXP (x, 0)) == var))
2034 /* Get TEM as a valid MEM in the mode presently in the insn.
2036 We don't worry about the possibility of MATCH_DUP here; it
2037 is highly unlikely and would be tricky to handle. */
2039 tem = XEXP (x, 0);
2040 if (GET_CODE (tem) == SUBREG)
2042 if (GET_MODE_BITSIZE (GET_MODE (tem))
2043 > GET_MODE_BITSIZE (GET_MODE (var)))
2045 replacement = find_fixup_replacement (replacements, var);
2046 if (replacement->new == 0)
2047 replacement->new = gen_reg_rtx (GET_MODE (var));
2048 SUBREG_REG (tem) = replacement->new;
2050 /* The following code works only if we have a MEM, so we
2051 need to handle the subreg here. We directly substitute
2052 it assuming that a subreg must be OK here. We already
2053 scheduled a replacement to copy the mem into the
2054 subreg. */
2055 XEXP (x, 0) = tem;
2056 return;
2058 else
2059 tem = fixup_memory_subreg (tem, insn, promoted_mode, 0);
2061 else
2062 tem = fixup_stack_1 (tem, insn);
2064 /* Unless we want to load from memory, get TEM into the proper mode
2065 for an extract from memory. This can only be done if the
2066 extract is at a constant position and length. */
2068 if (! flag_force_mem && GET_CODE (XEXP (x, 1)) == CONST_INT
2069 && GET_CODE (XEXP (x, 2)) == CONST_INT
2070 && ! mode_dependent_address_p (XEXP (tem, 0))
2071 && ! MEM_VOLATILE_P (tem))
2073 enum machine_mode wanted_mode = VOIDmode;
2074 enum machine_mode is_mode = GET_MODE (tem);
2075 HOST_WIDE_INT pos = INTVAL (XEXP (x, 2));
2077 if (GET_CODE (x) == ZERO_EXTRACT)
2079 enum machine_mode new_mode
2080 = mode_for_extraction (EP_extzv, 1);
2081 if (new_mode != MAX_MACHINE_MODE)
2082 wanted_mode = new_mode;
2084 else if (GET_CODE (x) == SIGN_EXTRACT)
2086 enum machine_mode new_mode
2087 = mode_for_extraction (EP_extv, 1);
2088 if (new_mode != MAX_MACHINE_MODE)
2089 wanted_mode = new_mode;
2092 /* If we have a narrower mode, we can do something. */
2093 if (wanted_mode != VOIDmode
2094 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2096 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2097 rtx old_pos = XEXP (x, 2);
2098 rtx newmem;
2100 /* If the bytes and bits are counted differently, we
2101 must adjust the offset. */
2102 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2103 offset = (GET_MODE_SIZE (is_mode)
2104 - GET_MODE_SIZE (wanted_mode) - offset);
2106 pos %= GET_MODE_BITSIZE (wanted_mode);
2108 newmem = adjust_address_nv (tem, wanted_mode, offset);
2110 /* Make the change and see if the insn remains valid. */
2111 INSN_CODE (insn) = -1;
2112 XEXP (x, 0) = newmem;
2113 XEXP (x, 2) = GEN_INT (pos);
2115 if (recog_memoized (insn) >= 0)
2116 return;
2118 /* Otherwise, restore old position. XEXP (x, 0) will be
2119 restored later. */
2120 XEXP (x, 2) = old_pos;
2124 /* If we get here, the bitfield extract insn can't accept a memory
2125 reference. Copy the input into a register. */
2127 tem1 = gen_reg_rtx (GET_MODE (tem));
2128 emit_insn_before (gen_move_insn (tem1, tem), insn);
2129 XEXP (x, 0) = tem1;
2130 return;
2132 break;
2134 case SUBREG:
2135 if (SUBREG_REG (x) == var)
2137 /* If this is a special SUBREG made because VAR was promoted
2138 from a wider mode, replace it with VAR and call ourself
2139 recursively, this time saying that the object previously
2140 had its current mode (by virtue of the SUBREG). */
2142 if (SUBREG_PROMOTED_VAR_P (x))
2144 *loc = var;
2145 fixup_var_refs_1 (var, GET_MODE (var), loc, insn, replacements,
2146 no_share);
2147 return;
2150 /* If this SUBREG makes VAR wider, it has become a paradoxical
2151 SUBREG with VAR in memory, but these aren't allowed at this
2152 stage of the compilation. So load VAR into a pseudo and take
2153 a SUBREG of that pseudo. */
2154 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (var)))
2156 replacement = find_fixup_replacement (replacements, var);
2157 if (replacement->new == 0)
2158 replacement->new = gen_reg_rtx (promoted_mode);
2159 SUBREG_REG (x) = replacement->new;
2160 return;
2163 /* See if we have already found a replacement for this SUBREG.
2164 If so, use it. Otherwise, make a MEM and see if the insn
2165 is recognized. If not, or if we should force MEM into a register,
2166 make a pseudo for this SUBREG. */
2167 replacement = find_fixup_replacement (replacements, x);
2168 if (replacement->new)
2170 *loc = replacement->new;
2171 return;
2174 replacement->new = *loc = fixup_memory_subreg (x, insn,
2175 promoted_mode, 0);
2177 INSN_CODE (insn) = -1;
2178 if (! flag_force_mem && recog_memoized (insn) >= 0)
2179 return;
2181 *loc = replacement->new = gen_reg_rtx (GET_MODE (x));
2182 return;
2184 break;
2186 case SET:
2187 /* First do special simplification of bit-field references. */
2188 if (GET_CODE (SET_DEST (x)) == SIGN_EXTRACT
2189 || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2190 optimize_bit_field (x, insn, 0);
2191 if (GET_CODE (SET_SRC (x)) == SIGN_EXTRACT
2192 || GET_CODE (SET_SRC (x)) == ZERO_EXTRACT)
2193 optimize_bit_field (x, insn, 0);
2195 /* For a paradoxical SUBREG inside a ZERO_EXTRACT, load the object
2196 into a register and then store it back out. */
2197 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2198 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG
2199 && SUBREG_REG (XEXP (SET_DEST (x), 0)) == var
2200 && (GET_MODE_SIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2201 > GET_MODE_SIZE (GET_MODE (var))))
2203 replacement = find_fixup_replacement (replacements, var);
2204 if (replacement->new == 0)
2205 replacement->new = gen_reg_rtx (GET_MODE (var));
2207 SUBREG_REG (XEXP (SET_DEST (x), 0)) = replacement->new;
2208 emit_insn_after (gen_move_insn (var, replacement->new), insn);
2211 /* If SET_DEST is now a paradoxical SUBREG, put the result of this
2212 insn into a pseudo and store the low part of the pseudo into VAR. */
2213 if (GET_CODE (SET_DEST (x)) == SUBREG
2214 && SUBREG_REG (SET_DEST (x)) == var
2215 && (GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
2216 > GET_MODE_SIZE (GET_MODE (var))))
2218 SET_DEST (x) = tem = gen_reg_rtx (GET_MODE (SET_DEST (x)));
2219 emit_insn_after (gen_move_insn (var, gen_lowpart (GET_MODE (var),
2220 tem)),
2221 insn);
2222 break;
2226 rtx dest = SET_DEST (x);
2227 rtx src = SET_SRC (x);
2228 rtx outerdest = dest;
2230 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2231 || GET_CODE (dest) == SIGN_EXTRACT
2232 || GET_CODE (dest) == ZERO_EXTRACT)
2233 dest = XEXP (dest, 0);
2235 if (GET_CODE (src) == SUBREG)
2236 src = SUBREG_REG (src);
2238 /* If VAR does not appear at the top level of the SET
2239 just scan the lower levels of the tree. */
2241 if (src != var && dest != var)
2242 break;
2244 /* We will need to rerecognize this insn. */
2245 INSN_CODE (insn) = -1;
2247 if (GET_CODE (outerdest) == ZERO_EXTRACT && dest == var
2248 && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
2250 /* Since this case will return, ensure we fixup all the
2251 operands here. */
2252 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 1),
2253 insn, replacements, no_share);
2254 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 2),
2255 insn, replacements, no_share);
2256 fixup_var_refs_1 (var, promoted_mode, &SET_SRC (x),
2257 insn, replacements, no_share);
2259 tem = XEXP (outerdest, 0);
2261 /* Clean up (SUBREG:SI (MEM:mode ...) 0)
2262 that may appear inside a ZERO_EXTRACT.
2263 This was legitimate when the MEM was a REG. */
2264 if (GET_CODE (tem) == SUBREG
2265 && SUBREG_REG (tem) == var)
2266 tem = fixup_memory_subreg (tem, insn, promoted_mode, 0);
2267 else
2268 tem = fixup_stack_1 (tem, insn);
2270 if (GET_CODE (XEXP (outerdest, 1)) == CONST_INT
2271 && GET_CODE (XEXP (outerdest, 2)) == CONST_INT
2272 && ! mode_dependent_address_p (XEXP (tem, 0))
2273 && ! MEM_VOLATILE_P (tem))
2275 enum machine_mode wanted_mode;
2276 enum machine_mode is_mode = GET_MODE (tem);
2277 HOST_WIDE_INT pos = INTVAL (XEXP (outerdest, 2));
2279 wanted_mode = mode_for_extraction (EP_insv, 0);
2281 /* If we have a narrower mode, we can do something. */
2282 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2284 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2285 rtx old_pos = XEXP (outerdest, 2);
2286 rtx newmem;
2288 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2289 offset = (GET_MODE_SIZE (is_mode)
2290 - GET_MODE_SIZE (wanted_mode) - offset);
2292 pos %= GET_MODE_BITSIZE (wanted_mode);
2294 newmem = adjust_address_nv (tem, wanted_mode, offset);
2296 /* Make the change and see if the insn remains valid. */
2297 INSN_CODE (insn) = -1;
2298 XEXP (outerdest, 0) = newmem;
2299 XEXP (outerdest, 2) = GEN_INT (pos);
2301 if (recog_memoized (insn) >= 0)
2302 return;
2304 /* Otherwise, restore old position. XEXP (x, 0) will be
2305 restored later. */
2306 XEXP (outerdest, 2) = old_pos;
2310 /* If we get here, the bit-field store doesn't allow memory
2311 or isn't located at a constant position. Load the value into
2312 a register, do the store, and put it back into memory. */
2314 tem1 = gen_reg_rtx (GET_MODE (tem));
2315 emit_insn_before (gen_move_insn (tem1, tem), insn);
2316 emit_insn_after (gen_move_insn (tem, tem1), insn);
2317 XEXP (outerdest, 0) = tem1;
2318 return;
2321 /* STRICT_LOW_PART is a no-op on memory references
2322 and it can cause combinations to be unrecognizable,
2323 so eliminate it. */
2325 if (dest == var && GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2326 SET_DEST (x) = XEXP (SET_DEST (x), 0);
2328 /* A valid insn to copy VAR into or out of a register
2329 must be left alone, to avoid an infinite loop here.
2330 If the reference to VAR is by a subreg, fix that up,
2331 since SUBREG is not valid for a memref.
2332 Also fix up the address of the stack slot.
2334 Note that we must not try to recognize the insn until
2335 after we know that we have valid addresses and no
2336 (subreg (mem ...) ...) constructs, since these interfere
2337 with determining the validity of the insn. */
2339 if ((SET_SRC (x) == var
2340 || (GET_CODE (SET_SRC (x)) == SUBREG
2341 && SUBREG_REG (SET_SRC (x)) == var))
2342 && (GET_CODE (SET_DEST (x)) == REG
2343 || (GET_CODE (SET_DEST (x)) == SUBREG
2344 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG))
2345 && GET_MODE (var) == promoted_mode
2346 && x == single_set (insn))
2348 rtx pat, last;
2350 if (GET_CODE (SET_SRC (x)) == SUBREG
2351 && (GET_MODE_SIZE (GET_MODE (SET_SRC (x)))
2352 > GET_MODE_SIZE (GET_MODE (var))))
2354 /* This (subreg VAR) is now a paradoxical subreg. We need
2355 to replace VAR instead of the subreg. */
2356 replacement = find_fixup_replacement (replacements, var);
2357 if (replacement->new == NULL_RTX)
2358 replacement->new = gen_reg_rtx (GET_MODE (var));
2359 SUBREG_REG (SET_SRC (x)) = replacement->new;
2361 else
2363 replacement = find_fixup_replacement (replacements, SET_SRC (x));
2364 if (replacement->new)
2365 SET_SRC (x) = replacement->new;
2366 else if (GET_CODE (SET_SRC (x)) == SUBREG)
2367 SET_SRC (x) = replacement->new
2368 = fixup_memory_subreg (SET_SRC (x), insn, promoted_mode,
2370 else
2371 SET_SRC (x) = replacement->new
2372 = fixup_stack_1 (SET_SRC (x), insn);
2375 if (recog_memoized (insn) >= 0)
2376 return;
2378 /* INSN is not valid, but we know that we want to
2379 copy SET_SRC (x) to SET_DEST (x) in some way. So
2380 we generate the move and see whether it requires more
2381 than one insn. If it does, we emit those insns and
2382 delete INSN. Otherwise, we an just replace the pattern
2383 of INSN; we have already verified above that INSN has
2384 no other function that to do X. */
2386 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2387 if (NEXT_INSN (pat) != NULL_RTX)
2389 last = emit_insn_before (pat, insn);
2391 /* INSN might have REG_RETVAL or other important notes, so
2392 we need to store the pattern of the last insn in the
2393 sequence into INSN similarly to the normal case. LAST
2394 should not have REG_NOTES, but we allow them if INSN has
2395 no REG_NOTES. */
2396 if (REG_NOTES (last) && REG_NOTES (insn))
2397 abort ();
2398 if (REG_NOTES (last))
2399 REG_NOTES (insn) = REG_NOTES (last);
2400 PATTERN (insn) = PATTERN (last);
2402 delete_insn (last);
2404 else
2405 PATTERN (insn) = PATTERN (pat);
2407 return;
2410 if ((SET_DEST (x) == var
2411 || (GET_CODE (SET_DEST (x)) == SUBREG
2412 && SUBREG_REG (SET_DEST (x)) == var))
2413 && (GET_CODE (SET_SRC (x)) == REG
2414 || (GET_CODE (SET_SRC (x)) == SUBREG
2415 && GET_CODE (SUBREG_REG (SET_SRC (x))) == REG))
2416 && GET_MODE (var) == promoted_mode
2417 && x == single_set (insn))
2419 rtx pat, last;
2421 if (GET_CODE (SET_DEST (x)) == SUBREG)
2422 SET_DEST (x) = fixup_memory_subreg (SET_DEST (x), insn,
2423 promoted_mode, 0);
2424 else
2425 SET_DEST (x) = fixup_stack_1 (SET_DEST (x), insn);
2427 if (recog_memoized (insn) >= 0)
2428 return;
2430 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2431 if (NEXT_INSN (pat) != NULL_RTX)
2433 last = emit_insn_before (pat, insn);
2435 /* INSN might have REG_RETVAL or other important notes, so
2436 we need to store the pattern of the last insn in the
2437 sequence into INSN similarly to the normal case. LAST
2438 should not have REG_NOTES, but we allow them if INSN has
2439 no REG_NOTES. */
2440 if (REG_NOTES (last) && REG_NOTES (insn))
2441 abort ();
2442 if (REG_NOTES (last))
2443 REG_NOTES (insn) = REG_NOTES (last);
2444 PATTERN (insn) = PATTERN (last);
2446 delete_insn (last);
2448 else
2449 PATTERN (insn) = PATTERN (pat);
2451 return;
2454 /* Otherwise, storing into VAR must be handled specially
2455 by storing into a temporary and copying that into VAR
2456 with a new insn after this one. Note that this case
2457 will be used when storing into a promoted scalar since
2458 the insn will now have different modes on the input
2459 and output and hence will be invalid (except for the case
2460 of setting it to a constant, which does not need any
2461 change if it is valid). We generate extra code in that case,
2462 but combine.c will eliminate it. */
2464 if (dest == var)
2466 rtx temp;
2467 rtx fixeddest = SET_DEST (x);
2468 enum machine_mode temp_mode;
2470 /* STRICT_LOW_PART can be discarded, around a MEM. */
2471 if (GET_CODE (fixeddest) == STRICT_LOW_PART)
2472 fixeddest = XEXP (fixeddest, 0);
2473 /* Convert (SUBREG (MEM)) to a MEM in a changed mode. */
2474 if (GET_CODE (fixeddest) == SUBREG)
2476 fixeddest = fixup_memory_subreg (fixeddest, insn,
2477 promoted_mode, 0);
2478 temp_mode = GET_MODE (fixeddest);
2480 else
2482 fixeddest = fixup_stack_1 (fixeddest, insn);
2483 temp_mode = promoted_mode;
2486 temp = gen_reg_rtx (temp_mode);
2488 emit_insn_after (gen_move_insn (fixeddest,
2489 gen_lowpart (GET_MODE (fixeddest),
2490 temp)),
2491 insn);
2493 SET_DEST (x) = temp;
2497 default:
2498 break;
2501 /* Nothing special about this RTX; fix its operands. */
2503 fmt = GET_RTX_FORMAT (code);
2504 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2506 if (fmt[i] == 'e')
2507 fixup_var_refs_1 (var, promoted_mode, &XEXP (x, i), insn, replacements,
2508 no_share);
2509 else if (fmt[i] == 'E')
2511 int j;
2512 for (j = 0; j < XVECLEN (x, i); j++)
2513 fixup_var_refs_1 (var, promoted_mode, &XVECEXP (x, i, j),
2514 insn, replacements, no_share);
2519 /* Previously, X had the form (SUBREG:m1 (REG:PROMOTED_MODE ...)).
2520 The REG was placed on the stack, so X now has the form (SUBREG:m1
2521 (MEM:m2 ...)).
2523 Return an rtx (MEM:m1 newaddr) which is equivalent. If any insns
2524 must be emitted to compute NEWADDR, put them before INSN.
2526 UNCRITICAL nonzero means accept paradoxical subregs.
2527 This is used for subregs found inside REG_NOTES. */
2529 static rtx
2530 fixup_memory_subreg (x, insn, promoted_mode, uncritical)
2531 rtx x;
2532 rtx insn;
2533 enum machine_mode promoted_mode;
2534 int uncritical;
2536 int offset;
2537 rtx mem = SUBREG_REG (x);
2538 rtx addr = XEXP (mem, 0);
2539 enum machine_mode mode = GET_MODE (x);
2540 rtx result, seq;
2542 /* Paradoxical SUBREGs are usually invalid during RTL generation. */
2543 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (mem)) && ! uncritical)
2544 abort ();
2546 offset = SUBREG_BYTE (x);
2547 if (BYTES_BIG_ENDIAN)
2548 /* If the PROMOTED_MODE is wider than the mode of the MEM, adjust
2549 the offset so that it points to the right location within the
2550 MEM. */
2551 offset -= (GET_MODE_SIZE (promoted_mode) - GET_MODE_SIZE (GET_MODE (mem)));
2553 if (!flag_force_addr
2554 && memory_address_p (mode, plus_constant (addr, offset)))
2555 /* Shortcut if no insns need be emitted. */
2556 return adjust_address (mem, mode, offset);
2558 start_sequence ();
2559 result = adjust_address (mem, mode, offset);
2560 seq = get_insns ();
2561 end_sequence ();
2563 emit_insn_before (seq, insn);
2564 return result;
2567 /* Do fixup_memory_subreg on all (SUBREG (MEM ...) ...) contained in X.
2568 Replace subexpressions of X in place.
2569 If X itself is a (SUBREG (MEM ...) ...), return the replacement expression.
2570 Otherwise return X, with its contents possibly altered.
2572 INSN, PROMOTED_MODE and UNCRITICAL are as for
2573 fixup_memory_subreg. */
2575 static rtx
2576 walk_fixup_memory_subreg (x, insn, promoted_mode, uncritical)
2577 rtx x;
2578 rtx insn;
2579 enum machine_mode promoted_mode;
2580 int uncritical;
2582 enum rtx_code code;
2583 const char *fmt;
2584 int i;
2586 if (x == 0)
2587 return 0;
2589 code = GET_CODE (x);
2591 if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
2592 return fixup_memory_subreg (x, insn, promoted_mode, uncritical);
2594 /* Nothing special about this RTX; fix its operands. */
2596 fmt = GET_RTX_FORMAT (code);
2597 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2599 if (fmt[i] == 'e')
2600 XEXP (x, i) = walk_fixup_memory_subreg (XEXP (x, i), insn,
2601 promoted_mode, uncritical);
2602 else if (fmt[i] == 'E')
2604 int j;
2605 for (j = 0; j < XVECLEN (x, i); j++)
2606 XVECEXP (x, i, j)
2607 = walk_fixup_memory_subreg (XVECEXP (x, i, j), insn,
2608 promoted_mode, uncritical);
2611 return x;
2614 /* For each memory ref within X, if it refers to a stack slot
2615 with an out of range displacement, put the address in a temp register
2616 (emitting new insns before INSN to load these registers)
2617 and alter the memory ref to use that register.
2618 Replace each such MEM rtx with a copy, to avoid clobberage. */
2620 static rtx
2621 fixup_stack_1 (x, insn)
2622 rtx x;
2623 rtx insn;
2625 int i;
2626 RTX_CODE code = GET_CODE (x);
2627 const char *fmt;
2629 if (code == MEM)
2631 rtx ad = XEXP (x, 0);
2632 /* If we have address of a stack slot but it's not valid
2633 (displacement is too large), compute the sum in a register. */
2634 if (GET_CODE (ad) == PLUS
2635 && GET_CODE (XEXP (ad, 0)) == REG
2636 && ((REGNO (XEXP (ad, 0)) >= FIRST_VIRTUAL_REGISTER
2637 && REGNO (XEXP (ad, 0)) <= LAST_VIRTUAL_REGISTER)
2638 || REGNO (XEXP (ad, 0)) == FRAME_POINTER_REGNUM
2639 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2640 || REGNO (XEXP (ad, 0)) == HARD_FRAME_POINTER_REGNUM
2641 #endif
2642 || REGNO (XEXP (ad, 0)) == STACK_POINTER_REGNUM
2643 || REGNO (XEXP (ad, 0)) == ARG_POINTER_REGNUM
2644 || XEXP (ad, 0) == current_function_internal_arg_pointer)
2645 && GET_CODE (XEXP (ad, 1)) == CONST_INT)
2647 rtx temp, seq;
2648 if (memory_address_p (GET_MODE (x), ad))
2649 return x;
2651 start_sequence ();
2652 temp = copy_to_reg (ad);
2653 seq = get_insns ();
2654 end_sequence ();
2655 emit_insn_before (seq, insn);
2656 return replace_equiv_address (x, temp);
2658 return x;
2661 fmt = GET_RTX_FORMAT (code);
2662 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2664 if (fmt[i] == 'e')
2665 XEXP (x, i) = fixup_stack_1 (XEXP (x, i), insn);
2666 else if (fmt[i] == 'E')
2668 int j;
2669 for (j = 0; j < XVECLEN (x, i); j++)
2670 XVECEXP (x, i, j) = fixup_stack_1 (XVECEXP (x, i, j), insn);
2673 return x;
2676 /* Optimization: a bit-field instruction whose field
2677 happens to be a byte or halfword in memory
2678 can be changed to a move instruction.
2680 We call here when INSN is an insn to examine or store into a bit-field.
2681 BODY is the SET-rtx to be altered.
2683 EQUIV_MEM is the table `reg_equiv_mem' if that is available; else 0.
2684 (Currently this is called only from function.c, and EQUIV_MEM
2685 is always 0.) */
2687 static void
2688 optimize_bit_field (body, insn, equiv_mem)
2689 rtx body;
2690 rtx insn;
2691 rtx *equiv_mem;
2693 rtx bitfield;
2694 int destflag;
2695 rtx seq = 0;
2696 enum machine_mode mode;
2698 if (GET_CODE (SET_DEST (body)) == SIGN_EXTRACT
2699 || GET_CODE (SET_DEST (body)) == ZERO_EXTRACT)
2700 bitfield = SET_DEST (body), destflag = 1;
2701 else
2702 bitfield = SET_SRC (body), destflag = 0;
2704 /* First check that the field being stored has constant size and position
2705 and is in fact a byte or halfword suitably aligned. */
2707 if (GET_CODE (XEXP (bitfield, 1)) == CONST_INT
2708 && GET_CODE (XEXP (bitfield, 2)) == CONST_INT
2709 && ((mode = mode_for_size (INTVAL (XEXP (bitfield, 1)), MODE_INT, 1))
2710 != BLKmode)
2711 && INTVAL (XEXP (bitfield, 2)) % INTVAL (XEXP (bitfield, 1)) == 0)
2713 rtx memref = 0;
2715 /* Now check that the containing word is memory, not a register,
2716 and that it is safe to change the machine mode. */
2718 if (GET_CODE (XEXP (bitfield, 0)) == MEM)
2719 memref = XEXP (bitfield, 0);
2720 else if (GET_CODE (XEXP (bitfield, 0)) == REG
2721 && equiv_mem != 0)
2722 memref = equiv_mem[REGNO (XEXP (bitfield, 0))];
2723 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2724 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == MEM)
2725 memref = SUBREG_REG (XEXP (bitfield, 0));
2726 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2727 && equiv_mem != 0
2728 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == REG)
2729 memref = equiv_mem[REGNO (SUBREG_REG (XEXP (bitfield, 0)))];
2731 if (memref
2732 && ! mode_dependent_address_p (XEXP (memref, 0))
2733 && ! MEM_VOLATILE_P (memref))
2735 /* Now adjust the address, first for any subreg'ing
2736 that we are now getting rid of,
2737 and then for which byte of the word is wanted. */
2739 HOST_WIDE_INT offset = INTVAL (XEXP (bitfield, 2));
2740 rtx insns;
2742 /* Adjust OFFSET to count bits from low-address byte. */
2743 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
2744 offset = (GET_MODE_BITSIZE (GET_MODE (XEXP (bitfield, 0)))
2745 - offset - INTVAL (XEXP (bitfield, 1)));
2747 /* Adjust OFFSET to count bytes from low-address byte. */
2748 offset /= BITS_PER_UNIT;
2749 if (GET_CODE (XEXP (bitfield, 0)) == SUBREG)
2751 offset += (SUBREG_BYTE (XEXP (bitfield, 0))
2752 / UNITS_PER_WORD) * UNITS_PER_WORD;
2753 if (BYTES_BIG_ENDIAN)
2754 offset -= (MIN (UNITS_PER_WORD,
2755 GET_MODE_SIZE (GET_MODE (XEXP (bitfield, 0))))
2756 - MIN (UNITS_PER_WORD,
2757 GET_MODE_SIZE (GET_MODE (memref))));
2760 start_sequence ();
2761 memref = adjust_address (memref, mode, offset);
2762 insns = get_insns ();
2763 end_sequence ();
2764 emit_insn_before (insns, insn);
2766 /* Store this memory reference where
2767 we found the bit field reference. */
2769 if (destflag)
2771 validate_change (insn, &SET_DEST (body), memref, 1);
2772 if (! CONSTANT_ADDRESS_P (SET_SRC (body)))
2774 rtx src = SET_SRC (body);
2775 while (GET_CODE (src) == SUBREG
2776 && SUBREG_BYTE (src) == 0)
2777 src = SUBREG_REG (src);
2778 if (GET_MODE (src) != GET_MODE (memref))
2779 src = gen_lowpart (GET_MODE (memref), SET_SRC (body));
2780 validate_change (insn, &SET_SRC (body), src, 1);
2782 else if (GET_MODE (SET_SRC (body)) != VOIDmode
2783 && GET_MODE (SET_SRC (body)) != GET_MODE (memref))
2784 /* This shouldn't happen because anything that didn't have
2785 one of these modes should have got converted explicitly
2786 and then referenced through a subreg.
2787 This is so because the original bit-field was
2788 handled by agg_mode and so its tree structure had
2789 the same mode that memref now has. */
2790 abort ();
2792 else
2794 rtx dest = SET_DEST (body);
2796 while (GET_CODE (dest) == SUBREG
2797 && SUBREG_BYTE (dest) == 0
2798 && (GET_MODE_CLASS (GET_MODE (dest))
2799 == GET_MODE_CLASS (GET_MODE (SUBREG_REG (dest))))
2800 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
2801 <= UNITS_PER_WORD))
2802 dest = SUBREG_REG (dest);
2804 validate_change (insn, &SET_DEST (body), dest, 1);
2806 if (GET_MODE (dest) == GET_MODE (memref))
2807 validate_change (insn, &SET_SRC (body), memref, 1);
2808 else
2810 /* Convert the mem ref to the destination mode. */
2811 rtx newreg = gen_reg_rtx (GET_MODE (dest));
2813 start_sequence ();
2814 convert_move (newreg, memref,
2815 GET_CODE (SET_SRC (body)) == ZERO_EXTRACT);
2816 seq = get_insns ();
2817 end_sequence ();
2819 validate_change (insn, &SET_SRC (body), newreg, 1);
2823 /* See if we can convert this extraction or insertion into
2824 a simple move insn. We might not be able to do so if this
2825 was, for example, part of a PARALLEL.
2827 If we succeed, write out any needed conversions. If we fail,
2828 it is hard to guess why we failed, so don't do anything
2829 special; just let the optimization be suppressed. */
2831 if (apply_change_group () && seq)
2832 emit_insn_before (seq, insn);
2837 /* These routines are responsible for converting virtual register references
2838 to the actual hard register references once RTL generation is complete.
2840 The following four variables are used for communication between the
2841 routines. They contain the offsets of the virtual registers from their
2842 respective hard registers. */
2844 static int in_arg_offset;
2845 static int var_offset;
2846 static int dynamic_offset;
2847 static int out_arg_offset;
2848 static int cfa_offset;
2850 /* In most machines, the stack pointer register is equivalent to the bottom
2851 of the stack. */
2853 #ifndef STACK_POINTER_OFFSET
2854 #define STACK_POINTER_OFFSET 0
2855 #endif
2857 /* If not defined, pick an appropriate default for the offset of dynamically
2858 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
2859 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
2861 #ifndef STACK_DYNAMIC_OFFSET
2863 /* The bottom of the stack points to the actual arguments. If
2864 REG_PARM_STACK_SPACE is defined, this includes the space for the register
2865 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
2866 stack space for register parameters is not pushed by the caller, but
2867 rather part of the fixed stack areas and hence not included in
2868 `current_function_outgoing_args_size'. Nevertheless, we must allow
2869 for it when allocating stack dynamic objects. */
2871 #if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
2872 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2873 ((ACCUMULATE_OUTGOING_ARGS \
2874 ? (current_function_outgoing_args_size + REG_PARM_STACK_SPACE (FNDECL)) : 0)\
2875 + (STACK_POINTER_OFFSET)) \
2877 #else
2878 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2879 ((ACCUMULATE_OUTGOING_ARGS ? current_function_outgoing_args_size : 0) \
2880 + (STACK_POINTER_OFFSET))
2881 #endif
2882 #endif
2884 /* On most machines, the CFA coincides with the first incoming parm. */
2886 #ifndef ARG_POINTER_CFA_OFFSET
2887 #define ARG_POINTER_CFA_OFFSET(FNDECL) FIRST_PARM_OFFSET (FNDECL)
2888 #endif
2890 /* Build up a (MEM (ADDRESSOF (REG))) rtx for a register REG that just had its
2891 address taken. DECL is the decl or SAVE_EXPR for the object stored in the
2892 register, for later use if we do need to force REG into the stack. REG is
2893 overwritten by the MEM like in put_reg_into_stack. */
2896 gen_mem_addressof (reg, decl)
2897 rtx reg;
2898 tree decl;
2900 rtx r = gen_rtx_ADDRESSOF (Pmode, gen_reg_rtx (GET_MODE (reg)),
2901 REGNO (reg), decl);
2903 /* Calculate this before we start messing with decl's RTL. */
2904 HOST_WIDE_INT set = decl ? get_alias_set (decl) : 0;
2906 /* If the original REG was a user-variable, then so is the REG whose
2907 address is being taken. Likewise for unchanging. */
2908 REG_USERVAR_P (XEXP (r, 0)) = REG_USERVAR_P (reg);
2909 RTX_UNCHANGING_P (XEXP (r, 0)) = RTX_UNCHANGING_P (reg);
2911 PUT_CODE (reg, MEM);
2912 MEM_ATTRS (reg) = 0;
2913 XEXP (reg, 0) = r;
2915 if (decl)
2917 tree type = TREE_TYPE (decl);
2918 enum machine_mode decl_mode
2919 = (DECL_P (decl) ? DECL_MODE (decl) : TYPE_MODE (TREE_TYPE (decl)));
2920 rtx decl_rtl = (TREE_CODE (decl) == SAVE_EXPR ? SAVE_EXPR_RTL (decl)
2921 : DECL_RTL_IF_SET (decl));
2923 PUT_MODE (reg, decl_mode);
2925 /* Clear DECL_RTL momentarily so functions below will work
2926 properly, then set it again. */
2927 if (DECL_P (decl) && decl_rtl == reg)
2928 SET_DECL_RTL (decl, 0);
2930 set_mem_attributes (reg, decl, 1);
2931 set_mem_alias_set (reg, set);
2933 if (DECL_P (decl) && decl_rtl == reg)
2934 SET_DECL_RTL (decl, reg);
2936 if (TREE_USED (decl) || (DECL_P (decl) && DECL_INITIAL (decl) != 0))
2937 fixup_var_refs (reg, GET_MODE (reg), TREE_UNSIGNED (type), reg, 0);
2939 else
2940 fixup_var_refs (reg, GET_MODE (reg), 0, reg, 0);
2942 return reg;
2945 /* If DECL has an RTL that is an ADDRESSOF rtx, put it into the stack. */
2947 void
2948 flush_addressof (decl)
2949 tree decl;
2951 if ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL)
2952 && DECL_RTL (decl) != 0
2953 && GET_CODE (DECL_RTL (decl)) == MEM
2954 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF
2955 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == REG)
2956 put_addressof_into_stack (XEXP (DECL_RTL (decl), 0), 0);
2959 /* Force the register pointed to by R, an ADDRESSOF rtx, into the stack. */
2961 static void
2962 put_addressof_into_stack (r, ht)
2963 rtx r;
2964 htab_t ht;
2966 tree decl, type;
2967 int volatile_p, used_p;
2969 rtx reg = XEXP (r, 0);
2971 if (GET_CODE (reg) != REG)
2972 abort ();
2974 decl = ADDRESSOF_DECL (r);
2975 if (decl)
2977 type = TREE_TYPE (decl);
2978 volatile_p = (TREE_CODE (decl) != SAVE_EXPR
2979 && TREE_THIS_VOLATILE (decl));
2980 used_p = (TREE_USED (decl)
2981 || (DECL_P (decl) && DECL_INITIAL (decl) != 0));
2983 else
2985 type = NULL_TREE;
2986 volatile_p = 0;
2987 used_p = 1;
2990 put_reg_into_stack (0, reg, type, GET_MODE (reg), GET_MODE (reg),
2991 volatile_p, ADDRESSOF_REGNO (r), used_p, ht);
2994 /* List of replacements made below in purge_addressof_1 when creating
2995 bitfield insertions. */
2996 static rtx purge_bitfield_addressof_replacements;
2998 /* List of replacements made below in purge_addressof_1 for patterns
2999 (MEM (ADDRESSOF (REG ...))). The key of the list entry is the
3000 corresponding (ADDRESSOF (REG ...)) and value is a substitution for
3001 the all pattern. List PURGE_BITFIELD_ADDRESSOF_REPLACEMENTS is not
3002 enough in complex cases, e.g. when some field values can be
3003 extracted by usage MEM with narrower mode. */
3004 static rtx purge_addressof_replacements;
3006 /* Helper function for purge_addressof. See if the rtx expression at *LOC
3007 in INSN needs to be changed. If FORCE, always put any ADDRESSOFs into
3008 the stack. If the function returns FALSE then the replacement could not
3009 be made. */
3011 static bool
3012 purge_addressof_1 (loc, insn, force, store, ht)
3013 rtx *loc;
3014 rtx insn;
3015 int force, store;
3016 htab_t ht;
3018 rtx x;
3019 RTX_CODE code;
3020 int i, j;
3021 const char *fmt;
3022 bool result = true;
3024 /* Re-start here to avoid recursion in common cases. */
3025 restart:
3027 x = *loc;
3028 if (x == 0)
3029 return true;
3031 code = GET_CODE (x);
3033 /* If we don't return in any of the cases below, we will recurse inside
3034 the RTX, which will normally result in any ADDRESSOF being forced into
3035 memory. */
3036 if (code == SET)
3038 result = purge_addressof_1 (&SET_DEST (x), insn, force, 1, ht);
3039 result &= purge_addressof_1 (&SET_SRC (x), insn, force, 0, ht);
3040 return result;
3042 else if (code == ADDRESSOF)
3044 rtx sub, insns;
3046 if (GET_CODE (XEXP (x, 0)) != MEM)
3048 put_addressof_into_stack (x, ht);
3049 return true;
3052 /* We must create a copy of the rtx because it was created by
3053 overwriting a REG rtx which is always shared. */
3054 sub = copy_rtx (XEXP (XEXP (x, 0), 0));
3055 if (validate_change (insn, loc, sub, 0)
3056 || validate_replace_rtx (x, sub, insn))
3057 return true;
3059 start_sequence ();
3060 sub = force_operand (sub, NULL_RTX);
3061 if (! validate_change (insn, loc, sub, 0)
3062 && ! validate_replace_rtx (x, sub, insn))
3063 abort ();
3065 insns = get_insns ();
3066 end_sequence ();
3067 emit_insn_before (insns, insn);
3068 return true;
3071 else if (code == MEM && GET_CODE (XEXP (x, 0)) == ADDRESSOF && ! force)
3073 rtx sub = XEXP (XEXP (x, 0), 0);
3075 if (GET_CODE (sub) == MEM)
3076 sub = adjust_address_nv (sub, GET_MODE (x), 0);
3077 else if (GET_CODE (sub) == REG
3078 && (MEM_VOLATILE_P (x) || GET_MODE (x) == BLKmode))
3080 else if (GET_CODE (sub) == REG && GET_MODE (x) != GET_MODE (sub))
3082 int size_x, size_sub;
3084 if (!insn)
3086 /* When processing REG_NOTES look at the list of
3087 replacements done on the insn to find the register that X
3088 was replaced by. */
3089 rtx tem;
3091 for (tem = purge_bitfield_addressof_replacements;
3092 tem != NULL_RTX;
3093 tem = XEXP (XEXP (tem, 1), 1))
3094 if (rtx_equal_p (x, XEXP (tem, 0)))
3096 *loc = XEXP (XEXP (tem, 1), 0);
3097 return true;
3100 /* See comment for purge_addressof_replacements. */
3101 for (tem = purge_addressof_replacements;
3102 tem != NULL_RTX;
3103 tem = XEXP (XEXP (tem, 1), 1))
3104 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3106 rtx z = XEXP (XEXP (tem, 1), 0);
3108 if (GET_MODE (x) == GET_MODE (z)
3109 || (GET_CODE (XEXP (XEXP (tem, 1), 0)) != REG
3110 && GET_CODE (XEXP (XEXP (tem, 1), 0)) != SUBREG))
3111 abort ();
3113 /* It can happen that the note may speak of things
3114 in a wider (or just different) mode than the
3115 code did. This is especially true of
3116 REG_RETVAL. */
3118 if (GET_CODE (z) == SUBREG && SUBREG_BYTE (z) == 0)
3119 z = SUBREG_REG (z);
3121 if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3122 && (GET_MODE_SIZE (GET_MODE (x))
3123 > GET_MODE_SIZE (GET_MODE (z))))
3125 /* This can occur as a result in invalid
3126 pointer casts, e.g. float f; ...
3127 *(long long int *)&f.
3128 ??? We could emit a warning here, but
3129 without a line number that wouldn't be
3130 very helpful. */
3131 z = gen_rtx_SUBREG (GET_MODE (x), z, 0);
3133 else
3134 z = gen_lowpart (GET_MODE (x), z);
3136 *loc = z;
3137 return true;
3140 /* Sometimes we may not be able to find the replacement. For
3141 example when the original insn was a MEM in a wider mode,
3142 and the note is part of a sign extension of a narrowed
3143 version of that MEM. Gcc testcase compile/990829-1.c can
3144 generate an example of this situation. Rather than complain
3145 we return false, which will prompt our caller to remove the
3146 offending note. */
3147 return false;
3150 size_x = GET_MODE_BITSIZE (GET_MODE (x));
3151 size_sub = GET_MODE_BITSIZE (GET_MODE (sub));
3153 /* Don't even consider working with paradoxical subregs,
3154 or the moral equivalent seen here. */
3155 if (size_x <= size_sub
3156 && int_mode_for_mode (GET_MODE (sub)) != BLKmode)
3158 /* Do a bitfield insertion to mirror what would happen
3159 in memory. */
3161 rtx val, seq;
3163 if (store)
3165 rtx p = PREV_INSN (insn);
3167 start_sequence ();
3168 val = gen_reg_rtx (GET_MODE (x));
3169 if (! validate_change (insn, loc, val, 0))
3171 /* Discard the current sequence and put the
3172 ADDRESSOF on stack. */
3173 end_sequence ();
3174 goto give_up;
3176 seq = get_insns ();
3177 end_sequence ();
3178 emit_insn_before (seq, insn);
3179 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3180 insn, ht);
3182 start_sequence ();
3183 store_bit_field (sub, size_x, 0, GET_MODE (x),
3184 val, GET_MODE_SIZE (GET_MODE (sub)));
3186 /* Make sure to unshare any shared rtl that store_bit_field
3187 might have created. */
3188 unshare_all_rtl_again (get_insns ());
3190 seq = get_insns ();
3191 end_sequence ();
3192 p = emit_insn_after (seq, insn);
3193 if (NEXT_INSN (insn))
3194 compute_insns_for_mem (NEXT_INSN (insn),
3195 p ? NEXT_INSN (p) : NULL_RTX,
3196 ht);
3198 else
3200 rtx p = PREV_INSN (insn);
3202 start_sequence ();
3203 val = extract_bit_field (sub, size_x, 0, 1, NULL_RTX,
3204 GET_MODE (x), GET_MODE (x),
3205 GET_MODE_SIZE (GET_MODE (sub)));
3207 if (! validate_change (insn, loc, val, 0))
3209 /* Discard the current sequence and put the
3210 ADDRESSOF on stack. */
3211 end_sequence ();
3212 goto give_up;
3215 seq = get_insns ();
3216 end_sequence ();
3217 emit_insn_before (seq, insn);
3218 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3219 insn, ht);
3222 /* Remember the replacement so that the same one can be done
3223 on the REG_NOTES. */
3224 purge_bitfield_addressof_replacements
3225 = gen_rtx_EXPR_LIST (VOIDmode, x,
3226 gen_rtx_EXPR_LIST
3227 (VOIDmode, val,
3228 purge_bitfield_addressof_replacements));
3230 /* We replaced with a reg -- all done. */
3231 return true;
3235 else if (validate_change (insn, loc, sub, 0))
3237 /* Remember the replacement so that the same one can be done
3238 on the REG_NOTES. */
3239 if (GET_CODE (sub) == REG || GET_CODE (sub) == SUBREG)
3241 rtx tem;
3243 for (tem = purge_addressof_replacements;
3244 tem != NULL_RTX;
3245 tem = XEXP (XEXP (tem, 1), 1))
3246 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3248 XEXP (XEXP (tem, 1), 0) = sub;
3249 return true;
3251 purge_addressof_replacements
3252 = gen_rtx (EXPR_LIST, VOIDmode, XEXP (x, 0),
3253 gen_rtx_EXPR_LIST (VOIDmode, sub,
3254 purge_addressof_replacements));
3255 return true;
3257 goto restart;
3261 give_up:
3262 /* Scan all subexpressions. */
3263 fmt = GET_RTX_FORMAT (code);
3264 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3266 if (*fmt == 'e')
3267 result &= purge_addressof_1 (&XEXP (x, i), insn, force, 0, ht);
3268 else if (*fmt == 'E')
3269 for (j = 0; j < XVECLEN (x, i); j++)
3270 result &= purge_addressof_1 (&XVECEXP (x, i, j), insn, force, 0, ht);
3273 return result;
3276 /* Return a hash value for K, a REG. */
3278 static hashval_t
3279 insns_for_mem_hash (k)
3280 const void * k;
3282 /* Use the address of the key for the hash value. */
3283 struct insns_for_mem_entry *m = (struct insns_for_mem_entry *) k;
3284 return (hashval_t) m->key;
3287 /* Return non-zero if K1 and K2 (two REGs) are the same. */
3289 static int
3290 insns_for_mem_comp (k1, k2)
3291 const void * k1;
3292 const void * k2;
3294 struct insns_for_mem_entry *m1 = (struct insns_for_mem_entry *) k1;
3295 struct insns_for_mem_entry *m2 = (struct insns_for_mem_entry *) k2;
3296 return m1->key == m2->key;
3299 struct insns_for_mem_walk_info
3301 /* The hash table that we are using to record which INSNs use which
3302 MEMs. */
3303 htab_t ht;
3305 /* The INSN we are currently processing. */
3306 rtx insn;
3308 /* Zero if we are walking to find ADDRESSOFs, one if we are walking
3309 to find the insns that use the REGs in the ADDRESSOFs. */
3310 int pass;
3313 /* Called from compute_insns_for_mem via for_each_rtx. If R is a REG
3314 that might be used in an ADDRESSOF expression, record this INSN in
3315 the hash table given by DATA (which is really a pointer to an
3316 insns_for_mem_walk_info structure). */
3318 static int
3319 insns_for_mem_walk (r, data)
3320 rtx *r;
3321 void *data;
3323 struct insns_for_mem_walk_info *ifmwi
3324 = (struct insns_for_mem_walk_info *) data;
3325 struct insns_for_mem_entry tmp;
3326 tmp.insns = NULL_RTX;
3328 if (ifmwi->pass == 0 && *r && GET_CODE (*r) == ADDRESSOF
3329 && GET_CODE (XEXP (*r, 0)) == REG)
3331 PTR *e;
3332 tmp.key = XEXP (*r, 0);
3333 e = htab_find_slot (ifmwi->ht, &tmp, INSERT);
3334 if (*e == NULL)
3336 *e = ggc_alloc (sizeof (tmp));
3337 memcpy (*e, &tmp, sizeof (tmp));
3340 else if (ifmwi->pass == 1 && *r && GET_CODE (*r) == REG)
3342 struct insns_for_mem_entry *ifme;
3343 tmp.key = *r;
3344 ifme = (struct insns_for_mem_entry *) htab_find (ifmwi->ht, &tmp);
3346 /* If we have not already recorded this INSN, do so now. Since
3347 we process the INSNs in order, we know that if we have
3348 recorded it it must be at the front of the list. */
3349 if (ifme && (!ifme->insns || XEXP (ifme->insns, 0) != ifmwi->insn))
3350 ifme->insns = gen_rtx_EXPR_LIST (VOIDmode, ifmwi->insn,
3351 ifme->insns);
3354 return 0;
3357 /* Walk the INSNS, until we reach LAST_INSN, recording which INSNs use
3358 which REGs in HT. */
3360 static void
3361 compute_insns_for_mem (insns, last_insn, ht)
3362 rtx insns;
3363 rtx last_insn;
3364 htab_t ht;
3366 rtx insn;
3367 struct insns_for_mem_walk_info ifmwi;
3368 ifmwi.ht = ht;
3370 for (ifmwi.pass = 0; ifmwi.pass < 2; ++ifmwi.pass)
3371 for (insn = insns; insn != last_insn; insn = NEXT_INSN (insn))
3372 if (INSN_P (insn))
3374 ifmwi.insn = insn;
3375 for_each_rtx (&insn, insns_for_mem_walk, &ifmwi);
3379 /* Helper function for purge_addressof called through for_each_rtx.
3380 Returns true iff the rtl is an ADDRESSOF. */
3382 static int
3383 is_addressof (rtl, data)
3384 rtx *rtl;
3385 void *data ATTRIBUTE_UNUSED;
3387 return GET_CODE (*rtl) == ADDRESSOF;
3390 /* Eliminate all occurrences of ADDRESSOF from INSNS. Elide any remaining
3391 (MEM (ADDRESSOF)) patterns, and force any needed registers into the
3392 stack. */
3394 void
3395 purge_addressof (insns)
3396 rtx insns;
3398 rtx insn;
3399 htab_t ht;
3401 /* When we actually purge ADDRESSOFs, we turn REGs into MEMs. That
3402 requires a fixup pass over the instruction stream to correct
3403 INSNs that depended on the REG being a REG, and not a MEM. But,
3404 these fixup passes are slow. Furthermore, most MEMs are not
3405 mentioned in very many instructions. So, we speed up the process
3406 by pre-calculating which REGs occur in which INSNs; that allows
3407 us to perform the fixup passes much more quickly. */
3408 ht = htab_create_ggc (1000, insns_for_mem_hash, insns_for_mem_comp, NULL);
3409 compute_insns_for_mem (insns, NULL_RTX, ht);
3411 for (insn = insns; insn; insn = NEXT_INSN (insn))
3412 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3413 || GET_CODE (insn) == CALL_INSN)
3415 if (! purge_addressof_1 (&PATTERN (insn), insn,
3416 asm_noperands (PATTERN (insn)) > 0, 0, ht))
3417 /* If we could not replace the ADDRESSOFs in the insn,
3418 something is wrong. */
3419 abort ();
3421 if (! purge_addressof_1 (&REG_NOTES (insn), NULL_RTX, 0, 0, ht))
3423 /* If we could not replace the ADDRESSOFs in the insn's notes,
3424 we can just remove the offending notes instead. */
3425 rtx note;
3427 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3429 /* If we find a REG_RETVAL note then the insn is a libcall.
3430 Such insns must have REG_EQUAL notes as well, in order
3431 for later passes of the compiler to work. So it is not
3432 safe to delete the notes here, and instead we abort. */
3433 if (REG_NOTE_KIND (note) == REG_RETVAL)
3434 abort ();
3435 if (for_each_rtx (&note, is_addressof, NULL))
3436 remove_note (insn, note);
3441 /* Clean up. */
3442 purge_bitfield_addressof_replacements = 0;
3443 purge_addressof_replacements = 0;
3445 /* REGs are shared. purge_addressof will destructively replace a REG
3446 with a MEM, which creates shared MEMs.
3448 Unfortunately, the children of put_reg_into_stack assume that MEMs
3449 referring to the same stack slot are shared (fixup_var_refs and
3450 the associated hash table code).
3452 So, we have to do another unsharing pass after we have flushed any
3453 REGs that had their address taken into the stack.
3455 It may be worth tracking whether or not we converted any REGs into
3456 MEMs to avoid this overhead when it is not needed. */
3457 unshare_all_rtl_again (get_insns ());
3460 /* Convert a SET of a hard subreg to a set of the appropriate hard
3461 register. A subroutine of purge_hard_subreg_sets. */
3463 static void
3464 purge_single_hard_subreg_set (pattern)
3465 rtx pattern;
3467 rtx reg = SET_DEST (pattern);
3468 enum machine_mode mode = GET_MODE (SET_DEST (pattern));
3469 int offset = 0;
3471 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG
3472 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
3474 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
3475 GET_MODE (SUBREG_REG (reg)),
3476 SUBREG_BYTE (reg),
3477 GET_MODE (reg));
3478 reg = SUBREG_REG (reg);
3482 if (GET_CODE (reg) == REG && REGNO (reg) < FIRST_PSEUDO_REGISTER)
3484 reg = gen_rtx_REG (mode, REGNO (reg) + offset);
3485 SET_DEST (pattern) = reg;
3489 /* Eliminate all occurrences of SETs of hard subregs from INSNS. The
3490 only such SETs that we expect to see are those left in because
3491 integrate can't handle sets of parts of a return value register.
3493 We don't use alter_subreg because we only want to eliminate subregs
3494 of hard registers. */
3496 void
3497 purge_hard_subreg_sets (insn)
3498 rtx insn;
3500 for (; insn; insn = NEXT_INSN (insn))
3502 if (INSN_P (insn))
3504 rtx pattern = PATTERN (insn);
3505 switch (GET_CODE (pattern))
3507 case SET:
3508 if (GET_CODE (SET_DEST (pattern)) == SUBREG)
3509 purge_single_hard_subreg_set (pattern);
3510 break;
3511 case PARALLEL:
3513 int j;
3514 for (j = XVECLEN (pattern, 0) - 1; j >= 0; j--)
3516 rtx inner_pattern = XVECEXP (pattern, 0, j);
3517 if (GET_CODE (inner_pattern) == SET
3518 && GET_CODE (SET_DEST (inner_pattern)) == SUBREG)
3519 purge_single_hard_subreg_set (inner_pattern);
3522 break;
3523 default:
3524 break;
3530 /* Pass through the INSNS of function FNDECL and convert virtual register
3531 references to hard register references. */
3533 void
3534 instantiate_virtual_regs (fndecl, insns)
3535 tree fndecl;
3536 rtx insns;
3538 rtx insn;
3539 unsigned int i;
3541 /* Compute the offsets to use for this function. */
3542 in_arg_offset = FIRST_PARM_OFFSET (fndecl);
3543 var_offset = STARTING_FRAME_OFFSET;
3544 dynamic_offset = STACK_DYNAMIC_OFFSET (fndecl);
3545 out_arg_offset = STACK_POINTER_OFFSET;
3546 cfa_offset = ARG_POINTER_CFA_OFFSET (fndecl);
3548 /* Scan all variables and parameters of this function. For each that is
3549 in memory, instantiate all virtual registers if the result is a valid
3550 address. If not, we do it later. That will handle most uses of virtual
3551 regs on many machines. */
3552 instantiate_decls (fndecl, 1);
3554 /* Initialize recognition, indicating that volatile is OK. */
3555 init_recog ();
3557 /* Scan through all the insns, instantiating every virtual register still
3558 present. */
3559 for (insn = insns; insn; insn = NEXT_INSN (insn))
3560 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3561 || GET_CODE (insn) == CALL_INSN)
3563 instantiate_virtual_regs_1 (&PATTERN (insn), insn, 1);
3564 instantiate_virtual_regs_1 (&REG_NOTES (insn), NULL_RTX, 0);
3565 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
3566 if (GET_CODE (insn) == CALL_INSN)
3567 instantiate_virtual_regs_1 (&CALL_INSN_FUNCTION_USAGE (insn),
3568 NULL_RTX, 0);
3571 /* Instantiate the stack slots for the parm registers, for later use in
3572 addressof elimination. */
3573 for (i = 0; i < max_parm_reg; ++i)
3574 if (parm_reg_stack_loc[i])
3575 instantiate_virtual_regs_1 (&parm_reg_stack_loc[i], NULL_RTX, 0);
3577 /* Now instantiate the remaining register equivalences for debugging info.
3578 These will not be valid addresses. */
3579 instantiate_decls (fndecl, 0);
3581 /* Indicate that, from now on, assign_stack_local should use
3582 frame_pointer_rtx. */
3583 virtuals_instantiated = 1;
3586 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
3587 all virtual registers in their DECL_RTL's.
3589 If VALID_ONLY, do this only if the resulting address is still valid.
3590 Otherwise, always do it. */
3592 static void
3593 instantiate_decls (fndecl, valid_only)
3594 tree fndecl;
3595 int valid_only;
3597 tree decl;
3599 /* Process all parameters of the function. */
3600 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
3602 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
3603 HOST_WIDE_INT size_rtl;
3605 instantiate_decl (DECL_RTL (decl), size, valid_only);
3607 /* If the parameter was promoted, then the incoming RTL mode may be
3608 larger than the declared type size. We must use the larger of
3609 the two sizes. */
3610 size_rtl = GET_MODE_SIZE (GET_MODE (DECL_INCOMING_RTL (decl)));
3611 size = MAX (size_rtl, size);
3612 instantiate_decl (DECL_INCOMING_RTL (decl), size, valid_only);
3615 /* Now process all variables defined in the function or its subblocks. */
3616 instantiate_decls_1 (DECL_INITIAL (fndecl), valid_only);
3619 /* Subroutine of instantiate_decls: Process all decls in the given
3620 BLOCK node and all its subblocks. */
3622 static void
3623 instantiate_decls_1 (let, valid_only)
3624 tree let;
3625 int valid_only;
3627 tree t;
3629 for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
3630 if (DECL_RTL_SET_P (t))
3631 instantiate_decl (DECL_RTL (t),
3632 int_size_in_bytes (TREE_TYPE (t)),
3633 valid_only);
3635 /* Process all subblocks. */
3636 for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
3637 instantiate_decls_1 (t, valid_only);
3640 /* Subroutine of the preceding procedures: Given RTL representing a
3641 decl and the size of the object, do any instantiation required.
3643 If VALID_ONLY is non-zero, it means that the RTL should only be
3644 changed if the new address is valid. */
3646 static void
3647 instantiate_decl (x, size, valid_only)
3648 rtx x;
3649 HOST_WIDE_INT size;
3650 int valid_only;
3652 enum machine_mode mode;
3653 rtx addr;
3655 /* If this is not a MEM, no need to do anything. Similarly if the
3656 address is a constant or a register that is not a virtual register. */
3658 if (x == 0 || GET_CODE (x) != MEM)
3659 return;
3661 addr = XEXP (x, 0);
3662 if (CONSTANT_P (addr)
3663 || (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == REG)
3664 || (GET_CODE (addr) == REG
3665 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
3666 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
3667 return;
3669 /* If we should only do this if the address is valid, copy the address.
3670 We need to do this so we can undo any changes that might make the
3671 address invalid. This copy is unfortunate, but probably can't be
3672 avoided. */
3674 if (valid_only)
3675 addr = copy_rtx (addr);
3677 instantiate_virtual_regs_1 (&addr, NULL_RTX, 0);
3679 if (valid_only && size >= 0)
3681 unsigned HOST_WIDE_INT decl_size = size;
3683 /* Now verify that the resulting address is valid for every integer or
3684 floating-point mode up to and including SIZE bytes long. We do this
3685 since the object might be accessed in any mode and frame addresses
3686 are shared. */
3688 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3689 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3690 mode = GET_MODE_WIDER_MODE (mode))
3691 if (! memory_address_p (mode, addr))
3692 return;
3694 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
3695 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3696 mode = GET_MODE_WIDER_MODE (mode))
3697 if (! memory_address_p (mode, addr))
3698 return;
3701 /* Put back the address now that we have updated it and we either know
3702 it is valid or we don't care whether it is valid. */
3704 XEXP (x, 0) = addr;
3707 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
3708 is a virtual register, return the equivalent hard register and set the
3709 offset indirectly through the pointer. Otherwise, return 0. */
3711 static rtx
3712 instantiate_new_reg (x, poffset)
3713 rtx x;
3714 HOST_WIDE_INT *poffset;
3716 rtx new;
3717 HOST_WIDE_INT offset;
3719 if (x == virtual_incoming_args_rtx)
3720 new = arg_pointer_rtx, offset = in_arg_offset;
3721 else if (x == virtual_stack_vars_rtx)
3722 new = frame_pointer_rtx, offset = var_offset;
3723 else if (x == virtual_stack_dynamic_rtx)
3724 new = stack_pointer_rtx, offset = dynamic_offset;
3725 else if (x == virtual_outgoing_args_rtx)
3726 new = stack_pointer_rtx, offset = out_arg_offset;
3727 else if (x == virtual_cfa_rtx)
3728 new = arg_pointer_rtx, offset = cfa_offset;
3729 else
3730 return 0;
3732 *poffset = offset;
3733 return new;
3736 /* Given a pointer to a piece of rtx and an optional pointer to the
3737 containing object, instantiate any virtual registers present in it.
3739 If EXTRA_INSNS, we always do the replacement and generate
3740 any extra insns before OBJECT. If it zero, we do nothing if replacement
3741 is not valid.
3743 Return 1 if we either had nothing to do or if we were able to do the
3744 needed replacement. Return 0 otherwise; we only return zero if
3745 EXTRA_INSNS is zero.
3747 We first try some simple transformations to avoid the creation of extra
3748 pseudos. */
3750 static int
3751 instantiate_virtual_regs_1 (loc, object, extra_insns)
3752 rtx *loc;
3753 rtx object;
3754 int extra_insns;
3756 rtx x;
3757 RTX_CODE code;
3758 rtx new = 0;
3759 HOST_WIDE_INT offset = 0;
3760 rtx temp;
3761 rtx seq;
3762 int i, j;
3763 const char *fmt;
3765 /* Re-start here to avoid recursion in common cases. */
3766 restart:
3768 x = *loc;
3769 if (x == 0)
3770 return 1;
3772 code = GET_CODE (x);
3774 /* Check for some special cases. */
3775 switch (code)
3777 case CONST_INT:
3778 case CONST_DOUBLE:
3779 case CONST_VECTOR:
3780 case CONST:
3781 case SYMBOL_REF:
3782 case CODE_LABEL:
3783 case PC:
3784 case CC0:
3785 case ASM_INPUT:
3786 case ADDR_VEC:
3787 case ADDR_DIFF_VEC:
3788 case RETURN:
3789 return 1;
3791 case SET:
3792 /* We are allowed to set the virtual registers. This means that
3793 the actual register should receive the source minus the
3794 appropriate offset. This is used, for example, in the handling
3795 of non-local gotos. */
3796 if ((new = instantiate_new_reg (SET_DEST (x), &offset)) != 0)
3798 rtx src = SET_SRC (x);
3800 /* We are setting the register, not using it, so the relevant
3801 offset is the negative of the offset to use were we using
3802 the register. */
3803 offset = - offset;
3804 instantiate_virtual_regs_1 (&src, NULL_RTX, 0);
3806 /* The only valid sources here are PLUS or REG. Just do
3807 the simplest possible thing to handle them. */
3808 if (GET_CODE (src) != REG && GET_CODE (src) != PLUS)
3809 abort ();
3811 start_sequence ();
3812 if (GET_CODE (src) != REG)
3813 temp = force_operand (src, NULL_RTX);
3814 else
3815 temp = src;
3816 temp = force_operand (plus_constant (temp, offset), NULL_RTX);
3817 seq = get_insns ();
3818 end_sequence ();
3820 emit_insn_before (seq, object);
3821 SET_DEST (x) = new;
3823 if (! validate_change (object, &SET_SRC (x), temp, 0)
3824 || ! extra_insns)
3825 abort ();
3827 return 1;
3830 instantiate_virtual_regs_1 (&SET_DEST (x), object, extra_insns);
3831 loc = &SET_SRC (x);
3832 goto restart;
3834 case PLUS:
3835 /* Handle special case of virtual register plus constant. */
3836 if (CONSTANT_P (XEXP (x, 1)))
3838 rtx old, new_offset;
3840 /* Check for (plus (plus VIRT foo) (const_int)) first. */
3841 if (GET_CODE (XEXP (x, 0)) == PLUS)
3843 if ((new = instantiate_new_reg (XEXP (XEXP (x, 0), 0), &offset)))
3845 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 1), object,
3846 extra_insns);
3847 new = gen_rtx_PLUS (Pmode, new, XEXP (XEXP (x, 0), 1));
3849 else
3851 loc = &XEXP (x, 0);
3852 goto restart;
3856 #ifdef POINTERS_EXTEND_UNSIGNED
3857 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
3858 we can commute the PLUS and SUBREG because pointers into the
3859 frame are well-behaved. */
3860 else if (GET_CODE (XEXP (x, 0)) == SUBREG && GET_MODE (x) == ptr_mode
3861 && GET_CODE (XEXP (x, 1)) == CONST_INT
3862 && 0 != (new
3863 = instantiate_new_reg (SUBREG_REG (XEXP (x, 0)),
3864 &offset))
3865 && validate_change (object, loc,
3866 plus_constant (gen_lowpart (ptr_mode,
3867 new),
3868 offset
3869 + INTVAL (XEXP (x, 1))),
3871 return 1;
3872 #endif
3873 else if ((new = instantiate_new_reg (XEXP (x, 0), &offset)) == 0)
3875 /* We know the second operand is a constant. Unless the
3876 first operand is a REG (which has been already checked),
3877 it needs to be checked. */
3878 if (GET_CODE (XEXP (x, 0)) != REG)
3880 loc = &XEXP (x, 0);
3881 goto restart;
3883 return 1;
3886 new_offset = plus_constant (XEXP (x, 1), offset);
3888 /* If the new constant is zero, try to replace the sum with just
3889 the register. */
3890 if (new_offset == const0_rtx
3891 && validate_change (object, loc, new, 0))
3892 return 1;
3894 /* Next try to replace the register and new offset.
3895 There are two changes to validate here and we can't assume that
3896 in the case of old offset equals new just changing the register
3897 will yield a valid insn. In the interests of a little efficiency,
3898 however, we only call validate change once (we don't queue up the
3899 changes and then call apply_change_group). */
3901 old = XEXP (x, 0);
3902 if (offset == 0
3903 ? ! validate_change (object, &XEXP (x, 0), new, 0)
3904 : (XEXP (x, 0) = new,
3905 ! validate_change (object, &XEXP (x, 1), new_offset, 0)))
3907 if (! extra_insns)
3909 XEXP (x, 0) = old;
3910 return 0;
3913 /* Otherwise copy the new constant into a register and replace
3914 constant with that register. */
3915 temp = gen_reg_rtx (Pmode);
3916 XEXP (x, 0) = new;
3917 if (validate_change (object, &XEXP (x, 1), temp, 0))
3918 emit_insn_before (gen_move_insn (temp, new_offset), object);
3919 else
3921 /* If that didn't work, replace this expression with a
3922 register containing the sum. */
3924 XEXP (x, 0) = old;
3925 new = gen_rtx_PLUS (Pmode, new, new_offset);
3927 start_sequence ();
3928 temp = force_operand (new, NULL_RTX);
3929 seq = get_insns ();
3930 end_sequence ();
3932 emit_insn_before (seq, object);
3933 if (! validate_change (object, loc, temp, 0)
3934 && ! validate_replace_rtx (x, temp, object))
3935 abort ();
3939 return 1;
3942 /* Fall through to generic two-operand expression case. */
3943 case EXPR_LIST:
3944 case CALL:
3945 case COMPARE:
3946 case MINUS:
3947 case MULT:
3948 case DIV: case UDIV:
3949 case MOD: case UMOD:
3950 case AND: case IOR: case XOR:
3951 case ROTATERT: case ROTATE:
3952 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
3953 case NE: case EQ:
3954 case GE: case GT: case GEU: case GTU:
3955 case LE: case LT: case LEU: case LTU:
3956 if (XEXP (x, 1) && ! CONSTANT_P (XEXP (x, 1)))
3957 instantiate_virtual_regs_1 (&XEXP (x, 1), object, extra_insns);
3958 loc = &XEXP (x, 0);
3959 goto restart;
3961 case MEM:
3962 /* Most cases of MEM that convert to valid addresses have already been
3963 handled by our scan of decls. The only special handling we
3964 need here is to make a copy of the rtx to ensure it isn't being
3965 shared if we have to change it to a pseudo.
3967 If the rtx is a simple reference to an address via a virtual register,
3968 it can potentially be shared. In such cases, first try to make it
3969 a valid address, which can also be shared. Otherwise, copy it and
3970 proceed normally.
3972 First check for common cases that need no processing. These are
3973 usually due to instantiation already being done on a previous instance
3974 of a shared rtx. */
3976 temp = XEXP (x, 0);
3977 if (CONSTANT_ADDRESS_P (temp)
3978 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3979 || temp == arg_pointer_rtx
3980 #endif
3981 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3982 || temp == hard_frame_pointer_rtx
3983 #endif
3984 || temp == frame_pointer_rtx)
3985 return 1;
3987 if (GET_CODE (temp) == PLUS
3988 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3989 && (XEXP (temp, 0) == frame_pointer_rtx
3990 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3991 || XEXP (temp, 0) == hard_frame_pointer_rtx
3992 #endif
3993 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3994 || XEXP (temp, 0) == arg_pointer_rtx
3995 #endif
3997 return 1;
3999 if (temp == virtual_stack_vars_rtx
4000 || temp == virtual_incoming_args_rtx
4001 || (GET_CODE (temp) == PLUS
4002 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
4003 && (XEXP (temp, 0) == virtual_stack_vars_rtx
4004 || XEXP (temp, 0) == virtual_incoming_args_rtx)))
4006 /* This MEM may be shared. If the substitution can be done without
4007 the need to generate new pseudos, we want to do it in place
4008 so all copies of the shared rtx benefit. The call below will
4009 only make substitutions if the resulting address is still
4010 valid.
4012 Note that we cannot pass X as the object in the recursive call
4013 since the insn being processed may not allow all valid
4014 addresses. However, if we were not passed on object, we can
4015 only modify X without copying it if X will have a valid
4016 address.
4018 ??? Also note that this can still lose if OBJECT is an insn that
4019 has less restrictions on an address that some other insn.
4020 In that case, we will modify the shared address. This case
4021 doesn't seem very likely, though. One case where this could
4022 happen is in the case of a USE or CLOBBER reference, but we
4023 take care of that below. */
4025 if (instantiate_virtual_regs_1 (&XEXP (x, 0),
4026 object ? object : x, 0))
4027 return 1;
4029 /* Otherwise make a copy and process that copy. We copy the entire
4030 RTL expression since it might be a PLUS which could also be
4031 shared. */
4032 *loc = x = copy_rtx (x);
4035 /* Fall through to generic unary operation case. */
4036 case PREFETCH:
4037 case SUBREG:
4038 case STRICT_LOW_PART:
4039 case NEG: case NOT:
4040 case PRE_DEC: case PRE_INC: case POST_DEC: case POST_INC:
4041 case SIGN_EXTEND: case ZERO_EXTEND:
4042 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
4043 case FLOAT: case FIX:
4044 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
4045 case ABS:
4046 case SQRT:
4047 case FFS:
4048 /* These case either have just one operand or we know that we need not
4049 check the rest of the operands. */
4050 loc = &XEXP (x, 0);
4051 goto restart;
4053 case USE:
4054 case CLOBBER:
4055 /* If the operand is a MEM, see if the change is a valid MEM. If not,
4056 go ahead and make the invalid one, but do it to a copy. For a REG,
4057 just make the recursive call, since there's no chance of a problem. */
4059 if ((GET_CODE (XEXP (x, 0)) == MEM
4060 && instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), XEXP (x, 0),
4062 || (GET_CODE (XEXP (x, 0)) == REG
4063 && instantiate_virtual_regs_1 (&XEXP (x, 0), object, 0)))
4064 return 1;
4066 XEXP (x, 0) = copy_rtx (XEXP (x, 0));
4067 loc = &XEXP (x, 0);
4068 goto restart;
4070 case REG:
4071 /* Try to replace with a PLUS. If that doesn't work, compute the sum
4072 in front of this insn and substitute the temporary. */
4073 if ((new = instantiate_new_reg (x, &offset)) != 0)
4075 temp = plus_constant (new, offset);
4076 if (!validate_change (object, loc, temp, 0))
4078 if (! extra_insns)
4079 return 0;
4081 start_sequence ();
4082 temp = force_operand (temp, NULL_RTX);
4083 seq = get_insns ();
4084 end_sequence ();
4086 emit_insn_before (seq, object);
4087 if (! validate_change (object, loc, temp, 0)
4088 && ! validate_replace_rtx (x, temp, object))
4089 abort ();
4093 return 1;
4095 case ADDRESSOF:
4096 if (GET_CODE (XEXP (x, 0)) == REG)
4097 return 1;
4099 else if (GET_CODE (XEXP (x, 0)) == MEM)
4101 /* If we have a (addressof (mem ..)), do any instantiation inside
4102 since we know we'll be making the inside valid when we finally
4103 remove the ADDRESSOF. */
4104 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), NULL_RTX, 0);
4105 return 1;
4107 break;
4109 default:
4110 break;
4113 /* Scan all subexpressions. */
4114 fmt = GET_RTX_FORMAT (code);
4115 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
4116 if (*fmt == 'e')
4118 if (!instantiate_virtual_regs_1 (&XEXP (x, i), object, extra_insns))
4119 return 0;
4121 else if (*fmt == 'E')
4122 for (j = 0; j < XVECLEN (x, i); j++)
4123 if (! instantiate_virtual_regs_1 (&XVECEXP (x, i, j), object,
4124 extra_insns))
4125 return 0;
4127 return 1;
4130 /* Optimization: assuming this function does not receive nonlocal gotos,
4131 delete the handlers for such, as well as the insns to establish
4132 and disestablish them. */
4134 static void
4135 delete_handlers ()
4137 rtx insn;
4138 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4140 /* Delete the handler by turning off the flag that would
4141 prevent jump_optimize from deleting it.
4142 Also permit deletion of the nonlocal labels themselves
4143 if nothing local refers to them. */
4144 if (GET_CODE (insn) == CODE_LABEL)
4146 tree t, last_t;
4148 LABEL_PRESERVE_P (insn) = 0;
4150 /* Remove it from the nonlocal_label list, to avoid confusing
4151 flow. */
4152 for (t = nonlocal_labels, last_t = 0; t;
4153 last_t = t, t = TREE_CHAIN (t))
4154 if (DECL_RTL (TREE_VALUE (t)) == insn)
4155 break;
4156 if (t)
4158 if (! last_t)
4159 nonlocal_labels = TREE_CHAIN (nonlocal_labels);
4160 else
4161 TREE_CHAIN (last_t) = TREE_CHAIN (t);
4164 if (GET_CODE (insn) == INSN)
4166 int can_delete = 0;
4167 rtx t;
4168 for (t = nonlocal_goto_handler_slots; t != 0; t = XEXP (t, 1))
4169 if (reg_mentioned_p (t, PATTERN (insn)))
4171 can_delete = 1;
4172 break;
4174 if (can_delete
4175 || (nonlocal_goto_stack_level != 0
4176 && reg_mentioned_p (nonlocal_goto_stack_level,
4177 PATTERN (insn))))
4178 delete_related_insns (insn);
4184 max_parm_reg_num ()
4186 return max_parm_reg;
4189 /* Return the first insn following those generated by `assign_parms'. */
4192 get_first_nonparm_insn ()
4194 if (last_parm_insn)
4195 return NEXT_INSN (last_parm_insn);
4196 return get_insns ();
4199 /* Return the first NOTE_INSN_BLOCK_BEG note in the function.
4200 Crash if there is none. */
4203 get_first_block_beg ()
4205 rtx searcher;
4206 rtx insn = get_first_nonparm_insn ();
4208 for (searcher = insn; searcher; searcher = NEXT_INSN (searcher))
4209 if (GET_CODE (searcher) == NOTE
4210 && NOTE_LINE_NUMBER (searcher) == NOTE_INSN_BLOCK_BEG)
4211 return searcher;
4213 abort (); /* Invalid call to this function. (See comments above.) */
4214 return NULL_RTX;
4217 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
4218 This means a type for which function calls must pass an address to the
4219 function or get an address back from the function.
4220 EXP may be a type node or an expression (whose type is tested). */
4223 aggregate_value_p (exp)
4224 tree exp;
4226 int i, regno, nregs;
4227 rtx reg;
4229 tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
4231 if (TREE_CODE (type) == VOID_TYPE)
4232 return 0;
4233 if (RETURN_IN_MEMORY (type))
4234 return 1;
4235 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
4236 and thus can't be returned in registers. */
4237 if (TREE_ADDRESSABLE (type))
4238 return 1;
4239 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
4240 return 1;
4241 /* Make sure we have suitable call-clobbered regs to return
4242 the value in; if not, we must return it in memory. */
4243 reg = hard_function_value (type, 0, 0);
4245 /* If we have something other than a REG (e.g. a PARALLEL), then assume
4246 it is OK. */
4247 if (GET_CODE (reg) != REG)
4248 return 0;
4250 regno = REGNO (reg);
4251 nregs = HARD_REGNO_NREGS (regno, TYPE_MODE (type));
4252 for (i = 0; i < nregs; i++)
4253 if (! call_used_regs[regno + i])
4254 return 1;
4255 return 0;
4258 /* Assign RTL expressions to the function's parameters.
4259 This may involve copying them into registers and using
4260 those registers as the RTL for them. */
4262 void
4263 assign_parms (fndecl)
4264 tree fndecl;
4266 tree parm;
4267 rtx entry_parm = 0;
4268 rtx stack_parm = 0;
4269 CUMULATIVE_ARGS args_so_far;
4270 enum machine_mode promoted_mode, passed_mode;
4271 enum machine_mode nominal_mode, promoted_nominal_mode;
4272 int unsignedp;
4273 /* Total space needed so far for args on the stack,
4274 given as a constant and a tree-expression. */
4275 struct args_size stack_args_size;
4276 tree fntype = TREE_TYPE (fndecl);
4277 tree fnargs = DECL_ARGUMENTS (fndecl);
4278 /* This is used for the arg pointer when referring to stack args. */
4279 rtx internal_arg_pointer;
4280 /* This is a dummy PARM_DECL that we used for the function result if
4281 the function returns a structure. */
4282 tree function_result_decl = 0;
4283 #ifdef SETUP_INCOMING_VARARGS
4284 int varargs_setup = 0;
4285 #endif
4286 rtx conversion_insns = 0;
4287 struct args_size alignment_pad;
4289 /* Nonzero if function takes extra anonymous args.
4290 This means the last named arg must be on the stack
4291 right before the anonymous ones. */
4292 int stdarg
4293 = (TYPE_ARG_TYPES (fntype) != 0
4294 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4295 != void_type_node));
4297 current_function_stdarg = stdarg;
4299 /* If the reg that the virtual arg pointer will be translated into is
4300 not a fixed reg or is the stack pointer, make a copy of the virtual
4301 arg pointer, and address parms via the copy. The frame pointer is
4302 considered fixed even though it is not marked as such.
4304 The second time through, simply use ap to avoid generating rtx. */
4306 if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
4307 || ! (fixed_regs[ARG_POINTER_REGNUM]
4308 || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM)))
4309 internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
4310 else
4311 internal_arg_pointer = virtual_incoming_args_rtx;
4312 current_function_internal_arg_pointer = internal_arg_pointer;
4314 stack_args_size.constant = 0;
4315 stack_args_size.var = 0;
4317 /* If struct value address is treated as the first argument, make it so. */
4318 if (aggregate_value_p (DECL_RESULT (fndecl))
4319 && ! current_function_returns_pcc_struct
4320 && struct_value_incoming_rtx == 0)
4322 tree type = build_pointer_type (TREE_TYPE (fntype));
4324 function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
4326 DECL_ARG_TYPE (function_result_decl) = type;
4327 TREE_CHAIN (function_result_decl) = fnargs;
4328 fnargs = function_result_decl;
4331 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
4332 parm_reg_stack_loc = (rtx *) ggc_alloc_cleared (max_parm_reg * sizeof (rtx));
4334 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
4335 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far, fntype, NULL_RTX);
4336 #else
4337 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, 0);
4338 #endif
4340 /* We haven't yet found an argument that we must push and pretend the
4341 caller did. */
4342 current_function_pretend_args_size = 0;
4344 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
4346 struct args_size stack_offset;
4347 struct args_size arg_size;
4348 int passed_pointer = 0;
4349 int did_conversion = 0;
4350 tree passed_type = DECL_ARG_TYPE (parm);
4351 tree nominal_type = TREE_TYPE (parm);
4352 int pretend_named;
4353 int last_named = 0, named_arg;
4355 /* Set LAST_NAMED if this is last named arg before last
4356 anonymous args. */
4357 if (stdarg)
4359 tree tem;
4361 for (tem = TREE_CHAIN (parm); tem; tem = TREE_CHAIN (tem))
4362 if (DECL_NAME (tem))
4363 break;
4365 if (tem == 0)
4366 last_named = 1;
4368 /* Set NAMED_ARG if this arg should be treated as a named arg. For
4369 most machines, if this is a varargs/stdarg function, then we treat
4370 the last named arg as if it were anonymous too. */
4371 named_arg = STRICT_ARGUMENT_NAMING ? 1 : ! last_named;
4373 if (TREE_TYPE (parm) == error_mark_node
4374 /* This can happen after weird syntax errors
4375 or if an enum type is defined among the parms. */
4376 || TREE_CODE (parm) != PARM_DECL
4377 || passed_type == NULL)
4379 SET_DECL_RTL (parm, gen_rtx_MEM (BLKmode, const0_rtx));
4380 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4381 TREE_USED (parm) = 1;
4382 continue;
4385 /* Find mode of arg as it is passed, and mode of arg
4386 as it should be during execution of this function. */
4387 passed_mode = TYPE_MODE (passed_type);
4388 nominal_mode = TYPE_MODE (nominal_type);
4390 /* If the parm's mode is VOID, its value doesn't matter,
4391 and avoid the usual things like emit_move_insn that could crash. */
4392 if (nominal_mode == VOIDmode)
4394 SET_DECL_RTL (parm, const0_rtx);
4395 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4396 continue;
4399 /* If the parm is to be passed as a transparent union, use the
4400 type of the first field for the tests below. We have already
4401 verified that the modes are the same. */
4402 if (DECL_TRANSPARENT_UNION (parm)
4403 || (TREE_CODE (passed_type) == UNION_TYPE
4404 && TYPE_TRANSPARENT_UNION (passed_type)))
4405 passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
4407 /* See if this arg was passed by invisible reference. It is if
4408 it is an object whose size depends on the contents of the
4409 object itself or if the machine requires these objects be passed
4410 that way. */
4412 if ((TREE_CODE (TYPE_SIZE (passed_type)) != INTEGER_CST
4413 && contains_placeholder_p (TYPE_SIZE (passed_type)))
4414 || TREE_ADDRESSABLE (passed_type)
4415 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
4416 || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, passed_mode,
4417 passed_type, named_arg)
4418 #endif
4421 passed_type = nominal_type = build_pointer_type (passed_type);
4422 passed_pointer = 1;
4423 passed_mode = nominal_mode = Pmode;
4425 /* See if the frontend wants to pass this by invisible reference. */
4426 else if (passed_type != nominal_type
4427 && POINTER_TYPE_P (passed_type)
4428 && TREE_TYPE (passed_type) == nominal_type)
4430 nominal_type = passed_type;
4431 passed_pointer = 1;
4432 passed_mode = nominal_mode = Pmode;
4435 promoted_mode = passed_mode;
4437 #ifdef PROMOTE_FUNCTION_ARGS
4438 /* Compute the mode in which the arg is actually extended to. */
4439 unsignedp = TREE_UNSIGNED (passed_type);
4440 promoted_mode = promote_mode (passed_type, promoted_mode, &unsignedp, 1);
4441 #endif
4443 /* Let machine desc say which reg (if any) the parm arrives in.
4444 0 means it arrives on the stack. */
4445 #ifdef FUNCTION_INCOMING_ARG
4446 entry_parm = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4447 passed_type, named_arg);
4448 #else
4449 entry_parm = FUNCTION_ARG (args_so_far, promoted_mode,
4450 passed_type, named_arg);
4451 #endif
4453 if (entry_parm == 0)
4454 promoted_mode = passed_mode;
4456 #ifdef SETUP_INCOMING_VARARGS
4457 /* If this is the last named parameter, do any required setup for
4458 varargs or stdargs. We need to know about the case of this being an
4459 addressable type, in which case we skip the registers it
4460 would have arrived in.
4462 For stdargs, LAST_NAMED will be set for two parameters, the one that
4463 is actually the last named, and the dummy parameter. We only
4464 want to do this action once.
4466 Also, indicate when RTL generation is to be suppressed. */
4467 if (last_named && !varargs_setup)
4469 SETUP_INCOMING_VARARGS (args_so_far, promoted_mode, passed_type,
4470 current_function_pretend_args_size, 0);
4471 varargs_setup = 1;
4473 #endif
4475 /* Determine parm's home in the stack,
4476 in case it arrives in the stack or we should pretend it did.
4478 Compute the stack position and rtx where the argument arrives
4479 and its size.
4481 There is one complexity here: If this was a parameter that would
4482 have been passed in registers, but wasn't only because it is
4483 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
4484 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
4485 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of
4486 0 as it was the previous time. */
4488 pretend_named = named_arg || PRETEND_OUTGOING_VARARGS_NAMED;
4489 locate_and_pad_parm (promoted_mode, passed_type,
4490 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4492 #else
4493 #ifdef FUNCTION_INCOMING_ARG
4494 FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4495 passed_type,
4496 pretend_named) != 0,
4497 #else
4498 FUNCTION_ARG (args_so_far, promoted_mode,
4499 passed_type,
4500 pretend_named) != 0,
4501 #endif
4502 #endif
4503 fndecl, &stack_args_size, &stack_offset, &arg_size,
4504 &alignment_pad);
4507 rtx offset_rtx = ARGS_SIZE_RTX (stack_offset);
4509 if (offset_rtx == const0_rtx)
4510 stack_parm = gen_rtx_MEM (promoted_mode, internal_arg_pointer);
4511 else
4512 stack_parm = gen_rtx_MEM (promoted_mode,
4513 gen_rtx_PLUS (Pmode,
4514 internal_arg_pointer,
4515 offset_rtx));
4517 set_mem_attributes (stack_parm, parm, 1);
4520 /* If this parameter was passed both in registers and in the stack,
4521 use the copy on the stack. */
4522 if (MUST_PASS_IN_STACK (promoted_mode, passed_type))
4523 entry_parm = 0;
4525 #ifdef FUNCTION_ARG_PARTIAL_NREGS
4526 /* If this parm was passed part in regs and part in memory,
4527 pretend it arrived entirely in memory
4528 by pushing the register-part onto the stack.
4530 In the special case of a DImode or DFmode that is split,
4531 we could put it together in a pseudoreg directly,
4532 but for now that's not worth bothering with. */
4534 if (entry_parm)
4536 int nregs = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, promoted_mode,
4537 passed_type, named_arg);
4539 if (nregs > 0)
4541 current_function_pretend_args_size
4542 = (((nregs * UNITS_PER_WORD) + (PARM_BOUNDARY / BITS_PER_UNIT) - 1)
4543 / (PARM_BOUNDARY / BITS_PER_UNIT)
4544 * (PARM_BOUNDARY / BITS_PER_UNIT));
4546 /* Handle calls that pass values in multiple non-contiguous
4547 locations. The Irix 6 ABI has examples of this. */
4548 if (GET_CODE (entry_parm) == PARALLEL)
4549 emit_group_store (validize_mem (stack_parm), entry_parm,
4550 int_size_in_bytes (TREE_TYPE (parm)));
4552 else
4553 move_block_from_reg (REGNO (entry_parm),
4554 validize_mem (stack_parm), nregs,
4555 int_size_in_bytes (TREE_TYPE (parm)));
4557 entry_parm = stack_parm;
4560 #endif
4562 /* If we didn't decide this parm came in a register,
4563 by default it came on the stack. */
4564 if (entry_parm == 0)
4565 entry_parm = stack_parm;
4567 /* Record permanently how this parm was passed. */
4568 DECL_INCOMING_RTL (parm) = entry_parm;
4570 /* If there is actually space on the stack for this parm,
4571 count it in stack_args_size; otherwise set stack_parm to 0
4572 to indicate there is no preallocated stack slot for the parm. */
4574 if (entry_parm == stack_parm
4575 || (GET_CODE (entry_parm) == PARALLEL
4576 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
4577 #if defined (REG_PARM_STACK_SPACE) && ! defined (MAYBE_REG_PARM_STACK_SPACE)
4578 /* On some machines, even if a parm value arrives in a register
4579 there is still an (uninitialized) stack slot allocated for it.
4581 ??? When MAYBE_REG_PARM_STACK_SPACE is defined, we can't tell
4582 whether this parameter already has a stack slot allocated,
4583 because an arg block exists only if current_function_args_size
4584 is larger than some threshold, and we haven't calculated that
4585 yet. So, for now, we just assume that stack slots never exist
4586 in this case. */
4587 || REG_PARM_STACK_SPACE (fndecl) > 0
4588 #endif
4591 stack_args_size.constant += arg_size.constant;
4592 if (arg_size.var)
4593 ADD_PARM_SIZE (stack_args_size, arg_size.var);
4595 else
4596 /* No stack slot was pushed for this parm. */
4597 stack_parm = 0;
4599 /* Update info on where next arg arrives in registers. */
4601 FUNCTION_ARG_ADVANCE (args_so_far, promoted_mode,
4602 passed_type, named_arg);
4604 /* If we can't trust the parm stack slot to be aligned enough
4605 for its ultimate type, don't use that slot after entry.
4606 We'll make another stack slot, if we need one. */
4608 unsigned int thisparm_boundary
4609 = FUNCTION_ARG_BOUNDARY (promoted_mode, passed_type);
4611 if (GET_MODE_ALIGNMENT (nominal_mode) > thisparm_boundary)
4612 stack_parm = 0;
4615 /* If parm was passed in memory, and we need to convert it on entry,
4616 don't store it back in that same slot. */
4617 if (entry_parm != 0
4618 && nominal_mode != BLKmode && nominal_mode != passed_mode)
4619 stack_parm = 0;
4621 /* When an argument is passed in multiple locations, we can't
4622 make use of this information, but we can save some copying if
4623 the whole argument is passed in a single register. */
4624 if (GET_CODE (entry_parm) == PARALLEL
4625 && nominal_mode != BLKmode && passed_mode != BLKmode)
4627 int i, len = XVECLEN (entry_parm, 0);
4629 for (i = 0; i < len; i++)
4630 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
4631 && GET_CODE (XEXP (XVECEXP (entry_parm, 0, i), 0)) == REG
4632 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
4633 == passed_mode)
4634 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
4636 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
4637 DECL_INCOMING_RTL (parm) = entry_parm;
4638 break;
4642 /* ENTRY_PARM is an RTX for the parameter as it arrives,
4643 in the mode in which it arrives.
4644 STACK_PARM is an RTX for a stack slot where the parameter can live
4645 during the function (in case we want to put it there).
4646 STACK_PARM is 0 if no stack slot was pushed for it.
4648 Now output code if necessary to convert ENTRY_PARM to
4649 the type in which this function declares it,
4650 and store that result in an appropriate place,
4651 which may be a pseudo reg, may be STACK_PARM,
4652 or may be a local stack slot if STACK_PARM is 0.
4654 Set DECL_RTL to that place. */
4656 if (nominal_mode == BLKmode || GET_CODE (entry_parm) == PARALLEL)
4658 /* If a BLKmode arrives in registers, copy it to a stack slot.
4659 Handle calls that pass values in multiple non-contiguous
4660 locations. The Irix 6 ABI has examples of this. */
4661 if (GET_CODE (entry_parm) == REG
4662 || GET_CODE (entry_parm) == PARALLEL)
4664 int size_stored
4665 = CEIL_ROUND (int_size_in_bytes (TREE_TYPE (parm)),
4666 UNITS_PER_WORD);
4668 /* Note that we will be storing an integral number of words.
4669 So we have to be careful to ensure that we allocate an
4670 integral number of words. We do this below in the
4671 assign_stack_local if space was not allocated in the argument
4672 list. If it was, this will not work if PARM_BOUNDARY is not
4673 a multiple of BITS_PER_WORD. It isn't clear how to fix this
4674 if it becomes a problem. */
4676 if (stack_parm == 0)
4678 stack_parm
4679 = assign_stack_local (GET_MODE (entry_parm),
4680 size_stored, 0);
4681 set_mem_attributes (stack_parm, parm, 1);
4684 else if (PARM_BOUNDARY % BITS_PER_WORD != 0)
4685 abort ();
4687 /* Handle calls that pass values in multiple non-contiguous
4688 locations. The Irix 6 ABI has examples of this. */
4689 if (GET_CODE (entry_parm) == PARALLEL)
4690 emit_group_store (validize_mem (stack_parm), entry_parm,
4691 int_size_in_bytes (TREE_TYPE (parm)));
4692 else
4693 move_block_from_reg (REGNO (entry_parm),
4694 validize_mem (stack_parm),
4695 size_stored / UNITS_PER_WORD,
4696 int_size_in_bytes (TREE_TYPE (parm)));
4698 SET_DECL_RTL (parm, stack_parm);
4700 else if (! ((! optimize
4701 && ! DECL_REGISTER (parm))
4702 || TREE_SIDE_EFFECTS (parm)
4703 /* If -ffloat-store specified, don't put explicit
4704 float variables into registers. */
4705 || (flag_float_store
4706 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
4707 /* Always assign pseudo to structure return or item passed
4708 by invisible reference. */
4709 || passed_pointer || parm == function_result_decl)
4711 /* Store the parm in a pseudoregister during the function, but we
4712 may need to do it in a wider mode. */
4714 rtx parmreg;
4715 unsigned int regno, regnoi = 0, regnor = 0;
4717 unsignedp = TREE_UNSIGNED (TREE_TYPE (parm));
4719 promoted_nominal_mode
4720 = promote_mode (TREE_TYPE (parm), nominal_mode, &unsignedp, 0);
4722 parmreg = gen_reg_rtx (promoted_nominal_mode);
4723 mark_user_reg (parmreg);
4725 /* If this was an item that we received a pointer to, set DECL_RTL
4726 appropriately. */
4727 if (passed_pointer)
4729 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (passed_type)),
4730 parmreg);
4731 set_mem_attributes (x, parm, 1);
4732 SET_DECL_RTL (parm, x);
4734 else
4736 SET_DECL_RTL (parm, parmreg);
4737 maybe_set_unchanging (DECL_RTL (parm), parm);
4740 /* Copy the value into the register. */
4741 if (nominal_mode != passed_mode
4742 || promoted_nominal_mode != promoted_mode)
4744 int save_tree_used;
4745 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
4746 mode, by the caller. We now have to convert it to
4747 NOMINAL_MODE, if different. However, PARMREG may be in
4748 a different mode than NOMINAL_MODE if it is being stored
4749 promoted.
4751 If ENTRY_PARM is a hard register, it might be in a register
4752 not valid for operating in its mode (e.g., an odd-numbered
4753 register for a DFmode). In that case, moves are the only
4754 thing valid, so we can't do a convert from there. This
4755 occurs when the calling sequence allow such misaligned
4756 usages.
4758 In addition, the conversion may involve a call, which could
4759 clobber parameters which haven't been copied to pseudo
4760 registers yet. Therefore, we must first copy the parm to
4761 a pseudo reg here, and save the conversion until after all
4762 parameters have been moved. */
4764 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4766 emit_move_insn (tempreg, validize_mem (entry_parm));
4768 push_to_sequence (conversion_insns);
4769 tempreg = convert_to_mode (nominal_mode, tempreg, unsignedp);
4771 if (GET_CODE (tempreg) == SUBREG
4772 && GET_MODE (tempreg) == nominal_mode
4773 && GET_CODE (SUBREG_REG (tempreg)) == REG
4774 && nominal_mode == passed_mode
4775 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (entry_parm)
4776 && GET_MODE_SIZE (GET_MODE (tempreg))
4777 < GET_MODE_SIZE (GET_MODE (entry_parm)))
4779 /* The argument is already sign/zero extended, so note it
4780 into the subreg. */
4781 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
4782 SUBREG_PROMOTED_UNSIGNED_SET (tempreg, unsignedp);
4785 /* TREE_USED gets set erroneously during expand_assignment. */
4786 save_tree_used = TREE_USED (parm);
4787 expand_assignment (parm,
4788 make_tree (nominal_type, tempreg), 0, 0);
4789 TREE_USED (parm) = save_tree_used;
4790 conversion_insns = get_insns ();
4791 did_conversion = 1;
4792 end_sequence ();
4794 else
4795 emit_move_insn (parmreg, validize_mem (entry_parm));
4797 /* If we were passed a pointer but the actual value
4798 can safely live in a register, put it in one. */
4799 if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
4800 /* If by-reference argument was promoted, demote it. */
4801 && (TYPE_MODE (TREE_TYPE (parm)) != GET_MODE (DECL_RTL (parm))
4802 || ! ((! optimize
4803 && ! DECL_REGISTER (parm))
4804 || TREE_SIDE_EFFECTS (parm)
4805 /* If -ffloat-store specified, don't put explicit
4806 float variables into registers. */
4807 || (flag_float_store
4808 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))))
4810 /* We can't use nominal_mode, because it will have been set to
4811 Pmode above. We must use the actual mode of the parm. */
4812 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
4813 mark_user_reg (parmreg);
4814 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
4816 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
4817 int unsigned_p = TREE_UNSIGNED (TREE_TYPE (parm));
4818 push_to_sequence (conversion_insns);
4819 emit_move_insn (tempreg, DECL_RTL (parm));
4820 SET_DECL_RTL (parm,
4821 convert_to_mode (GET_MODE (parmreg),
4822 tempreg,
4823 unsigned_p));
4824 emit_move_insn (parmreg, DECL_RTL (parm));
4825 conversion_insns = get_insns();
4826 did_conversion = 1;
4827 end_sequence ();
4829 else
4830 emit_move_insn (parmreg, DECL_RTL (parm));
4831 SET_DECL_RTL (parm, parmreg);
4832 /* STACK_PARM is the pointer, not the parm, and PARMREG is
4833 now the parm. */
4834 stack_parm = 0;
4836 #ifdef FUNCTION_ARG_CALLEE_COPIES
4837 /* If we are passed an arg by reference and it is our responsibility
4838 to make a copy, do it now.
4839 PASSED_TYPE and PASSED mode now refer to the pointer, not the
4840 original argument, so we must recreate them in the call to
4841 FUNCTION_ARG_CALLEE_COPIES. */
4842 /* ??? Later add code to handle the case that if the argument isn't
4843 modified, don't do the copy. */
4845 else if (passed_pointer
4846 && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
4847 TYPE_MODE (DECL_ARG_TYPE (parm)),
4848 DECL_ARG_TYPE (parm),
4849 named_arg)
4850 && ! TREE_ADDRESSABLE (DECL_ARG_TYPE (parm)))
4852 rtx copy;
4853 tree type = DECL_ARG_TYPE (parm);
4855 /* This sequence may involve a library call perhaps clobbering
4856 registers that haven't been copied to pseudos yet. */
4858 push_to_sequence (conversion_insns);
4860 if (!COMPLETE_TYPE_P (type)
4861 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4862 /* This is a variable sized object. */
4863 copy = gen_rtx_MEM (BLKmode,
4864 allocate_dynamic_stack_space
4865 (expr_size (parm), NULL_RTX,
4866 TYPE_ALIGN (type)));
4867 else
4868 copy = assign_stack_temp (TYPE_MODE (type),
4869 int_size_in_bytes (type), 1);
4870 set_mem_attributes (copy, parm, 1);
4872 store_expr (parm, copy, 0);
4873 emit_move_insn (parmreg, XEXP (copy, 0));
4874 conversion_insns = get_insns ();
4875 did_conversion = 1;
4876 end_sequence ();
4878 #endif /* FUNCTION_ARG_CALLEE_COPIES */
4880 /* In any case, record the parm's desired stack location
4881 in case we later discover it must live in the stack.
4883 If it is a COMPLEX value, store the stack location for both
4884 halves. */
4886 if (GET_CODE (parmreg) == CONCAT)
4887 regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
4888 else
4889 regno = REGNO (parmreg);
4891 if (regno >= max_parm_reg)
4893 rtx *new;
4894 int old_max_parm_reg = max_parm_reg;
4896 /* It's slow to expand this one register at a time,
4897 but it's also rare and we need max_parm_reg to be
4898 precisely correct. */
4899 max_parm_reg = regno + 1;
4900 new = (rtx *) ggc_realloc (parm_reg_stack_loc,
4901 max_parm_reg * sizeof (rtx));
4902 memset ((char *) (new + old_max_parm_reg), 0,
4903 (max_parm_reg - old_max_parm_reg) * sizeof (rtx));
4904 parm_reg_stack_loc = new;
4907 if (GET_CODE (parmreg) == CONCAT)
4909 enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
4911 regnor = REGNO (gen_realpart (submode, parmreg));
4912 regnoi = REGNO (gen_imagpart (submode, parmreg));
4914 if (stack_parm != 0)
4916 parm_reg_stack_loc[regnor]
4917 = gen_realpart (submode, stack_parm);
4918 parm_reg_stack_loc[regnoi]
4919 = gen_imagpart (submode, stack_parm);
4921 else
4923 parm_reg_stack_loc[regnor] = 0;
4924 parm_reg_stack_loc[regnoi] = 0;
4927 else
4928 parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
4930 /* Mark the register as eliminable if we did no conversion
4931 and it was copied from memory at a fixed offset,
4932 and the arg pointer was not copied to a pseudo-reg.
4933 If the arg pointer is a pseudo reg or the offset formed
4934 an invalid address, such memory-equivalences
4935 as we make here would screw up life analysis for it. */
4936 if (nominal_mode == passed_mode
4937 && ! did_conversion
4938 && stack_parm != 0
4939 && GET_CODE (stack_parm) == MEM
4940 && stack_offset.var == 0
4941 && reg_mentioned_p (virtual_incoming_args_rtx,
4942 XEXP (stack_parm, 0)))
4944 rtx linsn = get_last_insn ();
4945 rtx sinsn, set;
4947 /* Mark complex types separately. */
4948 if (GET_CODE (parmreg) == CONCAT)
4949 /* Scan backwards for the set of the real and
4950 imaginary parts. */
4951 for (sinsn = linsn; sinsn != 0;
4952 sinsn = prev_nonnote_insn (sinsn))
4954 set = single_set (sinsn);
4955 if (set != 0
4956 && SET_DEST (set) == regno_reg_rtx [regnoi])
4957 REG_NOTES (sinsn)
4958 = gen_rtx_EXPR_LIST (REG_EQUIV,
4959 parm_reg_stack_loc[regnoi],
4960 REG_NOTES (sinsn));
4961 else if (set != 0
4962 && SET_DEST (set) == regno_reg_rtx [regnor])
4963 REG_NOTES (sinsn)
4964 = gen_rtx_EXPR_LIST (REG_EQUIV,
4965 parm_reg_stack_loc[regnor],
4966 REG_NOTES (sinsn));
4968 else if ((set = single_set (linsn)) != 0
4969 && SET_DEST (set) == parmreg)
4970 REG_NOTES (linsn)
4971 = gen_rtx_EXPR_LIST (REG_EQUIV,
4972 stack_parm, REG_NOTES (linsn));
4975 /* For pointer data type, suggest pointer register. */
4976 if (POINTER_TYPE_P (TREE_TYPE (parm)))
4977 mark_reg_pointer (parmreg,
4978 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4980 /* If something wants our address, try to use ADDRESSOF. */
4981 if (TREE_ADDRESSABLE (parm))
4983 /* If we end up putting something into the stack,
4984 fixup_var_refs_insns will need to make a pass over
4985 all the instructions. It looks through the pending
4986 sequences -- but it can't see the ones in the
4987 CONVERSION_INSNS, if they're not on the sequence
4988 stack. So, we go back to that sequence, just so that
4989 the fixups will happen. */
4990 push_to_sequence (conversion_insns);
4991 put_var_into_stack (parm);
4992 conversion_insns = get_insns ();
4993 end_sequence ();
4996 else
4998 /* Value must be stored in the stack slot STACK_PARM
4999 during function execution. */
5001 if (promoted_mode != nominal_mode)
5003 /* Conversion is required. */
5004 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
5006 emit_move_insn (tempreg, validize_mem (entry_parm));
5008 push_to_sequence (conversion_insns);
5009 entry_parm = convert_to_mode (nominal_mode, tempreg,
5010 TREE_UNSIGNED (TREE_TYPE (parm)));
5011 if (stack_parm)
5012 /* ??? This may need a big-endian conversion on sparc64. */
5013 stack_parm = adjust_address (stack_parm, nominal_mode, 0);
5015 conversion_insns = get_insns ();
5016 did_conversion = 1;
5017 end_sequence ();
5020 if (entry_parm != stack_parm)
5022 if (stack_parm == 0)
5024 stack_parm
5025 = assign_stack_local (GET_MODE (entry_parm),
5026 GET_MODE_SIZE (GET_MODE (entry_parm)), 0);
5027 set_mem_attributes (stack_parm, parm, 1);
5030 if (promoted_mode != nominal_mode)
5032 push_to_sequence (conversion_insns);
5033 emit_move_insn (validize_mem (stack_parm),
5034 validize_mem (entry_parm));
5035 conversion_insns = get_insns ();
5036 end_sequence ();
5038 else
5039 emit_move_insn (validize_mem (stack_parm),
5040 validize_mem (entry_parm));
5043 SET_DECL_RTL (parm, stack_parm);
5046 /* If this "parameter" was the place where we are receiving the
5047 function's incoming structure pointer, set up the result. */
5048 if (parm == function_result_decl)
5050 tree result = DECL_RESULT (fndecl);
5051 rtx addr = DECL_RTL (parm);
5052 rtx x;
5054 #ifdef POINTERS_EXTEND_UNSIGNED
5055 if (GET_MODE (addr) != Pmode)
5056 addr = convert_memory_address (Pmode, addr);
5057 #endif
5059 x = gen_rtx_MEM (DECL_MODE (result), addr);
5060 set_mem_attributes (x, result, 1);
5061 SET_DECL_RTL (result, x);
5064 if (GET_CODE (DECL_RTL (parm)) == REG)
5065 REGNO_DECL (REGNO (DECL_RTL (parm))) = parm;
5066 else if (GET_CODE (DECL_RTL (parm)) == CONCAT)
5068 REGNO_DECL (REGNO (XEXP (DECL_RTL (parm), 0))) = parm;
5069 REGNO_DECL (REGNO (XEXP (DECL_RTL (parm), 1))) = parm;
5074 /* Output all parameter conversion instructions (possibly including calls)
5075 now that all parameters have been copied out of hard registers. */
5076 emit_insn (conversion_insns);
5078 last_parm_insn = get_last_insn ();
5080 current_function_args_size = stack_args_size.constant;
5082 /* Adjust function incoming argument size for alignment and
5083 minimum length. */
5085 #ifdef REG_PARM_STACK_SPACE
5086 #ifndef MAYBE_REG_PARM_STACK_SPACE
5087 current_function_args_size = MAX (current_function_args_size,
5088 REG_PARM_STACK_SPACE (fndecl));
5089 #endif
5090 #endif
5092 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
5094 current_function_args_size
5095 = ((current_function_args_size + STACK_BYTES - 1)
5096 / STACK_BYTES) * STACK_BYTES;
5098 #ifdef ARGS_GROW_DOWNWARD
5099 current_function_arg_offset_rtx
5100 = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
5101 : expand_expr (size_diffop (stack_args_size.var,
5102 size_int (-stack_args_size.constant)),
5103 NULL_RTX, VOIDmode, 0));
5104 #else
5105 current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
5106 #endif
5108 /* See how many bytes, if any, of its args a function should try to pop
5109 on return. */
5111 current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
5112 current_function_args_size);
5114 /* For stdarg.h function, save info about
5115 regs and stack space used by the named args. */
5117 current_function_args_info = args_so_far;
5119 /* Set the rtx used for the function return value. Put this in its
5120 own variable so any optimizers that need this information don't have
5121 to include tree.h. Do this here so it gets done when an inlined
5122 function gets output. */
5124 current_function_return_rtx
5125 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
5126 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
5128 /* If scalar return value was computed in a pseudo-reg, or was a named
5129 return value that got dumped to the stack, copy that to the hard
5130 return register. */
5131 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
5133 tree decl_result = DECL_RESULT (fndecl);
5134 rtx decl_rtl = DECL_RTL (decl_result);
5136 if (REG_P (decl_rtl)
5137 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5138 : DECL_REGISTER (decl_result))
5140 rtx real_decl_rtl;
5142 #ifdef FUNCTION_OUTGOING_VALUE
5143 real_decl_rtl = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl_result),
5144 fndecl);
5145 #else
5146 real_decl_rtl = FUNCTION_VALUE (TREE_TYPE (decl_result),
5147 fndecl);
5148 #endif
5149 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
5150 /* The delay slot scheduler assumes that current_function_return_rtx
5151 holds the hard register containing the return value, not a
5152 temporary pseudo. */
5153 current_function_return_rtx = real_decl_rtl;
5158 /* Indicate whether REGNO is an incoming argument to the current function
5159 that was promoted to a wider mode. If so, return the RTX for the
5160 register (to get its mode). PMODE and PUNSIGNEDP are set to the mode
5161 that REGNO is promoted from and whether the promotion was signed or
5162 unsigned. */
5164 #ifdef PROMOTE_FUNCTION_ARGS
5167 promoted_input_arg (regno, pmode, punsignedp)
5168 unsigned int regno;
5169 enum machine_mode *pmode;
5170 int *punsignedp;
5172 tree arg;
5174 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
5175 arg = TREE_CHAIN (arg))
5176 if (GET_CODE (DECL_INCOMING_RTL (arg)) == REG
5177 && REGNO (DECL_INCOMING_RTL (arg)) == regno
5178 && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
5180 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
5181 int unsignedp = TREE_UNSIGNED (TREE_TYPE (arg));
5183 mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
5184 if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
5185 && mode != DECL_MODE (arg))
5187 *pmode = DECL_MODE (arg);
5188 *punsignedp = unsignedp;
5189 return DECL_INCOMING_RTL (arg);
5193 return 0;
5196 #endif
5198 /* Compute the size and offset from the start of the stacked arguments for a
5199 parm passed in mode PASSED_MODE and with type TYPE.
5201 INITIAL_OFFSET_PTR points to the current offset into the stacked
5202 arguments.
5204 The starting offset and size for this parm are returned in *OFFSET_PTR
5205 and *ARG_SIZE_PTR, respectively.
5207 IN_REGS is non-zero if the argument will be passed in registers. It will
5208 never be set if REG_PARM_STACK_SPACE is not defined.
5210 FNDECL is the function in which the argument was defined.
5212 There are two types of rounding that are done. The first, controlled by
5213 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
5214 list to be aligned to the specific boundary (in bits). This rounding
5215 affects the initial and starting offsets, but not the argument size.
5217 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
5218 optionally rounds the size of the parm to PARM_BOUNDARY. The
5219 initial offset is not affected by this rounding, while the size always
5220 is and the starting offset may be. */
5222 /* offset_ptr will be negative for ARGS_GROW_DOWNWARD case;
5223 initial_offset_ptr is positive because locate_and_pad_parm's
5224 callers pass in the total size of args so far as
5225 initial_offset_ptr. arg_size_ptr is always positive. */
5227 void
5228 locate_and_pad_parm (passed_mode, type, in_regs, fndecl,
5229 initial_offset_ptr, offset_ptr, arg_size_ptr,
5230 alignment_pad)
5231 enum machine_mode passed_mode;
5232 tree type;
5233 int in_regs ATTRIBUTE_UNUSED;
5234 tree fndecl ATTRIBUTE_UNUSED;
5235 struct args_size *initial_offset_ptr;
5236 struct args_size *offset_ptr;
5237 struct args_size *arg_size_ptr;
5238 struct args_size *alignment_pad;
5241 tree sizetree
5242 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
5243 enum direction where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
5244 int boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
5245 #ifdef ARGS_GROW_DOWNWARD
5246 tree s2 = sizetree;
5247 #endif
5249 #ifdef REG_PARM_STACK_SPACE
5250 /* If we have found a stack parm before we reach the end of the
5251 area reserved for registers, skip that area. */
5252 if (! in_regs)
5254 int reg_parm_stack_space = 0;
5256 #ifdef MAYBE_REG_PARM_STACK_SPACE
5257 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
5258 #else
5259 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
5260 #endif
5261 if (reg_parm_stack_space > 0)
5263 if (initial_offset_ptr->var)
5265 initial_offset_ptr->var
5266 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
5267 ssize_int (reg_parm_stack_space));
5268 initial_offset_ptr->constant = 0;
5270 else if (initial_offset_ptr->constant < reg_parm_stack_space)
5271 initial_offset_ptr->constant = reg_parm_stack_space;
5274 #endif /* REG_PARM_STACK_SPACE */
5276 arg_size_ptr->var = 0;
5277 arg_size_ptr->constant = 0;
5278 alignment_pad->var = 0;
5279 alignment_pad->constant = 0;
5281 #ifdef ARGS_GROW_DOWNWARD
5282 if (initial_offset_ptr->var)
5284 offset_ptr->constant = 0;
5285 offset_ptr->var = size_binop (MINUS_EXPR, ssize_int (0),
5286 initial_offset_ptr->var);
5288 else
5290 offset_ptr->constant = -initial_offset_ptr->constant;
5291 offset_ptr->var = 0;
5294 if (where_pad != none
5295 && (!host_integerp (sizetree, 1)
5296 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5297 s2 = round_up (s2, PARM_BOUNDARY / BITS_PER_UNIT);
5298 SUB_PARM_SIZE (*offset_ptr, s2);
5300 if (!in_regs
5301 #ifdef REG_PARM_STACK_SPACE
5302 || REG_PARM_STACK_SPACE (fndecl) > 0
5303 #endif
5305 pad_to_arg_alignment (offset_ptr, boundary, alignment_pad);
5307 if (initial_offset_ptr->var)
5308 arg_size_ptr->var = size_binop (MINUS_EXPR,
5309 size_binop (MINUS_EXPR,
5310 ssize_int (0),
5311 initial_offset_ptr->var),
5312 offset_ptr->var);
5314 else
5315 arg_size_ptr->constant = (-initial_offset_ptr->constant
5316 - offset_ptr->constant);
5318 /* Pad_below needs the pre-rounded size to know how much to pad below.
5319 We only pad parameters which are not in registers as they have their
5320 padding done elsewhere. */
5321 if (where_pad == downward
5322 && !in_regs)
5323 pad_below (offset_ptr, passed_mode, sizetree);
5325 #else /* !ARGS_GROW_DOWNWARD */
5326 if (!in_regs
5327 #ifdef REG_PARM_STACK_SPACE
5328 || REG_PARM_STACK_SPACE (fndecl) > 0
5329 #endif
5331 pad_to_arg_alignment (initial_offset_ptr, boundary, alignment_pad);
5332 *offset_ptr = *initial_offset_ptr;
5334 #ifdef PUSH_ROUNDING
5335 if (passed_mode != BLKmode)
5336 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
5337 #endif
5339 /* Pad_below needs the pre-rounded size to know how much to pad below
5340 so this must be done before rounding up. */
5341 if (where_pad == downward
5342 /* However, BLKmode args passed in regs have their padding done elsewhere.
5343 The stack slot must be able to hold the entire register. */
5344 && !(in_regs && passed_mode == BLKmode))
5345 pad_below (offset_ptr, passed_mode, sizetree);
5347 if (where_pad != none
5348 && (!host_integerp (sizetree, 1)
5349 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5350 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5352 ADD_PARM_SIZE (*arg_size_ptr, sizetree);
5353 #endif /* ARGS_GROW_DOWNWARD */
5356 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
5357 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
5359 static void
5360 pad_to_arg_alignment (offset_ptr, boundary, alignment_pad)
5361 struct args_size *offset_ptr;
5362 int boundary;
5363 struct args_size *alignment_pad;
5365 tree save_var = NULL_TREE;
5366 HOST_WIDE_INT save_constant = 0;
5368 int boundary_in_bytes = boundary / BITS_PER_UNIT;
5370 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5372 save_var = offset_ptr->var;
5373 save_constant = offset_ptr->constant;
5376 alignment_pad->var = NULL_TREE;
5377 alignment_pad->constant = 0;
5379 if (boundary > BITS_PER_UNIT)
5381 if (offset_ptr->var)
5383 offset_ptr->var =
5384 #ifdef ARGS_GROW_DOWNWARD
5385 round_down
5386 #else
5387 round_up
5388 #endif
5389 (ARGS_SIZE_TREE (*offset_ptr),
5390 boundary / BITS_PER_UNIT);
5391 offset_ptr->constant = 0; /*?*/
5392 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5393 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
5394 save_var);
5396 else
5398 offset_ptr->constant =
5399 #ifdef ARGS_GROW_DOWNWARD
5400 FLOOR_ROUND (offset_ptr->constant, boundary_in_bytes);
5401 #else
5402 CEIL_ROUND (offset_ptr->constant, boundary_in_bytes);
5403 #endif
5404 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5405 alignment_pad->constant = offset_ptr->constant - save_constant;
5410 static void
5411 pad_below (offset_ptr, passed_mode, sizetree)
5412 struct args_size *offset_ptr;
5413 enum machine_mode passed_mode;
5414 tree sizetree;
5416 if (passed_mode != BLKmode)
5418 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
5419 offset_ptr->constant
5420 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
5421 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
5422 - GET_MODE_SIZE (passed_mode));
5424 else
5426 if (TREE_CODE (sizetree) != INTEGER_CST
5427 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
5429 /* Round the size up to multiple of PARM_BOUNDARY bits. */
5430 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5431 /* Add it in. */
5432 ADD_PARM_SIZE (*offset_ptr, s2);
5433 SUB_PARM_SIZE (*offset_ptr, sizetree);
5438 /* Walk the tree of blocks describing the binding levels within a function
5439 and warn about uninitialized variables.
5440 This is done after calling flow_analysis and before global_alloc
5441 clobbers the pseudo-regs to hard regs. */
5443 void
5444 uninitialized_vars_warning (block)
5445 tree block;
5447 tree decl, sub;
5448 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5450 if (warn_uninitialized
5451 && TREE_CODE (decl) == VAR_DECL
5452 /* These warnings are unreliable for and aggregates
5453 because assigning the fields one by one can fail to convince
5454 flow.c that the entire aggregate was initialized.
5455 Unions are troublesome because members may be shorter. */
5456 && ! AGGREGATE_TYPE_P (TREE_TYPE (decl))
5457 && DECL_RTL (decl) != 0
5458 && GET_CODE (DECL_RTL (decl)) == REG
5459 /* Global optimizations can make it difficult to determine if a
5460 particular variable has been initialized. However, a VAR_DECL
5461 with a nonzero DECL_INITIAL had an initializer, so do not
5462 claim it is potentially uninitialized.
5464 We do not care about the actual value in DECL_INITIAL, so we do
5465 not worry that it may be a dangling pointer. */
5466 && DECL_INITIAL (decl) == NULL_TREE
5467 && regno_uninitialized (REGNO (DECL_RTL (decl))))
5468 warning_with_decl (decl,
5469 "`%s' might be used uninitialized in this function");
5470 if (extra_warnings
5471 && TREE_CODE (decl) == VAR_DECL
5472 && DECL_RTL (decl) != 0
5473 && GET_CODE (DECL_RTL (decl)) == REG
5474 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5475 warning_with_decl (decl,
5476 "variable `%s' might be clobbered by `longjmp' or `vfork'");
5478 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5479 uninitialized_vars_warning (sub);
5482 /* Do the appropriate part of uninitialized_vars_warning
5483 but for arguments instead of local variables. */
5485 void
5486 setjmp_args_warning ()
5488 tree decl;
5489 for (decl = DECL_ARGUMENTS (current_function_decl);
5490 decl; decl = TREE_CHAIN (decl))
5491 if (DECL_RTL (decl) != 0
5492 && GET_CODE (DECL_RTL (decl)) == REG
5493 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5494 warning_with_decl (decl,
5495 "argument `%s' might be clobbered by `longjmp' or `vfork'");
5498 /* If this function call setjmp, put all vars into the stack
5499 unless they were declared `register'. */
5501 void
5502 setjmp_protect (block)
5503 tree block;
5505 tree decl, sub;
5506 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5507 if ((TREE_CODE (decl) == VAR_DECL
5508 || TREE_CODE (decl) == PARM_DECL)
5509 && DECL_RTL (decl) != 0
5510 && (GET_CODE (DECL_RTL (decl)) == REG
5511 || (GET_CODE (DECL_RTL (decl)) == MEM
5512 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5513 /* If this variable came from an inline function, it must be
5514 that its life doesn't overlap the setjmp. If there was a
5515 setjmp in the function, it would already be in memory. We
5516 must exclude such variable because their DECL_RTL might be
5517 set to strange things such as virtual_stack_vars_rtx. */
5518 && ! DECL_FROM_INLINE (decl)
5519 && (
5520 #ifdef NON_SAVING_SETJMP
5521 /* If longjmp doesn't restore the registers,
5522 don't put anything in them. */
5523 NON_SAVING_SETJMP
5525 #endif
5526 ! DECL_REGISTER (decl)))
5527 put_var_into_stack (decl);
5528 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5529 setjmp_protect (sub);
5532 /* Like the previous function, but for args instead of local variables. */
5534 void
5535 setjmp_protect_args ()
5537 tree decl;
5538 for (decl = DECL_ARGUMENTS (current_function_decl);
5539 decl; decl = TREE_CHAIN (decl))
5540 if ((TREE_CODE (decl) == VAR_DECL
5541 || TREE_CODE (decl) == PARM_DECL)
5542 && DECL_RTL (decl) != 0
5543 && (GET_CODE (DECL_RTL (decl)) == REG
5544 || (GET_CODE (DECL_RTL (decl)) == MEM
5545 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5546 && (
5547 /* If longjmp doesn't restore the registers,
5548 don't put anything in them. */
5549 #ifdef NON_SAVING_SETJMP
5550 NON_SAVING_SETJMP
5552 #endif
5553 ! DECL_REGISTER (decl)))
5554 put_var_into_stack (decl);
5557 /* Return the context-pointer register corresponding to DECL,
5558 or 0 if it does not need one. */
5561 lookup_static_chain (decl)
5562 tree decl;
5564 tree context = decl_function_context (decl);
5565 tree link;
5567 if (context == 0
5568 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_NO_STATIC_CHAIN (decl)))
5569 return 0;
5571 /* We treat inline_function_decl as an alias for the current function
5572 because that is the inline function whose vars, types, etc.
5573 are being merged into the current function.
5574 See expand_inline_function. */
5575 if (context == current_function_decl || context == inline_function_decl)
5576 return virtual_stack_vars_rtx;
5578 for (link = context_display; link; link = TREE_CHAIN (link))
5579 if (TREE_PURPOSE (link) == context)
5580 return RTL_EXPR_RTL (TREE_VALUE (link));
5582 abort ();
5585 /* Convert a stack slot address ADDR for variable VAR
5586 (from a containing function)
5587 into an address valid in this function (using a static chain). */
5590 fix_lexical_addr (addr, var)
5591 rtx addr;
5592 tree var;
5594 rtx basereg;
5595 HOST_WIDE_INT displacement;
5596 tree context = decl_function_context (var);
5597 struct function *fp;
5598 rtx base = 0;
5600 /* If this is the present function, we need not do anything. */
5601 if (context == current_function_decl || context == inline_function_decl)
5602 return addr;
5604 fp = find_function_data (context);
5606 if (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == MEM)
5607 addr = XEXP (XEXP (addr, 0), 0);
5609 /* Decode given address as base reg plus displacement. */
5610 if (GET_CODE (addr) == REG)
5611 basereg = addr, displacement = 0;
5612 else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
5613 basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
5614 else
5615 abort ();
5617 /* We accept vars reached via the containing function's
5618 incoming arg pointer and via its stack variables pointer. */
5619 if (basereg == fp->internal_arg_pointer)
5621 /* If reached via arg pointer, get the arg pointer value
5622 out of that function's stack frame.
5624 There are two cases: If a separate ap is needed, allocate a
5625 slot in the outer function for it and dereference it that way.
5626 This is correct even if the real ap is actually a pseudo.
5627 Otherwise, just adjust the offset from the frame pointer to
5628 compensate. */
5630 #ifdef NEED_SEPARATE_AP
5631 rtx addr;
5633 addr = get_arg_pointer_save_area (fp);
5634 addr = fix_lexical_addr (XEXP (addr, 0), var);
5635 addr = memory_address (Pmode, addr);
5637 base = gen_rtx_MEM (Pmode, addr);
5638 set_mem_alias_set (base, get_frame_alias_set ());
5639 base = copy_to_reg (base);
5640 #else
5641 displacement += (FIRST_PARM_OFFSET (context) - STARTING_FRAME_OFFSET);
5642 base = lookup_static_chain (var);
5643 #endif
5646 else if (basereg == virtual_stack_vars_rtx)
5648 /* This is the same code as lookup_static_chain, duplicated here to
5649 avoid an extra call to decl_function_context. */
5650 tree link;
5652 for (link = context_display; link; link = TREE_CHAIN (link))
5653 if (TREE_PURPOSE (link) == context)
5655 base = RTL_EXPR_RTL (TREE_VALUE (link));
5656 break;
5660 if (base == 0)
5661 abort ();
5663 /* Use same offset, relative to appropriate static chain or argument
5664 pointer. */
5665 return plus_constant (base, displacement);
5668 /* Return the address of the trampoline for entering nested fn FUNCTION.
5669 If necessary, allocate a trampoline (in the stack frame)
5670 and emit rtl to initialize its contents (at entry to this function). */
5673 trampoline_address (function)
5674 tree function;
5676 tree link;
5677 tree rtlexp;
5678 rtx tramp;
5679 struct function *fp;
5680 tree fn_context;
5682 /* Find an existing trampoline and return it. */
5683 for (link = trampoline_list; link; link = TREE_CHAIN (link))
5684 if (TREE_PURPOSE (link) == function)
5685 return
5686 adjust_trampoline_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0));
5688 for (fp = outer_function_chain; fp; fp = fp->outer)
5689 for (link = fp->x_trampoline_list; link; link = TREE_CHAIN (link))
5690 if (TREE_PURPOSE (link) == function)
5692 tramp = fix_lexical_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0),
5693 function);
5694 return adjust_trampoline_addr (tramp);
5697 /* None exists; we must make one. */
5699 /* Find the `struct function' for the function containing FUNCTION. */
5700 fp = 0;
5701 fn_context = decl_function_context (function);
5702 if (fn_context != current_function_decl
5703 && fn_context != inline_function_decl)
5704 fp = find_function_data (fn_context);
5706 /* Allocate run-time space for this trampoline
5707 (usually in the defining function's stack frame). */
5708 #ifdef ALLOCATE_TRAMPOLINE
5709 tramp = ALLOCATE_TRAMPOLINE (fp);
5710 #else
5711 /* If rounding needed, allocate extra space
5712 to ensure we have TRAMPOLINE_SIZE bytes left after rounding up. */
5713 #define TRAMPOLINE_REAL_SIZE \
5714 (TRAMPOLINE_SIZE + (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT) - 1)
5715 tramp = assign_stack_local_1 (BLKmode, TRAMPOLINE_REAL_SIZE, 0,
5716 fp ? fp : cfun);
5717 #endif
5719 /* Record the trampoline for reuse and note it for later initialization
5720 by expand_function_end. */
5721 if (fp != 0)
5723 rtlexp = make_node (RTL_EXPR);
5724 RTL_EXPR_RTL (rtlexp) = tramp;
5725 fp->x_trampoline_list = tree_cons (function, rtlexp,
5726 fp->x_trampoline_list);
5728 else
5730 /* Make the RTL_EXPR node temporary, not momentary, so that the
5731 trampoline_list doesn't become garbage. */
5732 rtlexp = make_node (RTL_EXPR);
5734 RTL_EXPR_RTL (rtlexp) = tramp;
5735 trampoline_list = tree_cons (function, rtlexp, trampoline_list);
5738 tramp = fix_lexical_addr (XEXP (tramp, 0), function);
5739 return adjust_trampoline_addr (tramp);
5742 /* Given a trampoline address,
5743 round it to multiple of TRAMPOLINE_ALIGNMENT. */
5745 static rtx
5746 round_trampoline_addr (tramp)
5747 rtx tramp;
5749 /* Round address up to desired boundary. */
5750 rtx temp = gen_reg_rtx (Pmode);
5751 rtx addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5752 rtx mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5754 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5755 temp, 0, OPTAB_LIB_WIDEN);
5756 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5757 temp, 0, OPTAB_LIB_WIDEN);
5759 return tramp;
5762 /* Given a trampoline address, round it then apply any
5763 platform-specific adjustments so that the result can be used for a
5764 function call . */
5766 static rtx
5767 adjust_trampoline_addr (tramp)
5768 rtx tramp;
5770 tramp = round_trampoline_addr (tramp);
5771 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5772 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5773 #endif
5774 return tramp;
5777 /* Put all this function's BLOCK nodes including those that are chained
5778 onto the first block into a vector, and return it.
5779 Also store in each NOTE for the beginning or end of a block
5780 the index of that block in the vector.
5781 The arguments are BLOCK, the chain of top-level blocks of the function,
5782 and INSNS, the insn chain of the function. */
5784 void
5785 identify_blocks ()
5787 int n_blocks;
5788 tree *block_vector, *last_block_vector;
5789 tree *block_stack;
5790 tree block = DECL_INITIAL (current_function_decl);
5792 if (block == 0)
5793 return;
5795 /* Fill the BLOCK_VECTOR with all of the BLOCKs in this function, in
5796 depth-first order. */
5797 block_vector = get_block_vector (block, &n_blocks);
5798 block_stack = (tree *) xmalloc (n_blocks * sizeof (tree));
5800 last_block_vector = identify_blocks_1 (get_insns (),
5801 block_vector + 1,
5802 block_vector + n_blocks,
5803 block_stack);
5805 /* If we didn't use all of the subblocks, we've misplaced block notes. */
5806 /* ??? This appears to happen all the time. Latent bugs elsewhere? */
5807 if (0 && last_block_vector != block_vector + n_blocks)
5808 abort ();
5810 free (block_vector);
5811 free (block_stack);
5814 /* Subroutine of identify_blocks. Do the block substitution on the
5815 insn chain beginning with INSNS. Recurse for CALL_PLACEHOLDER chains.
5817 BLOCK_STACK is pushed and popped for each BLOCK_BEGIN/BLOCK_END pair.
5818 BLOCK_VECTOR is incremented for each block seen. */
5820 static tree *
5821 identify_blocks_1 (insns, block_vector, end_block_vector, orig_block_stack)
5822 rtx insns;
5823 tree *block_vector;
5824 tree *end_block_vector;
5825 tree *orig_block_stack;
5827 rtx insn;
5828 tree *block_stack = orig_block_stack;
5830 for (insn = insns; insn; insn = NEXT_INSN (insn))
5832 if (GET_CODE (insn) == NOTE)
5834 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5836 tree b;
5838 /* If there are more block notes than BLOCKs, something
5839 is badly wrong. */
5840 if (block_vector == end_block_vector)
5841 abort ();
5843 b = *block_vector++;
5844 NOTE_BLOCK (insn) = b;
5845 *block_stack++ = b;
5847 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5849 /* If there are more NOTE_INSN_BLOCK_ENDs than
5850 NOTE_INSN_BLOCK_BEGs, something is badly wrong. */
5851 if (block_stack == orig_block_stack)
5852 abort ();
5854 NOTE_BLOCK (insn) = *--block_stack;
5857 else if (GET_CODE (insn) == CALL_INSN
5858 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5860 rtx cp = PATTERN (insn);
5862 block_vector = identify_blocks_1 (XEXP (cp, 0), block_vector,
5863 end_block_vector, block_stack);
5864 if (XEXP (cp, 1))
5865 block_vector = identify_blocks_1 (XEXP (cp, 1), block_vector,
5866 end_block_vector, block_stack);
5867 if (XEXP (cp, 2))
5868 block_vector = identify_blocks_1 (XEXP (cp, 2), block_vector,
5869 end_block_vector, block_stack);
5873 /* If there are more NOTE_INSN_BLOCK_BEGINs than NOTE_INSN_BLOCK_ENDs,
5874 something is badly wrong. */
5875 if (block_stack != orig_block_stack)
5876 abort ();
5878 return block_vector;
5881 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
5882 and create duplicate blocks. */
5883 /* ??? Need an option to either create block fragments or to create
5884 abstract origin duplicates of a source block. It really depends
5885 on what optimization has been performed. */
5887 void
5888 reorder_blocks ()
5890 tree block = DECL_INITIAL (current_function_decl);
5891 varray_type block_stack;
5893 if (block == NULL_TREE)
5894 return;
5896 VARRAY_TREE_INIT (block_stack, 10, "block_stack");
5898 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
5899 reorder_blocks_0 (block);
5901 /* Prune the old trees away, so that they don't get in the way. */
5902 BLOCK_SUBBLOCKS (block) = NULL_TREE;
5903 BLOCK_CHAIN (block) = NULL_TREE;
5905 /* Recreate the block tree from the note nesting. */
5906 reorder_blocks_1 (get_insns (), block, &block_stack);
5907 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
5909 /* Remove deleted blocks from the block fragment chains. */
5910 reorder_fix_fragments (block);
5913 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
5915 static void
5916 reorder_blocks_0 (block)
5917 tree block;
5919 while (block)
5921 TREE_ASM_WRITTEN (block) = 0;
5922 reorder_blocks_0 (BLOCK_SUBBLOCKS (block));
5923 block = BLOCK_CHAIN (block);
5927 static void
5928 reorder_blocks_1 (insns, current_block, p_block_stack)
5929 rtx insns;
5930 tree current_block;
5931 varray_type *p_block_stack;
5933 rtx insn;
5935 for (insn = insns; insn; insn = NEXT_INSN (insn))
5937 if (GET_CODE (insn) == NOTE)
5939 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5941 tree block = NOTE_BLOCK (insn);
5943 /* If we have seen this block before, that means it now
5944 spans multiple address regions. Create a new fragment. */
5945 if (TREE_ASM_WRITTEN (block))
5947 tree new_block = copy_node (block);
5948 tree origin;
5950 origin = (BLOCK_FRAGMENT_ORIGIN (block)
5951 ? BLOCK_FRAGMENT_ORIGIN (block)
5952 : block);
5953 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
5954 BLOCK_FRAGMENT_CHAIN (new_block)
5955 = BLOCK_FRAGMENT_CHAIN (origin);
5956 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
5958 NOTE_BLOCK (insn) = new_block;
5959 block = new_block;
5962 BLOCK_SUBBLOCKS (block) = 0;
5963 TREE_ASM_WRITTEN (block) = 1;
5964 BLOCK_SUPERCONTEXT (block) = current_block;
5965 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
5966 BLOCK_SUBBLOCKS (current_block) = block;
5967 current_block = block;
5968 VARRAY_PUSH_TREE (*p_block_stack, block);
5970 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5972 NOTE_BLOCK (insn) = VARRAY_TOP_TREE (*p_block_stack);
5973 VARRAY_POP (*p_block_stack);
5974 BLOCK_SUBBLOCKS (current_block)
5975 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
5976 current_block = BLOCK_SUPERCONTEXT (current_block);
5979 else if (GET_CODE (insn) == CALL_INSN
5980 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5982 rtx cp = PATTERN (insn);
5983 reorder_blocks_1 (XEXP (cp, 0), current_block, p_block_stack);
5984 if (XEXP (cp, 1))
5985 reorder_blocks_1 (XEXP (cp, 1), current_block, p_block_stack);
5986 if (XEXP (cp, 2))
5987 reorder_blocks_1 (XEXP (cp, 2), current_block, p_block_stack);
5992 /* Rationalize BLOCK_FRAGMENT_ORIGIN. If an origin block no longer
5993 appears in the block tree, select one of the fragments to become
5994 the new origin block. */
5996 static void
5997 reorder_fix_fragments (block)
5998 tree block;
6000 while (block)
6002 tree dup_origin = BLOCK_FRAGMENT_ORIGIN (block);
6003 tree new_origin = NULL_TREE;
6005 if (dup_origin)
6007 if (! TREE_ASM_WRITTEN (dup_origin))
6009 new_origin = BLOCK_FRAGMENT_CHAIN (dup_origin);
6011 /* Find the first of the remaining fragments. There must
6012 be at least one -- the current block. */
6013 while (! TREE_ASM_WRITTEN (new_origin))
6014 new_origin = BLOCK_FRAGMENT_CHAIN (new_origin);
6015 BLOCK_FRAGMENT_ORIGIN (new_origin) = NULL_TREE;
6018 else if (! dup_origin)
6019 new_origin = block;
6021 /* Re-root the rest of the fragments to the new origin. In the
6022 case that DUP_ORIGIN was null, that means BLOCK was the origin
6023 of a chain of fragments and we want to remove those fragments
6024 that didn't make it to the output. */
6025 if (new_origin)
6027 tree *pp = &BLOCK_FRAGMENT_CHAIN (new_origin);
6028 tree chain = *pp;
6030 while (chain)
6032 if (TREE_ASM_WRITTEN (chain))
6034 BLOCK_FRAGMENT_ORIGIN (chain) = new_origin;
6035 *pp = chain;
6036 pp = &BLOCK_FRAGMENT_CHAIN (chain);
6038 chain = BLOCK_FRAGMENT_CHAIN (chain);
6040 *pp = NULL_TREE;
6043 reorder_fix_fragments (BLOCK_SUBBLOCKS (block));
6044 block = BLOCK_CHAIN (block);
6048 /* Reverse the order of elements in the chain T of blocks,
6049 and return the new head of the chain (old last element). */
6051 static tree
6052 blocks_nreverse (t)
6053 tree t;
6055 tree prev = 0, decl, next;
6056 for (decl = t; decl; decl = next)
6058 next = BLOCK_CHAIN (decl);
6059 BLOCK_CHAIN (decl) = prev;
6060 prev = decl;
6062 return prev;
6065 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
6066 non-NULL, list them all into VECTOR, in a depth-first preorder
6067 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
6068 blocks. */
6070 static int
6071 all_blocks (block, vector)
6072 tree block;
6073 tree *vector;
6075 int n_blocks = 0;
6077 while (block)
6079 TREE_ASM_WRITTEN (block) = 0;
6081 /* Record this block. */
6082 if (vector)
6083 vector[n_blocks] = block;
6085 ++n_blocks;
6087 /* Record the subblocks, and their subblocks... */
6088 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
6089 vector ? vector + n_blocks : 0);
6090 block = BLOCK_CHAIN (block);
6093 return n_blocks;
6096 /* Return a vector containing all the blocks rooted at BLOCK. The
6097 number of elements in the vector is stored in N_BLOCKS_P. The
6098 vector is dynamically allocated; it is the caller's responsibility
6099 to call `free' on the pointer returned. */
6101 static tree *
6102 get_block_vector (block, n_blocks_p)
6103 tree block;
6104 int *n_blocks_p;
6106 tree *block_vector;
6108 *n_blocks_p = all_blocks (block, NULL);
6109 block_vector = (tree *) xmalloc (*n_blocks_p * sizeof (tree));
6110 all_blocks (block, block_vector);
6112 return block_vector;
6115 static int next_block_index = 2;
6117 /* Set BLOCK_NUMBER for all the blocks in FN. */
6119 void
6120 number_blocks (fn)
6121 tree fn;
6123 int i;
6124 int n_blocks;
6125 tree *block_vector;
6127 /* For SDB and XCOFF debugging output, we start numbering the blocks
6128 from 1 within each function, rather than keeping a running
6129 count. */
6130 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
6131 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
6132 next_block_index = 1;
6133 #endif
6135 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
6137 /* The top-level BLOCK isn't numbered at all. */
6138 for (i = 1; i < n_blocks; ++i)
6139 /* We number the blocks from two. */
6140 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
6142 free (block_vector);
6144 return;
6147 /* If VAR is present in a subblock of BLOCK, return the subblock. */
6149 tree
6150 debug_find_var_in_block_tree (var, block)
6151 tree var;
6152 tree block;
6154 tree t;
6156 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
6157 if (t == var)
6158 return block;
6160 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
6162 tree ret = debug_find_var_in_block_tree (var, t);
6163 if (ret)
6164 return ret;
6167 return NULL_TREE;
6170 /* Allocate a function structure and reset its contents to the defaults. */
6172 static void
6173 prepare_function_start ()
6175 cfun = (struct function *) ggc_alloc_cleared (sizeof (struct function));
6177 init_stmt_for_function ();
6178 init_eh_for_function ();
6180 cse_not_expected = ! optimize;
6182 /* Caller save not needed yet. */
6183 caller_save_needed = 0;
6185 /* No stack slots have been made yet. */
6186 stack_slot_list = 0;
6188 current_function_has_nonlocal_label = 0;
6189 current_function_has_nonlocal_goto = 0;
6191 /* There is no stack slot for handling nonlocal gotos. */
6192 nonlocal_goto_handler_slots = 0;
6193 nonlocal_goto_stack_level = 0;
6195 /* No labels have been declared for nonlocal use. */
6196 nonlocal_labels = 0;
6197 nonlocal_goto_handler_labels = 0;
6199 /* No function calls so far in this function. */
6200 function_call_count = 0;
6202 /* No parm regs have been allocated.
6203 (This is important for output_inline_function.) */
6204 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
6206 /* Initialize the RTL mechanism. */
6207 init_emit ();
6209 /* Initialize the queue of pending postincrement and postdecrements,
6210 and some other info in expr.c. */
6211 init_expr ();
6213 /* We haven't done register allocation yet. */
6214 reg_renumber = 0;
6216 init_varasm_status (cfun);
6218 /* Clear out data used for inlining. */
6219 cfun->inlinable = 0;
6220 cfun->original_decl_initial = 0;
6221 cfun->original_arg_vector = 0;
6223 cfun->stack_alignment_needed = STACK_BOUNDARY;
6224 cfun->preferred_stack_boundary = STACK_BOUNDARY;
6226 /* Set if a call to setjmp is seen. */
6227 current_function_calls_setjmp = 0;
6229 /* Set if a call to longjmp is seen. */
6230 current_function_calls_longjmp = 0;
6232 current_function_calls_alloca = 0;
6233 current_function_contains_functions = 0;
6234 current_function_is_leaf = 0;
6235 current_function_nothrow = 0;
6236 current_function_sp_is_unchanging = 0;
6237 current_function_uses_only_leaf_regs = 0;
6238 current_function_has_computed_jump = 0;
6239 current_function_is_thunk = 0;
6241 current_function_returns_pcc_struct = 0;
6242 current_function_returns_struct = 0;
6243 current_function_epilogue_delay_list = 0;
6244 current_function_uses_const_pool = 0;
6245 current_function_uses_pic_offset_table = 0;
6246 current_function_cannot_inline = 0;
6248 /* We have not yet needed to make a label to jump to for tail-recursion. */
6249 tail_recursion_label = 0;
6251 /* We haven't had a need to make a save area for ap yet. */
6252 arg_pointer_save_area = 0;
6254 /* No stack slots allocated yet. */
6255 frame_offset = 0;
6257 /* No SAVE_EXPRs in this function yet. */
6258 save_expr_regs = 0;
6260 /* No RTL_EXPRs in this function yet. */
6261 rtl_expr_chain = 0;
6263 /* Set up to allocate temporaries. */
6264 init_temp_slots ();
6266 /* Indicate that we need to distinguish between the return value of the
6267 present function and the return value of a function being called. */
6268 rtx_equal_function_value_matters = 1;
6270 /* Indicate that we have not instantiated virtual registers yet. */
6271 virtuals_instantiated = 0;
6273 /* Indicate that we want CONCATs now. */
6274 generating_concat_p = 1;
6276 /* Indicate we have no need of a frame pointer yet. */
6277 frame_pointer_needed = 0;
6279 /* By default assume not stdarg. */
6280 current_function_stdarg = 0;
6282 /* We haven't made any trampolines for this function yet. */
6283 trampoline_list = 0;
6285 init_pending_stack_adjust ();
6286 inhibit_defer_pop = 0;
6288 current_function_outgoing_args_size = 0;
6290 current_function_funcdef_no = funcdef_no++;
6292 cfun->arc_profile = profile_arc_flag || flag_test_coverage;
6294 cfun->arc_profile = profile_arc_flag || flag_test_coverage;
6296 cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
6298 (*lang_hooks.function.init) (cfun);
6299 if (init_machine_status)
6300 cfun->machine = (*init_machine_status) ();
6303 /* Initialize the rtl expansion mechanism so that we can do simple things
6304 like generate sequences. This is used to provide a context during global
6305 initialization of some passes. */
6306 void
6307 init_dummy_function_start ()
6309 prepare_function_start ();
6312 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
6313 and initialize static variables for generating RTL for the statements
6314 of the function. */
6316 void
6317 init_function_start (subr, filename, line)
6318 tree subr;
6319 const char *filename;
6320 int line;
6322 prepare_function_start ();
6324 current_function_name = (*lang_hooks.decl_printable_name) (subr, 2);
6325 cfun->decl = subr;
6327 /* Nonzero if this is a nested function that uses a static chain. */
6329 current_function_needs_context
6330 = (decl_function_context (current_function_decl) != 0
6331 && ! DECL_NO_STATIC_CHAIN (current_function_decl));
6333 /* Within function body, compute a type's size as soon it is laid out. */
6334 immediate_size_expand++;
6336 /* Prevent ever trying to delete the first instruction of a function.
6337 Also tell final how to output a linenum before the function prologue.
6338 Note linenums could be missing, e.g. when compiling a Java .class file. */
6339 if (line > 0)
6340 emit_line_note (filename, line);
6342 /* Make sure first insn is a note even if we don't want linenums.
6343 This makes sure the first insn will never be deleted.
6344 Also, final expects a note to appear there. */
6345 emit_note (NULL, NOTE_INSN_DELETED);
6347 /* Set flags used by final.c. */
6348 if (aggregate_value_p (DECL_RESULT (subr)))
6350 #ifdef PCC_STATIC_STRUCT_RETURN
6351 current_function_returns_pcc_struct = 1;
6352 #endif
6353 current_function_returns_struct = 1;
6356 /* Warn if this value is an aggregate type,
6357 regardless of which calling convention we are using for it. */
6358 if (warn_aggregate_return
6359 && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
6360 warning ("function returns an aggregate");
6362 current_function_returns_pointer
6363 = POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
6366 /* Make sure all values used by the optimization passes have sane
6367 defaults. */
6368 void
6369 init_function_for_compilation ()
6371 reg_renumber = 0;
6373 /* No prologue/epilogue insns yet. */
6374 VARRAY_GROW (prologue, 0);
6375 VARRAY_GROW (epilogue, 0);
6376 VARRAY_GROW (sibcall_epilogue, 0);
6379 /* Expand a call to __main at the beginning of a possible main function. */
6381 #if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
6382 #undef HAS_INIT_SECTION
6383 #define HAS_INIT_SECTION
6384 #endif
6386 void
6387 expand_main_function ()
6389 #ifdef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
6390 if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN)
6392 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
6393 rtx tmp, seq;
6395 start_sequence ();
6396 /* Forcibly align the stack. */
6397 #ifdef STACK_GROWS_DOWNWARD
6398 tmp = expand_simple_binop (Pmode, AND, stack_pointer_rtx, GEN_INT(-align),
6399 stack_pointer_rtx, 1, OPTAB_WIDEN);
6400 #else
6401 tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
6402 GEN_INT (align - 1), NULL_RTX, 1, OPTAB_WIDEN);
6403 tmp = expand_simple_binop (Pmode, AND, tmp, GEN_INT (-align),
6404 stack_pointer_rtx, 1, OPTAB_WIDEN);
6405 #endif
6406 if (tmp != stack_pointer_rtx)
6407 emit_move_insn (stack_pointer_rtx, tmp);
6409 /* Enlist allocate_dynamic_stack_space to pick up the pieces. */
6410 tmp = force_reg (Pmode, const0_rtx);
6411 allocate_dynamic_stack_space (tmp, NULL_RTX, BIGGEST_ALIGNMENT);
6412 seq = get_insns ();
6413 end_sequence ();
6415 for (tmp = get_last_insn (); tmp; tmp = PREV_INSN (tmp))
6416 if (NOTE_P (tmp) && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_FUNCTION_BEG)
6417 break;
6418 if (tmp)
6419 emit_insn_before (seq, tmp);
6420 else
6421 emit_insn (seq);
6423 #endif
6425 #ifndef HAS_INIT_SECTION
6426 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, NAME__MAIN), LCT_NORMAL,
6427 VOIDmode, 0);
6428 #endif
6431 /* The PENDING_SIZES represent the sizes of variable-sized types.
6432 Create RTL for the various sizes now (using temporary variables),
6433 so that we can refer to the sizes from the RTL we are generating
6434 for the current function. The PENDING_SIZES are a TREE_LIST. The
6435 TREE_VALUE of each node is a SAVE_EXPR. */
6437 void
6438 expand_pending_sizes (pending_sizes)
6439 tree pending_sizes;
6441 tree tem;
6443 /* Evaluate now the sizes of any types declared among the arguments. */
6444 for (tem = pending_sizes; tem; tem = TREE_CHAIN (tem))
6446 expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode, 0);
6447 /* Flush the queue in case this parameter declaration has
6448 side-effects. */
6449 emit_queue ();
6453 /* Start the RTL for a new function, and set variables used for
6454 emitting RTL.
6455 SUBR is the FUNCTION_DECL node.
6456 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
6457 the function's parameters, which must be run at any return statement. */
6459 void
6460 expand_function_start (subr, parms_have_cleanups)
6461 tree subr;
6462 int parms_have_cleanups;
6464 tree tem;
6465 rtx last_ptr = NULL_RTX;
6467 /* Make sure volatile mem refs aren't considered
6468 valid operands of arithmetic insns. */
6469 init_recog_no_volatile ();
6471 current_function_instrument_entry_exit
6472 = (flag_instrument_function_entry_exit
6473 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6475 current_function_profile
6476 = (profile_flag
6477 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6479 current_function_limit_stack
6480 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
6482 /* If function gets a static chain arg, store it in the stack frame.
6483 Do this first, so it gets the first stack slot offset. */
6484 if (current_function_needs_context)
6486 last_ptr = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
6488 /* Delay copying static chain if it is not a register to avoid
6489 conflicts with regs used for parameters. */
6490 if (! SMALL_REGISTER_CLASSES
6491 || GET_CODE (static_chain_incoming_rtx) == REG)
6492 emit_move_insn (last_ptr, static_chain_incoming_rtx);
6495 /* If the parameters of this function need cleaning up, get a label
6496 for the beginning of the code which executes those cleanups. This must
6497 be done before doing anything with return_label. */
6498 if (parms_have_cleanups)
6499 cleanup_label = gen_label_rtx ();
6500 else
6501 cleanup_label = 0;
6503 /* Make the label for return statements to jump to. Do not special
6504 case machines with special return instructions -- they will be
6505 handled later during jump, ifcvt, or epilogue creation. */
6506 return_label = gen_label_rtx ();
6508 /* Initialize rtx used to return the value. */
6509 /* Do this before assign_parms so that we copy the struct value address
6510 before any library calls that assign parms might generate. */
6512 /* Decide whether to return the value in memory or in a register. */
6513 if (aggregate_value_p (DECL_RESULT (subr)))
6515 /* Returning something that won't go in a register. */
6516 rtx value_address = 0;
6518 #ifdef PCC_STATIC_STRUCT_RETURN
6519 if (current_function_returns_pcc_struct)
6521 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
6522 value_address = assemble_static_space (size);
6524 else
6525 #endif
6527 /* Expect to be passed the address of a place to store the value.
6528 If it is passed as an argument, assign_parms will take care of
6529 it. */
6530 if (struct_value_incoming_rtx)
6532 value_address = gen_reg_rtx (Pmode);
6533 emit_move_insn (value_address, struct_value_incoming_rtx);
6536 if (value_address)
6538 rtx x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), value_address);
6539 set_mem_attributes (x, DECL_RESULT (subr), 1);
6540 SET_DECL_RTL (DECL_RESULT (subr), x);
6543 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
6544 /* If return mode is void, this decl rtl should not be used. */
6545 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
6546 else
6548 /* Compute the return values into a pseudo reg, which we will copy
6549 into the true return register after the cleanups are done. */
6551 /* In order to figure out what mode to use for the pseudo, we
6552 figure out what the mode of the eventual return register will
6553 actually be, and use that. */
6554 rtx hard_reg
6555 = hard_function_value (TREE_TYPE (DECL_RESULT (subr)),
6556 subr, 1);
6558 /* Structures that are returned in registers are not aggregate_value_p,
6559 so we may see a PARALLEL. Don't play pseudo games with this. */
6560 if (! REG_P (hard_reg))
6561 SET_DECL_RTL (DECL_RESULT (subr), hard_reg);
6562 else
6564 /* Create the pseudo. */
6565 SET_DECL_RTL (DECL_RESULT (subr), gen_reg_rtx (GET_MODE (hard_reg)));
6567 /* Needed because we may need to move this to memory
6568 in case it's a named return value whose address is taken. */
6569 DECL_REGISTER (DECL_RESULT (subr)) = 1;
6573 /* Initialize rtx for parameters and local variables.
6574 In some cases this requires emitting insns. */
6576 assign_parms (subr);
6578 /* Copy the static chain now if it wasn't a register. The delay is to
6579 avoid conflicts with the parameter passing registers. */
6581 if (SMALL_REGISTER_CLASSES && current_function_needs_context)
6582 if (GET_CODE (static_chain_incoming_rtx) != REG)
6583 emit_move_insn (last_ptr, static_chain_incoming_rtx);
6585 /* The following was moved from init_function_start.
6586 The move is supposed to make sdb output more accurate. */
6587 /* Indicate the beginning of the function body,
6588 as opposed to parm setup. */
6589 emit_note (NULL, NOTE_INSN_FUNCTION_BEG);
6591 if (GET_CODE (get_last_insn ()) != NOTE)
6592 emit_note (NULL, NOTE_INSN_DELETED);
6593 parm_birth_insn = get_last_insn ();
6595 context_display = 0;
6596 if (current_function_needs_context)
6598 /* Fetch static chain values for containing functions. */
6599 tem = decl_function_context (current_function_decl);
6600 /* Copy the static chain pointer into a pseudo. If we have
6601 small register classes, copy the value from memory if
6602 static_chain_incoming_rtx is a REG. */
6603 if (tem)
6605 /* If the static chain originally came in a register, put it back
6606 there, then move it out in the next insn. The reason for
6607 this peculiar code is to satisfy function integration. */
6608 if (SMALL_REGISTER_CLASSES
6609 && GET_CODE (static_chain_incoming_rtx) == REG)
6610 emit_move_insn (static_chain_incoming_rtx, last_ptr);
6611 last_ptr = copy_to_reg (static_chain_incoming_rtx);
6614 while (tem)
6616 tree rtlexp = make_node (RTL_EXPR);
6618 RTL_EXPR_RTL (rtlexp) = last_ptr;
6619 context_display = tree_cons (tem, rtlexp, context_display);
6620 tem = decl_function_context (tem);
6621 if (tem == 0)
6622 break;
6623 /* Chain thru stack frames, assuming pointer to next lexical frame
6624 is found at the place we always store it. */
6625 #ifdef FRAME_GROWS_DOWNWARD
6626 last_ptr = plus_constant (last_ptr,
6627 -(HOST_WIDE_INT) GET_MODE_SIZE (Pmode));
6628 #endif
6629 last_ptr = gen_rtx_MEM (Pmode, memory_address (Pmode, last_ptr));
6630 set_mem_alias_set (last_ptr, get_frame_alias_set ());
6631 last_ptr = copy_to_reg (last_ptr);
6633 /* If we are not optimizing, ensure that we know that this
6634 piece of context is live over the entire function. */
6635 if (! optimize)
6636 save_expr_regs = gen_rtx_EXPR_LIST (VOIDmode, last_ptr,
6637 save_expr_regs);
6641 if (current_function_instrument_entry_exit)
6643 rtx fun = DECL_RTL (current_function_decl);
6644 if (GET_CODE (fun) == MEM)
6645 fun = XEXP (fun, 0);
6646 else
6647 abort ();
6648 emit_library_call (profile_function_entry_libfunc, LCT_NORMAL, VOIDmode,
6649 2, fun, Pmode,
6650 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6652 hard_frame_pointer_rtx),
6653 Pmode);
6656 if (current_function_profile)
6658 #ifdef PROFILE_HOOK
6659 PROFILE_HOOK (current_function_funcdef_no);
6660 #endif
6663 /* After the display initializations is where the tail-recursion label
6664 should go, if we end up needing one. Ensure we have a NOTE here
6665 since some things (like trampolines) get placed before this. */
6666 tail_recursion_reentry = emit_note (NULL, NOTE_INSN_DELETED);
6668 /* Evaluate now the sizes of any types declared among the arguments. */
6669 expand_pending_sizes (nreverse (get_pending_sizes ()));
6671 /* Make sure there is a line number after the function entry setup code. */
6672 force_next_line_note ();
6675 /* Undo the effects of init_dummy_function_start. */
6676 void
6677 expand_dummy_function_end ()
6679 /* End any sequences that failed to be closed due to syntax errors. */
6680 while (in_sequence_p ())
6681 end_sequence ();
6683 /* Outside function body, can't compute type's actual size
6684 until next function's body starts. */
6686 free_after_parsing (cfun);
6687 free_after_compilation (cfun);
6688 cfun = 0;
6691 /* Call DOIT for each hard register used as a return value from
6692 the current function. */
6694 void
6695 diddle_return_value (doit, arg)
6696 void (*doit) PARAMS ((rtx, void *));
6697 void *arg;
6699 rtx outgoing = current_function_return_rtx;
6701 if (! outgoing)
6702 return;
6704 if (GET_CODE (outgoing) == REG)
6705 (*doit) (outgoing, arg);
6706 else if (GET_CODE (outgoing) == PARALLEL)
6708 int i;
6710 for (i = 0; i < XVECLEN (outgoing, 0); i++)
6712 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
6714 if (GET_CODE (x) == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
6715 (*doit) (x, arg);
6720 static void
6721 do_clobber_return_reg (reg, arg)
6722 rtx reg;
6723 void *arg ATTRIBUTE_UNUSED;
6725 emit_insn (gen_rtx_CLOBBER (VOIDmode, reg));
6728 void
6729 clobber_return_register ()
6731 diddle_return_value (do_clobber_return_reg, NULL);
6733 /* In case we do use pseudo to return value, clobber it too. */
6734 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6736 tree decl_result = DECL_RESULT (current_function_decl);
6737 rtx decl_rtl = DECL_RTL (decl_result);
6738 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
6740 do_clobber_return_reg (decl_rtl, NULL);
6745 static void
6746 do_use_return_reg (reg, arg)
6747 rtx reg;
6748 void *arg ATTRIBUTE_UNUSED;
6750 emit_insn (gen_rtx_USE (VOIDmode, reg));
6753 void
6754 use_return_register ()
6756 diddle_return_value (do_use_return_reg, NULL);
6759 static GTY(()) rtx initial_trampoline;
6761 /* Generate RTL for the end of the current function.
6762 FILENAME and LINE are the current position in the source file.
6764 It is up to language-specific callers to do cleanups for parameters--
6765 or else, supply 1 for END_BINDINGS and we will call expand_end_bindings. */
6767 void
6768 expand_function_end (filename, line, end_bindings)
6769 const char *filename;
6770 int line;
6771 int end_bindings;
6773 tree link;
6774 rtx clobber_after;
6776 finish_expr_for_function ();
6778 /* If arg_pointer_save_area was referenced only from a nested
6779 function, we will not have initialized it yet. Do that now. */
6780 if (arg_pointer_save_area && ! cfun->arg_pointer_save_area_init)
6781 get_arg_pointer_save_area (cfun);
6783 #ifdef NON_SAVING_SETJMP
6784 /* Don't put any variables in registers if we call setjmp
6785 on a machine that fails to restore the registers. */
6786 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
6788 if (DECL_INITIAL (current_function_decl) != error_mark_node)
6789 setjmp_protect (DECL_INITIAL (current_function_decl));
6791 setjmp_protect_args ();
6793 #endif
6795 /* Initialize any trampolines required by this function. */
6796 for (link = trampoline_list; link; link = TREE_CHAIN (link))
6798 tree function = TREE_PURPOSE (link);
6799 rtx context ATTRIBUTE_UNUSED = lookup_static_chain (function);
6800 rtx tramp = RTL_EXPR_RTL (TREE_VALUE (link));
6801 #ifdef TRAMPOLINE_TEMPLATE
6802 rtx blktramp;
6803 #endif
6804 rtx seq;
6806 #ifdef TRAMPOLINE_TEMPLATE
6807 /* First make sure this compilation has a template for
6808 initializing trampolines. */
6809 if (initial_trampoline == 0)
6811 initial_trampoline
6812 = gen_rtx_MEM (BLKmode, assemble_trampoline_template ());
6813 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
6815 #endif
6817 /* Generate insns to initialize the trampoline. */
6818 start_sequence ();
6819 tramp = round_trampoline_addr (XEXP (tramp, 0));
6820 #ifdef TRAMPOLINE_TEMPLATE
6821 blktramp = replace_equiv_address (initial_trampoline, tramp);
6822 emit_block_move (blktramp, initial_trampoline,
6823 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
6824 #endif
6825 INITIALIZE_TRAMPOLINE (tramp, XEXP (DECL_RTL (function), 0), context);
6826 seq = get_insns ();
6827 end_sequence ();
6829 /* Put those insns at entry to the containing function (this one). */
6830 emit_insn_before (seq, tail_recursion_reentry);
6833 /* If we are doing stack checking and this function makes calls,
6834 do a stack probe at the start of the function to ensure we have enough
6835 space for another stack frame. */
6836 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
6838 rtx insn, seq;
6840 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6841 if (GET_CODE (insn) == CALL_INSN)
6843 start_sequence ();
6844 probe_stack_range (STACK_CHECK_PROTECT,
6845 GEN_INT (STACK_CHECK_MAX_FRAME_SIZE));
6846 seq = get_insns ();
6847 end_sequence ();
6848 emit_insn_before (seq, tail_recursion_reentry);
6849 break;
6853 /* Warn about unused parms if extra warnings were specified. */
6854 /* Either ``-W -Wunused'' or ``-Wunused-parameter'' enables this
6855 warning. WARN_UNUSED_PARAMETER is negative when set by
6856 -Wunused. */
6857 if (warn_unused_parameter > 0
6858 || (warn_unused_parameter < 0 && extra_warnings))
6860 tree decl;
6862 for (decl = DECL_ARGUMENTS (current_function_decl);
6863 decl; decl = TREE_CHAIN (decl))
6864 if (! TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
6865 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
6866 warning_with_decl (decl, "unused parameter `%s'");
6869 /* Delete handlers for nonlocal gotos if nothing uses them. */
6870 if (nonlocal_goto_handler_slots != 0
6871 && ! current_function_has_nonlocal_label)
6872 delete_handlers ();
6874 /* End any sequences that failed to be closed due to syntax errors. */
6875 while (in_sequence_p ())
6876 end_sequence ();
6878 /* Outside function body, can't compute type's actual size
6879 until next function's body starts. */
6880 immediate_size_expand--;
6882 clear_pending_stack_adjust ();
6883 do_pending_stack_adjust ();
6885 /* Mark the end of the function body.
6886 If control reaches this insn, the function can drop through
6887 without returning a value. */
6888 emit_note (NULL, NOTE_INSN_FUNCTION_END);
6890 /* Must mark the last line number note in the function, so that the test
6891 coverage code can avoid counting the last line twice. This just tells
6892 the code to ignore the immediately following line note, since there
6893 already exists a copy of this note somewhere above. This line number
6894 note is still needed for debugging though, so we can't delete it. */
6895 if (flag_test_coverage)
6896 emit_note (NULL, NOTE_INSN_REPEATED_LINE_NUMBER);
6898 /* Output a linenumber for the end of the function.
6899 SDB depends on this. */
6900 emit_line_note_force (filename, line);
6902 /* Before the return label (if any), clobber the return
6903 registers so that they are not propagated live to the rest of
6904 the function. This can only happen with functions that drop
6905 through; if there had been a return statement, there would
6906 have either been a return rtx, or a jump to the return label.
6908 We delay actual code generation after the current_function_value_rtx
6909 is computed. */
6910 clobber_after = get_last_insn ();
6912 /* Output the label for the actual return from the function,
6913 if one is expected. This happens either because a function epilogue
6914 is used instead of a return instruction, or because a return was done
6915 with a goto in order to run local cleanups, or because of pcc-style
6916 structure returning. */
6917 if (return_label)
6918 emit_label (return_label);
6920 /* C++ uses this. */
6921 if (end_bindings)
6922 expand_end_bindings (0, 0, 0);
6924 if (current_function_instrument_entry_exit)
6926 rtx fun = DECL_RTL (current_function_decl);
6927 if (GET_CODE (fun) == MEM)
6928 fun = XEXP (fun, 0);
6929 else
6930 abort ();
6931 emit_library_call (profile_function_exit_libfunc, LCT_NORMAL, VOIDmode,
6932 2, fun, Pmode,
6933 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6935 hard_frame_pointer_rtx),
6936 Pmode);
6939 /* Let except.c know where it should emit the call to unregister
6940 the function context for sjlj exceptions. */
6941 if (flag_exceptions && USING_SJLJ_EXCEPTIONS)
6942 sjlj_emit_function_exit_after (get_last_insn ());
6944 /* If we had calls to alloca, and this machine needs
6945 an accurate stack pointer to exit the function,
6946 insert some code to save and restore the stack pointer. */
6947 #ifdef EXIT_IGNORE_STACK
6948 if (! EXIT_IGNORE_STACK)
6949 #endif
6950 if (current_function_calls_alloca)
6952 rtx tem = 0;
6954 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
6955 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
6958 /* If scalar return value was computed in a pseudo-reg, or was a named
6959 return value that got dumped to the stack, copy that to the hard
6960 return register. */
6961 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6963 tree decl_result = DECL_RESULT (current_function_decl);
6964 rtx decl_rtl = DECL_RTL (decl_result);
6966 if (REG_P (decl_rtl)
6967 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
6968 : DECL_REGISTER (decl_result))
6970 rtx real_decl_rtl = current_function_return_rtx;
6972 /* This should be set in assign_parms. */
6973 if (! REG_FUNCTION_VALUE_P (real_decl_rtl))
6974 abort ();
6976 /* If this is a BLKmode structure being returned in registers,
6977 then use the mode computed in expand_return. Note that if
6978 decl_rtl is memory, then its mode may have been changed,
6979 but that current_function_return_rtx has not. */
6980 if (GET_MODE (real_decl_rtl) == BLKmode)
6981 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
6983 /* If a named return value dumped decl_return to memory, then
6984 we may need to re-do the PROMOTE_MODE signed/unsigned
6985 extension. */
6986 if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
6988 int unsignedp = TREE_UNSIGNED (TREE_TYPE (decl_result));
6990 #ifdef PROMOTE_FUNCTION_RETURN
6991 promote_mode (TREE_TYPE (decl_result), GET_MODE (decl_rtl),
6992 &unsignedp, 1);
6993 #endif
6995 convert_move (real_decl_rtl, decl_rtl, unsignedp);
6997 else if (GET_CODE (real_decl_rtl) == PARALLEL)
6998 emit_group_load (real_decl_rtl, decl_rtl,
6999 int_size_in_bytes (TREE_TYPE (decl_result)));
7000 else
7001 emit_move_insn (real_decl_rtl, decl_rtl);
7005 /* If returning a structure, arrange to return the address of the value
7006 in a place where debuggers expect to find it.
7008 If returning a structure PCC style,
7009 the caller also depends on this value.
7010 And current_function_returns_pcc_struct is not necessarily set. */
7011 if (current_function_returns_struct
7012 || current_function_returns_pcc_struct)
7014 rtx value_address
7015 = XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
7016 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
7017 #ifdef FUNCTION_OUTGOING_VALUE
7018 rtx outgoing
7019 = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
7020 current_function_decl);
7021 #else
7022 rtx outgoing
7023 = FUNCTION_VALUE (build_pointer_type (type), current_function_decl);
7024 #endif
7026 /* Mark this as a function return value so integrate will delete the
7027 assignment and USE below when inlining this function. */
7028 REG_FUNCTION_VALUE_P (outgoing) = 1;
7030 #ifdef POINTERS_EXTEND_UNSIGNED
7031 /* The address may be ptr_mode and OUTGOING may be Pmode. */
7032 if (GET_MODE (outgoing) != GET_MODE (value_address))
7033 value_address = convert_memory_address (GET_MODE (outgoing),
7034 value_address);
7035 #endif
7037 emit_move_insn (outgoing, value_address);
7039 /* Show return register used to hold result (in this case the address
7040 of the result. */
7041 current_function_return_rtx = outgoing;
7044 /* If this is an implementation of throw, do what's necessary to
7045 communicate between __builtin_eh_return and the epilogue. */
7046 expand_eh_return ();
7048 /* Emit the actual code to clobber return register. */
7050 rtx seq, after;
7052 start_sequence ();
7053 clobber_return_register ();
7054 seq = get_insns ();
7055 end_sequence ();
7057 after = emit_insn_after (seq, clobber_after);
7059 if (clobber_after != after)
7060 cfun->x_clobber_return_insn = after;
7063 /* ??? This should no longer be necessary since stupid is no longer with
7064 us, but there are some parts of the compiler (eg reload_combine, and
7065 sh mach_dep_reorg) that still try and compute their own lifetime info
7066 instead of using the general framework. */
7067 use_return_register ();
7069 /* Fix up any gotos that jumped out to the outermost
7070 binding level of the function.
7071 Must follow emitting RETURN_LABEL. */
7073 /* If you have any cleanups to do at this point,
7074 and they need to create temporary variables,
7075 then you will lose. */
7076 expand_fixups (get_insns ());
7080 get_arg_pointer_save_area (f)
7081 struct function *f;
7083 rtx ret = f->x_arg_pointer_save_area;
7085 if (! ret)
7087 ret = assign_stack_local_1 (Pmode, GET_MODE_SIZE (Pmode), 0, f);
7088 f->x_arg_pointer_save_area = ret;
7091 if (f == cfun && ! f->arg_pointer_save_area_init)
7093 rtx seq;
7095 /* Save the arg pointer at the beginning of the function. The
7096 generated stack slot may not be a valid memory address, so we
7097 have to check it and fix it if necessary. */
7098 start_sequence ();
7099 emit_move_insn (validize_mem (ret), virtual_incoming_args_rtx);
7100 seq = get_insns ();
7101 end_sequence ();
7103 push_topmost_sequence ();
7104 emit_insn_after (seq, get_insns ());
7105 pop_topmost_sequence ();
7108 return ret;
7111 /* Extend a vector that records the INSN_UIDs of INSNS
7112 (a list of one or more insns). */
7114 static void
7115 record_insns (insns, vecp)
7116 rtx insns;
7117 varray_type *vecp;
7119 int i, len;
7120 rtx tmp;
7122 tmp = insns;
7123 len = 0;
7124 while (tmp != NULL_RTX)
7126 len++;
7127 tmp = NEXT_INSN (tmp);
7130 i = VARRAY_SIZE (*vecp);
7131 VARRAY_GROW (*vecp, i + len);
7132 tmp = insns;
7133 while (tmp != NULL_RTX)
7135 VARRAY_INT (*vecp, i) = INSN_UID (tmp);
7136 i++;
7137 tmp = NEXT_INSN (tmp);
7141 /* Determine how many INSN_UIDs in VEC are part of INSN. Because we can
7142 be running after reorg, SEQUENCE rtl is possible. */
7144 static int
7145 contains (insn, vec)
7146 rtx insn;
7147 varray_type vec;
7149 int i, j;
7151 if (GET_CODE (insn) == INSN
7152 && GET_CODE (PATTERN (insn)) == SEQUENCE)
7154 int count = 0;
7155 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
7156 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7157 if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == VARRAY_INT (vec, j))
7158 count++;
7159 return count;
7161 else
7163 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7164 if (INSN_UID (insn) == VARRAY_INT (vec, j))
7165 return 1;
7167 return 0;
7171 prologue_epilogue_contains (insn)
7172 rtx insn;
7174 if (contains (insn, prologue))
7175 return 1;
7176 if (contains (insn, epilogue))
7177 return 1;
7178 return 0;
7182 sibcall_epilogue_contains (insn)
7183 rtx insn;
7185 if (sibcall_epilogue)
7186 return contains (insn, sibcall_epilogue);
7187 return 0;
7190 #ifdef HAVE_return
7191 /* Insert gen_return at the end of block BB. This also means updating
7192 block_for_insn appropriately. */
7194 static void
7195 emit_return_into_block (bb, line_note)
7196 basic_block bb;
7197 rtx line_note;
7199 rtx p, end;
7201 p = NEXT_INSN (bb->end);
7202 end = emit_jump_insn_after (gen_return (), bb->end);
7203 if (line_note)
7204 emit_line_note_after (NOTE_SOURCE_FILE (line_note),
7205 NOTE_LINE_NUMBER (line_note), PREV_INSN (bb->end));
7207 #endif /* HAVE_return */
7209 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
7211 /* These functions convert the epilogue into a variant that does not modify the
7212 stack pointer. This is used in cases where a function returns an object
7213 whose size is not known until it is computed. The called function leaves the
7214 object on the stack, leaves the stack depressed, and returns a pointer to
7215 the object.
7217 What we need to do is track all modifications and references to the stack
7218 pointer, deleting the modifications and changing the references to point to
7219 the location the stack pointer would have pointed to had the modifications
7220 taken place.
7222 These functions need to be portable so we need to make as few assumptions
7223 about the epilogue as we can. However, the epilogue basically contains
7224 three things: instructions to reset the stack pointer, instructions to
7225 reload registers, possibly including the frame pointer, and an
7226 instruction to return to the caller.
7228 If we can't be sure of what a relevant epilogue insn is doing, we abort.
7229 We also make no attempt to validate the insns we make since if they are
7230 invalid, we probably can't do anything valid. The intent is that these
7231 routines get "smarter" as more and more machines start to use them and
7232 they try operating on different epilogues.
7234 We use the following structure to track what the part of the epilogue that
7235 we've already processed has done. We keep two copies of the SP equivalence,
7236 one for use during the insn we are processing and one for use in the next
7237 insn. The difference is because one part of a PARALLEL may adjust SP
7238 and the other may use it. */
7240 struct epi_info
7242 rtx sp_equiv_reg; /* REG that SP is set from, perhaps SP. */
7243 HOST_WIDE_INT sp_offset; /* Offset from SP_EQUIV_REG of present SP. */
7244 rtx new_sp_equiv_reg; /* REG to be used at end of insn. */
7245 HOST_WIDE_INT new_sp_offset; /* Offset to be used at end of insn. */
7246 rtx equiv_reg_src; /* If nonzero, the value that SP_EQUIV_REG
7247 should be set to once we no longer need
7248 its value. */
7251 static void handle_epilogue_set PARAMS ((rtx, struct epi_info *));
7252 static void emit_equiv_load PARAMS ((struct epi_info *));
7254 /* Modify INSN, a list of one or more insns that is part of the epilogue, to
7255 no modifications to the stack pointer. Return the new list of insns. */
7257 static rtx
7258 keep_stack_depressed (insns)
7259 rtx insns;
7261 int j;
7262 struct epi_info info;
7263 rtx insn, next;
7265 /* If the epilogue is just a single instruction, it ust be OK as is. */
7267 if (NEXT_INSN (insns) == NULL_RTX)
7268 return insns;
7270 /* Otherwise, start a sequence, initialize the information we have, and
7271 process all the insns we were given. */
7272 start_sequence ();
7274 info.sp_equiv_reg = stack_pointer_rtx;
7275 info.sp_offset = 0;
7276 info.equiv_reg_src = 0;
7278 insn = insns;
7279 next = NULL_RTX;
7280 while (insn != NULL_RTX)
7282 next = NEXT_INSN (insn);
7284 if (!INSN_P (insn))
7286 add_insn (insn);
7287 insn = next;
7288 continue;
7291 /* If this insn references the register that SP is equivalent to and
7292 we have a pending load to that register, we must force out the load
7293 first and then indicate we no longer know what SP's equivalent is. */
7294 if (info.equiv_reg_src != 0
7295 && reg_referenced_p (info.sp_equiv_reg, PATTERN (insn)))
7297 emit_equiv_load (&info);
7298 info.sp_equiv_reg = 0;
7301 info.new_sp_equiv_reg = info.sp_equiv_reg;
7302 info.new_sp_offset = info.sp_offset;
7304 /* If this is a (RETURN) and the return address is on the stack,
7305 update the address and change to an indirect jump. */
7306 if (GET_CODE (PATTERN (insn)) == RETURN
7307 || (GET_CODE (PATTERN (insn)) == PARALLEL
7308 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == RETURN))
7310 rtx retaddr = INCOMING_RETURN_ADDR_RTX;
7311 rtx base = 0;
7312 HOST_WIDE_INT offset = 0;
7313 rtx jump_insn, jump_set;
7315 /* If the return address is in a register, we can emit the insn
7316 unchanged. Otherwise, it must be a MEM and we see what the
7317 base register and offset are. In any case, we have to emit any
7318 pending load to the equivalent reg of SP, if any. */
7319 if (GET_CODE (retaddr) == REG)
7321 emit_equiv_load (&info);
7322 add_insn (insn);
7323 insn = next;
7324 continue;
7326 else if (GET_CODE (retaddr) == MEM
7327 && GET_CODE (XEXP (retaddr, 0)) == REG)
7328 base = gen_rtx_REG (Pmode, REGNO (XEXP (retaddr, 0))), offset = 0;
7329 else if (GET_CODE (retaddr) == MEM
7330 && GET_CODE (XEXP (retaddr, 0)) == PLUS
7331 && GET_CODE (XEXP (XEXP (retaddr, 0), 0)) == REG
7332 && GET_CODE (XEXP (XEXP (retaddr, 0), 1)) == CONST_INT)
7334 base = gen_rtx_REG (Pmode, REGNO (XEXP (XEXP (retaddr, 0), 0)));
7335 offset = INTVAL (XEXP (XEXP (retaddr, 0), 1));
7337 else
7338 abort ();
7340 /* If the base of the location containing the return pointer
7341 is SP, we must update it with the replacement address. Otherwise,
7342 just build the necessary MEM. */
7343 retaddr = plus_constant (base, offset);
7344 if (base == stack_pointer_rtx)
7345 retaddr = simplify_replace_rtx (retaddr, stack_pointer_rtx,
7346 plus_constant (info.sp_equiv_reg,
7347 info.sp_offset));
7349 retaddr = gen_rtx_MEM (Pmode, retaddr);
7351 /* If there is a pending load to the equivalent register for SP
7352 and we reference that register, we must load our address into
7353 a scratch register and then do that load. */
7354 if (info.equiv_reg_src
7355 && reg_overlap_mentioned_p (info.equiv_reg_src, retaddr))
7357 unsigned int regno;
7358 rtx reg;
7360 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
7361 if (HARD_REGNO_MODE_OK (regno, Pmode)
7362 && !fixed_regs[regno]
7363 && TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)
7364 && !REGNO_REG_SET_P (EXIT_BLOCK_PTR->global_live_at_start,
7365 regno)
7366 && !refers_to_regno_p (regno,
7367 regno + HARD_REGNO_NREGS (regno,
7368 Pmode),
7369 info.equiv_reg_src, NULL))
7370 break;
7372 if (regno == FIRST_PSEUDO_REGISTER)
7373 abort ();
7375 reg = gen_rtx_REG (Pmode, regno);
7376 emit_move_insn (reg, retaddr);
7377 retaddr = reg;
7380 emit_equiv_load (&info);
7381 jump_insn = emit_jump_insn (gen_indirect_jump (retaddr));
7383 /* Show the SET in the above insn is a RETURN. */
7384 jump_set = single_set (jump_insn);
7385 if (jump_set == 0)
7386 abort ();
7387 else
7388 SET_IS_RETURN_P (jump_set) = 1;
7391 /* If SP is not mentioned in the pattern and its equivalent register, if
7392 any, is not modified, just emit it. Otherwise, if neither is set,
7393 replace the reference to SP and emit the insn. If none of those are
7394 true, handle each SET individually. */
7395 else if (!reg_mentioned_p (stack_pointer_rtx, PATTERN (insn))
7396 && (info.sp_equiv_reg == stack_pointer_rtx
7397 || !reg_set_p (info.sp_equiv_reg, insn)))
7398 add_insn (insn);
7399 else if (! reg_set_p (stack_pointer_rtx, insn)
7400 && (info.sp_equiv_reg == stack_pointer_rtx
7401 || !reg_set_p (info.sp_equiv_reg, insn)))
7403 if (! validate_replace_rtx (stack_pointer_rtx,
7404 plus_constant (info.sp_equiv_reg,
7405 info.sp_offset),
7406 insn))
7407 abort ();
7409 add_insn (insn);
7411 else if (GET_CODE (PATTERN (insn)) == SET)
7412 handle_epilogue_set (PATTERN (insn), &info);
7413 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
7415 for (j = 0; j < XVECLEN (PATTERN (insn), 0); j++)
7416 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET)
7417 handle_epilogue_set (XVECEXP (PATTERN (insn), 0, j), &info);
7419 else
7420 add_insn (insn);
7422 info.sp_equiv_reg = info.new_sp_equiv_reg;
7423 info.sp_offset = info.new_sp_offset;
7425 insn = next;
7428 insns = get_insns ();
7429 end_sequence ();
7430 return insns;
7433 /* SET is a SET from an insn in the epilogue. P is a pointer to the epi_info
7434 structure that contains information about what we've seen so far. We
7435 process this SET by either updating that data or by emitting one or
7436 more insns. */
7438 static void
7439 handle_epilogue_set (set, p)
7440 rtx set;
7441 struct epi_info *p;
7443 /* First handle the case where we are setting SP. Record what it is being
7444 set from. If unknown, abort. */
7445 if (reg_set_p (stack_pointer_rtx, set))
7447 if (SET_DEST (set) != stack_pointer_rtx)
7448 abort ();
7450 if (GET_CODE (SET_SRC (set)) == PLUS
7451 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
7453 p->new_sp_equiv_reg = XEXP (SET_SRC (set), 0);
7454 p->new_sp_offset = INTVAL (XEXP (SET_SRC (set), 1));
7456 else
7457 p->new_sp_equiv_reg = SET_SRC (set), p->new_sp_offset = 0;
7459 /* If we are adjusting SP, we adjust from the old data. */
7460 if (p->new_sp_equiv_reg == stack_pointer_rtx)
7462 p->new_sp_equiv_reg = p->sp_equiv_reg;
7463 p->new_sp_offset += p->sp_offset;
7466 if (p->new_sp_equiv_reg == 0 || GET_CODE (p->new_sp_equiv_reg) != REG)
7467 abort ();
7469 return;
7472 /* Next handle the case where we are setting SP's equivalent register.
7473 If we already have a value to set it to, abort. We could update, but
7474 there seems little point in handling that case. Note that we have
7475 to allow for the case where we are setting the register set in
7476 the previous part of a PARALLEL inside a single insn. But use the
7477 old offset for any updates within this insn. */
7478 else if (p->new_sp_equiv_reg != 0 && reg_set_p (p->new_sp_equiv_reg, set))
7480 if (!rtx_equal_p (p->new_sp_equiv_reg, SET_DEST (set))
7481 || p->equiv_reg_src != 0)
7482 abort ();
7483 else
7484 p->equiv_reg_src
7485 = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7486 plus_constant (p->sp_equiv_reg,
7487 p->sp_offset));
7490 /* Otherwise, replace any references to SP in the insn to its new value
7491 and emit the insn. */
7492 else
7494 SET_SRC (set) = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7495 plus_constant (p->sp_equiv_reg,
7496 p->sp_offset));
7497 SET_DEST (set) = simplify_replace_rtx (SET_DEST (set), stack_pointer_rtx,
7498 plus_constant (p->sp_equiv_reg,
7499 p->sp_offset));
7500 emit_insn (set);
7504 /* Emit an insn to do the load shown in p->equiv_reg_src, if needed. */
7506 static void
7507 emit_equiv_load (p)
7508 struct epi_info *p;
7510 if (p->equiv_reg_src != 0)
7511 emit_move_insn (p->sp_equiv_reg, p->equiv_reg_src);
7513 p->equiv_reg_src = 0;
7515 #endif
7517 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
7518 this into place with notes indicating where the prologue ends and where
7519 the epilogue begins. Update the basic block information when possible. */
7521 void
7522 thread_prologue_and_epilogue_insns (f)
7523 rtx f ATTRIBUTE_UNUSED;
7525 int inserted = 0;
7526 edge e;
7527 #if defined (HAVE_sibcall_epilogue) || defined (HAVE_epilogue) || defined (HAVE_return) || defined (HAVE_prologue)
7528 rtx seq;
7529 #endif
7530 #ifdef HAVE_prologue
7531 rtx prologue_end = NULL_RTX;
7532 #endif
7533 #if defined (HAVE_epilogue) || defined(HAVE_return)
7534 rtx epilogue_end = NULL_RTX;
7535 #endif
7537 #ifdef HAVE_prologue
7538 if (HAVE_prologue)
7540 start_sequence ();
7541 seq = gen_prologue ();
7542 emit_insn (seq);
7544 /* Retain a map of the prologue insns. */
7545 record_insns (seq, &prologue);
7546 prologue_end = emit_note (NULL, NOTE_INSN_PROLOGUE_END);
7548 seq = get_insns ();
7549 end_sequence ();
7551 /* Can't deal with multiple successors of the entry block
7552 at the moment. Function should always have at least one
7553 entry point. */
7554 if (!ENTRY_BLOCK_PTR->succ || ENTRY_BLOCK_PTR->succ->succ_next)
7555 abort ();
7557 insert_insn_on_edge (seq, ENTRY_BLOCK_PTR->succ);
7558 inserted = 1;
7560 #endif
7562 /* If the exit block has no non-fake predecessors, we don't need
7563 an epilogue. */
7564 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7565 if ((e->flags & EDGE_FAKE) == 0)
7566 break;
7567 if (e == NULL)
7568 goto epilogue_done;
7570 #ifdef HAVE_return
7571 if (optimize && HAVE_return)
7573 /* If we're allowed to generate a simple return instruction,
7574 then by definition we don't need a full epilogue. Examine
7575 the block that falls through to EXIT. If it does not
7576 contain any code, examine its predecessors and try to
7577 emit (conditional) return instructions. */
7579 basic_block last;
7580 edge e_next;
7581 rtx label;
7583 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7584 if (e->flags & EDGE_FALLTHRU)
7585 break;
7586 if (e == NULL)
7587 goto epilogue_done;
7588 last = e->src;
7590 /* Verify that there are no active instructions in the last block. */
7591 label = last->end;
7592 while (label && GET_CODE (label) != CODE_LABEL)
7594 if (active_insn_p (label))
7595 break;
7596 label = PREV_INSN (label);
7599 if (last->head == label && GET_CODE (label) == CODE_LABEL)
7601 rtx epilogue_line_note = NULL_RTX;
7603 /* Locate the line number associated with the closing brace,
7604 if we can find one. */
7605 for (seq = get_last_insn ();
7606 seq && ! active_insn_p (seq);
7607 seq = PREV_INSN (seq))
7608 if (GET_CODE (seq) == NOTE && NOTE_LINE_NUMBER (seq) > 0)
7610 epilogue_line_note = seq;
7611 break;
7614 for (e = last->pred; e; e = e_next)
7616 basic_block bb = e->src;
7617 rtx jump;
7619 e_next = e->pred_next;
7620 if (bb == ENTRY_BLOCK_PTR)
7621 continue;
7623 jump = bb->end;
7624 if ((GET_CODE (jump) != JUMP_INSN) || JUMP_LABEL (jump) != label)
7625 continue;
7627 /* If we have an unconditional jump, we can replace that
7628 with a simple return instruction. */
7629 if (simplejump_p (jump))
7631 emit_return_into_block (bb, epilogue_line_note);
7632 delete_insn (jump);
7635 /* If we have a conditional jump, we can try to replace
7636 that with a conditional return instruction. */
7637 else if (condjump_p (jump))
7639 rtx ret, *loc;
7641 ret = SET_SRC (PATTERN (jump));
7642 if (GET_CODE (XEXP (ret, 1)) == LABEL_REF)
7643 loc = &XEXP (ret, 1);
7644 else
7645 loc = &XEXP (ret, 2);
7646 ret = gen_rtx_RETURN (VOIDmode);
7648 if (! validate_change (jump, loc, ret, 0))
7649 continue;
7650 if (JUMP_LABEL (jump))
7651 LABEL_NUSES (JUMP_LABEL (jump))--;
7653 /* If this block has only one successor, it both jumps
7654 and falls through to the fallthru block, so we can't
7655 delete the edge. */
7656 if (bb->succ->succ_next == NULL)
7657 continue;
7659 else
7660 continue;
7662 /* Fix up the CFG for the successful change we just made. */
7663 redirect_edge_succ (e, EXIT_BLOCK_PTR);
7666 /* Emit a return insn for the exit fallthru block. Whether
7667 this is still reachable will be determined later. */
7669 emit_barrier_after (last->end);
7670 emit_return_into_block (last, epilogue_line_note);
7671 epilogue_end = last->end;
7672 last->succ->flags &= ~EDGE_FALLTHRU;
7673 goto epilogue_done;
7676 #endif
7677 #ifdef HAVE_epilogue
7678 if (HAVE_epilogue)
7680 /* Find the edge that falls through to EXIT. Other edges may exist
7681 due to RETURN instructions, but those don't need epilogues.
7682 There really shouldn't be a mixture -- either all should have
7683 been converted or none, however... */
7685 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7686 if (e->flags & EDGE_FALLTHRU)
7687 break;
7688 if (e == NULL)
7689 goto epilogue_done;
7691 start_sequence ();
7692 epilogue_end = emit_note (NULL, NOTE_INSN_EPILOGUE_BEG);
7694 seq = gen_epilogue ();
7696 #ifdef INCOMING_RETURN_ADDR_RTX
7697 /* If this function returns with the stack depressed and we can support
7698 it, massage the epilogue to actually do that. */
7699 if (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
7700 && TYPE_RETURNS_STACK_DEPRESSED (TREE_TYPE (current_function_decl)))
7701 seq = keep_stack_depressed (seq);
7702 #endif
7704 emit_jump_insn (seq);
7706 /* Retain a map of the epilogue insns. */
7707 record_insns (seq, &epilogue);
7709 seq = get_insns ();
7710 end_sequence ();
7712 insert_insn_on_edge (seq, e);
7713 inserted = 1;
7715 #endif
7716 epilogue_done:
7718 if (inserted)
7719 commit_edge_insertions ();
7721 #ifdef HAVE_sibcall_epilogue
7722 /* Emit sibling epilogues before any sibling call sites. */
7723 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7725 basic_block bb = e->src;
7726 rtx insn = bb->end;
7727 rtx i;
7728 rtx newinsn;
7730 if (GET_CODE (insn) != CALL_INSN
7731 || ! SIBLING_CALL_P (insn))
7732 continue;
7734 start_sequence ();
7735 emit_insn (gen_sibcall_epilogue ());
7736 seq = get_insns ();
7737 end_sequence ();
7739 /* Retain a map of the epilogue insns. Used in life analysis to
7740 avoid getting rid of sibcall epilogue insns. Do this before we
7741 actually emit the sequence. */
7742 record_insns (seq, &sibcall_epilogue);
7744 i = PREV_INSN (insn);
7745 newinsn = emit_insn_before (seq, insn);
7747 #endif
7749 #ifdef HAVE_prologue
7750 if (prologue_end)
7752 rtx insn, prev;
7754 /* GDB handles `break f' by setting a breakpoint on the first
7755 line note after the prologue. Which means (1) that if
7756 there are line number notes before where we inserted the
7757 prologue we should move them, and (2) we should generate a
7758 note before the end of the first basic block, if there isn't
7759 one already there.
7761 ??? This behavior is completely broken when dealing with
7762 multiple entry functions. We simply place the note always
7763 into first basic block and let alternate entry points
7764 to be missed.
7767 for (insn = prologue_end; insn; insn = prev)
7769 prev = PREV_INSN (insn);
7770 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7772 /* Note that we cannot reorder the first insn in the
7773 chain, since rest_of_compilation relies on that
7774 remaining constant. */
7775 if (prev == NULL)
7776 break;
7777 reorder_insns (insn, insn, prologue_end);
7781 /* Find the last line number note in the first block. */
7782 for (insn = ENTRY_BLOCK_PTR->next_bb->end;
7783 insn != prologue_end && insn;
7784 insn = PREV_INSN (insn))
7785 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7786 break;
7788 /* If we didn't find one, make a copy of the first line number
7789 we run across. */
7790 if (! insn)
7792 for (insn = next_active_insn (prologue_end);
7793 insn;
7794 insn = PREV_INSN (insn))
7795 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7797 emit_line_note_after (NOTE_SOURCE_FILE (insn),
7798 NOTE_LINE_NUMBER (insn),
7799 prologue_end);
7800 break;
7804 #endif
7805 #ifdef HAVE_epilogue
7806 if (epilogue_end)
7808 rtx insn, next;
7810 /* Similarly, move any line notes that appear after the epilogue.
7811 There is no need, however, to be quite so anal about the existence
7812 of such a note. */
7813 for (insn = epilogue_end; insn; insn = next)
7815 next = NEXT_INSN (insn);
7816 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7817 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
7820 #endif
7823 /* Reposition the prologue-end and epilogue-begin notes after instruction
7824 scheduling and delayed branch scheduling. */
7826 void
7827 reposition_prologue_and_epilogue_notes (f)
7828 rtx f ATTRIBUTE_UNUSED;
7830 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
7831 rtx insn, last, note;
7832 int len;
7834 if ((len = VARRAY_SIZE (prologue)) > 0)
7836 last = 0, note = 0;
7838 /* Scan from the beginning until we reach the last prologue insn.
7839 We apparently can't depend on basic_block_{head,end} after
7840 reorg has run. */
7841 for (insn = f; insn; insn = NEXT_INSN (insn))
7843 if (GET_CODE (insn) == NOTE)
7845 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
7846 note = insn;
7848 else if (contains (insn, prologue))
7850 last = insn;
7851 if (--len == 0)
7852 break;
7856 if (last)
7858 rtx next;
7860 /* Find the prologue-end note if we haven't already, and
7861 move it to just after the last prologue insn. */
7862 if (note == 0)
7864 for (note = last; (note = NEXT_INSN (note));)
7865 if (GET_CODE (note) == NOTE
7866 && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
7867 break;
7870 next = NEXT_INSN (note);
7872 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
7873 if (GET_CODE (last) == CODE_LABEL)
7874 last = NEXT_INSN (last);
7875 reorder_insns (note, note, last);
7879 if ((len = VARRAY_SIZE (epilogue)) > 0)
7881 last = 0, note = 0;
7883 /* Scan from the end until we reach the first epilogue insn.
7884 We apparently can't depend on basic_block_{head,end} after
7885 reorg has run. */
7886 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
7888 if (GET_CODE (insn) == NOTE)
7890 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
7891 note = insn;
7893 else if (contains (insn, epilogue))
7895 last = insn;
7896 if (--len == 0)
7897 break;
7901 if (last)
7903 /* Find the epilogue-begin note if we haven't already, and
7904 move it to just before the first epilogue insn. */
7905 if (note == 0)
7907 for (note = insn; (note = PREV_INSN (note));)
7908 if (GET_CODE (note) == NOTE
7909 && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
7910 break;
7913 if (PREV_INSN (last) != note)
7914 reorder_insns (note, note, PREV_INSN (last));
7917 #endif /* HAVE_prologue or HAVE_epilogue */
7920 /* Called once, at initialization, to initialize function.c. */
7922 void
7923 init_function_once ()
7925 VARRAY_INT_INIT (prologue, 0, "prologue");
7926 VARRAY_INT_INIT (epilogue, 0, "epilogue");
7927 VARRAY_INT_INIT (sibcall_epilogue, 0, "sibcall_epilogue");
7930 #include "gt-function.h"