PR c++/8795
[official-gcc.git] / gcc / function.c
blobfd7c7f793503002132dda18b5b9b765a813929a0
1 /* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This file handles the generation of rtl code from tree structure
23 at the level of the function as a whole.
24 It creates the rtl expressions for parameters and auto variables
25 and has full responsibility for allocating stack slots.
27 `expand_function_start' is called at the beginning of a function,
28 before the function body is parsed, and `expand_function_end' is
29 called after parsing the body.
31 Call `assign_stack_local' to allocate a stack slot for a local variable.
32 This is usually done during the RTL generation for the function body,
33 but it can also be done in the reload pass when a pseudo-register does
34 not get a hard register.
36 Call `put_var_into_stack' when you learn, belatedly, that a variable
37 previously given a pseudo-register must in fact go in the stack.
38 This function changes the DECL_RTL to be a stack slot instead of a reg
39 then scans all the RTL instructions so far generated to correct them. */
41 #include "config.h"
42 #include "system.h"
43 #include "coretypes.h"
44 #include "tm.h"
45 #include "rtl.h"
46 #include "tree.h"
47 #include "flags.h"
48 #include "except.h"
49 #include "function.h"
50 #include "expr.h"
51 #include "optabs.h"
52 #include "libfuncs.h"
53 #include "regs.h"
54 #include "hard-reg-set.h"
55 #include "insn-config.h"
56 #include "recog.h"
57 #include "output.h"
58 #include "basic-block.h"
59 #include "toplev.h"
60 #include "hashtab.h"
61 #include "ggc.h"
62 #include "tm_p.h"
63 #include "integrate.h"
64 #include "langhooks.h"
66 #ifndef TRAMPOLINE_ALIGNMENT
67 #define TRAMPOLINE_ALIGNMENT FUNCTION_BOUNDARY
68 #endif
70 #ifndef LOCAL_ALIGNMENT
71 #define LOCAL_ALIGNMENT(TYPE, ALIGNMENT) ALIGNMENT
72 #endif
74 #ifndef STACK_ALIGNMENT_NEEDED
75 #define STACK_ALIGNMENT_NEEDED 1
76 #endif
78 /* Some systems use __main in a way incompatible with its use in gcc, in these
79 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
80 give the same symbol without quotes for an alternative entry point. You
81 must define both, or neither. */
82 #ifndef NAME__MAIN
83 #define NAME__MAIN "__main"
84 #endif
86 /* Round a value to the lowest integer less than it that is a multiple of
87 the required alignment. Avoid using division in case the value is
88 negative. Assume the alignment is a power of two. */
89 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
91 /* Similar, but round to the next highest integer that meets the
92 alignment. */
93 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
95 /* NEED_SEPARATE_AP means that we cannot derive ap from the value of fp
96 during rtl generation. If they are different register numbers, this is
97 always true. It may also be true if
98 FIRST_PARM_OFFSET - STARTING_FRAME_OFFSET is not a constant during rtl
99 generation. See fix_lexical_addr for details. */
101 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
102 #define NEED_SEPARATE_AP
103 #endif
105 /* Nonzero if function being compiled doesn't contain any calls
106 (ignoring the prologue and epilogue). This is set prior to
107 local register allocation and is valid for the remaining
108 compiler passes. */
109 int current_function_is_leaf;
111 /* Nonzero if function being compiled doesn't contain any instructions
112 that can throw an exception. This is set prior to final. */
114 int current_function_nothrow;
116 /* Nonzero if function being compiled doesn't modify the stack pointer
117 (ignoring the prologue and epilogue). This is only valid after
118 life_analysis has run. */
119 int current_function_sp_is_unchanging;
121 /* Nonzero if the function being compiled is a leaf function which only
122 uses leaf registers. This is valid after reload (specifically after
123 sched2) and is useful only if the port defines LEAF_REGISTERS. */
124 int current_function_uses_only_leaf_regs;
126 /* Nonzero once virtual register instantiation has been done.
127 assign_stack_local uses frame_pointer_rtx when this is nonzero.
128 calls.c:emit_library_call_value_1 uses it to set up
129 post-instantiation libcalls. */
130 int virtuals_instantiated;
132 /* Nonzero if at least one trampoline has been created. */
133 int trampolines_created;
135 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
136 static GTY(()) int funcdef_no;
138 /* These variables hold pointers to functions to create and destroy
139 target specific, per-function data structures. */
140 struct machine_function * (*init_machine_status) (void);
142 /* The FUNCTION_DECL for an inline function currently being expanded. */
143 tree inline_function_decl;
145 /* The currently compiled function. */
146 struct function *cfun = 0;
148 /* These arrays record the INSN_UIDs of the prologue and epilogue insns. */
149 static GTY(()) varray_type prologue;
150 static GTY(()) varray_type epilogue;
152 /* Array of INSN_UIDs to hold the INSN_UIDs for each sibcall epilogue
153 in this function. */
154 static GTY(()) varray_type sibcall_epilogue;
156 /* In order to evaluate some expressions, such as function calls returning
157 structures in memory, we need to temporarily allocate stack locations.
158 We record each allocated temporary in the following structure.
160 Associated with each temporary slot is a nesting level. When we pop up
161 one level, all temporaries associated with the previous level are freed.
162 Normally, all temporaries are freed after the execution of the statement
163 in which they were created. However, if we are inside a ({...}) grouping,
164 the result may be in a temporary and hence must be preserved. If the
165 result could be in a temporary, we preserve it if we can determine which
166 one it is in. If we cannot determine which temporary may contain the
167 result, all temporaries are preserved. A temporary is preserved by
168 pretending it was allocated at the previous nesting level.
170 Automatic variables are also assigned temporary slots, at the nesting
171 level where they are defined. They are marked a "kept" so that
172 free_temp_slots will not free them. */
174 struct temp_slot GTY(())
176 /* Points to next temporary slot. */
177 struct temp_slot *next;
178 /* The rtx to used to reference the slot. */
179 rtx slot;
180 /* The rtx used to represent the address if not the address of the
181 slot above. May be an EXPR_LIST if multiple addresses exist. */
182 rtx address;
183 /* The alignment (in bits) of the slot. */
184 unsigned int align;
185 /* The size, in units, of the slot. */
186 HOST_WIDE_INT size;
187 /* The type of the object in the slot, or zero if it doesn't correspond
188 to a type. We use this to determine whether a slot can be reused.
189 It can be reused if objects of the type of the new slot will always
190 conflict with objects of the type of the old slot. */
191 tree type;
192 /* The value of `sequence_rtl_expr' when this temporary is allocated. */
193 tree rtl_expr;
194 /* Nonzero if this temporary is currently in use. */
195 char in_use;
196 /* Nonzero if this temporary has its address taken. */
197 char addr_taken;
198 /* Nesting level at which this slot is being used. */
199 int level;
200 /* Nonzero if this should survive a call to free_temp_slots. */
201 int keep;
202 /* The offset of the slot from the frame_pointer, including extra space
203 for alignment. This info is for combine_temp_slots. */
204 HOST_WIDE_INT base_offset;
205 /* The size of the slot, including extra space for alignment. This
206 info is for combine_temp_slots. */
207 HOST_WIDE_INT full_size;
210 /* This structure is used to record MEMs or pseudos used to replace VAR, any
211 SUBREGs of VAR, and any MEMs containing VAR as an address. We need to
212 maintain this list in case two operands of an insn were required to match;
213 in that case we must ensure we use the same replacement. */
215 struct fixup_replacement GTY(())
217 rtx old;
218 rtx new;
219 struct fixup_replacement *next;
222 struct insns_for_mem_entry
224 /* A MEM. */
225 rtx key;
226 /* These are the INSNs which reference the MEM. */
227 rtx insns;
230 /* Forward declarations. */
232 static rtx assign_stack_local_1 (enum machine_mode, HOST_WIDE_INT, int,
233 struct function *);
234 static struct temp_slot *find_temp_slot_from_address (rtx);
235 static void put_reg_into_stack (struct function *, rtx, tree, enum machine_mode,
236 enum machine_mode, int, unsigned int, int, htab_t);
237 static void schedule_fixup_var_refs (struct function *, rtx, tree, enum machine_mode,
238 htab_t);
239 static void fixup_var_refs (rtx, enum machine_mode, int, rtx, htab_t);
240 static struct fixup_replacement
241 *find_fixup_replacement (struct fixup_replacement **, rtx);
242 static void fixup_var_refs_insns (rtx, rtx, enum machine_mode, int, int, rtx);
243 static void fixup_var_refs_insns_with_hash (htab_t, rtx, enum machine_mode, int, rtx);
244 static void fixup_var_refs_insn (rtx, rtx, enum machine_mode, int, int, rtx);
245 static void fixup_var_refs_1 (rtx, enum machine_mode, rtx *, rtx,
246 struct fixup_replacement **, rtx);
247 static rtx fixup_memory_subreg (rtx, rtx, enum machine_mode, int);
248 static rtx walk_fixup_memory_subreg (rtx, rtx, enum machine_mode, int);
249 static rtx fixup_stack_1 (rtx, rtx);
250 static void optimize_bit_field (rtx, rtx, rtx *);
251 static void instantiate_decls (tree, int);
252 static void instantiate_decls_1 (tree, int);
253 static void instantiate_decl (rtx, HOST_WIDE_INT, int);
254 static rtx instantiate_new_reg (rtx, HOST_WIDE_INT *);
255 static int instantiate_virtual_regs_1 (rtx *, rtx, int);
256 static void delete_handlers (void);
257 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
258 static void pad_below (struct args_size *, enum machine_mode, tree);
259 static rtx round_trampoline_addr (rtx);
260 static rtx adjust_trampoline_addr (rtx);
261 static tree *identify_blocks_1 (rtx, tree *, tree *, tree *);
262 static void reorder_blocks_0 (tree);
263 static void reorder_blocks_1 (rtx, tree, varray_type *);
264 static void reorder_fix_fragments (tree);
265 static tree blocks_nreverse (tree);
266 static int all_blocks (tree, tree *);
267 static tree *get_block_vector (tree, int *);
268 extern tree debug_find_var_in_block_tree (tree, tree);
269 /* We always define `record_insns' even if its not used so that we
270 can always export `prologue_epilogue_contains'. */
271 static void record_insns (rtx, varray_type *) ATTRIBUTE_UNUSED;
272 static int contains (rtx, varray_type);
273 #ifdef HAVE_return
274 static void emit_return_into_block (basic_block, rtx);
275 #endif
276 static void put_addressof_into_stack (rtx, htab_t);
277 static bool purge_addressof_1 (rtx *, rtx, int, int, int, htab_t);
278 static void purge_single_hard_subreg_set (rtx);
279 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
280 static rtx keep_stack_depressed (rtx);
281 #endif
282 static int is_addressof (rtx *, void *);
283 static hashval_t insns_for_mem_hash (const void *);
284 static int insns_for_mem_comp (const void *, const void *);
285 static int insns_for_mem_walk (rtx *, void *);
286 static void compute_insns_for_mem (rtx, rtx, htab_t);
287 static void prepare_function_start (void);
288 static void do_clobber_return_reg (rtx, void *);
289 static void do_use_return_reg (rtx, void *);
290 static void instantiate_virtual_regs_lossage (rtx);
291 static tree split_complex_args (tree);
292 static void set_insn_locators (rtx, int) ATTRIBUTE_UNUSED;
294 /* Pointer to chain of `struct function' for containing functions. */
295 static GTY(()) struct function *outer_function_chain;
297 /* List of insns that were postponed by purge_addressof_1. */
298 static rtx postponed_insns;
300 /* Given a function decl for a containing function,
301 return the `struct function' for it. */
303 struct function *
304 find_function_data (tree decl)
306 struct function *p;
308 for (p = outer_function_chain; p; p = p->outer)
309 if (p->decl == decl)
310 return p;
312 abort ();
315 /* Save the current context for compilation of a nested function.
316 This is called from language-specific code. The caller should use
317 the enter_nested langhook to save any language-specific state,
318 since this function knows only about language-independent
319 variables. */
321 void
322 push_function_context_to (tree context)
324 struct function *p;
326 if (context)
328 if (context == current_function_decl)
329 cfun->contains_functions = 1;
330 else
332 struct function *containing = find_function_data (context);
333 containing->contains_functions = 1;
337 if (cfun == 0)
338 init_dummy_function_start ();
339 p = cfun;
341 p->outer = outer_function_chain;
342 outer_function_chain = p;
343 p->fixup_var_refs_queue = 0;
345 (*lang_hooks.function.enter_nested) (p);
347 cfun = 0;
350 void
351 push_function_context (void)
353 push_function_context_to (current_function_decl);
356 /* Restore the last saved context, at the end of a nested function.
357 This function is called from language-specific code. */
359 void
360 pop_function_context_from (tree context ATTRIBUTE_UNUSED)
362 struct function *p = outer_function_chain;
363 struct var_refs_queue *queue;
365 cfun = p;
366 outer_function_chain = p->outer;
368 current_function_decl = p->decl;
369 reg_renumber = 0;
371 restore_emit_status (p);
373 (*lang_hooks.function.leave_nested) (p);
375 /* Finish doing put_var_into_stack for any of our variables which became
376 addressable during the nested function. If only one entry has to be
377 fixed up, just do that one. Otherwise, first make a list of MEMs that
378 are not to be unshared. */
379 if (p->fixup_var_refs_queue == 0)
381 else if (p->fixup_var_refs_queue->next == 0)
382 fixup_var_refs (p->fixup_var_refs_queue->modified,
383 p->fixup_var_refs_queue->promoted_mode,
384 p->fixup_var_refs_queue->unsignedp,
385 p->fixup_var_refs_queue->modified, 0);
386 else
388 rtx list = 0;
390 for (queue = p->fixup_var_refs_queue; queue; queue = queue->next)
391 list = gen_rtx_EXPR_LIST (VOIDmode, queue->modified, list);
393 for (queue = p->fixup_var_refs_queue; queue; queue = queue->next)
394 fixup_var_refs (queue->modified, queue->promoted_mode,
395 queue->unsignedp, list, 0);
399 p->fixup_var_refs_queue = 0;
401 /* Reset variables that have known state during rtx generation. */
402 rtx_equal_function_value_matters = 1;
403 virtuals_instantiated = 0;
404 generating_concat_p = 1;
407 void
408 pop_function_context (void)
410 pop_function_context_from (current_function_decl);
413 /* Clear out all parts of the state in F that can safely be discarded
414 after the function has been parsed, but not compiled, to let
415 garbage collection reclaim the memory. */
417 void
418 free_after_parsing (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 (struct function *f)
436 f->eh = NULL;
437 f->expr = NULL;
438 f->emit = NULL;
439 f->varasm = NULL;
440 f->machine = NULL;
442 f->x_temp_slots = NULL;
443 f->arg_offset_rtx = NULL;
444 f->return_rtx = NULL;
445 f->internal_arg_pointer = NULL;
446 f->x_nonlocal_labels = NULL;
447 f->x_nonlocal_goto_handler_slots = NULL;
448 f->x_nonlocal_goto_handler_labels = NULL;
449 f->x_nonlocal_goto_stack_level = NULL;
450 f->x_cleanup_label = NULL;
451 f->x_return_label = NULL;
452 f->computed_goto_common_label = NULL;
453 f->computed_goto_common_reg = NULL;
454 f->x_save_expr_regs = NULL;
455 f->x_stack_slot_list = NULL;
456 f->x_rtl_expr_chain = NULL;
457 f->x_tail_recursion_label = NULL;
458 f->x_tail_recursion_reentry = NULL;
459 f->x_arg_pointer_save_area = NULL;
460 f->x_clobber_return_insn = NULL;
461 f->x_context_display = NULL;
462 f->x_trampoline_list = NULL;
463 f->x_parm_birth_insn = NULL;
464 f->x_last_parm_insn = NULL;
465 f->x_parm_reg_stack_loc = NULL;
466 f->fixup_var_refs_queue = NULL;
467 f->original_arg_vector = NULL;
468 f->original_decl_initial = NULL;
469 f->inl_last_parm_insn = NULL;
470 f->epilogue_delay_list = NULL;
473 /* Allocate fixed slots in the stack frame of the current function. */
475 /* Return size needed for stack frame based on slots so far allocated in
476 function F.
477 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
478 the caller may have to do that. */
480 HOST_WIDE_INT
481 get_func_frame_size (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 (void)
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 (enum machine_mode mode, HOST_WIDE_INT size, int align,
513 struct function *function)
515 rtx x, addr;
516 int bigend_correction = 0;
517 int alignment;
518 int frame_off, frame_alignment, frame_phase;
520 if (align == 0)
522 tree type;
524 if (mode == BLKmode)
525 alignment = BIGGEST_ALIGNMENT;
526 else
527 alignment = GET_MODE_ALIGNMENT (mode);
529 /* Allow the target to (possibly) increase the alignment of this
530 stack slot. */
531 type = (*lang_hooks.types.type_for_mode) (mode, 0);
532 if (type)
533 alignment = LOCAL_ALIGNMENT (type, alignment);
535 alignment /= BITS_PER_UNIT;
537 else if (align == -1)
539 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
540 size = CEIL_ROUND (size, alignment);
542 else
543 alignment = align / BITS_PER_UNIT;
545 #ifdef FRAME_GROWS_DOWNWARD
546 function->x_frame_offset -= size;
547 #endif
549 /* Ignore alignment we can't do with expected alignment of the boundary. */
550 if (alignment * BITS_PER_UNIT > PREFERRED_STACK_BOUNDARY)
551 alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
553 if (function->stack_alignment_needed < alignment * BITS_PER_UNIT)
554 function->stack_alignment_needed = alignment * BITS_PER_UNIT;
556 /* Calculate how many bytes the start of local variables is off from
557 stack alignment. */
558 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
559 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
560 frame_phase = frame_off ? frame_alignment - frame_off : 0;
562 /* Round the frame offset to the specified alignment. The default is
563 to always honor requests to align the stack but a port may choose to
564 do its own stack alignment by defining STACK_ALIGNMENT_NEEDED. */
565 if (STACK_ALIGNMENT_NEEDED
566 || mode != BLKmode
567 || size != 0)
569 /* We must be careful here, since FRAME_OFFSET might be negative and
570 division with a negative dividend isn't as well defined as we might
571 like. So we instead assume that ALIGNMENT is a power of two and
572 use logical operations which are unambiguous. */
573 #ifdef FRAME_GROWS_DOWNWARD
574 function->x_frame_offset
575 = (FLOOR_ROUND (function->x_frame_offset - frame_phase, alignment)
576 + frame_phase);
577 #else
578 function->x_frame_offset
579 = (CEIL_ROUND (function->x_frame_offset - frame_phase, alignment)
580 + frame_phase);
581 #endif
584 /* On a big-endian machine, if we are allocating more space than we will use,
585 use the least significant bytes of those that are allocated. */
586 if (BYTES_BIG_ENDIAN && mode != BLKmode)
587 bigend_correction = size - GET_MODE_SIZE (mode);
589 /* If we have already instantiated virtual registers, return the actual
590 address relative to the frame pointer. */
591 if (function == cfun && virtuals_instantiated)
592 addr = plus_constant (frame_pointer_rtx,
593 trunc_int_for_mode
594 (frame_offset + bigend_correction
595 + STARTING_FRAME_OFFSET, Pmode));
596 else
597 addr = plus_constant (virtual_stack_vars_rtx,
598 trunc_int_for_mode
599 (function->x_frame_offset + bigend_correction,
600 Pmode));
602 #ifndef FRAME_GROWS_DOWNWARD
603 function->x_frame_offset += size;
604 #endif
606 x = gen_rtx_MEM (mode, addr);
608 function->x_stack_slot_list
609 = gen_rtx_EXPR_LIST (VOIDmode, x, function->x_stack_slot_list);
611 return x;
614 /* Wrapper around assign_stack_local_1; assign a local stack slot for the
615 current function. */
618 assign_stack_local (enum machine_mode mode, HOST_WIDE_INT size, int align)
620 return assign_stack_local_1 (mode, size, align, cfun);
623 /* Allocate a temporary stack slot and record it for possible later
624 reuse.
626 MODE is the machine mode to be given to the returned rtx.
628 SIZE is the size in units of the space required. We do no rounding here
629 since assign_stack_local will do any required rounding.
631 KEEP is 1 if this slot is to be retained after a call to
632 free_temp_slots. Automatic variables for a block are allocated
633 with this flag. KEEP is 2 if we allocate a longer term temporary,
634 whose lifetime is controlled by CLEANUP_POINT_EXPRs. KEEP is 3
635 if we are to allocate something at an inner level to be treated as
636 a variable in the block (e.g., a SAVE_EXPR).
638 TYPE is the type that will be used for the stack slot. */
641 assign_stack_temp_for_type (enum machine_mode mode, HOST_WIDE_INT size, int keep,
642 tree type)
644 unsigned int align;
645 struct temp_slot *p, *best_p = 0;
646 rtx slot;
648 /* If SIZE is -1 it means that somebody tried to allocate a temporary
649 of a variable size. */
650 if (size == -1)
651 abort ();
653 if (mode == BLKmode)
654 align = BIGGEST_ALIGNMENT;
655 else
656 align = GET_MODE_ALIGNMENT (mode);
658 if (! type)
659 type = (*lang_hooks.types.type_for_mode) (mode, 0);
661 if (type)
662 align = LOCAL_ALIGNMENT (type, align);
664 /* Try to find an available, already-allocated temporary of the proper
665 mode which meets the size and alignment requirements. Choose the
666 smallest one with the closest alignment. */
667 for (p = temp_slots; p; p = p->next)
668 if (p->align >= align && p->size >= size && GET_MODE (p->slot) == mode
669 && ! p->in_use
670 && objects_must_conflict_p (p->type, type)
671 && (best_p == 0 || best_p->size > p->size
672 || (best_p->size == p->size && best_p->align > p->align)))
674 if (p->align == align && p->size == size)
676 best_p = 0;
677 break;
679 best_p = p;
682 /* Make our best, if any, the one to use. */
683 if (best_p)
685 /* If there are enough aligned bytes left over, make them into a new
686 temp_slot so that the extra bytes don't get wasted. Do this only
687 for BLKmode slots, so that we can be sure of the alignment. */
688 if (GET_MODE (best_p->slot) == BLKmode)
690 int alignment = best_p->align / BITS_PER_UNIT;
691 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
693 if (best_p->size - rounded_size >= alignment)
695 p = ggc_alloc (sizeof (struct temp_slot));
696 p->in_use = p->addr_taken = 0;
697 p->size = best_p->size - rounded_size;
698 p->base_offset = best_p->base_offset + rounded_size;
699 p->full_size = best_p->full_size - rounded_size;
700 p->slot = gen_rtx_MEM (BLKmode,
701 plus_constant (XEXP (best_p->slot, 0),
702 rounded_size));
703 p->align = best_p->align;
704 p->address = 0;
705 p->rtl_expr = 0;
706 p->type = best_p->type;
707 p->next = temp_slots;
708 temp_slots = p;
710 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
711 stack_slot_list);
713 best_p->size = rounded_size;
714 best_p->full_size = rounded_size;
718 p = best_p;
721 /* If we still didn't find one, make a new temporary. */
722 if (p == 0)
724 HOST_WIDE_INT frame_offset_old = frame_offset;
726 p = ggc_alloc (sizeof (struct temp_slot));
728 /* We are passing an explicit alignment request to assign_stack_local.
729 One side effect of that is assign_stack_local will not round SIZE
730 to ensure the frame offset remains suitably aligned.
732 So for requests which depended on the rounding of SIZE, we go ahead
733 and round it now. We also make sure ALIGNMENT is at least
734 BIGGEST_ALIGNMENT. */
735 if (mode == BLKmode && align < BIGGEST_ALIGNMENT)
736 abort ();
737 p->slot = assign_stack_local (mode,
738 (mode == BLKmode
739 ? CEIL_ROUND (size, (int) align / BITS_PER_UNIT)
740 : size),
741 align);
743 p->align = align;
745 /* The following slot size computation is necessary because we don't
746 know the actual size of the temporary slot until assign_stack_local
747 has performed all the frame alignment and size rounding for the
748 requested temporary. Note that extra space added for alignment
749 can be either above or below this stack slot depending on which
750 way the frame grows. We include the extra space if and only if it
751 is above this slot. */
752 #ifdef FRAME_GROWS_DOWNWARD
753 p->size = frame_offset_old - frame_offset;
754 #else
755 p->size = size;
756 #endif
758 /* Now define the fields used by combine_temp_slots. */
759 #ifdef FRAME_GROWS_DOWNWARD
760 p->base_offset = frame_offset;
761 p->full_size = frame_offset_old - frame_offset;
762 #else
763 p->base_offset = frame_offset_old;
764 p->full_size = frame_offset - frame_offset_old;
765 #endif
766 p->address = 0;
767 p->next = temp_slots;
768 temp_slots = p;
771 p->in_use = 1;
772 p->addr_taken = 0;
773 p->rtl_expr = seq_rtl_expr;
774 p->type = type;
776 if (keep == 2)
778 p->level = target_temp_slot_level;
779 p->keep = 0;
781 else if (keep == 3)
783 p->level = var_temp_slot_level;
784 p->keep = 0;
786 else
788 p->level = temp_slot_level;
789 p->keep = keep;
793 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
794 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
795 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
797 /* If we know the alias set for the memory that will be used, use
798 it. If there's no TYPE, then we don't know anything about the
799 alias set for the memory. */
800 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
801 set_mem_align (slot, align);
803 /* If a type is specified, set the relevant flags. */
804 if (type != 0)
806 RTX_UNCHANGING_P (slot) = (lang_hooks.honor_readonly
807 && TYPE_READONLY (type));
808 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
809 MEM_SET_IN_STRUCT_P (slot, AGGREGATE_TYPE_P (type));
812 return slot;
815 /* Allocate a temporary stack slot and record it for possible later
816 reuse. First three arguments are same as in preceding function. */
819 assign_stack_temp (enum machine_mode mode, HOST_WIDE_INT size, int keep)
821 return assign_stack_temp_for_type (mode, size, keep, NULL_TREE);
824 /* Assign a temporary.
825 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
826 and so that should be used in error messages. In either case, we
827 allocate of the given type.
828 KEEP is as for assign_stack_temp.
829 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
830 it is 0 if a register is OK.
831 DONT_PROMOTE is 1 if we should not promote values in register
832 to wider modes. */
835 assign_temp (tree type_or_decl, int keep, 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 ("%Hsize of variable '%D' is too large",
881 &DECL_SOURCE_LOCATION (decl), decl);
882 size = 1;
885 tmp = assign_stack_temp_for_type (mode, size, keep, type);
886 return tmp;
889 #ifndef PROMOTE_FOR_CALL_ONLY
890 if (! dont_promote)
891 mode = promote_mode (type, mode, &unsignedp, 0);
892 #endif
894 return gen_reg_rtx (mode);
897 /* Combine temporary stack slots which are adjacent on the stack.
899 This allows for better use of already allocated stack space. This is only
900 done for BLKmode slots because we can be sure that we won't have alignment
901 problems in this case. */
903 void
904 combine_temp_slots (void)
906 struct temp_slot *p, *q;
907 struct temp_slot *prev_p, *prev_q;
908 int num_slots;
910 /* We can't combine slots, because the information about which slot
911 is in which alias set will be lost. */
912 if (flag_strict_aliasing)
913 return;
915 /* If there are a lot of temp slots, don't do anything unless
916 high levels of optimization. */
917 if (! flag_expensive_optimizations)
918 for (p = temp_slots, num_slots = 0; p; p = p->next, num_slots++)
919 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
920 return;
922 for (p = temp_slots, prev_p = 0; p; p = prev_p ? prev_p->next : temp_slots)
924 int delete_p = 0;
926 if (! p->in_use && GET_MODE (p->slot) == BLKmode)
927 for (q = p->next, prev_q = p; q; q = prev_q->next)
929 int delete_q = 0;
930 if (! q->in_use && GET_MODE (q->slot) == BLKmode)
932 if (p->base_offset + p->full_size == q->base_offset)
934 /* Q comes after P; combine Q into P. */
935 p->size += q->size;
936 p->full_size += q->full_size;
937 delete_q = 1;
939 else if (q->base_offset + q->full_size == p->base_offset)
941 /* P comes after Q; combine P into Q. */
942 q->size += p->size;
943 q->full_size += p->full_size;
944 delete_p = 1;
945 break;
948 /* Either delete Q or advance past it. */
949 if (delete_q)
950 prev_q->next = q->next;
951 else
952 prev_q = q;
954 /* Either delete P or advance past it. */
955 if (delete_p)
957 if (prev_p)
958 prev_p->next = p->next;
959 else
960 temp_slots = p->next;
962 else
963 prev_p = p;
967 /* Find the temp slot corresponding to the object at address X. */
969 static struct temp_slot *
970 find_temp_slot_from_address (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 (rtx old, rtx new)
1013 struct temp_slot *p;
1015 if (rtx_equal_p (old, new))
1016 return;
1018 p = find_temp_slot_from_address (old);
1020 /* If we didn't find one, see if both OLD is a PLUS. If so, and NEW
1021 is a register, see if one operand of the PLUS is a temporary
1022 location. If so, NEW points into it. Otherwise, if both OLD and
1023 NEW are a PLUS and if there is a register in common between them.
1024 If so, try a recursive call on those values. */
1025 if (p == 0)
1027 if (GET_CODE (old) != PLUS)
1028 return;
1030 if (GET_CODE (new) == REG)
1032 update_temp_slot_address (XEXP (old, 0), new);
1033 update_temp_slot_address (XEXP (old, 1), new);
1034 return;
1036 else if (GET_CODE (new) != PLUS)
1037 return;
1039 if (rtx_equal_p (XEXP (old, 0), XEXP (new, 0)))
1040 update_temp_slot_address (XEXP (old, 1), XEXP (new, 1));
1041 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 0)))
1042 update_temp_slot_address (XEXP (old, 0), XEXP (new, 1));
1043 else if (rtx_equal_p (XEXP (old, 0), XEXP (new, 1)))
1044 update_temp_slot_address (XEXP (old, 1), XEXP (new, 0));
1045 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 1)))
1046 update_temp_slot_address (XEXP (old, 0), XEXP (new, 0));
1048 return;
1051 /* Otherwise add an alias for the temp's address. */
1052 else if (p->address == 0)
1053 p->address = new;
1054 else
1056 if (GET_CODE (p->address) != EXPR_LIST)
1057 p->address = gen_rtx_EXPR_LIST (VOIDmode, p->address, NULL_RTX);
1059 p->address = gen_rtx_EXPR_LIST (VOIDmode, new, p->address);
1063 /* If X could be a reference to a temporary slot, mark the fact that its
1064 address was taken. */
1066 void
1067 mark_temp_addr_taken (rtx x)
1069 struct temp_slot *p;
1071 if (x == 0)
1072 return;
1074 /* If X is not in memory or is at a constant address, it cannot be in
1075 a temporary slot. */
1076 if (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1077 return;
1079 p = find_temp_slot_from_address (XEXP (x, 0));
1080 if (p != 0)
1081 p->addr_taken = 1;
1084 /* If X could be a reference to a temporary slot, mark that slot as
1085 belonging to the to one level higher than the current level. If X
1086 matched one of our slots, just mark that one. Otherwise, we can't
1087 easily predict which it is, so upgrade all of them. Kept slots
1088 need not be touched.
1090 This is called when an ({...}) construct occurs and a statement
1091 returns a value in memory. */
1093 void
1094 preserve_temp_slots (rtx x)
1096 struct temp_slot *p = 0;
1098 /* If there is no result, we still might have some objects whose address
1099 were taken, so we need to make sure they stay around. */
1100 if (x == 0)
1102 for (p = temp_slots; p; p = p->next)
1103 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1104 p->level--;
1106 return;
1109 /* If X is a register that is being used as a pointer, see if we have
1110 a temporary slot we know it points to. To be consistent with
1111 the code below, we really should preserve all non-kept slots
1112 if we can't find a match, but that seems to be much too costly. */
1113 if (GET_CODE (x) == REG && REG_POINTER (x))
1114 p = find_temp_slot_from_address (x);
1116 /* If X is not in memory or is at a constant address, it cannot be in
1117 a temporary slot, but it can contain something whose address was
1118 taken. */
1119 if (p == 0 && (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0))))
1121 for (p = temp_slots; p; p = p->next)
1122 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1123 p->level--;
1125 return;
1128 /* First see if we can find a match. */
1129 if (p == 0)
1130 p = find_temp_slot_from_address (XEXP (x, 0));
1132 if (p != 0)
1134 /* Move everything at our level whose address was taken to our new
1135 level in case we used its address. */
1136 struct temp_slot *q;
1138 if (p->level == temp_slot_level)
1140 for (q = temp_slots; q; q = q->next)
1141 if (q != p && q->addr_taken && q->level == p->level)
1142 q->level--;
1144 p->level--;
1145 p->addr_taken = 0;
1147 return;
1150 /* Otherwise, preserve all non-kept slots at this level. */
1151 for (p = temp_slots; p; p = p->next)
1152 if (p->in_use && p->level == temp_slot_level && ! p->keep)
1153 p->level--;
1156 /* X is the result of an RTL_EXPR. If it is a temporary slot associated
1157 with that RTL_EXPR, promote it into a temporary slot at the present
1158 level so it will not be freed when we free slots made in the
1159 RTL_EXPR. */
1161 void
1162 preserve_rtl_expr_result (rtx x)
1164 struct temp_slot *p;
1166 /* If X is not in memory or is at a constant address, it cannot be in
1167 a temporary slot. */
1168 if (x == 0 || GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1169 return;
1171 /* If we can find a match, move it to our level unless it is already at
1172 an upper level. */
1173 p = find_temp_slot_from_address (XEXP (x, 0));
1174 if (p != 0)
1176 p->level = MIN (p->level, temp_slot_level);
1177 p->rtl_expr = 0;
1180 return;
1183 /* Free all temporaries used so far. This is normally called at the end
1184 of generating code for a statement. Don't free any temporaries
1185 currently in use for an RTL_EXPR that hasn't yet been emitted.
1186 We could eventually do better than this since it can be reused while
1187 generating the same RTL_EXPR, but this is complex and probably not
1188 worthwhile. */
1190 void
1191 free_temp_slots (void)
1193 struct temp_slot *p;
1195 for (p = temp_slots; p; p = p->next)
1196 if (p->in_use && p->level == temp_slot_level && ! p->keep
1197 && p->rtl_expr == 0)
1198 p->in_use = 0;
1200 combine_temp_slots ();
1203 /* Free all temporary slots used in T, an RTL_EXPR node. */
1205 void
1206 free_temps_for_rtl_expr (tree t)
1208 struct temp_slot *p;
1210 for (p = temp_slots; p; p = p->next)
1211 if (p->rtl_expr == t)
1213 /* If this slot is below the current TEMP_SLOT_LEVEL, then it
1214 needs to be preserved. This can happen if a temporary in
1215 the RTL_EXPR was addressed; preserve_temp_slots will move
1216 the temporary into a higher level. */
1217 if (temp_slot_level <= p->level)
1218 p->in_use = 0;
1219 else
1220 p->rtl_expr = NULL_TREE;
1223 combine_temp_slots ();
1226 /* Mark all temporaries ever allocated in this function as not suitable
1227 for reuse until the current level is exited. */
1229 void
1230 mark_all_temps_used (void)
1232 struct temp_slot *p;
1234 for (p = temp_slots; p; p = p->next)
1236 p->in_use = p->keep = 1;
1237 p->level = MIN (p->level, temp_slot_level);
1241 /* Push deeper into the nesting level for stack temporaries. */
1243 void
1244 push_temp_slots (void)
1246 temp_slot_level++;
1249 /* Pop a temporary nesting level. All slots in use in the current level
1250 are freed. */
1252 void
1253 pop_temp_slots (void)
1255 struct temp_slot *p;
1257 for (p = temp_slots; p; p = p->next)
1258 if (p->in_use && p->level == temp_slot_level && p->rtl_expr == 0)
1259 p->in_use = 0;
1261 combine_temp_slots ();
1263 temp_slot_level--;
1266 /* Initialize temporary slots. */
1268 void
1269 init_temp_slots (void)
1271 /* We have not allocated any temporaries yet. */
1272 temp_slots = 0;
1273 temp_slot_level = 0;
1274 var_temp_slot_level = 0;
1275 target_temp_slot_level = 0;
1278 /* Retroactively move an auto variable from a register to a stack
1279 slot. This is done when an address-reference to the variable is
1280 seen. If RESCAN is true, all previously emitted instructions are
1281 examined and modified to handle the fact that DECL is now
1282 addressable. */
1284 void
1285 put_var_into_stack (tree decl, int rescan)
1287 rtx reg;
1288 enum machine_mode promoted_mode, decl_mode;
1289 struct function *function = 0;
1290 tree context;
1291 int can_use_addressof;
1292 int volatilep = TREE_CODE (decl) != SAVE_EXPR && TREE_THIS_VOLATILE (decl);
1293 int usedp = (TREE_USED (decl)
1294 || (TREE_CODE (decl) != SAVE_EXPR && DECL_INITIAL (decl) != 0));
1296 context = decl_function_context (decl);
1298 /* Get the current rtl used for this object and its original mode. */
1299 reg = (TREE_CODE (decl) == SAVE_EXPR
1300 ? SAVE_EXPR_RTL (decl)
1301 : DECL_RTL_IF_SET (decl));
1303 /* No need to do anything if decl has no rtx yet
1304 since in that case caller is setting TREE_ADDRESSABLE
1305 and a stack slot will be assigned when the rtl is made. */
1306 if (reg == 0)
1307 return;
1309 /* Get the declared mode for this object. */
1310 decl_mode = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
1311 : DECL_MODE (decl));
1312 /* Get the mode it's actually stored in. */
1313 promoted_mode = GET_MODE (reg);
1315 /* If this variable comes from an outer function, find that
1316 function's saved context. Don't use find_function_data here,
1317 because it might not be in any active function.
1318 FIXME: Is that really supposed to happen?
1319 It does in ObjC at least. */
1320 if (context != current_function_decl && context != inline_function_decl)
1321 for (function = outer_function_chain; function; function = function->outer)
1322 if (function->decl == context)
1323 break;
1325 /* If this is a variable-size object with a pseudo to address it,
1326 put that pseudo into the stack, if the var is nonlocal. */
1327 if (TREE_CODE (decl) != SAVE_EXPR && DECL_NONLOCAL (decl)
1328 && GET_CODE (reg) == MEM
1329 && GET_CODE (XEXP (reg, 0)) == REG
1330 && REGNO (XEXP (reg, 0)) > LAST_VIRTUAL_REGISTER)
1332 reg = XEXP (reg, 0);
1333 decl_mode = promoted_mode = GET_MODE (reg);
1336 can_use_addressof
1337 = (function == 0
1338 && optimize > 0
1339 /* FIXME make it work for promoted modes too */
1340 && decl_mode == promoted_mode
1341 #ifdef NON_SAVING_SETJMP
1342 && ! (NON_SAVING_SETJMP && current_function_calls_setjmp)
1343 #endif
1346 /* If we can't use ADDRESSOF, make sure we see through one we already
1347 generated. */
1348 if (! can_use_addressof && GET_CODE (reg) == MEM
1349 && GET_CODE (XEXP (reg, 0)) == ADDRESSOF)
1350 reg = XEXP (XEXP (reg, 0), 0);
1352 /* Now we should have a value that resides in one or more pseudo regs. */
1354 if (GET_CODE (reg) == REG)
1356 /* If this variable lives in the current function and we don't need
1357 to put things in the stack for the sake of setjmp, try to keep it
1358 in a register until we know we actually need the address. */
1359 if (can_use_addressof)
1360 gen_mem_addressof (reg, decl, rescan);
1361 else
1362 put_reg_into_stack (function, reg, TREE_TYPE (decl), promoted_mode,
1363 decl_mode, volatilep, 0, usedp, 0);
1365 else if (GET_CODE (reg) == CONCAT)
1367 /* A CONCAT contains two pseudos; put them both in the stack.
1368 We do it so they end up consecutive.
1369 We fixup references to the parts only after we fixup references
1370 to the whole CONCAT, lest we do double fixups for the latter
1371 references. */
1372 enum machine_mode part_mode = GET_MODE (XEXP (reg, 0));
1373 tree part_type = (*lang_hooks.types.type_for_mode) (part_mode, 0);
1374 rtx lopart = XEXP (reg, 0);
1375 rtx hipart = XEXP (reg, 1);
1376 #ifdef FRAME_GROWS_DOWNWARD
1377 /* Since part 0 should have a lower address, do it second. */
1378 put_reg_into_stack (function, hipart, part_type, part_mode,
1379 part_mode, volatilep, 0, 0, 0);
1380 put_reg_into_stack (function, lopart, part_type, part_mode,
1381 part_mode, volatilep, 0, 0, 0);
1382 #else
1383 put_reg_into_stack (function, lopart, part_type, part_mode,
1384 part_mode, volatilep, 0, 0, 0);
1385 put_reg_into_stack (function, hipart, part_type, part_mode,
1386 part_mode, volatilep, 0, 0, 0);
1387 #endif
1389 /* Change the CONCAT into a combined MEM for both parts. */
1390 PUT_CODE (reg, MEM);
1391 MEM_ATTRS (reg) = 0;
1393 /* set_mem_attributes uses DECL_RTL to avoid re-generating of
1394 already computed alias sets. Here we want to re-generate. */
1395 if (DECL_P (decl))
1396 SET_DECL_RTL (decl, NULL);
1397 set_mem_attributes (reg, decl, 1);
1398 if (DECL_P (decl))
1399 SET_DECL_RTL (decl, reg);
1401 /* The two parts are in memory order already.
1402 Use the lower parts address as ours. */
1403 XEXP (reg, 0) = XEXP (XEXP (reg, 0), 0);
1404 /* Prevent sharing of rtl that might lose. */
1405 if (GET_CODE (XEXP (reg, 0)) == PLUS)
1406 XEXP (reg, 0) = copy_rtx (XEXP (reg, 0));
1407 if (usedp && rescan)
1409 schedule_fixup_var_refs (function, reg, TREE_TYPE (decl),
1410 promoted_mode, 0);
1411 schedule_fixup_var_refs (function, lopart, part_type, part_mode, 0);
1412 schedule_fixup_var_refs (function, hipart, part_type, part_mode, 0);
1415 else
1416 return;
1419 /* Subroutine of put_var_into_stack. This puts a single pseudo reg REG
1420 into the stack frame of FUNCTION (0 means the current function).
1421 DECL_MODE is the machine mode of the user-level data type.
1422 PROMOTED_MODE is the machine mode of the register.
1423 VOLATILE_P is nonzero if this is for a "volatile" decl.
1424 USED_P is nonzero if this reg might have already been used in an insn. */
1426 static void
1427 put_reg_into_stack (struct function *function, rtx reg, tree type,
1428 enum machine_mode promoted_mode, enum machine_mode decl_mode,
1429 int volatile_p, unsigned int original_regno, int used_p, htab_t ht)
1431 struct function *func = function ? function : cfun;
1432 rtx new = 0;
1433 unsigned int regno = original_regno;
1435 if (regno == 0)
1436 regno = REGNO (reg);
1438 if (regno < func->x_max_parm_reg)
1439 new = func->x_parm_reg_stack_loc[regno];
1441 if (new == 0)
1442 new = assign_stack_local_1 (decl_mode, GET_MODE_SIZE (decl_mode), 0, func);
1444 PUT_CODE (reg, MEM);
1445 PUT_MODE (reg, decl_mode);
1446 XEXP (reg, 0) = XEXP (new, 0);
1447 MEM_ATTRS (reg) = 0;
1448 /* `volatil' bit means one thing for MEMs, another entirely for REGs. */
1449 MEM_VOLATILE_P (reg) = volatile_p;
1451 /* If this is a memory ref that contains aggregate components,
1452 mark it as such for cse and loop optimize. If we are reusing a
1453 previously generated stack slot, then we need to copy the bit in
1454 case it was set for other reasons. For instance, it is set for
1455 __builtin_va_alist. */
1456 if (type)
1458 MEM_SET_IN_STRUCT_P (reg,
1459 AGGREGATE_TYPE_P (type) || MEM_IN_STRUCT_P (new));
1460 set_mem_alias_set (reg, get_alias_set (type));
1463 if (used_p)
1464 schedule_fixup_var_refs (function, reg, type, promoted_mode, ht);
1467 /* Make sure that all refs to the variable, previously made
1468 when it was a register, are fixed up to be valid again.
1469 See function above for meaning of arguments. */
1471 static void
1472 schedule_fixup_var_refs (struct function *function, rtx reg, tree type,
1473 enum machine_mode promoted_mode, htab_t ht)
1475 int unsigned_p = type ? TREE_UNSIGNED (type) : 0;
1477 if (function != 0)
1479 struct var_refs_queue *temp;
1481 temp = ggc_alloc (sizeof (struct var_refs_queue));
1482 temp->modified = reg;
1483 temp->promoted_mode = promoted_mode;
1484 temp->unsignedp = unsigned_p;
1485 temp->next = function->fixup_var_refs_queue;
1486 function->fixup_var_refs_queue = temp;
1488 else
1489 /* Variable is local; fix it up now. */
1490 fixup_var_refs (reg, promoted_mode, unsigned_p, reg, ht);
1493 static void
1494 fixup_var_refs (rtx var, enum machine_mode promoted_mode, int unsignedp,
1495 rtx may_share, htab_t ht)
1497 tree pending;
1498 rtx first_insn = get_insns ();
1499 struct sequence_stack *stack = seq_stack;
1500 tree rtl_exps = rtl_expr_chain;
1502 /* If there's a hash table, it must record all uses of VAR. */
1503 if (ht)
1505 if (stack != 0)
1506 abort ();
1507 fixup_var_refs_insns_with_hash (ht, var, promoted_mode, unsignedp,
1508 may_share);
1509 return;
1512 fixup_var_refs_insns (first_insn, var, promoted_mode, unsignedp,
1513 stack == 0, may_share);
1515 /* Scan all pending sequences too. */
1516 for (; stack; stack = stack->next)
1518 push_to_full_sequence (stack->first, stack->last);
1519 fixup_var_refs_insns (stack->first, var, promoted_mode, unsignedp,
1520 stack->next != 0, may_share);
1521 /* Update remembered end of sequence
1522 in case we added an insn at the end. */
1523 stack->last = get_last_insn ();
1524 end_sequence ();
1527 /* Scan all waiting RTL_EXPRs too. */
1528 for (pending = rtl_exps; pending; pending = TREE_CHAIN (pending))
1530 rtx seq = RTL_EXPR_SEQUENCE (TREE_VALUE (pending));
1531 if (seq != const0_rtx && seq != 0)
1533 push_to_sequence (seq);
1534 fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0,
1535 may_share);
1536 end_sequence ();
1541 /* REPLACEMENTS is a pointer to a list of the struct fixup_replacement and X is
1542 some part of an insn. Return a struct fixup_replacement whose OLD
1543 value is equal to X. Allocate a new structure if no such entry exists. */
1545 static struct fixup_replacement *
1546 find_fixup_replacement (struct fixup_replacement **replacements, rtx x)
1548 struct fixup_replacement *p;
1550 /* See if we have already replaced this. */
1551 for (p = *replacements; p != 0 && ! rtx_equal_p (p->old, x); p = p->next)
1554 if (p == 0)
1556 p = xmalloc (sizeof (struct fixup_replacement));
1557 p->old = x;
1558 p->new = 0;
1559 p->next = *replacements;
1560 *replacements = p;
1563 return p;
1566 /* Scan the insn-chain starting with INSN for refs to VAR and fix them
1567 up. TOPLEVEL is nonzero if this chain is the main chain of insns
1568 for the current function. MAY_SHARE is either a MEM that is not
1569 to be unshared or a list of them. */
1571 static void
1572 fixup_var_refs_insns (rtx insn, rtx var, enum machine_mode promoted_mode,
1573 int unsignedp, int toplevel, rtx may_share)
1575 while (insn)
1577 /* fixup_var_refs_insn might modify insn, so save its next
1578 pointer now. */
1579 rtx next = NEXT_INSN (insn);
1581 /* CALL_PLACEHOLDERs are special; we have to switch into each of
1582 the three sequences they (potentially) contain, and process
1583 them recursively. The CALL_INSN itself is not interesting. */
1585 if (GET_CODE (insn) == CALL_INSN
1586 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1588 int i;
1590 /* Look at the Normal call, sibling call and tail recursion
1591 sequences attached to the CALL_PLACEHOLDER. */
1592 for (i = 0; i < 3; i++)
1594 rtx seq = XEXP (PATTERN (insn), i);
1595 if (seq)
1597 push_to_sequence (seq);
1598 fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0,
1599 may_share);
1600 XEXP (PATTERN (insn), i) = get_insns ();
1601 end_sequence ();
1606 else if (INSN_P (insn))
1607 fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, toplevel,
1608 may_share);
1610 insn = next;
1614 /* Look up the insns which reference VAR in HT and fix them up. Other
1615 arguments are the same as fixup_var_refs_insns.
1617 N.B. No need for special processing of CALL_PLACEHOLDERs here,
1618 because the hash table will point straight to the interesting insn
1619 (inside the CALL_PLACEHOLDER). */
1621 static void
1622 fixup_var_refs_insns_with_hash (htab_t ht, rtx var, enum machine_mode promoted_mode,
1623 int unsignedp, rtx may_share)
1625 struct insns_for_mem_entry tmp;
1626 struct insns_for_mem_entry *ime;
1627 rtx insn_list;
1629 tmp.key = var;
1630 ime = htab_find (ht, &tmp);
1631 for (insn_list = ime->insns; insn_list != 0; insn_list = XEXP (insn_list, 1))
1632 if (INSN_P (XEXP (insn_list, 0)))
1633 fixup_var_refs_insn (XEXP (insn_list, 0), var, promoted_mode,
1634 unsignedp, 1, may_share);
1638 /* Per-insn processing by fixup_var_refs_insns(_with_hash). INSN is
1639 the insn under examination, VAR is the variable to fix up
1640 references to, PROMOTED_MODE and UNSIGNEDP describe VAR, and
1641 TOPLEVEL is nonzero if this is the main insn chain for this
1642 function. */
1644 static void
1645 fixup_var_refs_insn (rtx insn, rtx var, enum machine_mode promoted_mode,
1646 int unsignedp, int toplevel, rtx no_share)
1648 rtx call_dest = 0;
1649 rtx set, prev, prev_set;
1650 rtx note;
1652 /* Remember the notes in case we delete the insn. */
1653 note = REG_NOTES (insn);
1655 /* If this is a CLOBBER of VAR, delete it.
1657 If it has a REG_LIBCALL note, delete the REG_LIBCALL
1658 and REG_RETVAL notes too. */
1659 if (GET_CODE (PATTERN (insn)) == CLOBBER
1660 && (XEXP (PATTERN (insn), 0) == var
1661 || (GET_CODE (XEXP (PATTERN (insn), 0)) == CONCAT
1662 && (XEXP (XEXP (PATTERN (insn), 0), 0) == var
1663 || XEXP (XEXP (PATTERN (insn), 0), 1) == var))))
1665 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0)
1666 /* The REG_LIBCALL note will go away since we are going to
1667 turn INSN into a NOTE, so just delete the
1668 corresponding REG_RETVAL note. */
1669 remove_note (XEXP (note, 0),
1670 find_reg_note (XEXP (note, 0), REG_RETVAL,
1671 NULL_RTX));
1673 delete_insn (insn);
1676 /* The insn to load VAR from a home in the arglist
1677 is now a no-op. When we see it, just delete it.
1678 Similarly if this is storing VAR from a register from which
1679 it was loaded in the previous insn. This will occur
1680 when an ADDRESSOF was made for an arglist slot. */
1681 else if (toplevel
1682 && (set = single_set (insn)) != 0
1683 && SET_DEST (set) == var
1684 /* If this represents the result of an insn group,
1685 don't delete the insn. */
1686 && find_reg_note (insn, REG_RETVAL, NULL_RTX) == 0
1687 && (rtx_equal_p (SET_SRC (set), var)
1688 || (GET_CODE (SET_SRC (set)) == REG
1689 && (prev = prev_nonnote_insn (insn)) != 0
1690 && (prev_set = single_set (prev)) != 0
1691 && SET_DEST (prev_set) == SET_SRC (set)
1692 && rtx_equal_p (SET_SRC (prev_set), var))))
1694 delete_insn (insn);
1696 else
1698 struct fixup_replacement *replacements = 0;
1699 rtx next_insn = NEXT_INSN (insn);
1701 if (SMALL_REGISTER_CLASSES)
1703 /* If the insn that copies the results of a CALL_INSN
1704 into a pseudo now references VAR, we have to use an
1705 intermediate pseudo since we want the life of the
1706 return value register to be only a single insn.
1708 If we don't use an intermediate pseudo, such things as
1709 address computations to make the address of VAR valid
1710 if it is not can be placed between the CALL_INSN and INSN.
1712 To make sure this doesn't happen, we record the destination
1713 of the CALL_INSN and see if the next insn uses both that
1714 and VAR. */
1716 if (call_dest != 0 && GET_CODE (insn) == INSN
1717 && reg_mentioned_p (var, PATTERN (insn))
1718 && reg_mentioned_p (call_dest, PATTERN (insn)))
1720 rtx temp = gen_reg_rtx (GET_MODE (call_dest));
1722 emit_insn_before (gen_move_insn (temp, call_dest), insn);
1724 PATTERN (insn) = replace_rtx (PATTERN (insn),
1725 call_dest, temp);
1728 if (GET_CODE (insn) == CALL_INSN
1729 && GET_CODE (PATTERN (insn)) == SET)
1730 call_dest = SET_DEST (PATTERN (insn));
1731 else if (GET_CODE (insn) == CALL_INSN
1732 && GET_CODE (PATTERN (insn)) == PARALLEL
1733 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1734 call_dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1735 else
1736 call_dest = 0;
1739 /* See if we have to do anything to INSN now that VAR is in
1740 memory. If it needs to be loaded into a pseudo, use a single
1741 pseudo for the entire insn in case there is a MATCH_DUP
1742 between two operands. We pass a pointer to the head of
1743 a list of struct fixup_replacements. If fixup_var_refs_1
1744 needs to allocate pseudos or replacement MEMs (for SUBREGs),
1745 it will record them in this list.
1747 If it allocated a pseudo for any replacement, we copy into
1748 it here. */
1750 fixup_var_refs_1 (var, promoted_mode, &PATTERN (insn), insn,
1751 &replacements, no_share);
1753 /* If this is last_parm_insn, and any instructions were output
1754 after it to fix it up, then we must set last_parm_insn to
1755 the last such instruction emitted. */
1756 if (insn == last_parm_insn)
1757 last_parm_insn = PREV_INSN (next_insn);
1759 while (replacements)
1761 struct fixup_replacement *next;
1763 if (GET_CODE (replacements->new) == REG)
1765 rtx insert_before;
1766 rtx seq;
1768 /* OLD might be a (subreg (mem)). */
1769 if (GET_CODE (replacements->old) == SUBREG)
1770 replacements->old
1771 = fixup_memory_subreg (replacements->old, insn,
1772 promoted_mode, 0);
1773 else
1774 replacements->old
1775 = fixup_stack_1 (replacements->old, insn);
1777 insert_before = insn;
1779 /* If we are changing the mode, do a conversion.
1780 This might be wasteful, but combine.c will
1781 eliminate much of the waste. */
1783 if (GET_MODE (replacements->new)
1784 != GET_MODE (replacements->old))
1786 start_sequence ();
1787 convert_move (replacements->new,
1788 replacements->old, unsignedp);
1789 seq = get_insns ();
1790 end_sequence ();
1792 else
1793 seq = gen_move_insn (replacements->new,
1794 replacements->old);
1796 emit_insn_before (seq, insert_before);
1799 next = replacements->next;
1800 free (replacements);
1801 replacements = next;
1805 /* Also fix up any invalid exprs in the REG_NOTES of this insn.
1806 But don't touch other insns referred to by reg-notes;
1807 we will get them elsewhere. */
1808 while (note)
1810 if (GET_CODE (note) != INSN_LIST)
1811 XEXP (note, 0)
1812 = walk_fixup_memory_subreg (XEXP (note, 0), insn,
1813 promoted_mode, 1);
1814 note = XEXP (note, 1);
1818 /* VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
1819 See if the rtx expression at *LOC in INSN needs to be changed.
1821 REPLACEMENTS is a pointer to a list head that starts out zero, but may
1822 contain a list of original rtx's and replacements. If we find that we need
1823 to modify this insn by replacing a memory reference with a pseudo or by
1824 making a new MEM to implement a SUBREG, we consult that list to see if
1825 we have already chosen a replacement. If none has already been allocated,
1826 we allocate it and update the list. fixup_var_refs_insn will copy VAR
1827 or the SUBREG, as appropriate, to the pseudo. */
1829 static void
1830 fixup_var_refs_1 (rtx var, enum machine_mode promoted_mode, rtx *loc, rtx insn,
1831 struct fixup_replacement **replacements, rtx no_share)
1833 int i;
1834 rtx x = *loc;
1835 RTX_CODE code = GET_CODE (x);
1836 const char *fmt;
1837 rtx tem, tem1;
1838 struct fixup_replacement *replacement;
1840 switch (code)
1842 case ADDRESSOF:
1843 if (XEXP (x, 0) == var)
1845 /* Prevent sharing of rtl that might lose. */
1846 rtx sub = copy_rtx (XEXP (var, 0));
1848 if (! validate_change (insn, loc, sub, 0))
1850 rtx y = gen_reg_rtx (GET_MODE (sub));
1851 rtx seq, new_insn;
1853 /* We should be able to replace with a register or all is lost.
1854 Note that we can't use validate_change to verify this, since
1855 we're not caring for replacing all dups simultaneously. */
1856 if (! validate_replace_rtx (*loc, y, insn))
1857 abort ();
1859 /* Careful! First try to recognize a direct move of the
1860 value, mimicking how things are done in gen_reload wrt
1861 PLUS. Consider what happens when insn is a conditional
1862 move instruction and addsi3 clobbers flags. */
1864 start_sequence ();
1865 new_insn = emit_insn (gen_rtx_SET (VOIDmode, y, sub));
1866 seq = get_insns ();
1867 end_sequence ();
1869 if (recog_memoized (new_insn) < 0)
1871 /* That failed. Fall back on force_operand and hope. */
1873 start_sequence ();
1874 sub = force_operand (sub, y);
1875 if (sub != y)
1876 emit_insn (gen_move_insn (y, sub));
1877 seq = get_insns ();
1878 end_sequence ();
1881 #ifdef HAVE_cc0
1882 /* Don't separate setter from user. */
1883 if (PREV_INSN (insn) && sets_cc0_p (PREV_INSN (insn)))
1884 insn = PREV_INSN (insn);
1885 #endif
1887 emit_insn_before (seq, insn);
1890 return;
1892 case MEM:
1893 if (var == x)
1895 /* If we already have a replacement, use it. Otherwise,
1896 try to fix up this address in case it is invalid. */
1898 replacement = find_fixup_replacement (replacements, var);
1899 if (replacement->new)
1901 *loc = replacement->new;
1902 return;
1905 *loc = replacement->new = x = fixup_stack_1 (x, insn);
1907 /* Unless we are forcing memory to register or we changed the mode,
1908 we can leave things the way they are if the insn is valid. */
1910 INSN_CODE (insn) = -1;
1911 if (! flag_force_mem && GET_MODE (x) == promoted_mode
1912 && recog_memoized (insn) >= 0)
1913 return;
1915 *loc = replacement->new = gen_reg_rtx (promoted_mode);
1916 return;
1919 /* If X contains VAR, we need to unshare it here so that we update
1920 each occurrence separately. But all identical MEMs in one insn
1921 must be replaced with the same rtx because of the possibility of
1922 MATCH_DUPs. */
1924 if (reg_mentioned_p (var, x))
1926 replacement = find_fixup_replacement (replacements, x);
1927 if (replacement->new == 0)
1928 replacement->new = copy_most_rtx (x, no_share);
1930 *loc = x = replacement->new;
1931 code = GET_CODE (x);
1933 break;
1935 case REG:
1936 case CC0:
1937 case PC:
1938 case CONST_INT:
1939 case CONST:
1940 case SYMBOL_REF:
1941 case LABEL_REF:
1942 case CONST_DOUBLE:
1943 case CONST_VECTOR:
1944 return;
1946 case SIGN_EXTRACT:
1947 case ZERO_EXTRACT:
1948 /* Note that in some cases those types of expressions are altered
1949 by optimize_bit_field, and do not survive to get here. */
1950 if (XEXP (x, 0) == var
1951 || (GET_CODE (XEXP (x, 0)) == SUBREG
1952 && SUBREG_REG (XEXP (x, 0)) == var))
1954 /* Get TEM as a valid MEM in the mode presently in the insn.
1956 We don't worry about the possibility of MATCH_DUP here; it
1957 is highly unlikely and would be tricky to handle. */
1959 tem = XEXP (x, 0);
1960 if (GET_CODE (tem) == SUBREG)
1962 if (GET_MODE_BITSIZE (GET_MODE (tem))
1963 > GET_MODE_BITSIZE (GET_MODE (var)))
1965 replacement = find_fixup_replacement (replacements, var);
1966 if (replacement->new == 0)
1967 replacement->new = gen_reg_rtx (GET_MODE (var));
1968 SUBREG_REG (tem) = replacement->new;
1970 /* The following code works only if we have a MEM, so we
1971 need to handle the subreg here. We directly substitute
1972 it assuming that a subreg must be OK here. We already
1973 scheduled a replacement to copy the mem into the
1974 subreg. */
1975 XEXP (x, 0) = tem;
1976 return;
1978 else
1979 tem = fixup_memory_subreg (tem, insn, promoted_mode, 0);
1981 else
1982 tem = fixup_stack_1 (tem, insn);
1984 /* Unless we want to load from memory, get TEM into the proper mode
1985 for an extract from memory. This can only be done if the
1986 extract is at a constant position and length. */
1988 if (! flag_force_mem && GET_CODE (XEXP (x, 1)) == CONST_INT
1989 && GET_CODE (XEXP (x, 2)) == CONST_INT
1990 && ! mode_dependent_address_p (XEXP (tem, 0))
1991 && ! MEM_VOLATILE_P (tem))
1993 enum machine_mode wanted_mode = VOIDmode;
1994 enum machine_mode is_mode = GET_MODE (tem);
1995 HOST_WIDE_INT pos = INTVAL (XEXP (x, 2));
1997 if (GET_CODE (x) == ZERO_EXTRACT)
1999 enum machine_mode new_mode
2000 = mode_for_extraction (EP_extzv, 1);
2001 if (new_mode != MAX_MACHINE_MODE)
2002 wanted_mode = new_mode;
2004 else if (GET_CODE (x) == SIGN_EXTRACT)
2006 enum machine_mode new_mode
2007 = mode_for_extraction (EP_extv, 1);
2008 if (new_mode != MAX_MACHINE_MODE)
2009 wanted_mode = new_mode;
2012 /* If we have a narrower mode, we can do something. */
2013 if (wanted_mode != VOIDmode
2014 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2016 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2017 rtx old_pos = XEXP (x, 2);
2018 rtx newmem;
2020 /* If the bytes and bits are counted differently, we
2021 must adjust the offset. */
2022 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2023 offset = (GET_MODE_SIZE (is_mode)
2024 - GET_MODE_SIZE (wanted_mode) - offset);
2026 pos %= GET_MODE_BITSIZE (wanted_mode);
2028 newmem = adjust_address_nv (tem, wanted_mode, offset);
2030 /* Make the change and see if the insn remains valid. */
2031 INSN_CODE (insn) = -1;
2032 XEXP (x, 0) = newmem;
2033 XEXP (x, 2) = GEN_INT (pos);
2035 if (recog_memoized (insn) >= 0)
2036 return;
2038 /* Otherwise, restore old position. XEXP (x, 0) will be
2039 restored later. */
2040 XEXP (x, 2) = old_pos;
2044 /* If we get here, the bitfield extract insn can't accept a memory
2045 reference. Copy the input into a register. */
2047 tem1 = gen_reg_rtx (GET_MODE (tem));
2048 emit_insn_before (gen_move_insn (tem1, tem), insn);
2049 XEXP (x, 0) = tem1;
2050 return;
2052 break;
2054 case SUBREG:
2055 if (SUBREG_REG (x) == var)
2057 /* If this is a special SUBREG made because VAR was promoted
2058 from a wider mode, replace it with VAR and call ourself
2059 recursively, this time saying that the object previously
2060 had its current mode (by virtue of the SUBREG). */
2062 if (SUBREG_PROMOTED_VAR_P (x))
2064 *loc = var;
2065 fixup_var_refs_1 (var, GET_MODE (var), loc, insn, replacements,
2066 no_share);
2067 return;
2070 /* If this SUBREG makes VAR wider, it has become a paradoxical
2071 SUBREG with VAR in memory, but these aren't allowed at this
2072 stage of the compilation. So load VAR into a pseudo and take
2073 a SUBREG of that pseudo. */
2074 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (var)))
2076 replacement = find_fixup_replacement (replacements, var);
2077 if (replacement->new == 0)
2078 replacement->new = gen_reg_rtx (promoted_mode);
2079 SUBREG_REG (x) = replacement->new;
2080 return;
2083 /* See if we have already found a replacement for this SUBREG.
2084 If so, use it. Otherwise, make a MEM and see if the insn
2085 is recognized. If not, or if we should force MEM into a register,
2086 make a pseudo for this SUBREG. */
2087 replacement = find_fixup_replacement (replacements, x);
2088 if (replacement->new)
2090 *loc = replacement->new;
2091 return;
2094 replacement->new = *loc = fixup_memory_subreg (x, insn,
2095 promoted_mode, 0);
2097 INSN_CODE (insn) = -1;
2098 if (! flag_force_mem && recog_memoized (insn) >= 0)
2099 return;
2101 *loc = replacement->new = gen_reg_rtx (GET_MODE (x));
2102 return;
2104 break;
2106 case SET:
2107 /* First do special simplification of bit-field references. */
2108 if (GET_CODE (SET_DEST (x)) == SIGN_EXTRACT
2109 || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2110 optimize_bit_field (x, insn, 0);
2111 if (GET_CODE (SET_SRC (x)) == SIGN_EXTRACT
2112 || GET_CODE (SET_SRC (x)) == ZERO_EXTRACT)
2113 optimize_bit_field (x, insn, 0);
2115 /* For a paradoxical SUBREG inside a ZERO_EXTRACT, load the object
2116 into a register and then store it back out. */
2117 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2118 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG
2119 && SUBREG_REG (XEXP (SET_DEST (x), 0)) == var
2120 && (GET_MODE_SIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2121 > GET_MODE_SIZE (GET_MODE (var))))
2123 replacement = find_fixup_replacement (replacements, var);
2124 if (replacement->new == 0)
2125 replacement->new = gen_reg_rtx (GET_MODE (var));
2127 SUBREG_REG (XEXP (SET_DEST (x), 0)) = replacement->new;
2128 emit_insn_after (gen_move_insn (var, replacement->new), insn);
2131 /* If SET_DEST is now a paradoxical SUBREG, put the result of this
2132 insn into a pseudo and store the low part of the pseudo into VAR. */
2133 if (GET_CODE (SET_DEST (x)) == SUBREG
2134 && SUBREG_REG (SET_DEST (x)) == var
2135 && (GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
2136 > GET_MODE_SIZE (GET_MODE (var))))
2138 SET_DEST (x) = tem = gen_reg_rtx (GET_MODE (SET_DEST (x)));
2139 emit_insn_after (gen_move_insn (var, gen_lowpart (GET_MODE (var),
2140 tem)),
2141 insn);
2142 break;
2146 rtx dest = SET_DEST (x);
2147 rtx src = SET_SRC (x);
2148 rtx outerdest = dest;
2150 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2151 || GET_CODE (dest) == SIGN_EXTRACT
2152 || GET_CODE (dest) == ZERO_EXTRACT)
2153 dest = XEXP (dest, 0);
2155 if (GET_CODE (src) == SUBREG)
2156 src = SUBREG_REG (src);
2158 /* If VAR does not appear at the top level of the SET
2159 just scan the lower levels of the tree. */
2161 if (src != var && dest != var)
2162 break;
2164 /* We will need to rerecognize this insn. */
2165 INSN_CODE (insn) = -1;
2167 if (GET_CODE (outerdest) == ZERO_EXTRACT && dest == var
2168 && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
2170 /* Since this case will return, ensure we fixup all the
2171 operands here. */
2172 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 1),
2173 insn, replacements, no_share);
2174 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 2),
2175 insn, replacements, no_share);
2176 fixup_var_refs_1 (var, promoted_mode, &SET_SRC (x),
2177 insn, replacements, no_share);
2179 tem = XEXP (outerdest, 0);
2181 /* Clean up (SUBREG:SI (MEM:mode ...) 0)
2182 that may appear inside a ZERO_EXTRACT.
2183 This was legitimate when the MEM was a REG. */
2184 if (GET_CODE (tem) == SUBREG
2185 && SUBREG_REG (tem) == var)
2186 tem = fixup_memory_subreg (tem, insn, promoted_mode, 0);
2187 else
2188 tem = fixup_stack_1 (tem, insn);
2190 if (GET_CODE (XEXP (outerdest, 1)) == CONST_INT
2191 && GET_CODE (XEXP (outerdest, 2)) == CONST_INT
2192 && ! mode_dependent_address_p (XEXP (tem, 0))
2193 && ! MEM_VOLATILE_P (tem))
2195 enum machine_mode wanted_mode;
2196 enum machine_mode is_mode = GET_MODE (tem);
2197 HOST_WIDE_INT pos = INTVAL (XEXP (outerdest, 2));
2199 wanted_mode = mode_for_extraction (EP_insv, 0);
2201 /* If we have a narrower mode, we can do something. */
2202 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2204 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2205 rtx old_pos = XEXP (outerdest, 2);
2206 rtx newmem;
2208 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2209 offset = (GET_MODE_SIZE (is_mode)
2210 - GET_MODE_SIZE (wanted_mode) - offset);
2212 pos %= GET_MODE_BITSIZE (wanted_mode);
2214 newmem = adjust_address_nv (tem, wanted_mode, offset);
2216 /* Make the change and see if the insn remains valid. */
2217 INSN_CODE (insn) = -1;
2218 XEXP (outerdest, 0) = newmem;
2219 XEXP (outerdest, 2) = GEN_INT (pos);
2221 if (recog_memoized (insn) >= 0)
2222 return;
2224 /* Otherwise, restore old position. XEXP (x, 0) will be
2225 restored later. */
2226 XEXP (outerdest, 2) = old_pos;
2230 /* If we get here, the bit-field store doesn't allow memory
2231 or isn't located at a constant position. Load the value into
2232 a register, do the store, and put it back into memory. */
2234 tem1 = gen_reg_rtx (GET_MODE (tem));
2235 emit_insn_before (gen_move_insn (tem1, tem), insn);
2236 emit_insn_after (gen_move_insn (tem, tem1), insn);
2237 XEXP (outerdest, 0) = tem1;
2238 return;
2241 /* STRICT_LOW_PART is a no-op on memory references
2242 and it can cause combinations to be unrecognizable,
2243 so eliminate it. */
2245 if (dest == var && GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2246 SET_DEST (x) = XEXP (SET_DEST (x), 0);
2248 /* A valid insn to copy VAR into or out of a register
2249 must be left alone, to avoid an infinite loop here.
2250 If the reference to VAR is by a subreg, fix that up,
2251 since SUBREG is not valid for a memref.
2252 Also fix up the address of the stack slot.
2254 Note that we must not try to recognize the insn until
2255 after we know that we have valid addresses and no
2256 (subreg (mem ...) ...) constructs, since these interfere
2257 with determining the validity of the insn. */
2259 if ((SET_SRC (x) == var
2260 || (GET_CODE (SET_SRC (x)) == SUBREG
2261 && SUBREG_REG (SET_SRC (x)) == var))
2262 && (GET_CODE (SET_DEST (x)) == REG
2263 || (GET_CODE (SET_DEST (x)) == SUBREG
2264 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG))
2265 && GET_MODE (var) == promoted_mode
2266 && x == single_set (insn))
2268 rtx pat, last;
2270 if (GET_CODE (SET_SRC (x)) == SUBREG
2271 && (GET_MODE_SIZE (GET_MODE (SET_SRC (x)))
2272 > GET_MODE_SIZE (GET_MODE (var))))
2274 /* This (subreg VAR) is now a paradoxical subreg. We need
2275 to replace VAR instead of the subreg. */
2276 replacement = find_fixup_replacement (replacements, var);
2277 if (replacement->new == NULL_RTX)
2278 replacement->new = gen_reg_rtx (GET_MODE (var));
2279 SUBREG_REG (SET_SRC (x)) = replacement->new;
2281 else
2283 replacement = find_fixup_replacement (replacements, SET_SRC (x));
2284 if (replacement->new)
2285 SET_SRC (x) = replacement->new;
2286 else if (GET_CODE (SET_SRC (x)) == SUBREG)
2287 SET_SRC (x) = replacement->new
2288 = fixup_memory_subreg (SET_SRC (x), insn, promoted_mode,
2290 else
2291 SET_SRC (x) = replacement->new
2292 = fixup_stack_1 (SET_SRC (x), insn);
2295 if (recog_memoized (insn) >= 0)
2296 return;
2298 /* INSN is not valid, but we know that we want to
2299 copy SET_SRC (x) to SET_DEST (x) in some way. So
2300 we generate the move and see whether it requires more
2301 than one insn. If it does, we emit those insns and
2302 delete INSN. Otherwise, we can just replace the pattern
2303 of INSN; we have already verified above that INSN has
2304 no other function that to do X. */
2306 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2307 if (NEXT_INSN (pat) != NULL_RTX)
2309 last = emit_insn_before (pat, insn);
2311 /* INSN might have REG_RETVAL or other important notes, so
2312 we need to store the pattern of the last insn in the
2313 sequence into INSN similarly to the normal case. LAST
2314 should not have REG_NOTES, but we allow them if INSN has
2315 no REG_NOTES. */
2316 if (REG_NOTES (last) && REG_NOTES (insn))
2317 abort ();
2318 if (REG_NOTES (last))
2319 REG_NOTES (insn) = REG_NOTES (last);
2320 PATTERN (insn) = PATTERN (last);
2322 delete_insn (last);
2324 else
2325 PATTERN (insn) = PATTERN (pat);
2327 return;
2330 if ((SET_DEST (x) == var
2331 || (GET_CODE (SET_DEST (x)) == SUBREG
2332 && SUBREG_REG (SET_DEST (x)) == var))
2333 && (GET_CODE (SET_SRC (x)) == REG
2334 || (GET_CODE (SET_SRC (x)) == SUBREG
2335 && GET_CODE (SUBREG_REG (SET_SRC (x))) == REG))
2336 && GET_MODE (var) == promoted_mode
2337 && x == single_set (insn))
2339 rtx pat, last;
2341 if (GET_CODE (SET_DEST (x)) == SUBREG)
2342 SET_DEST (x) = fixup_memory_subreg (SET_DEST (x), insn,
2343 promoted_mode, 0);
2344 else
2345 SET_DEST (x) = fixup_stack_1 (SET_DEST (x), insn);
2347 if (recog_memoized (insn) >= 0)
2348 return;
2350 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2351 if (NEXT_INSN (pat) != NULL_RTX)
2353 last = emit_insn_before (pat, insn);
2355 /* INSN might have REG_RETVAL or other important notes, so
2356 we need to store the pattern of the last insn in the
2357 sequence into INSN similarly to the normal case. LAST
2358 should not have REG_NOTES, but we allow them if INSN has
2359 no REG_NOTES. */
2360 if (REG_NOTES (last) && REG_NOTES (insn))
2361 abort ();
2362 if (REG_NOTES (last))
2363 REG_NOTES (insn) = REG_NOTES (last);
2364 PATTERN (insn) = PATTERN (last);
2366 delete_insn (last);
2368 else
2369 PATTERN (insn) = PATTERN (pat);
2371 return;
2374 /* Otherwise, storing into VAR must be handled specially
2375 by storing into a temporary and copying that into VAR
2376 with a new insn after this one. Note that this case
2377 will be used when storing into a promoted scalar since
2378 the insn will now have different modes on the input
2379 and output and hence will be invalid (except for the case
2380 of setting it to a constant, which does not need any
2381 change if it is valid). We generate extra code in that case,
2382 but combine.c will eliminate it. */
2384 if (dest == var)
2386 rtx temp;
2387 rtx fixeddest = SET_DEST (x);
2388 enum machine_mode temp_mode;
2390 /* STRICT_LOW_PART can be discarded, around a MEM. */
2391 if (GET_CODE (fixeddest) == STRICT_LOW_PART)
2392 fixeddest = XEXP (fixeddest, 0);
2393 /* Convert (SUBREG (MEM)) to a MEM in a changed mode. */
2394 if (GET_CODE (fixeddest) == SUBREG)
2396 fixeddest = fixup_memory_subreg (fixeddest, insn,
2397 promoted_mode, 0);
2398 temp_mode = GET_MODE (fixeddest);
2400 else
2402 fixeddest = fixup_stack_1 (fixeddest, insn);
2403 temp_mode = promoted_mode;
2406 temp = gen_reg_rtx (temp_mode);
2408 emit_insn_after (gen_move_insn (fixeddest,
2409 gen_lowpart (GET_MODE (fixeddest),
2410 temp)),
2411 insn);
2413 SET_DEST (x) = temp;
2417 default:
2418 break;
2421 /* Nothing special about this RTX; fix its operands. */
2423 fmt = GET_RTX_FORMAT (code);
2424 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2426 if (fmt[i] == 'e')
2427 fixup_var_refs_1 (var, promoted_mode, &XEXP (x, i), insn, replacements,
2428 no_share);
2429 else if (fmt[i] == 'E')
2431 int j;
2432 for (j = 0; j < XVECLEN (x, i); j++)
2433 fixup_var_refs_1 (var, promoted_mode, &XVECEXP (x, i, j),
2434 insn, replacements, no_share);
2439 /* Previously, X had the form (SUBREG:m1 (REG:PROMOTED_MODE ...)).
2440 The REG was placed on the stack, so X now has the form (SUBREG:m1
2441 (MEM:m2 ...)).
2443 Return an rtx (MEM:m1 newaddr) which is equivalent. If any insns
2444 must be emitted to compute NEWADDR, put them before INSN.
2446 UNCRITICAL nonzero means accept paradoxical subregs.
2447 This is used for subregs found inside REG_NOTES. */
2449 static rtx
2450 fixup_memory_subreg (rtx x, rtx insn, enum machine_mode promoted_mode, int uncritical)
2452 int offset;
2453 rtx mem = SUBREG_REG (x);
2454 rtx addr = XEXP (mem, 0);
2455 enum machine_mode mode = GET_MODE (x);
2456 rtx result, seq;
2458 /* Paradoxical SUBREGs are usually invalid during RTL generation. */
2459 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (mem)) && ! uncritical)
2460 abort ();
2462 offset = SUBREG_BYTE (x);
2463 if (BYTES_BIG_ENDIAN)
2464 /* If the PROMOTED_MODE is wider than the mode of the MEM, adjust
2465 the offset so that it points to the right location within the
2466 MEM. */
2467 offset -= (GET_MODE_SIZE (promoted_mode) - GET_MODE_SIZE (GET_MODE (mem)));
2469 if (!flag_force_addr
2470 && memory_address_p (mode, plus_constant (addr, offset)))
2471 /* Shortcut if no insns need be emitted. */
2472 return adjust_address (mem, mode, offset);
2474 start_sequence ();
2475 result = adjust_address (mem, mode, offset);
2476 seq = get_insns ();
2477 end_sequence ();
2479 emit_insn_before (seq, insn);
2480 return result;
2483 /* Do fixup_memory_subreg on all (SUBREG (MEM ...) ...) contained in X.
2484 Replace subexpressions of X in place.
2485 If X itself is a (SUBREG (MEM ...) ...), return the replacement expression.
2486 Otherwise return X, with its contents possibly altered.
2488 INSN, PROMOTED_MODE and UNCRITICAL are as for
2489 fixup_memory_subreg. */
2491 static rtx
2492 walk_fixup_memory_subreg (rtx x, rtx insn, enum machine_mode promoted_mode,
2493 int uncritical)
2495 enum rtx_code code;
2496 const char *fmt;
2497 int i;
2499 if (x == 0)
2500 return 0;
2502 code = GET_CODE (x);
2504 if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
2505 return fixup_memory_subreg (x, insn, promoted_mode, uncritical);
2507 /* Nothing special about this RTX; fix its operands. */
2509 fmt = GET_RTX_FORMAT (code);
2510 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2512 if (fmt[i] == 'e')
2513 XEXP (x, i) = walk_fixup_memory_subreg (XEXP (x, i), insn,
2514 promoted_mode, uncritical);
2515 else if (fmt[i] == 'E')
2517 int j;
2518 for (j = 0; j < XVECLEN (x, i); j++)
2519 XVECEXP (x, i, j)
2520 = walk_fixup_memory_subreg (XVECEXP (x, i, j), insn,
2521 promoted_mode, uncritical);
2524 return x;
2527 /* For each memory ref within X, if it refers to a stack slot
2528 with an out of range displacement, put the address in a temp register
2529 (emitting new insns before INSN to load these registers)
2530 and alter the memory ref to use that register.
2531 Replace each such MEM rtx with a copy, to avoid clobberage. */
2533 static rtx
2534 fixup_stack_1 (rtx x, rtx insn)
2536 int i;
2537 RTX_CODE code = GET_CODE (x);
2538 const char *fmt;
2540 if (code == MEM)
2542 rtx ad = XEXP (x, 0);
2543 /* If we have address of a stack slot but it's not valid
2544 (displacement is too large), compute the sum in a register. */
2545 if (GET_CODE (ad) == PLUS
2546 && GET_CODE (XEXP (ad, 0)) == REG
2547 && ((REGNO (XEXP (ad, 0)) >= FIRST_VIRTUAL_REGISTER
2548 && REGNO (XEXP (ad, 0)) <= LAST_VIRTUAL_REGISTER)
2549 || REGNO (XEXP (ad, 0)) == FRAME_POINTER_REGNUM
2550 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2551 || REGNO (XEXP (ad, 0)) == HARD_FRAME_POINTER_REGNUM
2552 #endif
2553 || REGNO (XEXP (ad, 0)) == STACK_POINTER_REGNUM
2554 || REGNO (XEXP (ad, 0)) == ARG_POINTER_REGNUM
2555 || XEXP (ad, 0) == current_function_internal_arg_pointer)
2556 && GET_CODE (XEXP (ad, 1)) == CONST_INT)
2558 rtx temp, seq;
2559 if (memory_address_p (GET_MODE (x), ad))
2560 return x;
2562 start_sequence ();
2563 temp = copy_to_reg (ad);
2564 seq = get_insns ();
2565 end_sequence ();
2566 emit_insn_before (seq, insn);
2567 return replace_equiv_address (x, temp);
2569 return x;
2572 fmt = GET_RTX_FORMAT (code);
2573 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2575 if (fmt[i] == 'e')
2576 XEXP (x, i) = fixup_stack_1 (XEXP (x, i), insn);
2577 else if (fmt[i] == 'E')
2579 int j;
2580 for (j = 0; j < XVECLEN (x, i); j++)
2581 XVECEXP (x, i, j) = fixup_stack_1 (XVECEXP (x, i, j), insn);
2584 return x;
2587 /* Optimization: a bit-field instruction whose field
2588 happens to be a byte or halfword in memory
2589 can be changed to a move instruction.
2591 We call here when INSN is an insn to examine or store into a bit-field.
2592 BODY is the SET-rtx to be altered.
2594 EQUIV_MEM is the table `reg_equiv_mem' if that is available; else 0.
2595 (Currently this is called only from function.c, and EQUIV_MEM
2596 is always 0.) */
2598 static void
2599 optimize_bit_field (rtx body, rtx insn, rtx *equiv_mem)
2601 rtx bitfield;
2602 int destflag;
2603 rtx seq = 0;
2604 enum machine_mode mode;
2606 if (GET_CODE (SET_DEST (body)) == SIGN_EXTRACT
2607 || GET_CODE (SET_DEST (body)) == ZERO_EXTRACT)
2608 bitfield = SET_DEST (body), destflag = 1;
2609 else
2610 bitfield = SET_SRC (body), destflag = 0;
2612 /* First check that the field being stored has constant size and position
2613 and is in fact a byte or halfword suitably aligned. */
2615 if (GET_CODE (XEXP (bitfield, 1)) == CONST_INT
2616 && GET_CODE (XEXP (bitfield, 2)) == CONST_INT
2617 && ((mode = mode_for_size (INTVAL (XEXP (bitfield, 1)), MODE_INT, 1))
2618 != BLKmode)
2619 && INTVAL (XEXP (bitfield, 2)) % INTVAL (XEXP (bitfield, 1)) == 0)
2621 rtx memref = 0;
2623 /* Now check that the containing word is memory, not a register,
2624 and that it is safe to change the machine mode. */
2626 if (GET_CODE (XEXP (bitfield, 0)) == MEM)
2627 memref = XEXP (bitfield, 0);
2628 else if (GET_CODE (XEXP (bitfield, 0)) == REG
2629 && equiv_mem != 0)
2630 memref = equiv_mem[REGNO (XEXP (bitfield, 0))];
2631 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2632 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == MEM)
2633 memref = SUBREG_REG (XEXP (bitfield, 0));
2634 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2635 && equiv_mem != 0
2636 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == REG)
2637 memref = equiv_mem[REGNO (SUBREG_REG (XEXP (bitfield, 0)))];
2639 if (memref
2640 && ! mode_dependent_address_p (XEXP (memref, 0))
2641 && ! MEM_VOLATILE_P (memref))
2643 /* Now adjust the address, first for any subreg'ing
2644 that we are now getting rid of,
2645 and then for which byte of the word is wanted. */
2647 HOST_WIDE_INT offset = INTVAL (XEXP (bitfield, 2));
2648 rtx insns;
2650 /* Adjust OFFSET to count bits from low-address byte. */
2651 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
2652 offset = (GET_MODE_BITSIZE (GET_MODE (XEXP (bitfield, 0)))
2653 - offset - INTVAL (XEXP (bitfield, 1)));
2655 /* Adjust OFFSET to count bytes from low-address byte. */
2656 offset /= BITS_PER_UNIT;
2657 if (GET_CODE (XEXP (bitfield, 0)) == SUBREG)
2659 offset += (SUBREG_BYTE (XEXP (bitfield, 0))
2660 / UNITS_PER_WORD) * UNITS_PER_WORD;
2661 if (BYTES_BIG_ENDIAN)
2662 offset -= (MIN (UNITS_PER_WORD,
2663 GET_MODE_SIZE (GET_MODE (XEXP (bitfield, 0))))
2664 - MIN (UNITS_PER_WORD,
2665 GET_MODE_SIZE (GET_MODE (memref))));
2668 start_sequence ();
2669 memref = adjust_address (memref, mode, offset);
2670 insns = get_insns ();
2671 end_sequence ();
2672 emit_insn_before (insns, insn);
2674 /* Store this memory reference where
2675 we found the bit field reference. */
2677 if (destflag)
2679 validate_change (insn, &SET_DEST (body), memref, 1);
2680 if (! CONSTANT_ADDRESS_P (SET_SRC (body)))
2682 rtx src = SET_SRC (body);
2683 while (GET_CODE (src) == SUBREG
2684 && SUBREG_BYTE (src) == 0)
2685 src = SUBREG_REG (src);
2686 if (GET_MODE (src) != GET_MODE (memref))
2687 src = gen_lowpart (GET_MODE (memref), SET_SRC (body));
2688 validate_change (insn, &SET_SRC (body), src, 1);
2690 else if (GET_MODE (SET_SRC (body)) != VOIDmode
2691 && GET_MODE (SET_SRC (body)) != GET_MODE (memref))
2692 /* This shouldn't happen because anything that didn't have
2693 one of these modes should have got converted explicitly
2694 and then referenced through a subreg.
2695 This is so because the original bit-field was
2696 handled by agg_mode and so its tree structure had
2697 the same mode that memref now has. */
2698 abort ();
2700 else
2702 rtx dest = SET_DEST (body);
2704 while (GET_CODE (dest) == SUBREG
2705 && SUBREG_BYTE (dest) == 0
2706 && (GET_MODE_CLASS (GET_MODE (dest))
2707 == GET_MODE_CLASS (GET_MODE (SUBREG_REG (dest))))
2708 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
2709 <= UNITS_PER_WORD))
2710 dest = SUBREG_REG (dest);
2712 validate_change (insn, &SET_DEST (body), dest, 1);
2714 if (GET_MODE (dest) == GET_MODE (memref))
2715 validate_change (insn, &SET_SRC (body), memref, 1);
2716 else
2718 /* Convert the mem ref to the destination mode. */
2719 rtx newreg = gen_reg_rtx (GET_MODE (dest));
2721 start_sequence ();
2722 convert_move (newreg, memref,
2723 GET_CODE (SET_SRC (body)) == ZERO_EXTRACT);
2724 seq = get_insns ();
2725 end_sequence ();
2727 validate_change (insn, &SET_SRC (body), newreg, 1);
2731 /* See if we can convert this extraction or insertion into
2732 a simple move insn. We might not be able to do so if this
2733 was, for example, part of a PARALLEL.
2735 If we succeed, write out any needed conversions. If we fail,
2736 it is hard to guess why we failed, so don't do anything
2737 special; just let the optimization be suppressed. */
2739 if (apply_change_group () && seq)
2740 emit_insn_before (seq, insn);
2745 /* These routines are responsible for converting virtual register references
2746 to the actual hard register references once RTL generation is complete.
2748 The following four variables are used for communication between the
2749 routines. They contain the offsets of the virtual registers from their
2750 respective hard registers. */
2752 static int in_arg_offset;
2753 static int var_offset;
2754 static int dynamic_offset;
2755 static int out_arg_offset;
2756 static int cfa_offset;
2758 /* In most machines, the stack pointer register is equivalent to the bottom
2759 of the stack. */
2761 #ifndef STACK_POINTER_OFFSET
2762 #define STACK_POINTER_OFFSET 0
2763 #endif
2765 /* If not defined, pick an appropriate default for the offset of dynamically
2766 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
2767 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
2769 #ifndef STACK_DYNAMIC_OFFSET
2771 /* The bottom of the stack points to the actual arguments. If
2772 REG_PARM_STACK_SPACE is defined, this includes the space for the register
2773 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
2774 stack space for register parameters is not pushed by the caller, but
2775 rather part of the fixed stack areas and hence not included in
2776 `current_function_outgoing_args_size'. Nevertheless, we must allow
2777 for it when allocating stack dynamic objects. */
2779 #if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
2780 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2781 ((ACCUMULATE_OUTGOING_ARGS \
2782 ? (current_function_outgoing_args_size + REG_PARM_STACK_SPACE (FNDECL)) : 0)\
2783 + (STACK_POINTER_OFFSET)) \
2785 #else
2786 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2787 ((ACCUMULATE_OUTGOING_ARGS ? current_function_outgoing_args_size : 0) \
2788 + (STACK_POINTER_OFFSET))
2789 #endif
2790 #endif
2792 /* On most machines, the CFA coincides with the first incoming parm. */
2794 #ifndef ARG_POINTER_CFA_OFFSET
2795 #define ARG_POINTER_CFA_OFFSET(FNDECL) FIRST_PARM_OFFSET (FNDECL)
2796 #endif
2798 /* Build up a (MEM (ADDRESSOF (REG))) rtx for a register REG that just
2799 had its address taken. DECL is the decl or SAVE_EXPR for the
2800 object stored in the register, for later use if we do need to force
2801 REG into the stack. REG is overwritten by the MEM like in
2802 put_reg_into_stack. RESCAN is true if previously emitted
2803 instructions must be rescanned and modified now that the REG has
2804 been transformed. */
2807 gen_mem_addressof (rtx reg, tree decl, int rescan)
2809 rtx r = gen_rtx_ADDRESSOF (Pmode, gen_reg_rtx (GET_MODE (reg)),
2810 REGNO (reg), decl);
2812 /* Calculate this before we start messing with decl's RTL. */
2813 HOST_WIDE_INT set = decl ? get_alias_set (decl) : 0;
2815 /* If the original REG was a user-variable, then so is the REG whose
2816 address is being taken. Likewise for unchanging. */
2817 REG_USERVAR_P (XEXP (r, 0)) = REG_USERVAR_P (reg);
2818 RTX_UNCHANGING_P (XEXP (r, 0)) = RTX_UNCHANGING_P (reg);
2820 PUT_CODE (reg, MEM);
2821 MEM_ATTRS (reg) = 0;
2822 XEXP (reg, 0) = r;
2824 if (decl)
2826 tree type = TREE_TYPE (decl);
2827 enum machine_mode decl_mode
2828 = (DECL_P (decl) ? DECL_MODE (decl) : TYPE_MODE (TREE_TYPE (decl)));
2829 rtx decl_rtl = (TREE_CODE (decl) == SAVE_EXPR ? SAVE_EXPR_RTL (decl)
2830 : DECL_RTL_IF_SET (decl));
2832 PUT_MODE (reg, decl_mode);
2834 /* Clear DECL_RTL momentarily so functions below will work
2835 properly, then set it again. */
2836 if (DECL_P (decl) && decl_rtl == reg)
2837 SET_DECL_RTL (decl, 0);
2839 set_mem_attributes (reg, decl, 1);
2840 set_mem_alias_set (reg, set);
2842 if (DECL_P (decl) && decl_rtl == reg)
2843 SET_DECL_RTL (decl, reg);
2845 if (rescan
2846 && (TREE_USED (decl) || (DECL_P (decl) && DECL_INITIAL (decl) != 0)))
2847 fixup_var_refs (reg, GET_MODE (reg), TREE_UNSIGNED (type), reg, 0);
2849 else if (rescan)
2850 fixup_var_refs (reg, GET_MODE (reg), 0, reg, 0);
2852 return reg;
2855 /* If DECL has an RTL that is an ADDRESSOF rtx, put it into the stack. */
2857 void
2858 flush_addressof (tree decl)
2860 if ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL)
2861 && DECL_RTL (decl) != 0
2862 && GET_CODE (DECL_RTL (decl)) == MEM
2863 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF
2864 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == REG)
2865 put_addressof_into_stack (XEXP (DECL_RTL (decl), 0), 0);
2868 /* Force the register pointed to by R, an ADDRESSOF rtx, into the stack. */
2870 static void
2871 put_addressof_into_stack (rtx r, htab_t ht)
2873 tree decl, type;
2874 int volatile_p, used_p;
2876 rtx reg = XEXP (r, 0);
2878 if (GET_CODE (reg) != REG)
2879 abort ();
2881 decl = ADDRESSOF_DECL (r);
2882 if (decl)
2884 type = TREE_TYPE (decl);
2885 volatile_p = (TREE_CODE (decl) != SAVE_EXPR
2886 && TREE_THIS_VOLATILE (decl));
2887 used_p = (TREE_USED (decl)
2888 || (DECL_P (decl) && DECL_INITIAL (decl) != 0));
2890 else
2892 type = NULL_TREE;
2893 volatile_p = 0;
2894 used_p = 1;
2897 put_reg_into_stack (0, reg, type, GET_MODE (reg), GET_MODE (reg),
2898 volatile_p, ADDRESSOF_REGNO (r), used_p, ht);
2901 /* List of replacements made below in purge_addressof_1 when creating
2902 bitfield insertions. */
2903 static rtx purge_bitfield_addressof_replacements;
2905 /* List of replacements made below in purge_addressof_1 for patterns
2906 (MEM (ADDRESSOF (REG ...))). The key of the list entry is the
2907 corresponding (ADDRESSOF (REG ...)) and value is a substitution for
2908 the all pattern. List PURGE_BITFIELD_ADDRESSOF_REPLACEMENTS is not
2909 enough in complex cases, e.g. when some field values can be
2910 extracted by usage MEM with narrower mode. */
2911 static rtx purge_addressof_replacements;
2913 /* Helper function for purge_addressof. See if the rtx expression at *LOC
2914 in INSN needs to be changed. If FORCE, always put any ADDRESSOFs into
2915 the stack. If the function returns FALSE then the replacement could not
2916 be made. If MAY_POSTPONE is true and we would not put the addressof
2917 to stack, postpone processing of the insn. */
2919 static bool
2920 purge_addressof_1 (rtx *loc, rtx insn, int force, int store, int may_postpone,
2921 htab_t ht)
2923 rtx x;
2924 RTX_CODE code;
2925 int i, j;
2926 const char *fmt;
2927 bool result = true;
2929 /* Re-start here to avoid recursion in common cases. */
2930 restart:
2932 x = *loc;
2933 if (x == 0)
2934 return true;
2936 code = GET_CODE (x);
2938 /* If we don't return in any of the cases below, we will recurse inside
2939 the RTX, which will normally result in any ADDRESSOF being forced into
2940 memory. */
2941 if (code == SET)
2943 result = purge_addressof_1 (&SET_DEST (x), insn, force, 1,
2944 may_postpone, ht);
2945 result &= purge_addressof_1 (&SET_SRC (x), insn, force, 0,
2946 may_postpone, ht);
2947 return result;
2949 else if (code == ADDRESSOF)
2951 rtx sub, insns;
2953 if (GET_CODE (XEXP (x, 0)) != MEM)
2954 put_addressof_into_stack (x, ht);
2956 /* We must create a copy of the rtx because it was created by
2957 overwriting a REG rtx which is always shared. */
2958 sub = copy_rtx (XEXP (XEXP (x, 0), 0));
2959 if (validate_change (insn, loc, sub, 0)
2960 || validate_replace_rtx (x, sub, insn))
2961 return true;
2963 start_sequence ();
2965 /* If SUB is a hard or virtual register, try it as a pseudo-register.
2966 Otherwise, perhaps SUB is an expression, so generate code to compute
2967 it. */
2968 if (GET_CODE (sub) == REG && REGNO (sub) <= LAST_VIRTUAL_REGISTER)
2969 sub = copy_to_reg (sub);
2970 else
2971 sub = force_operand (sub, NULL_RTX);
2973 if (! validate_change (insn, loc, sub, 0)
2974 && ! validate_replace_rtx (x, sub, insn))
2975 abort ();
2977 insns = get_insns ();
2978 end_sequence ();
2979 emit_insn_before (insns, insn);
2980 return true;
2983 else if (code == MEM && GET_CODE (XEXP (x, 0)) == ADDRESSOF && ! force)
2985 rtx sub = XEXP (XEXP (x, 0), 0);
2987 if (GET_CODE (sub) == MEM)
2988 sub = adjust_address_nv (sub, GET_MODE (x), 0);
2989 else if (GET_CODE (sub) == REG
2990 && (MEM_VOLATILE_P (x) || GET_MODE (x) == BLKmode))
2992 else if (GET_CODE (sub) == REG && GET_MODE (x) != GET_MODE (sub))
2994 int size_x, size_sub;
2996 if (may_postpone)
2998 /* Postpone for now, so that we do not emit bitfield arithmetics
2999 unless there is some benefit from it. */
3000 if (!postponed_insns || XEXP (postponed_insns, 0) != insn)
3001 postponed_insns = alloc_INSN_LIST (insn, postponed_insns);
3002 return true;
3005 if (!insn)
3007 /* When processing REG_NOTES look at the list of
3008 replacements done on the insn to find the register that X
3009 was replaced by. */
3010 rtx tem;
3012 for (tem = purge_bitfield_addressof_replacements;
3013 tem != NULL_RTX;
3014 tem = XEXP (XEXP (tem, 1), 1))
3015 if (rtx_equal_p (x, XEXP (tem, 0)))
3017 *loc = XEXP (XEXP (tem, 1), 0);
3018 return true;
3021 /* See comment for purge_addressof_replacements. */
3022 for (tem = purge_addressof_replacements;
3023 tem != NULL_RTX;
3024 tem = XEXP (XEXP (tem, 1), 1))
3025 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3027 rtx z = XEXP (XEXP (tem, 1), 0);
3029 if (GET_MODE (x) == GET_MODE (z)
3030 || (GET_CODE (XEXP (XEXP (tem, 1), 0)) != REG
3031 && GET_CODE (XEXP (XEXP (tem, 1), 0)) != SUBREG))
3032 abort ();
3034 /* It can happen that the note may speak of things
3035 in a wider (or just different) mode than the
3036 code did. This is especially true of
3037 REG_RETVAL. */
3039 if (GET_CODE (z) == SUBREG && SUBREG_BYTE (z) == 0)
3040 z = SUBREG_REG (z);
3042 if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3043 && (GET_MODE_SIZE (GET_MODE (x))
3044 > GET_MODE_SIZE (GET_MODE (z))))
3046 /* This can occur as a result in invalid
3047 pointer casts, e.g. float f; ...
3048 *(long long int *)&f.
3049 ??? We could emit a warning here, but
3050 without a line number that wouldn't be
3051 very helpful. */
3052 z = gen_rtx_SUBREG (GET_MODE (x), z, 0);
3054 else
3055 z = gen_lowpart (GET_MODE (x), z);
3057 *loc = z;
3058 return true;
3061 /* When we are processing the REG_NOTES of the last instruction
3062 of a libcall, there will be typically no replacements
3063 for that insn; the replacements happened before, piecemeal
3064 fashion. OTOH we are not interested in the details of
3065 this for the REG_EQUAL note, we want to know the big picture,
3066 which can be succinctly described with a simple SUBREG.
3067 Note that removing the REG_EQUAL note is not an option
3068 on the last insn of a libcall, so we must do a replacement. */
3069 if (! purge_addressof_replacements
3070 && ! purge_bitfield_addressof_replacements)
3072 /* In compile/990107-1.c:7 compiled at -O1 -m1 for sh-elf,
3073 we got
3074 (mem:DI (addressof:SI (reg/v:DF 160) 159 0x401c8510)
3075 [0 S8 A32]), which can be expressed with a simple
3076 same-size subreg */
3077 if ((GET_MODE_SIZE (GET_MODE (x))
3078 == GET_MODE_SIZE (GET_MODE (sub)))
3079 /* Again, invalid pointer casts (as in
3080 compile/990203-1.c) can require paradoxical
3081 subregs. */
3082 || (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3083 && (GET_MODE_SIZE (GET_MODE (x))
3084 > GET_MODE_SIZE (GET_MODE (sub)))))
3086 *loc = gen_rtx_SUBREG (GET_MODE (x), sub, 0);
3087 return true;
3089 /* ??? Are there other cases we should handle? */
3091 /* Sometimes we may not be able to find the replacement. For
3092 example when the original insn was a MEM in a wider mode,
3093 and the note is part of a sign extension of a narrowed
3094 version of that MEM. Gcc testcase compile/990829-1.c can
3095 generate an example of this situation. Rather than complain
3096 we return false, which will prompt our caller to remove the
3097 offending note. */
3098 return false;
3101 size_x = GET_MODE_BITSIZE (GET_MODE (x));
3102 size_sub = GET_MODE_BITSIZE (GET_MODE (sub));
3104 /* Do not frob unchanging MEMs. If a later reference forces the
3105 pseudo to the stack, we can wind up with multiple writes to
3106 an unchanging memory, which is invalid. */
3107 if (RTX_UNCHANGING_P (x) && size_x != size_sub)
3110 /* Don't even consider working with paradoxical subregs,
3111 or the moral equivalent seen here. */
3112 else if (size_x <= size_sub
3113 && int_mode_for_mode (GET_MODE (sub)) != BLKmode)
3115 /* Do a bitfield insertion to mirror what would happen
3116 in memory. */
3118 rtx val, seq;
3120 if (store)
3122 rtx p = PREV_INSN (insn);
3124 start_sequence ();
3125 val = gen_reg_rtx (GET_MODE (x));
3126 if (! validate_change (insn, loc, val, 0))
3128 /* Discard the current sequence and put the
3129 ADDRESSOF on stack. */
3130 end_sequence ();
3131 goto give_up;
3133 seq = get_insns ();
3134 end_sequence ();
3135 emit_insn_before (seq, insn);
3136 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3137 insn, ht);
3139 start_sequence ();
3140 store_bit_field (sub, size_x, 0, GET_MODE (x),
3141 val, GET_MODE_SIZE (GET_MODE (sub)));
3143 /* Make sure to unshare any shared rtl that store_bit_field
3144 might have created. */
3145 unshare_all_rtl_again (get_insns ());
3147 seq = get_insns ();
3148 end_sequence ();
3149 p = emit_insn_after (seq, insn);
3150 if (NEXT_INSN (insn))
3151 compute_insns_for_mem (NEXT_INSN (insn),
3152 p ? NEXT_INSN (p) : NULL_RTX,
3153 ht);
3155 else
3157 rtx p = PREV_INSN (insn);
3159 start_sequence ();
3160 val = extract_bit_field (sub, size_x, 0, 1, NULL_RTX,
3161 GET_MODE (x), GET_MODE (x),
3162 GET_MODE_SIZE (GET_MODE (sub)));
3164 if (! validate_change (insn, loc, val, 0))
3166 /* Discard the current sequence and put the
3167 ADDRESSOF on stack. */
3168 end_sequence ();
3169 goto give_up;
3172 seq = get_insns ();
3173 end_sequence ();
3174 emit_insn_before (seq, insn);
3175 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3176 insn, ht);
3179 /* Remember the replacement so that the same one can be done
3180 on the REG_NOTES. */
3181 purge_bitfield_addressof_replacements
3182 = gen_rtx_EXPR_LIST (VOIDmode, x,
3183 gen_rtx_EXPR_LIST
3184 (VOIDmode, val,
3185 purge_bitfield_addressof_replacements));
3187 /* We replaced with a reg -- all done. */
3188 return true;
3192 else if (validate_change (insn, loc, sub, 0))
3194 /* Remember the replacement so that the same one can be done
3195 on the REG_NOTES. */
3196 if (GET_CODE (sub) == REG || GET_CODE (sub) == SUBREG)
3198 rtx tem;
3200 for (tem = purge_addressof_replacements;
3201 tem != NULL_RTX;
3202 tem = XEXP (XEXP (tem, 1), 1))
3203 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3205 XEXP (XEXP (tem, 1), 0) = sub;
3206 return true;
3208 purge_addressof_replacements
3209 = gen_rtx (EXPR_LIST, VOIDmode, XEXP (x, 0),
3210 gen_rtx_EXPR_LIST (VOIDmode, sub,
3211 purge_addressof_replacements));
3212 return true;
3214 goto restart;
3218 give_up:
3219 /* Scan all subexpressions. */
3220 fmt = GET_RTX_FORMAT (code);
3221 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3223 if (*fmt == 'e')
3224 result &= purge_addressof_1 (&XEXP (x, i), insn, force, 0,
3225 may_postpone, ht);
3226 else if (*fmt == 'E')
3227 for (j = 0; j < XVECLEN (x, i); j++)
3228 result &= purge_addressof_1 (&XVECEXP (x, i, j), insn, force, 0,
3229 may_postpone, ht);
3232 return result;
3235 /* Return a hash value for K, a REG. */
3237 static hashval_t
3238 insns_for_mem_hash (const void *k)
3240 /* Use the address of the key for the hash value. */
3241 struct insns_for_mem_entry *m = (struct insns_for_mem_entry *) k;
3242 return htab_hash_pointer (m->key);
3245 /* Return nonzero if K1 and K2 (two REGs) are the same. */
3247 static int
3248 insns_for_mem_comp (const void *k1, const void *k2)
3250 struct insns_for_mem_entry *m1 = (struct insns_for_mem_entry *) k1;
3251 struct insns_for_mem_entry *m2 = (struct insns_for_mem_entry *) k2;
3252 return m1->key == m2->key;
3255 struct insns_for_mem_walk_info
3257 /* The hash table that we are using to record which INSNs use which
3258 MEMs. */
3259 htab_t ht;
3261 /* The INSN we are currently processing. */
3262 rtx insn;
3264 /* Zero if we are walking to find ADDRESSOFs, one if we are walking
3265 to find the insns that use the REGs in the ADDRESSOFs. */
3266 int pass;
3269 /* Called from compute_insns_for_mem via for_each_rtx. If R is a REG
3270 that might be used in an ADDRESSOF expression, record this INSN in
3271 the hash table given by DATA (which is really a pointer to an
3272 insns_for_mem_walk_info structure). */
3274 static int
3275 insns_for_mem_walk (rtx *r, void *data)
3277 struct insns_for_mem_walk_info *ifmwi
3278 = (struct insns_for_mem_walk_info *) data;
3279 struct insns_for_mem_entry tmp;
3280 tmp.insns = NULL_RTX;
3282 if (ifmwi->pass == 0 && *r && GET_CODE (*r) == ADDRESSOF
3283 && GET_CODE (XEXP (*r, 0)) == REG)
3285 void **e;
3286 tmp.key = XEXP (*r, 0);
3287 e = htab_find_slot (ifmwi->ht, &tmp, INSERT);
3288 if (*e == NULL)
3290 *e = ggc_alloc (sizeof (tmp));
3291 memcpy (*e, &tmp, sizeof (tmp));
3294 else if (ifmwi->pass == 1 && *r && GET_CODE (*r) == REG)
3296 struct insns_for_mem_entry *ifme;
3297 tmp.key = *r;
3298 ifme = htab_find (ifmwi->ht, &tmp);
3300 /* If we have not already recorded this INSN, do so now. Since
3301 we process the INSNs in order, we know that if we have
3302 recorded it it must be at the front of the list. */
3303 if (ifme && (!ifme->insns || XEXP (ifme->insns, 0) != ifmwi->insn))
3304 ifme->insns = gen_rtx_EXPR_LIST (VOIDmode, ifmwi->insn,
3305 ifme->insns);
3308 return 0;
3311 /* Walk the INSNS, until we reach LAST_INSN, recording which INSNs use
3312 which REGs in HT. */
3314 static void
3315 compute_insns_for_mem (rtx insns, rtx last_insn, htab_t ht)
3317 rtx insn;
3318 struct insns_for_mem_walk_info ifmwi;
3319 ifmwi.ht = ht;
3321 for (ifmwi.pass = 0; ifmwi.pass < 2; ++ifmwi.pass)
3322 for (insn = insns; insn != last_insn; insn = NEXT_INSN (insn))
3323 if (INSN_P (insn))
3325 ifmwi.insn = insn;
3326 for_each_rtx (&insn, insns_for_mem_walk, &ifmwi);
3330 /* Helper function for purge_addressof called through for_each_rtx.
3331 Returns true iff the rtl is an ADDRESSOF. */
3333 static int
3334 is_addressof (rtx *rtl, void *data ATTRIBUTE_UNUSED)
3336 return GET_CODE (*rtl) == ADDRESSOF;
3339 /* Eliminate all occurrences of ADDRESSOF from INSNS. Elide any remaining
3340 (MEM (ADDRESSOF)) patterns, and force any needed registers into the
3341 stack. */
3343 void
3344 purge_addressof (rtx insns)
3346 rtx insn, tmp;
3347 htab_t ht;
3349 /* When we actually purge ADDRESSOFs, we turn REGs into MEMs. That
3350 requires a fixup pass over the instruction stream to correct
3351 INSNs that depended on the REG being a REG, and not a MEM. But,
3352 these fixup passes are slow. Furthermore, most MEMs are not
3353 mentioned in very many instructions. So, we speed up the process
3354 by pre-calculating which REGs occur in which INSNs; that allows
3355 us to perform the fixup passes much more quickly. */
3356 ht = htab_create_ggc (1000, insns_for_mem_hash, insns_for_mem_comp, NULL);
3357 compute_insns_for_mem (insns, NULL_RTX, ht);
3359 postponed_insns = NULL;
3361 for (insn = insns; insn; insn = NEXT_INSN (insn))
3362 if (INSN_P (insn))
3364 if (! purge_addressof_1 (&PATTERN (insn), insn,
3365 asm_noperands (PATTERN (insn)) > 0, 0, 1, ht))
3366 /* If we could not replace the ADDRESSOFs in the insn,
3367 something is wrong. */
3368 abort ();
3370 if (! purge_addressof_1 (&REG_NOTES (insn), NULL_RTX, 0, 0, 0, ht))
3372 /* If we could not replace the ADDRESSOFs in the insn's notes,
3373 we can just remove the offending notes instead. */
3374 rtx note;
3376 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3378 /* If we find a REG_RETVAL note then the insn is a libcall.
3379 Such insns must have REG_EQUAL notes as well, in order
3380 for later passes of the compiler to work. So it is not
3381 safe to delete the notes here, and instead we abort. */
3382 if (REG_NOTE_KIND (note) == REG_RETVAL)
3383 abort ();
3384 if (for_each_rtx (&note, is_addressof, NULL))
3385 remove_note (insn, note);
3390 /* Process the postponed insns. */
3391 while (postponed_insns)
3393 insn = XEXP (postponed_insns, 0);
3394 tmp = postponed_insns;
3395 postponed_insns = XEXP (postponed_insns, 1);
3396 free_INSN_LIST_node (tmp);
3398 if (! purge_addressof_1 (&PATTERN (insn), insn,
3399 asm_noperands (PATTERN (insn)) > 0, 0, 0, ht))
3400 abort ();
3403 /* Clean up. */
3404 purge_bitfield_addressof_replacements = 0;
3405 purge_addressof_replacements = 0;
3407 /* REGs are shared. purge_addressof will destructively replace a REG
3408 with a MEM, which creates shared MEMs.
3410 Unfortunately, the children of put_reg_into_stack assume that MEMs
3411 referring to the same stack slot are shared (fixup_var_refs and
3412 the associated hash table code).
3414 So, we have to do another unsharing pass after we have flushed any
3415 REGs that had their address taken into the stack.
3417 It may be worth tracking whether or not we converted any REGs into
3418 MEMs to avoid this overhead when it is not needed. */
3419 unshare_all_rtl_again (get_insns ());
3422 /* Convert a SET of a hard subreg to a set of the appropriate hard
3423 register. A subroutine of purge_hard_subreg_sets. */
3425 static void
3426 purge_single_hard_subreg_set (rtx pattern)
3428 rtx reg = SET_DEST (pattern);
3429 enum machine_mode mode = GET_MODE (SET_DEST (pattern));
3430 int offset = 0;
3432 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG
3433 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
3435 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
3436 GET_MODE (SUBREG_REG (reg)),
3437 SUBREG_BYTE (reg),
3438 GET_MODE (reg));
3439 reg = SUBREG_REG (reg);
3443 if (GET_CODE (reg) == REG && REGNO (reg) < FIRST_PSEUDO_REGISTER)
3445 reg = gen_rtx_REG (mode, REGNO (reg) + offset);
3446 SET_DEST (pattern) = reg;
3450 /* Eliminate all occurrences of SETs of hard subregs from INSNS. The
3451 only such SETs that we expect to see are those left in because
3452 integrate can't handle sets of parts of a return value register.
3454 We don't use alter_subreg because we only want to eliminate subregs
3455 of hard registers. */
3457 void
3458 purge_hard_subreg_sets (rtx insn)
3460 for (; insn; insn = NEXT_INSN (insn))
3462 if (INSN_P (insn))
3464 rtx pattern = PATTERN (insn);
3465 switch (GET_CODE (pattern))
3467 case SET:
3468 if (GET_CODE (SET_DEST (pattern)) == SUBREG)
3469 purge_single_hard_subreg_set (pattern);
3470 break;
3471 case PARALLEL:
3473 int j;
3474 for (j = XVECLEN (pattern, 0) - 1; j >= 0; j--)
3476 rtx inner_pattern = XVECEXP (pattern, 0, j);
3477 if (GET_CODE (inner_pattern) == SET
3478 && GET_CODE (SET_DEST (inner_pattern)) == SUBREG)
3479 purge_single_hard_subreg_set (inner_pattern);
3482 break;
3483 default:
3484 break;
3490 /* Pass through the INSNS of function FNDECL and convert virtual register
3491 references to hard register references. */
3493 void
3494 instantiate_virtual_regs (tree fndecl, rtx insns)
3496 rtx insn;
3497 unsigned int i;
3499 /* Compute the offsets to use for this function. */
3500 in_arg_offset = FIRST_PARM_OFFSET (fndecl);
3501 var_offset = STARTING_FRAME_OFFSET;
3502 dynamic_offset = STACK_DYNAMIC_OFFSET (fndecl);
3503 out_arg_offset = STACK_POINTER_OFFSET;
3504 cfa_offset = ARG_POINTER_CFA_OFFSET (fndecl);
3506 /* Scan all variables and parameters of this function. For each that is
3507 in memory, instantiate all virtual registers if the result is a valid
3508 address. If not, we do it later. That will handle most uses of virtual
3509 regs on many machines. */
3510 instantiate_decls (fndecl, 1);
3512 /* Initialize recognition, indicating that volatile is OK. */
3513 init_recog ();
3515 /* Scan through all the insns, instantiating every virtual register still
3516 present. */
3517 for (insn = insns; insn; insn = NEXT_INSN (insn))
3518 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3519 || GET_CODE (insn) == CALL_INSN)
3521 instantiate_virtual_regs_1 (&PATTERN (insn), insn, 1);
3522 if (INSN_DELETED_P (insn))
3523 continue;
3524 instantiate_virtual_regs_1 (&REG_NOTES (insn), NULL_RTX, 0);
3525 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
3526 if (GET_CODE (insn) == CALL_INSN)
3527 instantiate_virtual_regs_1 (&CALL_INSN_FUNCTION_USAGE (insn),
3528 NULL_RTX, 0);
3530 /* Past this point all ASM statements should match. Verify that
3531 to avoid failures later in the compilation process. */
3532 if (asm_noperands (PATTERN (insn)) >= 0
3533 && ! check_asm_operands (PATTERN (insn)))
3534 instantiate_virtual_regs_lossage (insn);
3537 /* Instantiate the stack slots for the parm registers, for later use in
3538 addressof elimination. */
3539 for (i = 0; i < max_parm_reg; ++i)
3540 if (parm_reg_stack_loc[i])
3541 instantiate_virtual_regs_1 (&parm_reg_stack_loc[i], NULL_RTX, 0);
3543 /* Now instantiate the remaining register equivalences for debugging info.
3544 These will not be valid addresses. */
3545 instantiate_decls (fndecl, 0);
3547 /* Indicate that, from now on, assign_stack_local should use
3548 frame_pointer_rtx. */
3549 virtuals_instantiated = 1;
3552 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
3553 all virtual registers in their DECL_RTL's.
3555 If VALID_ONLY, do this only if the resulting address is still valid.
3556 Otherwise, always do it. */
3558 static void
3559 instantiate_decls (tree fndecl, int valid_only)
3561 tree decl;
3563 /* Process all parameters of the function. */
3564 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
3566 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
3567 HOST_WIDE_INT size_rtl;
3569 instantiate_decl (DECL_RTL (decl), size, valid_only);
3571 /* If the parameter was promoted, then the incoming RTL mode may be
3572 larger than the declared type size. We must use the larger of
3573 the two sizes. */
3574 size_rtl = GET_MODE_SIZE (GET_MODE (DECL_INCOMING_RTL (decl)));
3575 size = MAX (size_rtl, size);
3576 instantiate_decl (DECL_INCOMING_RTL (decl), size, valid_only);
3579 /* Now process all variables defined in the function or its subblocks. */
3580 instantiate_decls_1 (DECL_INITIAL (fndecl), valid_only);
3583 /* Subroutine of instantiate_decls: Process all decls in the given
3584 BLOCK node and all its subblocks. */
3586 static void
3587 instantiate_decls_1 (tree let, int valid_only)
3589 tree t;
3591 for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
3592 if (DECL_RTL_SET_P (t))
3593 instantiate_decl (DECL_RTL (t),
3594 int_size_in_bytes (TREE_TYPE (t)),
3595 valid_only);
3597 /* Process all subblocks. */
3598 for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
3599 instantiate_decls_1 (t, valid_only);
3602 /* Subroutine of the preceding procedures: Given RTL representing a
3603 decl and the size of the object, do any instantiation required.
3605 If VALID_ONLY is nonzero, it means that the RTL should only be
3606 changed if the new address is valid. */
3608 static void
3609 instantiate_decl (rtx x, HOST_WIDE_INT size, int valid_only)
3611 enum machine_mode mode;
3612 rtx addr;
3614 /* If this is not a MEM, no need to do anything. Similarly if the
3615 address is a constant or a register that is not a virtual register. */
3617 if (x == 0 || GET_CODE (x) != MEM)
3618 return;
3620 addr = XEXP (x, 0);
3621 if (CONSTANT_P (addr)
3622 || (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == REG)
3623 || (GET_CODE (addr) == REG
3624 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
3625 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
3626 return;
3628 /* If we should only do this if the address is valid, copy the address.
3629 We need to do this so we can undo any changes that might make the
3630 address invalid. This copy is unfortunate, but probably can't be
3631 avoided. */
3633 if (valid_only)
3634 addr = copy_rtx (addr);
3636 instantiate_virtual_regs_1 (&addr, NULL_RTX, 0);
3638 if (valid_only && size >= 0)
3640 unsigned HOST_WIDE_INT decl_size = size;
3642 /* Now verify that the resulting address is valid for every integer or
3643 floating-point mode up to and including SIZE bytes long. We do this
3644 since the object might be accessed in any mode and frame addresses
3645 are shared. */
3647 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3648 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3649 mode = GET_MODE_WIDER_MODE (mode))
3650 if (! memory_address_p (mode, addr))
3651 return;
3653 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
3654 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3655 mode = GET_MODE_WIDER_MODE (mode))
3656 if (! memory_address_p (mode, addr))
3657 return;
3660 /* Put back the address now that we have updated it and we either know
3661 it is valid or we don't care whether it is valid. */
3663 XEXP (x, 0) = addr;
3666 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
3667 is a virtual register, return the equivalent hard register and set the
3668 offset indirectly through the pointer. Otherwise, return 0. */
3670 static rtx
3671 instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
3673 rtx new;
3674 HOST_WIDE_INT offset;
3676 if (x == virtual_incoming_args_rtx)
3677 new = arg_pointer_rtx, offset = in_arg_offset;
3678 else if (x == virtual_stack_vars_rtx)
3679 new = frame_pointer_rtx, offset = var_offset;
3680 else if (x == virtual_stack_dynamic_rtx)
3681 new = stack_pointer_rtx, offset = dynamic_offset;
3682 else if (x == virtual_outgoing_args_rtx)
3683 new = stack_pointer_rtx, offset = out_arg_offset;
3684 else if (x == virtual_cfa_rtx)
3685 new = arg_pointer_rtx, offset = cfa_offset;
3686 else
3687 return 0;
3689 *poffset = offset;
3690 return new;
3694 /* Called when instantiate_virtual_regs has failed to update the instruction.
3695 Usually this means that non-matching instruction has been emit, however for
3696 asm statements it may be the problem in the constraints. */
3697 static void
3698 instantiate_virtual_regs_lossage (rtx insn)
3700 if (asm_noperands (PATTERN (insn)) >= 0)
3702 error_for_asm (insn, "impossible constraint in `asm'");
3703 delete_insn (insn);
3705 else
3706 abort ();
3708 /* Given a pointer to a piece of rtx and an optional pointer to the
3709 containing object, instantiate any virtual registers present in it.
3711 If EXTRA_INSNS, we always do the replacement and generate
3712 any extra insns before OBJECT. If it zero, we do nothing if replacement
3713 is not valid.
3715 Return 1 if we either had nothing to do or if we were able to do the
3716 needed replacement. Return 0 otherwise; we only return zero if
3717 EXTRA_INSNS is zero.
3719 We first try some simple transformations to avoid the creation of extra
3720 pseudos. */
3722 static int
3723 instantiate_virtual_regs_1 (rtx *loc, rtx object, int extra_insns)
3725 rtx x;
3726 RTX_CODE code;
3727 rtx new = 0;
3728 HOST_WIDE_INT offset = 0;
3729 rtx temp;
3730 rtx seq;
3731 int i, j;
3732 const char *fmt;
3734 /* Re-start here to avoid recursion in common cases. */
3735 restart:
3737 x = *loc;
3738 if (x == 0)
3739 return 1;
3741 /* We may have detected and deleted invalid asm statements. */
3742 if (object && INSN_P (object) && INSN_DELETED_P (object))
3743 return 1;
3745 code = GET_CODE (x);
3747 /* Check for some special cases. */
3748 switch (code)
3750 case CONST_INT:
3751 case CONST_DOUBLE:
3752 case CONST_VECTOR:
3753 case CONST:
3754 case SYMBOL_REF:
3755 case CODE_LABEL:
3756 case PC:
3757 case CC0:
3758 case ASM_INPUT:
3759 case ADDR_VEC:
3760 case ADDR_DIFF_VEC:
3761 case RETURN:
3762 return 1;
3764 case SET:
3765 /* We are allowed to set the virtual registers. This means that
3766 the actual register should receive the source minus the
3767 appropriate offset. This is used, for example, in the handling
3768 of non-local gotos. */
3769 if ((new = instantiate_new_reg (SET_DEST (x), &offset)) != 0)
3771 rtx src = SET_SRC (x);
3773 /* We are setting the register, not using it, so the relevant
3774 offset is the negative of the offset to use were we using
3775 the register. */
3776 offset = - offset;
3777 instantiate_virtual_regs_1 (&src, NULL_RTX, 0);
3779 /* The only valid sources here are PLUS or REG. Just do
3780 the simplest possible thing to handle them. */
3781 if (GET_CODE (src) != REG && GET_CODE (src) != PLUS)
3783 instantiate_virtual_regs_lossage (object);
3784 return 1;
3787 start_sequence ();
3788 if (GET_CODE (src) != REG)
3789 temp = force_operand (src, NULL_RTX);
3790 else
3791 temp = src;
3792 temp = force_operand (plus_constant (temp, offset), NULL_RTX);
3793 seq = get_insns ();
3794 end_sequence ();
3796 emit_insn_before (seq, object);
3797 SET_DEST (x) = new;
3799 if (! validate_change (object, &SET_SRC (x), temp, 0)
3800 || ! extra_insns)
3801 instantiate_virtual_regs_lossage (object);
3803 return 1;
3806 instantiate_virtual_regs_1 (&SET_DEST (x), object, extra_insns);
3807 loc = &SET_SRC (x);
3808 goto restart;
3810 case PLUS:
3811 /* Handle special case of virtual register plus constant. */
3812 if (CONSTANT_P (XEXP (x, 1)))
3814 rtx old, new_offset;
3816 /* Check for (plus (plus VIRT foo) (const_int)) first. */
3817 if (GET_CODE (XEXP (x, 0)) == PLUS)
3819 if ((new = instantiate_new_reg (XEXP (XEXP (x, 0), 0), &offset)))
3821 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 1), object,
3822 extra_insns);
3823 new = gen_rtx_PLUS (Pmode, new, XEXP (XEXP (x, 0), 1));
3825 else
3827 loc = &XEXP (x, 0);
3828 goto restart;
3832 #ifdef POINTERS_EXTEND_UNSIGNED
3833 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
3834 we can commute the PLUS and SUBREG because pointers into the
3835 frame are well-behaved. */
3836 else if (GET_CODE (XEXP (x, 0)) == SUBREG && GET_MODE (x) == ptr_mode
3837 && GET_CODE (XEXP (x, 1)) == CONST_INT
3838 && 0 != (new
3839 = instantiate_new_reg (SUBREG_REG (XEXP (x, 0)),
3840 &offset))
3841 && validate_change (object, loc,
3842 plus_constant (gen_lowpart (ptr_mode,
3843 new),
3844 offset
3845 + INTVAL (XEXP (x, 1))),
3847 return 1;
3848 #endif
3849 else if ((new = instantiate_new_reg (XEXP (x, 0), &offset)) == 0)
3851 /* We know the second operand is a constant. Unless the
3852 first operand is a REG (which has been already checked),
3853 it needs to be checked. */
3854 if (GET_CODE (XEXP (x, 0)) != REG)
3856 loc = &XEXP (x, 0);
3857 goto restart;
3859 return 1;
3862 new_offset = plus_constant (XEXP (x, 1), offset);
3864 /* If the new constant is zero, try to replace the sum with just
3865 the register. */
3866 if (new_offset == const0_rtx
3867 && validate_change (object, loc, new, 0))
3868 return 1;
3870 /* Next try to replace the register and new offset.
3871 There are two changes to validate here and we can't assume that
3872 in the case of old offset equals new just changing the register
3873 will yield a valid insn. In the interests of a little efficiency,
3874 however, we only call validate change once (we don't queue up the
3875 changes and then call apply_change_group). */
3877 old = XEXP (x, 0);
3878 if (offset == 0
3879 ? ! validate_change (object, &XEXP (x, 0), new, 0)
3880 : (XEXP (x, 0) = new,
3881 ! validate_change (object, &XEXP (x, 1), new_offset, 0)))
3883 if (! extra_insns)
3885 XEXP (x, 0) = old;
3886 return 0;
3889 /* Otherwise copy the new constant into a register and replace
3890 constant with that register. */
3891 temp = gen_reg_rtx (Pmode);
3892 XEXP (x, 0) = new;
3893 if (validate_change (object, &XEXP (x, 1), temp, 0))
3894 emit_insn_before (gen_move_insn (temp, new_offset), object);
3895 else
3897 /* If that didn't work, replace this expression with a
3898 register containing the sum. */
3900 XEXP (x, 0) = old;
3901 new = gen_rtx_PLUS (Pmode, new, new_offset);
3903 start_sequence ();
3904 temp = force_operand (new, NULL_RTX);
3905 seq = get_insns ();
3906 end_sequence ();
3908 emit_insn_before (seq, object);
3909 if (! validate_change (object, loc, temp, 0)
3910 && ! validate_replace_rtx (x, temp, object))
3912 instantiate_virtual_regs_lossage (object);
3913 return 1;
3918 return 1;
3921 /* Fall through to generic two-operand expression case. */
3922 case EXPR_LIST:
3923 case CALL:
3924 case COMPARE:
3925 case MINUS:
3926 case MULT:
3927 case DIV: case UDIV:
3928 case MOD: case UMOD:
3929 case AND: case IOR: case XOR:
3930 case ROTATERT: case ROTATE:
3931 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
3932 case NE: case EQ:
3933 case GE: case GT: case GEU: case GTU:
3934 case LE: case LT: case LEU: case LTU:
3935 if (XEXP (x, 1) && ! CONSTANT_P (XEXP (x, 1)))
3936 instantiate_virtual_regs_1 (&XEXP (x, 1), object, extra_insns);
3937 loc = &XEXP (x, 0);
3938 goto restart;
3940 case MEM:
3941 /* Most cases of MEM that convert to valid addresses have already been
3942 handled by our scan of decls. The only special handling we
3943 need here is to make a copy of the rtx to ensure it isn't being
3944 shared if we have to change it to a pseudo.
3946 If the rtx is a simple reference to an address via a virtual register,
3947 it can potentially be shared. In such cases, first try to make it
3948 a valid address, which can also be shared. Otherwise, copy it and
3949 proceed normally.
3951 First check for common cases that need no processing. These are
3952 usually due to instantiation already being done on a previous instance
3953 of a shared rtx. */
3955 temp = XEXP (x, 0);
3956 if (CONSTANT_ADDRESS_P (temp)
3957 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3958 || temp == arg_pointer_rtx
3959 #endif
3960 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3961 || temp == hard_frame_pointer_rtx
3962 #endif
3963 || temp == frame_pointer_rtx)
3964 return 1;
3966 if (GET_CODE (temp) == PLUS
3967 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3968 && (XEXP (temp, 0) == frame_pointer_rtx
3969 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3970 || XEXP (temp, 0) == hard_frame_pointer_rtx
3971 #endif
3972 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3973 || XEXP (temp, 0) == arg_pointer_rtx
3974 #endif
3976 return 1;
3978 if (temp == virtual_stack_vars_rtx
3979 || temp == virtual_incoming_args_rtx
3980 || (GET_CODE (temp) == PLUS
3981 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3982 && (XEXP (temp, 0) == virtual_stack_vars_rtx
3983 || XEXP (temp, 0) == virtual_incoming_args_rtx)))
3985 /* This MEM may be shared. If the substitution can be done without
3986 the need to generate new pseudos, we want to do it in place
3987 so all copies of the shared rtx benefit. The call below will
3988 only make substitutions if the resulting address is still
3989 valid.
3991 Note that we cannot pass X as the object in the recursive call
3992 since the insn being processed may not allow all valid
3993 addresses. However, if we were not passed on object, we can
3994 only modify X without copying it if X will have a valid
3995 address.
3997 ??? Also note that this can still lose if OBJECT is an insn that
3998 has less restrictions on an address that some other insn.
3999 In that case, we will modify the shared address. This case
4000 doesn't seem very likely, though. One case where this could
4001 happen is in the case of a USE or CLOBBER reference, but we
4002 take care of that below. */
4004 if (instantiate_virtual_regs_1 (&XEXP (x, 0),
4005 object ? object : x, 0))
4006 return 1;
4008 /* Otherwise make a copy and process that copy. We copy the entire
4009 RTL expression since it might be a PLUS which could also be
4010 shared. */
4011 *loc = x = copy_rtx (x);
4014 /* Fall through to generic unary operation case. */
4015 case PREFETCH:
4016 case SUBREG:
4017 case STRICT_LOW_PART:
4018 case NEG: case NOT:
4019 case PRE_DEC: case PRE_INC: case POST_DEC: case POST_INC:
4020 case SIGN_EXTEND: case ZERO_EXTEND:
4021 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
4022 case FLOAT: case FIX:
4023 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
4024 case ABS:
4025 case SQRT:
4026 case FFS:
4027 case CLZ: case CTZ:
4028 case POPCOUNT: case PARITY:
4029 /* These case either have just one operand or we know that we need not
4030 check the rest of the operands. */
4031 loc = &XEXP (x, 0);
4032 goto restart;
4034 case USE:
4035 case CLOBBER:
4036 /* If the operand is a MEM, see if the change is a valid MEM. If not,
4037 go ahead and make the invalid one, but do it to a copy. For a REG,
4038 just make the recursive call, since there's no chance of a problem. */
4040 if ((GET_CODE (XEXP (x, 0)) == MEM
4041 && instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), XEXP (x, 0),
4043 || (GET_CODE (XEXP (x, 0)) == REG
4044 && instantiate_virtual_regs_1 (&XEXP (x, 0), object, 0)))
4045 return 1;
4047 XEXP (x, 0) = copy_rtx (XEXP (x, 0));
4048 loc = &XEXP (x, 0);
4049 goto restart;
4051 case REG:
4052 /* Try to replace with a PLUS. If that doesn't work, compute the sum
4053 in front of this insn and substitute the temporary. */
4054 if ((new = instantiate_new_reg (x, &offset)) != 0)
4056 temp = plus_constant (new, offset);
4057 if (!validate_change (object, loc, temp, 0))
4059 if (! extra_insns)
4060 return 0;
4062 start_sequence ();
4063 temp = force_operand (temp, NULL_RTX);
4064 seq = get_insns ();
4065 end_sequence ();
4067 emit_insn_before (seq, object);
4068 if (! validate_change (object, loc, temp, 0)
4069 && ! validate_replace_rtx (x, temp, object))
4070 instantiate_virtual_regs_lossage (object);
4074 return 1;
4076 case ADDRESSOF:
4077 if (GET_CODE (XEXP (x, 0)) == REG)
4078 return 1;
4080 else if (GET_CODE (XEXP (x, 0)) == MEM)
4082 /* If we have a (addressof (mem ..)), do any instantiation inside
4083 since we know we'll be making the inside valid when we finally
4084 remove the ADDRESSOF. */
4085 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), NULL_RTX, 0);
4086 return 1;
4088 break;
4090 default:
4091 break;
4094 /* Scan all subexpressions. */
4095 fmt = GET_RTX_FORMAT (code);
4096 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
4097 if (*fmt == 'e')
4099 if (!instantiate_virtual_regs_1 (&XEXP (x, i), object, extra_insns))
4100 return 0;
4102 else if (*fmt == 'E')
4103 for (j = 0; j < XVECLEN (x, i); j++)
4104 if (! instantiate_virtual_regs_1 (&XVECEXP (x, i, j), object,
4105 extra_insns))
4106 return 0;
4108 return 1;
4111 /* Optimization: assuming this function does not receive nonlocal gotos,
4112 delete the handlers for such, as well as the insns to establish
4113 and disestablish them. */
4115 static void
4116 delete_handlers (void)
4118 rtx insn;
4119 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4121 /* Delete the handler by turning off the flag that would
4122 prevent jump_optimize from deleting it.
4123 Also permit deletion of the nonlocal labels themselves
4124 if nothing local refers to them. */
4125 if (GET_CODE (insn) == CODE_LABEL)
4127 tree t, last_t;
4129 LABEL_PRESERVE_P (insn) = 0;
4131 /* Remove it from the nonlocal_label list, to avoid confusing
4132 flow. */
4133 for (t = nonlocal_labels, last_t = 0; t;
4134 last_t = t, t = TREE_CHAIN (t))
4135 if (DECL_RTL (TREE_VALUE (t)) == insn)
4136 break;
4137 if (t)
4139 if (! last_t)
4140 nonlocal_labels = TREE_CHAIN (nonlocal_labels);
4141 else
4142 TREE_CHAIN (last_t) = TREE_CHAIN (t);
4145 if (GET_CODE (insn) == INSN)
4147 int can_delete = 0;
4148 rtx t;
4149 for (t = nonlocal_goto_handler_slots; t != 0; t = XEXP (t, 1))
4150 if (reg_mentioned_p (t, PATTERN (insn)))
4152 can_delete = 1;
4153 break;
4155 if (can_delete
4156 || (nonlocal_goto_stack_level != 0
4157 && reg_mentioned_p (nonlocal_goto_stack_level,
4158 PATTERN (insn))))
4159 delete_related_insns (insn);
4164 /* Return the first insn following those generated by `assign_parms'. */
4167 get_first_nonparm_insn (void)
4169 if (last_parm_insn)
4170 return NEXT_INSN (last_parm_insn);
4171 return get_insns ();
4174 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
4175 This means a type for which function calls must pass an address to the
4176 function or get an address back from the function.
4177 EXP may be a type node or an expression (whose type is tested). */
4180 aggregate_value_p (tree exp)
4182 int i, regno, nregs;
4183 rtx reg;
4185 tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
4187 if (TREE_CODE (type) == VOID_TYPE)
4188 return 0;
4189 if (RETURN_IN_MEMORY (type))
4190 return 1;
4191 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
4192 and thus can't be returned in registers. */
4193 if (TREE_ADDRESSABLE (type))
4194 return 1;
4195 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
4196 return 1;
4197 /* Make sure we have suitable call-clobbered regs to return
4198 the value in; if not, we must return it in memory. */
4199 reg = hard_function_value (type, 0, 0);
4201 /* If we have something other than a REG (e.g. a PARALLEL), then assume
4202 it is OK. */
4203 if (GET_CODE (reg) != REG)
4204 return 0;
4206 regno = REGNO (reg);
4207 nregs = HARD_REGNO_NREGS (regno, TYPE_MODE (type));
4208 for (i = 0; i < nregs; i++)
4209 if (! call_used_regs[regno + i])
4210 return 1;
4211 return 0;
4214 /* Assign RTL expressions to the function's parameters.
4215 This may involve copying them into registers and using
4216 those registers as the RTL for them. */
4218 void
4219 assign_parms (tree fndecl)
4221 tree parm;
4222 CUMULATIVE_ARGS args_so_far;
4223 /* Total space needed so far for args on the stack,
4224 given as a constant and a tree-expression. */
4225 struct args_size stack_args_size;
4226 tree fntype = TREE_TYPE (fndecl);
4227 tree fnargs = DECL_ARGUMENTS (fndecl), orig_fnargs;
4228 /* This is used for the arg pointer when referring to stack args. */
4229 rtx internal_arg_pointer;
4230 /* This is a dummy PARM_DECL that we used for the function result if
4231 the function returns a structure. */
4232 tree function_result_decl = 0;
4233 #ifdef SETUP_INCOMING_VARARGS
4234 int varargs_setup = 0;
4235 #endif
4236 int reg_parm_stack_space = 0;
4237 rtx conversion_insns = 0;
4239 /* Nonzero if function takes extra anonymous args.
4240 This means the last named arg must be on the stack
4241 right before the anonymous ones. */
4242 int stdarg
4243 = (TYPE_ARG_TYPES (fntype) != 0
4244 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4245 != void_type_node));
4247 current_function_stdarg = stdarg;
4249 /* If the reg that the virtual arg pointer will be translated into is
4250 not a fixed reg or is the stack pointer, make a copy of the virtual
4251 arg pointer, and address parms via the copy. The frame pointer is
4252 considered fixed even though it is not marked as such.
4254 The second time through, simply use ap to avoid generating rtx. */
4256 if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
4257 || ! (fixed_regs[ARG_POINTER_REGNUM]
4258 || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM)))
4259 internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
4260 else
4261 internal_arg_pointer = virtual_incoming_args_rtx;
4262 current_function_internal_arg_pointer = internal_arg_pointer;
4264 stack_args_size.constant = 0;
4265 stack_args_size.var = 0;
4267 /* If struct value address is treated as the first argument, make it so. */
4268 if (aggregate_value_p (DECL_RESULT (fndecl))
4269 && ! current_function_returns_pcc_struct
4270 && struct_value_incoming_rtx == 0)
4272 tree type = build_pointer_type (TREE_TYPE (fntype));
4274 function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
4276 DECL_ARG_TYPE (function_result_decl) = type;
4277 TREE_CHAIN (function_result_decl) = fnargs;
4278 fnargs = function_result_decl;
4281 orig_fnargs = fnargs;
4283 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
4284 parm_reg_stack_loc = ggc_alloc_cleared (max_parm_reg * sizeof (rtx));
4286 if (SPLIT_COMPLEX_ARGS)
4287 fnargs = split_complex_args (fnargs);
4289 #ifdef REG_PARM_STACK_SPACE
4290 #ifdef MAYBE_REG_PARM_STACK_SPACE
4291 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
4292 #else
4293 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
4294 #endif
4295 #endif
4297 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
4298 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far, fntype, NULL_RTX);
4299 #else
4300 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, fndecl);
4301 #endif
4303 /* We haven't yet found an argument that we must push and pretend the
4304 caller did. */
4305 current_function_pretend_args_size = 0;
4307 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
4309 rtx entry_parm;
4310 rtx stack_parm;
4311 enum machine_mode promoted_mode, passed_mode;
4312 enum machine_mode nominal_mode, promoted_nominal_mode;
4313 int unsignedp;
4314 struct locate_and_pad_arg_data locate;
4315 int passed_pointer = 0;
4316 int did_conversion = 0;
4317 tree passed_type = DECL_ARG_TYPE (parm);
4318 tree nominal_type = TREE_TYPE (parm);
4319 int last_named = 0, named_arg;
4320 int in_regs;
4321 int partial = 0;
4323 /* Set LAST_NAMED if this is last named arg before last
4324 anonymous args. */
4325 if (stdarg)
4327 tree tem;
4329 for (tem = TREE_CHAIN (parm); tem; tem = TREE_CHAIN (tem))
4330 if (DECL_NAME (tem))
4331 break;
4333 if (tem == 0)
4334 last_named = 1;
4336 /* Set NAMED_ARG if this arg should be treated as a named arg. For
4337 most machines, if this is a varargs/stdarg function, then we treat
4338 the last named arg as if it were anonymous too. */
4339 named_arg = STRICT_ARGUMENT_NAMING ? 1 : ! last_named;
4341 if (TREE_TYPE (parm) == error_mark_node
4342 /* This can happen after weird syntax errors
4343 or if an enum type is defined among the parms. */
4344 || TREE_CODE (parm) != PARM_DECL
4345 || passed_type == NULL)
4347 SET_DECL_RTL (parm, gen_rtx_MEM (BLKmode, const0_rtx));
4348 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4349 TREE_USED (parm) = 1;
4350 continue;
4353 /* Find mode of arg as it is passed, and mode of arg
4354 as it should be during execution of this function. */
4355 passed_mode = TYPE_MODE (passed_type);
4356 nominal_mode = TYPE_MODE (nominal_type);
4358 /* If the parm's mode is VOID, its value doesn't matter,
4359 and avoid the usual things like emit_move_insn that could crash. */
4360 if (nominal_mode == VOIDmode)
4362 SET_DECL_RTL (parm, const0_rtx);
4363 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4364 continue;
4367 /* If the parm is to be passed as a transparent union, use the
4368 type of the first field for the tests below. We have already
4369 verified that the modes are the same. */
4370 if (DECL_TRANSPARENT_UNION (parm)
4371 || (TREE_CODE (passed_type) == UNION_TYPE
4372 && TYPE_TRANSPARENT_UNION (passed_type)))
4373 passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
4375 /* See if this arg was passed by invisible reference. It is if
4376 it is an object whose size depends on the contents of the
4377 object itself or if the machine requires these objects be passed
4378 that way. */
4380 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (passed_type))
4381 || TREE_ADDRESSABLE (passed_type)
4382 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
4383 || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, passed_mode,
4384 passed_type, named_arg)
4385 #endif
4388 passed_type = nominal_type = build_pointer_type (passed_type);
4389 passed_pointer = 1;
4390 passed_mode = nominal_mode = Pmode;
4392 /* See if the frontend wants to pass this by invisible reference. */
4393 else if (passed_type != nominal_type
4394 && POINTER_TYPE_P (passed_type)
4395 && TREE_TYPE (passed_type) == nominal_type)
4397 nominal_type = passed_type;
4398 passed_pointer = 1;
4399 passed_mode = nominal_mode = Pmode;
4402 promoted_mode = passed_mode;
4404 #ifdef PROMOTE_FUNCTION_ARGS
4405 /* Compute the mode in which the arg is actually extended to. */
4406 unsignedp = TREE_UNSIGNED (passed_type);
4407 promoted_mode = promote_mode (passed_type, promoted_mode, &unsignedp, 1);
4408 #endif
4410 /* Let machine desc say which reg (if any) the parm arrives in.
4411 0 means it arrives on the stack. */
4412 #ifdef FUNCTION_INCOMING_ARG
4413 entry_parm = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4414 passed_type, named_arg);
4415 #else
4416 entry_parm = FUNCTION_ARG (args_so_far, promoted_mode,
4417 passed_type, named_arg);
4418 #endif
4420 if (entry_parm == 0)
4421 promoted_mode = passed_mode;
4423 #ifdef SETUP_INCOMING_VARARGS
4424 /* If this is the last named parameter, do any required setup for
4425 varargs or stdargs. We need to know about the case of this being an
4426 addressable type, in which case we skip the registers it
4427 would have arrived in.
4429 For stdargs, LAST_NAMED will be set for two parameters, the one that
4430 is actually the last named, and the dummy parameter. We only
4431 want to do this action once.
4433 Also, indicate when RTL generation is to be suppressed. */
4434 if (last_named && !varargs_setup)
4436 SETUP_INCOMING_VARARGS (args_so_far, promoted_mode, passed_type,
4437 current_function_pretend_args_size, 0);
4438 varargs_setup = 1;
4440 #endif
4442 /* Determine parm's home in the stack,
4443 in case it arrives in the stack or we should pretend it did.
4445 Compute the stack position and rtx where the argument arrives
4446 and its size.
4448 There is one complexity here: If this was a parameter that would
4449 have been passed in registers, but wasn't only because it is
4450 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
4451 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
4452 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of
4453 0 as it was the previous time. */
4454 in_regs = entry_parm != 0;
4455 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4456 in_regs = 1;
4457 #endif
4458 if (!in_regs && !named_arg)
4460 int pretend_named = PRETEND_OUTGOING_VARARGS_NAMED;
4461 if (pretend_named)
4463 #ifdef FUNCTION_INCOMING_ARG
4464 in_regs = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4465 passed_type,
4466 pretend_named) != 0;
4467 #else
4468 in_regs = FUNCTION_ARG (args_so_far, promoted_mode,
4469 passed_type,
4470 pretend_named) != 0;
4471 #endif
4475 /* If this parameter was passed both in registers and in the stack,
4476 use the copy on the stack. */
4477 if (MUST_PASS_IN_STACK (promoted_mode, passed_type))
4478 entry_parm = 0;
4480 #ifdef FUNCTION_ARG_PARTIAL_NREGS
4481 if (entry_parm)
4482 partial = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, promoted_mode,
4483 passed_type, named_arg);
4484 #endif
4486 memset (&locate, 0, sizeof (locate));
4487 locate_and_pad_parm (promoted_mode, passed_type, in_regs,
4488 entry_parm ? partial : 0, fndecl,
4489 &stack_args_size, &locate);
4492 rtx offset_rtx;
4494 /* If we're passing this arg using a reg, make its stack home
4495 the aligned stack slot. */
4496 if (entry_parm)
4497 offset_rtx = ARGS_SIZE_RTX (locate.slot_offset);
4498 else
4499 offset_rtx = ARGS_SIZE_RTX (locate.offset);
4501 if (offset_rtx == const0_rtx)
4502 stack_parm = gen_rtx_MEM (promoted_mode, internal_arg_pointer);
4503 else
4504 stack_parm = gen_rtx_MEM (promoted_mode,
4505 gen_rtx_PLUS (Pmode,
4506 internal_arg_pointer,
4507 offset_rtx));
4509 set_mem_attributes (stack_parm, parm, 1);
4510 if (entry_parm && MEM_ATTRS (stack_parm)->align < PARM_BOUNDARY)
4511 set_mem_align (stack_parm, PARM_BOUNDARY);
4513 /* Set also REG_ATTRS if parameter was passed in a register. */
4514 if (entry_parm)
4515 set_reg_attrs_for_parm (entry_parm, stack_parm);
4518 /* If this parm was passed part in regs and part in memory,
4519 pretend it arrived entirely in memory
4520 by pushing the register-part onto the stack.
4522 In the special case of a DImode or DFmode that is split,
4523 we could put it together in a pseudoreg directly,
4524 but for now that's not worth bothering with. */
4526 if (partial)
4528 #ifndef MAYBE_REG_PARM_STACK_SPACE
4529 /* When REG_PARM_STACK_SPACE is nonzero, stack space for
4530 split parameters was allocated by our caller, so we
4531 won't be pushing it in the prolog. */
4532 if (reg_parm_stack_space == 0)
4533 #endif
4534 current_function_pretend_args_size
4535 = (((partial * UNITS_PER_WORD) + (PARM_BOUNDARY / BITS_PER_UNIT) - 1)
4536 / (PARM_BOUNDARY / BITS_PER_UNIT)
4537 * (PARM_BOUNDARY / BITS_PER_UNIT));
4539 /* Handle calls that pass values in multiple non-contiguous
4540 locations. The Irix 6 ABI has examples of this. */
4541 if (GET_CODE (entry_parm) == PARALLEL)
4542 emit_group_store (validize_mem (stack_parm), entry_parm,
4543 TREE_TYPE (parm),
4544 int_size_in_bytes (TREE_TYPE (parm)));
4546 else
4547 move_block_from_reg (REGNO (entry_parm), validize_mem (stack_parm),
4548 partial);
4550 entry_parm = stack_parm;
4553 /* If we didn't decide this parm came in a register,
4554 by default it came on the stack. */
4555 if (entry_parm == 0)
4556 entry_parm = stack_parm;
4558 /* Record permanently how this parm was passed. */
4559 DECL_INCOMING_RTL (parm) = entry_parm;
4561 /* If there is actually space on the stack for this parm,
4562 count it in stack_args_size; otherwise set stack_parm to 0
4563 to indicate there is no preallocated stack slot for the parm. */
4565 if (entry_parm == stack_parm
4566 || (GET_CODE (entry_parm) == PARALLEL
4567 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
4568 #if defined (REG_PARM_STACK_SPACE) && ! defined (MAYBE_REG_PARM_STACK_SPACE)
4569 /* On some machines, even if a parm value arrives in a register
4570 there is still an (uninitialized) stack slot allocated for it.
4572 ??? When MAYBE_REG_PARM_STACK_SPACE is defined, we can't tell
4573 whether this parameter already has a stack slot allocated,
4574 because an arg block exists only if current_function_args_size
4575 is larger than some threshold, and we haven't calculated that
4576 yet. So, for now, we just assume that stack slots never exist
4577 in this case. */
4578 || REG_PARM_STACK_SPACE (fndecl) > 0
4579 #endif
4582 stack_args_size.constant += locate.size.constant;
4583 /* locate.size doesn't include the part in regs. */
4584 if (partial)
4585 stack_args_size.constant += current_function_pretend_args_size;
4586 if (locate.size.var)
4587 ADD_PARM_SIZE (stack_args_size, locate.size.var);
4589 else
4590 /* No stack slot was pushed for this parm. */
4591 stack_parm = 0;
4593 /* Update info on where next arg arrives in registers. */
4595 FUNCTION_ARG_ADVANCE (args_so_far, promoted_mode,
4596 passed_type, named_arg);
4598 /* If we can't trust the parm stack slot to be aligned enough
4599 for its ultimate type, don't use that slot after entry.
4600 We'll make another stack slot, if we need one. */
4602 unsigned int thisparm_boundary
4603 = FUNCTION_ARG_BOUNDARY (promoted_mode, passed_type);
4605 if (GET_MODE_ALIGNMENT (nominal_mode) > thisparm_boundary)
4606 stack_parm = 0;
4609 /* If parm was passed in memory, and we need to convert it on entry,
4610 don't store it back in that same slot. */
4611 if (entry_parm == stack_parm
4612 && nominal_mode != BLKmode && nominal_mode != passed_mode)
4613 stack_parm = 0;
4615 /* When an argument is passed in multiple locations, we can't
4616 make use of this information, but we can save some copying if
4617 the whole argument is passed in a single register. */
4618 if (GET_CODE (entry_parm) == PARALLEL
4619 && nominal_mode != BLKmode && passed_mode != BLKmode)
4621 int i, len = XVECLEN (entry_parm, 0);
4623 for (i = 0; i < len; i++)
4624 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
4625 && GET_CODE (XEXP (XVECEXP (entry_parm, 0, i), 0)) == REG
4626 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
4627 == passed_mode)
4628 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
4630 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
4631 DECL_INCOMING_RTL (parm) = entry_parm;
4632 break;
4636 /* ENTRY_PARM is an RTX for the parameter as it arrives,
4637 in the mode in which it arrives.
4638 STACK_PARM is an RTX for a stack slot where the parameter can live
4639 during the function (in case we want to put it there).
4640 STACK_PARM is 0 if no stack slot was pushed for it.
4642 Now output code if necessary to convert ENTRY_PARM to
4643 the type in which this function declares it,
4644 and store that result in an appropriate place,
4645 which may be a pseudo reg, may be STACK_PARM,
4646 or may be a local stack slot if STACK_PARM is 0.
4648 Set DECL_RTL to that place. */
4650 if (nominal_mode == BLKmode
4651 #ifdef BLOCK_REG_PADDING
4652 || (locate.where_pad == (BYTES_BIG_ENDIAN ? upward : downward)
4653 && GET_MODE_SIZE (promoted_mode) < UNITS_PER_WORD)
4654 #endif
4655 || GET_CODE (entry_parm) == PARALLEL)
4657 /* If a BLKmode arrives in registers, copy it to a stack slot.
4658 Handle calls that pass values in multiple non-contiguous
4659 locations. The Irix 6 ABI has examples of this. */
4660 if (GET_CODE (entry_parm) == REG
4661 || GET_CODE (entry_parm) == PARALLEL)
4663 int size = int_size_in_bytes (TREE_TYPE (parm));
4664 int size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
4665 rtx mem;
4667 /* Note that we will be storing an integral number of words.
4668 So we have to be careful to ensure that we allocate an
4669 integral number of words. We do this below in the
4670 assign_stack_local if space was not allocated in the argument
4671 list. If it was, this will not work if PARM_BOUNDARY is not
4672 a multiple of BITS_PER_WORD. It isn't clear how to fix this
4673 if it becomes a problem. */
4675 if (stack_parm == 0)
4677 stack_parm
4678 = assign_stack_local (GET_MODE (entry_parm),
4679 size_stored, 0);
4680 set_mem_attributes (stack_parm, parm, 1);
4683 else if (PARM_BOUNDARY % BITS_PER_WORD != 0)
4684 abort ();
4686 mem = validize_mem (stack_parm);
4688 /* Handle calls that pass values in multiple non-contiguous
4689 locations. The Irix 6 ABI has examples of this. */
4690 if (GET_CODE (entry_parm) == PARALLEL)
4691 emit_group_store (mem, entry_parm, TREE_TYPE (parm), size);
4693 else if (size == 0)
4696 /* If SIZE is that of a mode no bigger than a word, just use
4697 that mode's store operation. */
4698 else if (size <= UNITS_PER_WORD)
4700 enum machine_mode mode
4701 = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
4703 if (mode != BLKmode
4704 #ifdef BLOCK_REG_PADDING
4705 && (size == UNITS_PER_WORD
4706 || (BLOCK_REG_PADDING (mode, TREE_TYPE (parm), 1)
4707 != (BYTES_BIG_ENDIAN ? upward : downward)))
4708 #endif
4711 rtx reg = gen_rtx_REG (mode, REGNO (entry_parm));
4712 emit_move_insn (change_address (mem, mode, 0), reg);
4715 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
4716 machine must be aligned to the left before storing
4717 to memory. Note that the previous test doesn't
4718 handle all cases (e.g. SIZE == 3). */
4719 else if (size != UNITS_PER_WORD
4720 #ifdef BLOCK_REG_PADDING
4721 && (BLOCK_REG_PADDING (mode, TREE_TYPE (parm), 1)
4722 == downward)
4723 #else
4724 && BYTES_BIG_ENDIAN
4725 #endif
4728 rtx tem, x;
4729 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
4730 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
4732 x = expand_binop (word_mode, ashl_optab, reg,
4733 GEN_INT (by), 0, 1, OPTAB_WIDEN);
4734 tem = change_address (mem, word_mode, 0);
4735 emit_move_insn (tem, x);
4737 else
4738 move_block_from_reg (REGNO (entry_parm), mem,
4739 size_stored / UNITS_PER_WORD);
4741 else
4742 move_block_from_reg (REGNO (entry_parm), mem,
4743 size_stored / UNITS_PER_WORD);
4745 SET_DECL_RTL (parm, stack_parm);
4747 else if (! ((! optimize
4748 && ! DECL_REGISTER (parm))
4749 || TREE_SIDE_EFFECTS (parm)
4750 /* If -ffloat-store specified, don't put explicit
4751 float variables into registers. */
4752 || (flag_float_store
4753 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
4754 /* Always assign pseudo to structure return or item passed
4755 by invisible reference. */
4756 || passed_pointer || parm == function_result_decl)
4758 /* Store the parm in a pseudoregister during the function, but we
4759 may need to do it in a wider mode. */
4761 rtx parmreg;
4762 unsigned int regno, regnoi = 0, regnor = 0;
4764 unsignedp = TREE_UNSIGNED (TREE_TYPE (parm));
4766 promoted_nominal_mode
4767 = promote_mode (TREE_TYPE (parm), nominal_mode, &unsignedp, 0);
4769 parmreg = gen_reg_rtx (promoted_nominal_mode);
4770 mark_user_reg (parmreg);
4772 /* If this was an item that we received a pointer to, set DECL_RTL
4773 appropriately. */
4774 if (passed_pointer)
4776 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (passed_type)),
4777 parmreg);
4778 set_mem_attributes (x, parm, 1);
4779 SET_DECL_RTL (parm, x);
4781 else
4783 SET_DECL_RTL (parm, parmreg);
4784 maybe_set_unchanging (DECL_RTL (parm), parm);
4787 /* Copy the value into the register. */
4788 if (nominal_mode != passed_mode
4789 || promoted_nominal_mode != promoted_mode)
4791 int save_tree_used;
4792 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
4793 mode, by the caller. We now have to convert it to
4794 NOMINAL_MODE, if different. However, PARMREG may be in
4795 a different mode than NOMINAL_MODE if it is being stored
4796 promoted.
4798 If ENTRY_PARM is a hard register, it might be in a register
4799 not valid for operating in its mode (e.g., an odd-numbered
4800 register for a DFmode). In that case, moves are the only
4801 thing valid, so we can't do a convert from there. This
4802 occurs when the calling sequence allow such misaligned
4803 usages.
4805 In addition, the conversion may involve a call, which could
4806 clobber parameters which haven't been copied to pseudo
4807 registers yet. Therefore, we must first copy the parm to
4808 a pseudo reg here, and save the conversion until after all
4809 parameters have been moved. */
4811 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4813 emit_move_insn (tempreg, validize_mem (entry_parm));
4815 push_to_sequence (conversion_insns);
4816 tempreg = convert_to_mode (nominal_mode, tempreg, unsignedp);
4818 if (GET_CODE (tempreg) == SUBREG
4819 && GET_MODE (tempreg) == nominal_mode
4820 && GET_CODE (SUBREG_REG (tempreg)) == REG
4821 && nominal_mode == passed_mode
4822 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (entry_parm)
4823 && GET_MODE_SIZE (GET_MODE (tempreg))
4824 < GET_MODE_SIZE (GET_MODE (entry_parm)))
4826 /* The argument is already sign/zero extended, so note it
4827 into the subreg. */
4828 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
4829 SUBREG_PROMOTED_UNSIGNED_SET (tempreg, unsignedp);
4832 /* TREE_USED gets set erroneously during expand_assignment. */
4833 save_tree_used = TREE_USED (parm);
4834 expand_assignment (parm,
4835 make_tree (nominal_type, tempreg), 0);
4836 TREE_USED (parm) = save_tree_used;
4837 conversion_insns = get_insns ();
4838 did_conversion = 1;
4839 end_sequence ();
4841 else
4842 emit_move_insn (parmreg, validize_mem (entry_parm));
4844 /* If we were passed a pointer but the actual value
4845 can safely live in a register, put it in one. */
4846 if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
4847 /* If by-reference argument was promoted, demote it. */
4848 && (TYPE_MODE (TREE_TYPE (parm)) != GET_MODE (DECL_RTL (parm))
4849 || ! ((! optimize
4850 && ! DECL_REGISTER (parm))
4851 || TREE_SIDE_EFFECTS (parm)
4852 /* If -ffloat-store specified, don't put explicit
4853 float variables into registers. */
4854 || (flag_float_store
4855 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))))
4857 /* We can't use nominal_mode, because it will have been set to
4858 Pmode above. We must use the actual mode of the parm. */
4859 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
4860 mark_user_reg (parmreg);
4861 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
4863 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
4864 int unsigned_p = TREE_UNSIGNED (TREE_TYPE (parm));
4865 push_to_sequence (conversion_insns);
4866 emit_move_insn (tempreg, DECL_RTL (parm));
4867 SET_DECL_RTL (parm,
4868 convert_to_mode (GET_MODE (parmreg),
4869 tempreg,
4870 unsigned_p));
4871 emit_move_insn (parmreg, DECL_RTL (parm));
4872 conversion_insns = get_insns();
4873 did_conversion = 1;
4874 end_sequence ();
4876 else
4877 emit_move_insn (parmreg, DECL_RTL (parm));
4878 SET_DECL_RTL (parm, parmreg);
4879 /* STACK_PARM is the pointer, not the parm, and PARMREG is
4880 now the parm. */
4881 stack_parm = 0;
4883 #ifdef FUNCTION_ARG_CALLEE_COPIES
4884 /* If we are passed an arg by reference and it is our responsibility
4885 to make a copy, do it now.
4886 PASSED_TYPE and PASSED mode now refer to the pointer, not the
4887 original argument, so we must recreate them in the call to
4888 FUNCTION_ARG_CALLEE_COPIES. */
4889 /* ??? Later add code to handle the case that if the argument isn't
4890 modified, don't do the copy. */
4892 else if (passed_pointer
4893 && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
4894 TYPE_MODE (DECL_ARG_TYPE (parm)),
4895 DECL_ARG_TYPE (parm),
4896 named_arg)
4897 && ! TREE_ADDRESSABLE (DECL_ARG_TYPE (parm)))
4899 rtx copy;
4900 tree type = DECL_ARG_TYPE (parm);
4902 /* This sequence may involve a library call perhaps clobbering
4903 registers that haven't been copied to pseudos yet. */
4905 push_to_sequence (conversion_insns);
4907 if (!COMPLETE_TYPE_P (type)
4908 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4909 /* This is a variable sized object. */
4910 copy = gen_rtx_MEM (BLKmode,
4911 allocate_dynamic_stack_space
4912 (expr_size (parm), NULL_RTX,
4913 TYPE_ALIGN (type)));
4914 else
4915 copy = assign_stack_temp (TYPE_MODE (type),
4916 int_size_in_bytes (type), 1);
4917 set_mem_attributes (copy, parm, 1);
4919 store_expr (parm, copy, 0);
4920 emit_move_insn (parmreg, XEXP (copy, 0));
4921 conversion_insns = get_insns ();
4922 did_conversion = 1;
4923 end_sequence ();
4925 #endif /* FUNCTION_ARG_CALLEE_COPIES */
4927 /* In any case, record the parm's desired stack location
4928 in case we later discover it must live in the stack.
4930 If it is a COMPLEX value, store the stack location for both
4931 halves. */
4933 if (GET_CODE (parmreg) == CONCAT)
4934 regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
4935 else
4936 regno = REGNO (parmreg);
4938 if (regno >= max_parm_reg)
4940 rtx *new;
4941 int old_max_parm_reg = max_parm_reg;
4943 /* It's slow to expand this one register at a time,
4944 but it's also rare and we need max_parm_reg to be
4945 precisely correct. */
4946 max_parm_reg = regno + 1;
4947 new = ggc_realloc (parm_reg_stack_loc,
4948 max_parm_reg * sizeof (rtx));
4949 memset (new + old_max_parm_reg, 0,
4950 (max_parm_reg - old_max_parm_reg) * sizeof (rtx));
4951 parm_reg_stack_loc = new;
4954 if (GET_CODE (parmreg) == CONCAT)
4956 enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
4958 regnor = REGNO (gen_realpart (submode, parmreg));
4959 regnoi = REGNO (gen_imagpart (submode, parmreg));
4961 if (stack_parm != 0)
4963 parm_reg_stack_loc[regnor]
4964 = gen_realpart (submode, stack_parm);
4965 parm_reg_stack_loc[regnoi]
4966 = gen_imagpart (submode, stack_parm);
4968 else
4970 parm_reg_stack_loc[regnor] = 0;
4971 parm_reg_stack_loc[regnoi] = 0;
4974 else
4975 parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
4977 /* Mark the register as eliminable if we did no conversion
4978 and it was copied from memory at a fixed offset,
4979 and the arg pointer was not copied to a pseudo-reg.
4980 If the arg pointer is a pseudo reg or the offset formed
4981 an invalid address, such memory-equivalences
4982 as we make here would screw up life analysis for it. */
4983 if (nominal_mode == passed_mode
4984 && ! did_conversion
4985 && stack_parm != 0
4986 && GET_CODE (stack_parm) == MEM
4987 && locate.offset.var == 0
4988 && reg_mentioned_p (virtual_incoming_args_rtx,
4989 XEXP (stack_parm, 0)))
4991 rtx linsn = get_last_insn ();
4992 rtx sinsn, set;
4994 /* Mark complex types separately. */
4995 if (GET_CODE (parmreg) == CONCAT)
4996 /* Scan backwards for the set of the real and
4997 imaginary parts. */
4998 for (sinsn = linsn; sinsn != 0;
4999 sinsn = prev_nonnote_insn (sinsn))
5001 set = single_set (sinsn);
5002 if (set != 0
5003 && SET_DEST (set) == regno_reg_rtx [regnoi])
5004 REG_NOTES (sinsn)
5005 = gen_rtx_EXPR_LIST (REG_EQUIV,
5006 parm_reg_stack_loc[regnoi],
5007 REG_NOTES (sinsn));
5008 else if (set != 0
5009 && SET_DEST (set) == regno_reg_rtx [regnor])
5010 REG_NOTES (sinsn)
5011 = gen_rtx_EXPR_LIST (REG_EQUIV,
5012 parm_reg_stack_loc[regnor],
5013 REG_NOTES (sinsn));
5015 else if ((set = single_set (linsn)) != 0
5016 && SET_DEST (set) == parmreg)
5017 REG_NOTES (linsn)
5018 = gen_rtx_EXPR_LIST (REG_EQUIV,
5019 stack_parm, REG_NOTES (linsn));
5022 /* For pointer data type, suggest pointer register. */
5023 if (POINTER_TYPE_P (TREE_TYPE (parm)))
5024 mark_reg_pointer (parmreg,
5025 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
5027 /* If something wants our address, try to use ADDRESSOF. */
5028 if (TREE_ADDRESSABLE (parm))
5030 /* If we end up putting something into the stack,
5031 fixup_var_refs_insns will need to make a pass over
5032 all the instructions. It looks through the pending
5033 sequences -- but it can't see the ones in the
5034 CONVERSION_INSNS, if they're not on the sequence
5035 stack. So, we go back to that sequence, just so that
5036 the fixups will happen. */
5037 push_to_sequence (conversion_insns);
5038 put_var_into_stack (parm, /*rescan=*/true);
5039 conversion_insns = get_insns ();
5040 end_sequence ();
5043 else
5045 /* Value must be stored in the stack slot STACK_PARM
5046 during function execution. */
5048 if (promoted_mode != nominal_mode)
5050 /* Conversion is required. */
5051 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
5053 emit_move_insn (tempreg, validize_mem (entry_parm));
5055 push_to_sequence (conversion_insns);
5056 entry_parm = convert_to_mode (nominal_mode, tempreg,
5057 TREE_UNSIGNED (TREE_TYPE (parm)));
5058 if (stack_parm)
5059 /* ??? This may need a big-endian conversion on sparc64. */
5060 stack_parm = adjust_address (stack_parm, nominal_mode, 0);
5062 conversion_insns = get_insns ();
5063 did_conversion = 1;
5064 end_sequence ();
5067 if (entry_parm != stack_parm)
5069 if (stack_parm == 0)
5071 stack_parm
5072 = assign_stack_local (GET_MODE (entry_parm),
5073 GET_MODE_SIZE (GET_MODE (entry_parm)),
5075 set_mem_attributes (stack_parm, parm, 1);
5078 if (promoted_mode != nominal_mode)
5080 push_to_sequence (conversion_insns);
5081 emit_move_insn (validize_mem (stack_parm),
5082 validize_mem (entry_parm));
5083 conversion_insns = get_insns ();
5084 end_sequence ();
5086 else
5087 emit_move_insn (validize_mem (stack_parm),
5088 validize_mem (entry_parm));
5091 SET_DECL_RTL (parm, stack_parm);
5095 if (SPLIT_COMPLEX_ARGS && fnargs != orig_fnargs)
5097 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm))
5099 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE)
5101 SET_DECL_RTL (parm,
5102 gen_rtx_CONCAT (DECL_MODE (parm),
5103 DECL_RTL (fnargs),
5104 DECL_RTL (TREE_CHAIN (fnargs))));
5105 DECL_INCOMING_RTL (parm)
5106 = gen_rtx_CONCAT (DECL_MODE (parm),
5107 DECL_INCOMING_RTL (fnargs),
5108 DECL_INCOMING_RTL (TREE_CHAIN (fnargs)));
5109 fnargs = TREE_CHAIN (fnargs);
5111 else
5113 SET_DECL_RTL (parm, DECL_RTL (fnargs));
5114 DECL_INCOMING_RTL (parm) = DECL_INCOMING_RTL (fnargs);
5116 fnargs = TREE_CHAIN (fnargs);
5120 /* Output all parameter conversion instructions (possibly including calls)
5121 now that all parameters have been copied out of hard registers. */
5122 emit_insn (conversion_insns);
5124 /* If we are receiving a struct value address as the first argument, set up
5125 the RTL for the function result. As this might require code to convert
5126 the transmitted address to Pmode, we do this here to ensure that possible
5127 preliminary conversions of the address have been emitted already. */
5128 if (function_result_decl)
5130 tree result = DECL_RESULT (fndecl);
5131 rtx addr = DECL_RTL (function_result_decl);
5132 rtx x;
5134 #ifdef POINTERS_EXTEND_UNSIGNED
5135 if (GET_MODE (addr) != Pmode)
5136 addr = convert_memory_address (Pmode, addr);
5137 #endif
5139 x = gen_rtx_MEM (DECL_MODE (result), addr);
5140 set_mem_attributes (x, result, 1);
5141 SET_DECL_RTL (result, x);
5144 last_parm_insn = get_last_insn ();
5146 current_function_args_size = stack_args_size.constant;
5148 /* Adjust function incoming argument size for alignment and
5149 minimum length. */
5151 #ifdef REG_PARM_STACK_SPACE
5152 #ifndef MAYBE_REG_PARM_STACK_SPACE
5153 current_function_args_size = MAX (current_function_args_size,
5154 REG_PARM_STACK_SPACE (fndecl));
5155 #endif
5156 #endif
5158 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
5160 current_function_args_size
5161 = ((current_function_args_size + STACK_BYTES - 1)
5162 / STACK_BYTES) * STACK_BYTES;
5164 #ifdef ARGS_GROW_DOWNWARD
5165 current_function_arg_offset_rtx
5166 = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
5167 : expand_expr (size_diffop (stack_args_size.var,
5168 size_int (-stack_args_size.constant)),
5169 NULL_RTX, VOIDmode, 0));
5170 #else
5171 current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
5172 #endif
5174 /* See how many bytes, if any, of its args a function should try to pop
5175 on return. */
5177 current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
5178 current_function_args_size);
5180 /* For stdarg.h function, save info about
5181 regs and stack space used by the named args. */
5183 current_function_args_info = args_so_far;
5185 /* Set the rtx used for the function return value. Put this in its
5186 own variable so any optimizers that need this information don't have
5187 to include tree.h. Do this here so it gets done when an inlined
5188 function gets output. */
5190 current_function_return_rtx
5191 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
5192 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
5194 /* If scalar return value was computed in a pseudo-reg, or was a named
5195 return value that got dumped to the stack, copy that to the hard
5196 return register. */
5197 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
5199 tree decl_result = DECL_RESULT (fndecl);
5200 rtx decl_rtl = DECL_RTL (decl_result);
5202 if (REG_P (decl_rtl)
5203 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5204 : DECL_REGISTER (decl_result))
5206 rtx real_decl_rtl;
5208 #ifdef FUNCTION_OUTGOING_VALUE
5209 real_decl_rtl = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl_result),
5210 fndecl);
5211 #else
5212 real_decl_rtl = FUNCTION_VALUE (TREE_TYPE (decl_result),
5213 fndecl);
5214 #endif
5215 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
5216 /* The delay slot scheduler assumes that current_function_return_rtx
5217 holds the hard register containing the return value, not a
5218 temporary pseudo. */
5219 current_function_return_rtx = real_decl_rtl;
5224 /* If ARGS contains entries with complex types, split the entry into two
5225 entries of the component type. Return a new list of substitutions are
5226 needed, else the old list. */
5228 static tree
5229 split_complex_args (tree args)
5231 tree p;
5233 /* Before allocating memory, check for the common case of no complex. */
5234 for (p = args; p; p = TREE_CHAIN (p))
5235 if (TREE_CODE (TREE_TYPE (p)) == COMPLEX_TYPE)
5236 goto found;
5237 return args;
5239 found:
5240 args = copy_list (args);
5242 for (p = args; p; p = TREE_CHAIN (p))
5244 tree type = TREE_TYPE (p);
5245 if (TREE_CODE (type) == COMPLEX_TYPE)
5247 tree decl;
5248 tree subtype = TREE_TYPE (type);
5250 /* Rewrite the PARM_DECL's type with its component. */
5251 TREE_TYPE (p) = subtype;
5252 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
5253 DECL_MODE (p) = VOIDmode;
5254 DECL_SIZE (p) = NULL;
5255 DECL_SIZE_UNIT (p) = NULL;
5256 layout_decl (p, 0);
5258 /* Build a second synthetic decl. */
5259 decl = build_decl (PARM_DECL, NULL_TREE, subtype);
5260 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
5261 layout_decl (decl, 0);
5263 /* Splice it in; skip the new decl. */
5264 TREE_CHAIN (decl) = TREE_CHAIN (p);
5265 TREE_CHAIN (p) = decl;
5266 p = decl;
5270 return args;
5273 /* Indicate whether REGNO is an incoming argument to the current function
5274 that was promoted to a wider mode. If so, return the RTX for the
5275 register (to get its mode). PMODE and PUNSIGNEDP are set to the mode
5276 that REGNO is promoted from and whether the promotion was signed or
5277 unsigned. */
5279 #ifdef PROMOTE_FUNCTION_ARGS
5282 promoted_input_arg (unsigned int regno, enum machine_mode *pmode, int *punsignedp)
5284 tree arg;
5286 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
5287 arg = TREE_CHAIN (arg))
5288 if (GET_CODE (DECL_INCOMING_RTL (arg)) == REG
5289 && REGNO (DECL_INCOMING_RTL (arg)) == regno
5290 && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
5292 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
5293 int unsignedp = TREE_UNSIGNED (TREE_TYPE (arg));
5295 mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
5296 if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
5297 && mode != DECL_MODE (arg))
5299 *pmode = DECL_MODE (arg);
5300 *punsignedp = unsignedp;
5301 return DECL_INCOMING_RTL (arg);
5305 return 0;
5308 #endif
5310 /* Compute the size and offset from the start of the stacked arguments for a
5311 parm passed in mode PASSED_MODE and with type TYPE.
5313 INITIAL_OFFSET_PTR points to the current offset into the stacked
5314 arguments.
5316 The starting offset and size for this parm are returned in
5317 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
5318 nonzero, the offset is that of stack slot, which is returned in
5319 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
5320 padding required from the initial offset ptr to the stack slot.
5322 IN_REGS is nonzero if the argument will be passed in registers. It will
5323 never be set if REG_PARM_STACK_SPACE is not defined.
5325 FNDECL is the function in which the argument was defined.
5327 There are two types of rounding that are done. The first, controlled by
5328 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
5329 list to be aligned to the specific boundary (in bits). This rounding
5330 affects the initial and starting offsets, but not the argument size.
5332 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
5333 optionally rounds the size of the parm to PARM_BOUNDARY. The
5334 initial offset is not affected by this rounding, while the size always
5335 is and the starting offset may be. */
5337 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
5338 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
5339 callers pass in the total size of args so far as
5340 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
5342 void
5343 locate_and_pad_parm (enum machine_mode passed_mode, tree type, int in_regs,
5344 int partial, tree fndecl ATTRIBUTE_UNUSED,
5345 struct args_size *initial_offset_ptr,
5346 struct locate_and_pad_arg_data *locate)
5348 tree sizetree;
5349 enum direction where_pad;
5350 int boundary;
5351 int reg_parm_stack_space = 0;
5352 int part_size_in_regs;
5354 #ifdef REG_PARM_STACK_SPACE
5355 #ifdef MAYBE_REG_PARM_STACK_SPACE
5356 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
5357 #else
5358 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
5359 #endif
5361 /* If we have found a stack parm before we reach the end of the
5362 area reserved for registers, skip that area. */
5363 if (! in_regs)
5365 if (reg_parm_stack_space > 0)
5367 if (initial_offset_ptr->var)
5369 initial_offset_ptr->var
5370 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
5371 ssize_int (reg_parm_stack_space));
5372 initial_offset_ptr->constant = 0;
5374 else if (initial_offset_ptr->constant < reg_parm_stack_space)
5375 initial_offset_ptr->constant = reg_parm_stack_space;
5378 #endif /* REG_PARM_STACK_SPACE */
5380 part_size_in_regs = 0;
5381 if (reg_parm_stack_space == 0)
5382 part_size_in_regs = ((partial * UNITS_PER_WORD)
5383 / (PARM_BOUNDARY / BITS_PER_UNIT)
5384 * (PARM_BOUNDARY / BITS_PER_UNIT));
5386 sizetree
5387 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
5388 where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
5389 boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
5390 locate->where_pad = where_pad;
5392 #ifdef ARGS_GROW_DOWNWARD
5393 locate->slot_offset.constant = -initial_offset_ptr->constant;
5394 if (initial_offset_ptr->var)
5395 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
5396 initial_offset_ptr->var);
5399 tree s2 = sizetree;
5400 if (where_pad != none
5401 && (!host_integerp (sizetree, 1)
5402 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5403 s2 = round_up (s2, PARM_BOUNDARY / BITS_PER_UNIT);
5404 SUB_PARM_SIZE (locate->slot_offset, s2);
5407 locate->slot_offset.constant += part_size_in_regs;
5409 if (!in_regs
5410 #ifdef REG_PARM_STACK_SPACE
5411 || REG_PARM_STACK_SPACE (fndecl) > 0
5412 #endif
5414 pad_to_arg_alignment (&locate->slot_offset, boundary,
5415 &locate->alignment_pad);
5417 locate->size.constant = (-initial_offset_ptr->constant
5418 - locate->slot_offset.constant);
5419 if (initial_offset_ptr->var)
5420 locate->size.var = size_binop (MINUS_EXPR,
5421 size_binop (MINUS_EXPR,
5422 ssize_int (0),
5423 initial_offset_ptr->var),
5424 locate->slot_offset.var);
5426 /* Pad_below needs the pre-rounded size to know how much to pad
5427 below. */
5428 locate->offset = locate->slot_offset;
5429 if (where_pad == downward)
5430 pad_below (&locate->offset, passed_mode, sizetree);
5432 #else /* !ARGS_GROW_DOWNWARD */
5433 if (!in_regs
5434 #ifdef REG_PARM_STACK_SPACE
5435 || REG_PARM_STACK_SPACE (fndecl) > 0
5436 #endif
5438 pad_to_arg_alignment (initial_offset_ptr, boundary,
5439 &locate->alignment_pad);
5440 locate->slot_offset = *initial_offset_ptr;
5442 #ifdef PUSH_ROUNDING
5443 if (passed_mode != BLKmode)
5444 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
5445 #endif
5447 /* Pad_below needs the pre-rounded size to know how much to pad below
5448 so this must be done before rounding up. */
5449 locate->offset = locate->slot_offset;
5450 if (where_pad == downward)
5451 pad_below (&locate->offset, passed_mode, sizetree);
5453 if (where_pad != none
5454 && (!host_integerp (sizetree, 1)
5455 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5456 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5458 ADD_PARM_SIZE (locate->size, sizetree);
5460 locate->size.constant -= part_size_in_regs;
5461 #endif /* ARGS_GROW_DOWNWARD */
5464 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
5465 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
5467 static void
5468 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
5469 struct args_size *alignment_pad)
5471 tree save_var = NULL_TREE;
5472 HOST_WIDE_INT save_constant = 0;
5474 int boundary_in_bytes = boundary / BITS_PER_UNIT;
5476 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5478 save_var = offset_ptr->var;
5479 save_constant = offset_ptr->constant;
5482 alignment_pad->var = NULL_TREE;
5483 alignment_pad->constant = 0;
5485 if (boundary > BITS_PER_UNIT)
5487 if (offset_ptr->var)
5489 offset_ptr->var =
5490 #ifdef ARGS_GROW_DOWNWARD
5491 round_down
5492 #else
5493 round_up
5494 #endif
5495 (ARGS_SIZE_TREE (*offset_ptr),
5496 boundary / BITS_PER_UNIT);
5497 /* ARGS_SIZE_TREE includes constant term. */
5498 offset_ptr->constant = 0;
5499 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5500 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
5501 save_var);
5503 else
5505 offset_ptr->constant =
5506 #ifdef ARGS_GROW_DOWNWARD
5507 FLOOR_ROUND (offset_ptr->constant, boundary_in_bytes);
5508 #else
5509 CEIL_ROUND (offset_ptr->constant, boundary_in_bytes);
5510 #endif
5511 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5512 alignment_pad->constant = offset_ptr->constant - save_constant;
5517 static void
5518 pad_below (struct args_size *offset_ptr, enum machine_mode passed_mode, tree sizetree)
5520 if (passed_mode != BLKmode)
5522 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
5523 offset_ptr->constant
5524 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
5525 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
5526 - GET_MODE_SIZE (passed_mode));
5528 else
5530 if (TREE_CODE (sizetree) != INTEGER_CST
5531 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
5533 /* Round the size up to multiple of PARM_BOUNDARY bits. */
5534 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5535 /* Add it in. */
5536 ADD_PARM_SIZE (*offset_ptr, s2);
5537 SUB_PARM_SIZE (*offset_ptr, sizetree);
5542 /* Walk the tree of blocks describing the binding levels within a function
5543 and warn about uninitialized variables.
5544 This is done after calling flow_analysis and before global_alloc
5545 clobbers the pseudo-regs to hard regs. */
5547 void
5548 uninitialized_vars_warning (tree block)
5550 tree decl, sub;
5551 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5553 if (warn_uninitialized
5554 && TREE_CODE (decl) == VAR_DECL
5555 /* These warnings are unreliable for and aggregates
5556 because assigning the fields one by one can fail to convince
5557 flow.c that the entire aggregate was initialized.
5558 Unions are troublesome because members may be shorter. */
5559 && ! AGGREGATE_TYPE_P (TREE_TYPE (decl))
5560 && DECL_RTL (decl) != 0
5561 && GET_CODE (DECL_RTL (decl)) == REG
5562 /* Global optimizations can make it difficult to determine if a
5563 particular variable has been initialized. However, a VAR_DECL
5564 with a nonzero DECL_INITIAL had an initializer, so do not
5565 claim it is potentially uninitialized.
5567 When the DECL_INITIAL is NULL call the language hook to tell us
5568 if we want to warn. */
5569 && (DECL_INITIAL (decl) == NULL_TREE || lang_hooks.decl_uninit (decl))
5570 && regno_uninitialized (REGNO (DECL_RTL (decl))))
5571 warning ("%H'%D' might be used uninitialized in this function",
5572 &DECL_SOURCE_LOCATION (decl), decl);
5573 if (extra_warnings
5574 && TREE_CODE (decl) == VAR_DECL
5575 && DECL_RTL (decl) != 0
5576 && GET_CODE (DECL_RTL (decl)) == REG
5577 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5578 warning ("%Hvariable '%D' might be clobbered by `longjmp' or `vfork'",
5579 &DECL_SOURCE_LOCATION (decl), decl);
5581 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5582 uninitialized_vars_warning (sub);
5585 /* Do the appropriate part of uninitialized_vars_warning
5586 but for arguments instead of local variables. */
5588 void
5589 setjmp_args_warning (void)
5591 tree decl;
5592 for (decl = DECL_ARGUMENTS (current_function_decl);
5593 decl; decl = TREE_CHAIN (decl))
5594 if (DECL_RTL (decl) != 0
5595 && GET_CODE (DECL_RTL (decl)) == REG
5596 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5597 warning ("%Hargument '%D' might be clobbered by `longjmp' or `vfork'",
5598 &DECL_SOURCE_LOCATION (decl), decl);
5601 /* If this function call setjmp, put all vars into the stack
5602 unless they were declared `register'. */
5604 void
5605 setjmp_protect (tree block)
5607 tree decl, sub;
5608 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5609 if ((TREE_CODE (decl) == VAR_DECL
5610 || TREE_CODE (decl) == PARM_DECL)
5611 && DECL_RTL (decl) != 0
5612 && (GET_CODE (DECL_RTL (decl)) == REG
5613 || (GET_CODE (DECL_RTL (decl)) == MEM
5614 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5615 /* If this variable came from an inline function, it must be
5616 that its life doesn't overlap the setjmp. If there was a
5617 setjmp in the function, it would already be in memory. We
5618 must exclude such variable because their DECL_RTL might be
5619 set to strange things such as virtual_stack_vars_rtx. */
5620 && ! DECL_FROM_INLINE (decl)
5621 && (
5622 #ifdef NON_SAVING_SETJMP
5623 /* If longjmp doesn't restore the registers,
5624 don't put anything in them. */
5625 NON_SAVING_SETJMP
5627 #endif
5628 ! DECL_REGISTER (decl)))
5629 put_var_into_stack (decl, /*rescan=*/true);
5630 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5631 setjmp_protect (sub);
5634 /* Like the previous function, but for args instead of local variables. */
5636 void
5637 setjmp_protect_args (void)
5639 tree decl;
5640 for (decl = DECL_ARGUMENTS (current_function_decl);
5641 decl; decl = TREE_CHAIN (decl))
5642 if ((TREE_CODE (decl) == VAR_DECL
5643 || TREE_CODE (decl) == PARM_DECL)
5644 && DECL_RTL (decl) != 0
5645 && (GET_CODE (DECL_RTL (decl)) == REG
5646 || (GET_CODE (DECL_RTL (decl)) == MEM
5647 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5648 && (
5649 /* If longjmp doesn't restore the registers,
5650 don't put anything in them. */
5651 #ifdef NON_SAVING_SETJMP
5652 NON_SAVING_SETJMP
5654 #endif
5655 ! DECL_REGISTER (decl)))
5656 put_var_into_stack (decl, /*rescan=*/true);
5659 /* Return the context-pointer register corresponding to DECL,
5660 or 0 if it does not need one. */
5663 lookup_static_chain (tree decl)
5665 tree context = decl_function_context (decl);
5666 tree link;
5668 if (context == 0
5669 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_NO_STATIC_CHAIN (decl)))
5670 return 0;
5672 /* We treat inline_function_decl as an alias for the current function
5673 because that is the inline function whose vars, types, etc.
5674 are being merged into the current function.
5675 See expand_inline_function. */
5676 if (context == current_function_decl || context == inline_function_decl)
5677 return virtual_stack_vars_rtx;
5679 for (link = context_display; link; link = TREE_CHAIN (link))
5680 if (TREE_PURPOSE (link) == context)
5681 return RTL_EXPR_RTL (TREE_VALUE (link));
5683 abort ();
5686 /* Convert a stack slot address ADDR for variable VAR
5687 (from a containing function)
5688 into an address valid in this function (using a static chain). */
5691 fix_lexical_addr (rtx addr, tree var)
5693 rtx basereg;
5694 HOST_WIDE_INT displacement;
5695 tree context = decl_function_context (var);
5696 struct function *fp;
5697 rtx base = 0;
5699 /* If this is the present function, we need not do anything. */
5700 if (context == current_function_decl || context == inline_function_decl)
5701 return addr;
5703 fp = find_function_data (context);
5705 if (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == MEM)
5706 addr = XEXP (XEXP (addr, 0), 0);
5708 /* Decode given address as base reg plus displacement. */
5709 if (GET_CODE (addr) == REG)
5710 basereg = addr, displacement = 0;
5711 else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
5712 basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
5713 else
5714 abort ();
5716 /* We accept vars reached via the containing function's
5717 incoming arg pointer and via its stack variables pointer. */
5718 if (basereg == fp->internal_arg_pointer)
5720 /* If reached via arg pointer, get the arg pointer value
5721 out of that function's stack frame.
5723 There are two cases: If a separate ap is needed, allocate a
5724 slot in the outer function for it and dereference it that way.
5725 This is correct even if the real ap is actually a pseudo.
5726 Otherwise, just adjust the offset from the frame pointer to
5727 compensate. */
5729 #ifdef NEED_SEPARATE_AP
5730 rtx addr;
5732 addr = get_arg_pointer_save_area (fp);
5733 addr = fix_lexical_addr (XEXP (addr, 0), var);
5734 addr = memory_address (Pmode, addr);
5736 base = gen_rtx_MEM (Pmode, addr);
5737 set_mem_alias_set (base, get_frame_alias_set ());
5738 base = copy_to_reg (base);
5739 #else
5740 displacement += (FIRST_PARM_OFFSET (context) - STARTING_FRAME_OFFSET);
5741 base = lookup_static_chain (var);
5742 #endif
5745 else if (basereg == virtual_stack_vars_rtx)
5747 /* This is the same code as lookup_static_chain, duplicated here to
5748 avoid an extra call to decl_function_context. */
5749 tree link;
5751 for (link = context_display; link; link = TREE_CHAIN (link))
5752 if (TREE_PURPOSE (link) == context)
5754 base = RTL_EXPR_RTL (TREE_VALUE (link));
5755 break;
5759 if (base == 0)
5760 abort ();
5762 /* Use same offset, relative to appropriate static chain or argument
5763 pointer. */
5764 return plus_constant (base, displacement);
5767 /* Return the address of the trampoline for entering nested fn FUNCTION.
5768 If necessary, allocate a trampoline (in the stack frame)
5769 and emit rtl to initialize its contents (at entry to this function). */
5772 trampoline_address (tree function)
5774 tree link;
5775 tree rtlexp;
5776 rtx tramp;
5777 struct function *fp;
5778 tree fn_context;
5780 /* Find an existing trampoline and return it. */
5781 for (link = trampoline_list; link; link = TREE_CHAIN (link))
5782 if (TREE_PURPOSE (link) == function)
5783 return
5784 adjust_trampoline_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0));
5786 for (fp = outer_function_chain; fp; fp = fp->outer)
5787 for (link = fp->x_trampoline_list; link; link = TREE_CHAIN (link))
5788 if (TREE_PURPOSE (link) == function)
5790 tramp = fix_lexical_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0),
5791 function);
5792 return adjust_trampoline_addr (tramp);
5795 /* None exists; we must make one. */
5797 /* Find the `struct function' for the function containing FUNCTION. */
5798 fp = 0;
5799 fn_context = decl_function_context (function);
5800 if (fn_context != current_function_decl
5801 && fn_context != inline_function_decl)
5802 fp = find_function_data (fn_context);
5804 /* Allocate run-time space for this trampoline. */
5805 /* If rounding needed, allocate extra space
5806 to ensure we have TRAMPOLINE_SIZE bytes left after rounding up. */
5807 #define TRAMPOLINE_REAL_SIZE \
5808 (TRAMPOLINE_SIZE + (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT) - 1)
5809 tramp = assign_stack_local_1 (BLKmode, TRAMPOLINE_REAL_SIZE, 0,
5810 fp ? fp : cfun);
5811 /* Record the trampoline for reuse and note it for later initialization
5812 by expand_function_end. */
5813 if (fp != 0)
5815 rtlexp = make_node (RTL_EXPR);
5816 RTL_EXPR_RTL (rtlexp) = tramp;
5817 fp->x_trampoline_list = tree_cons (function, rtlexp,
5818 fp->x_trampoline_list);
5820 else
5822 /* Make the RTL_EXPR node temporary, not momentary, so that the
5823 trampoline_list doesn't become garbage. */
5824 rtlexp = make_node (RTL_EXPR);
5826 RTL_EXPR_RTL (rtlexp) = tramp;
5827 trampoline_list = tree_cons (function, rtlexp, trampoline_list);
5830 tramp = fix_lexical_addr (XEXP (tramp, 0), function);
5831 return adjust_trampoline_addr (tramp);
5834 /* Given a trampoline address,
5835 round it to multiple of TRAMPOLINE_ALIGNMENT. */
5837 static rtx
5838 round_trampoline_addr (rtx tramp)
5840 /* Round address up to desired boundary. */
5841 rtx temp = gen_reg_rtx (Pmode);
5842 rtx addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5843 rtx mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5845 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5846 temp, 0, OPTAB_LIB_WIDEN);
5847 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5848 temp, 0, OPTAB_LIB_WIDEN);
5850 return tramp;
5853 /* Given a trampoline address, round it then apply any
5854 platform-specific adjustments so that the result can be used for a
5855 function call . */
5857 static rtx
5858 adjust_trampoline_addr (rtx tramp)
5860 tramp = round_trampoline_addr (tramp);
5861 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5862 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5863 #endif
5864 return tramp;
5867 /* Put all this function's BLOCK nodes including those that are chained
5868 onto the first block into a vector, and return it.
5869 Also store in each NOTE for the beginning or end of a block
5870 the index of that block in the vector.
5871 The arguments are BLOCK, the chain of top-level blocks of the function,
5872 and INSNS, the insn chain of the function. */
5874 void
5875 identify_blocks (void)
5877 int n_blocks;
5878 tree *block_vector, *last_block_vector;
5879 tree *block_stack;
5880 tree block = DECL_INITIAL (current_function_decl);
5882 if (block == 0)
5883 return;
5885 /* Fill the BLOCK_VECTOR with all of the BLOCKs in this function, in
5886 depth-first order. */
5887 block_vector = get_block_vector (block, &n_blocks);
5888 block_stack = xmalloc (n_blocks * sizeof (tree));
5890 last_block_vector = identify_blocks_1 (get_insns (),
5891 block_vector + 1,
5892 block_vector + n_blocks,
5893 block_stack);
5895 /* If we didn't use all of the subblocks, we've misplaced block notes. */
5896 /* ??? This appears to happen all the time. Latent bugs elsewhere? */
5897 if (0 && last_block_vector != block_vector + n_blocks)
5898 abort ();
5900 free (block_vector);
5901 free (block_stack);
5904 /* Subroutine of identify_blocks. Do the block substitution on the
5905 insn chain beginning with INSNS. Recurse for CALL_PLACEHOLDER chains.
5907 BLOCK_STACK is pushed and popped for each BLOCK_BEGIN/BLOCK_END pair.
5908 BLOCK_VECTOR is incremented for each block seen. */
5910 static tree *
5911 identify_blocks_1 (rtx insns, tree *block_vector, tree *end_block_vector,
5912 tree *orig_block_stack)
5914 rtx insn;
5915 tree *block_stack = orig_block_stack;
5917 for (insn = insns; insn; insn = NEXT_INSN (insn))
5919 if (GET_CODE (insn) == NOTE)
5921 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5923 tree b;
5925 /* If there are more block notes than BLOCKs, something
5926 is badly wrong. */
5927 if (block_vector == end_block_vector)
5928 abort ();
5930 b = *block_vector++;
5931 NOTE_BLOCK (insn) = b;
5932 *block_stack++ = b;
5934 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5936 /* If there are more NOTE_INSN_BLOCK_ENDs than
5937 NOTE_INSN_BLOCK_BEGs, something is badly wrong. */
5938 if (block_stack == orig_block_stack)
5939 abort ();
5941 NOTE_BLOCK (insn) = *--block_stack;
5944 else if (GET_CODE (insn) == CALL_INSN
5945 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5947 rtx cp = PATTERN (insn);
5949 block_vector = identify_blocks_1 (XEXP (cp, 0), block_vector,
5950 end_block_vector, block_stack);
5951 if (XEXP (cp, 1))
5952 block_vector = identify_blocks_1 (XEXP (cp, 1), block_vector,
5953 end_block_vector, block_stack);
5954 if (XEXP (cp, 2))
5955 block_vector = identify_blocks_1 (XEXP (cp, 2), block_vector,
5956 end_block_vector, block_stack);
5960 /* If there are more NOTE_INSN_BLOCK_BEGINs than NOTE_INSN_BLOCK_ENDs,
5961 something is badly wrong. */
5962 if (block_stack != orig_block_stack)
5963 abort ();
5965 return block_vector;
5968 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
5969 and create duplicate blocks. */
5970 /* ??? Need an option to either create block fragments or to create
5971 abstract origin duplicates of a source block. It really depends
5972 on what optimization has been performed. */
5974 void
5975 reorder_blocks (void)
5977 tree block = DECL_INITIAL (current_function_decl);
5978 varray_type block_stack;
5980 if (block == NULL_TREE)
5981 return;
5983 VARRAY_TREE_INIT (block_stack, 10, "block_stack");
5985 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
5986 reorder_blocks_0 (block);
5988 /* Prune the old trees away, so that they don't get in the way. */
5989 BLOCK_SUBBLOCKS (block) = NULL_TREE;
5990 BLOCK_CHAIN (block) = NULL_TREE;
5992 /* Recreate the block tree from the note nesting. */
5993 reorder_blocks_1 (get_insns (), block, &block_stack);
5994 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
5996 /* Remove deleted blocks from the block fragment chains. */
5997 reorder_fix_fragments (block);
6000 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
6002 static void
6003 reorder_blocks_0 (tree block)
6005 while (block)
6007 TREE_ASM_WRITTEN (block) = 0;
6008 reorder_blocks_0 (BLOCK_SUBBLOCKS (block));
6009 block = BLOCK_CHAIN (block);
6013 static void
6014 reorder_blocks_1 (rtx insns, tree current_block, varray_type *p_block_stack)
6016 rtx insn;
6018 for (insn = insns; insn; insn = NEXT_INSN (insn))
6020 if (GET_CODE (insn) == NOTE)
6022 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
6024 tree block = NOTE_BLOCK (insn);
6026 /* If we have seen this block before, that means it now
6027 spans multiple address regions. Create a new fragment. */
6028 if (TREE_ASM_WRITTEN (block))
6030 tree new_block = copy_node (block);
6031 tree origin;
6033 origin = (BLOCK_FRAGMENT_ORIGIN (block)
6034 ? BLOCK_FRAGMENT_ORIGIN (block)
6035 : block);
6036 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
6037 BLOCK_FRAGMENT_CHAIN (new_block)
6038 = BLOCK_FRAGMENT_CHAIN (origin);
6039 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
6041 NOTE_BLOCK (insn) = new_block;
6042 block = new_block;
6045 BLOCK_SUBBLOCKS (block) = 0;
6046 TREE_ASM_WRITTEN (block) = 1;
6047 /* When there's only one block for the entire function,
6048 current_block == block and we mustn't do this, it
6049 will cause infinite recursion. */
6050 if (block != current_block)
6052 BLOCK_SUPERCONTEXT (block) = current_block;
6053 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
6054 BLOCK_SUBBLOCKS (current_block) = block;
6055 current_block = block;
6057 VARRAY_PUSH_TREE (*p_block_stack, block);
6059 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
6061 NOTE_BLOCK (insn) = VARRAY_TOP_TREE (*p_block_stack);
6062 VARRAY_POP (*p_block_stack);
6063 BLOCK_SUBBLOCKS (current_block)
6064 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
6065 current_block = BLOCK_SUPERCONTEXT (current_block);
6068 else if (GET_CODE (insn) == CALL_INSN
6069 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
6071 rtx cp = PATTERN (insn);
6072 reorder_blocks_1 (XEXP (cp, 0), current_block, p_block_stack);
6073 if (XEXP (cp, 1))
6074 reorder_blocks_1 (XEXP (cp, 1), current_block, p_block_stack);
6075 if (XEXP (cp, 2))
6076 reorder_blocks_1 (XEXP (cp, 2), current_block, p_block_stack);
6081 /* Rationalize BLOCK_FRAGMENT_ORIGIN. If an origin block no longer
6082 appears in the block tree, select one of the fragments to become
6083 the new origin block. */
6085 static void
6086 reorder_fix_fragments (tree block)
6088 while (block)
6090 tree dup_origin = BLOCK_FRAGMENT_ORIGIN (block);
6091 tree new_origin = NULL_TREE;
6093 if (dup_origin)
6095 if (! TREE_ASM_WRITTEN (dup_origin))
6097 new_origin = BLOCK_FRAGMENT_CHAIN (dup_origin);
6099 /* Find the first of the remaining fragments. There must
6100 be at least one -- the current block. */
6101 while (! TREE_ASM_WRITTEN (new_origin))
6102 new_origin = BLOCK_FRAGMENT_CHAIN (new_origin);
6103 BLOCK_FRAGMENT_ORIGIN (new_origin) = NULL_TREE;
6106 else if (! dup_origin)
6107 new_origin = block;
6109 /* Re-root the rest of the fragments to the new origin. In the
6110 case that DUP_ORIGIN was null, that means BLOCK was the origin
6111 of a chain of fragments and we want to remove those fragments
6112 that didn't make it to the output. */
6113 if (new_origin)
6115 tree *pp = &BLOCK_FRAGMENT_CHAIN (new_origin);
6116 tree chain = *pp;
6118 while (chain)
6120 if (TREE_ASM_WRITTEN (chain))
6122 BLOCK_FRAGMENT_ORIGIN (chain) = new_origin;
6123 *pp = chain;
6124 pp = &BLOCK_FRAGMENT_CHAIN (chain);
6126 chain = BLOCK_FRAGMENT_CHAIN (chain);
6128 *pp = NULL_TREE;
6131 reorder_fix_fragments (BLOCK_SUBBLOCKS (block));
6132 block = BLOCK_CHAIN (block);
6136 /* Reverse the order of elements in the chain T of blocks,
6137 and return the new head of the chain (old last element). */
6139 static tree
6140 blocks_nreverse (tree t)
6142 tree prev = 0, decl, next;
6143 for (decl = t; decl; decl = next)
6145 next = BLOCK_CHAIN (decl);
6146 BLOCK_CHAIN (decl) = prev;
6147 prev = decl;
6149 return prev;
6152 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
6153 non-NULL, list them all into VECTOR, in a depth-first preorder
6154 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
6155 blocks. */
6157 static int
6158 all_blocks (tree block, tree *vector)
6160 int n_blocks = 0;
6162 while (block)
6164 TREE_ASM_WRITTEN (block) = 0;
6166 /* Record this block. */
6167 if (vector)
6168 vector[n_blocks] = block;
6170 ++n_blocks;
6172 /* Record the subblocks, and their subblocks... */
6173 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
6174 vector ? vector + n_blocks : 0);
6175 block = BLOCK_CHAIN (block);
6178 return n_blocks;
6181 /* Return a vector containing all the blocks rooted at BLOCK. The
6182 number of elements in the vector is stored in N_BLOCKS_P. The
6183 vector is dynamically allocated; it is the caller's responsibility
6184 to call `free' on the pointer returned. */
6186 static tree *
6187 get_block_vector (tree block, int *n_blocks_p)
6189 tree *block_vector;
6191 *n_blocks_p = all_blocks (block, NULL);
6192 block_vector = xmalloc (*n_blocks_p * sizeof (tree));
6193 all_blocks (block, block_vector);
6195 return block_vector;
6198 static GTY(()) int next_block_index = 2;
6200 /* Set BLOCK_NUMBER for all the blocks in FN. */
6202 void
6203 number_blocks (tree fn)
6205 int i;
6206 int n_blocks;
6207 tree *block_vector;
6209 /* For SDB and XCOFF debugging output, we start numbering the blocks
6210 from 1 within each function, rather than keeping a running
6211 count. */
6212 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
6213 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
6214 next_block_index = 1;
6215 #endif
6217 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
6219 /* The top-level BLOCK isn't numbered at all. */
6220 for (i = 1; i < n_blocks; ++i)
6221 /* We number the blocks from two. */
6222 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
6224 free (block_vector);
6226 return;
6229 /* If VAR is present in a subblock of BLOCK, return the subblock. */
6231 tree
6232 debug_find_var_in_block_tree (tree var, tree block)
6234 tree t;
6236 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
6237 if (t == var)
6238 return block;
6240 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
6242 tree ret = debug_find_var_in_block_tree (var, t);
6243 if (ret)
6244 return ret;
6247 return NULL_TREE;
6250 /* Allocate a function structure and reset its contents to the defaults. */
6252 static void
6253 prepare_function_start (void)
6255 cfun = ggc_alloc_cleared (sizeof (struct function));
6257 init_stmt_for_function ();
6258 init_eh_for_function ();
6260 cse_not_expected = ! optimize;
6262 /* Caller save not needed yet. */
6263 caller_save_needed = 0;
6265 /* No stack slots have been made yet. */
6266 stack_slot_list = 0;
6268 current_function_has_nonlocal_label = 0;
6269 current_function_has_nonlocal_goto = 0;
6271 /* There is no stack slot for handling nonlocal gotos. */
6272 nonlocal_goto_handler_slots = 0;
6273 nonlocal_goto_stack_level = 0;
6275 /* No labels have been declared for nonlocal use. */
6276 nonlocal_labels = 0;
6277 nonlocal_goto_handler_labels = 0;
6279 /* No function calls so far in this function. */
6280 function_call_count = 0;
6282 /* No parm regs have been allocated.
6283 (This is important for output_inline_function.) */
6284 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
6286 /* Initialize the RTL mechanism. */
6287 init_emit ();
6289 /* Initialize the queue of pending postincrement and postdecrements,
6290 and some other info in expr.c. */
6291 init_expr ();
6293 /* We haven't done register allocation yet. */
6294 reg_renumber = 0;
6296 init_varasm_status (cfun);
6298 /* Clear out data used for inlining. */
6299 cfun->inlinable = 0;
6300 cfun->original_decl_initial = 0;
6301 cfun->original_arg_vector = 0;
6303 cfun->stack_alignment_needed = STACK_BOUNDARY;
6304 cfun->preferred_stack_boundary = STACK_BOUNDARY;
6306 /* Set if a call to setjmp is seen. */
6307 current_function_calls_setjmp = 0;
6309 /* Set if a call to longjmp is seen. */
6310 current_function_calls_longjmp = 0;
6312 current_function_calls_alloca = 0;
6313 current_function_calls_eh_return = 0;
6314 current_function_calls_constant_p = 0;
6315 current_function_contains_functions = 0;
6316 current_function_is_leaf = 0;
6317 current_function_nothrow = 0;
6318 current_function_sp_is_unchanging = 0;
6319 current_function_uses_only_leaf_regs = 0;
6320 current_function_has_computed_jump = 0;
6321 current_function_is_thunk = 0;
6323 current_function_returns_pcc_struct = 0;
6324 current_function_returns_struct = 0;
6325 current_function_epilogue_delay_list = 0;
6326 current_function_uses_const_pool = 0;
6327 current_function_uses_pic_offset_table = 0;
6328 current_function_cannot_inline = 0;
6330 /* We have not yet needed to make a label to jump to for tail-recursion. */
6331 tail_recursion_label = 0;
6333 /* We haven't had a need to make a save area for ap yet. */
6334 arg_pointer_save_area = 0;
6336 /* No stack slots allocated yet. */
6337 frame_offset = 0;
6339 /* No SAVE_EXPRs in this function yet. */
6340 save_expr_regs = 0;
6342 /* No RTL_EXPRs in this function yet. */
6343 rtl_expr_chain = 0;
6345 /* Set up to allocate temporaries. */
6346 init_temp_slots ();
6348 /* Indicate that we need to distinguish between the return value of the
6349 present function and the return value of a function being called. */
6350 rtx_equal_function_value_matters = 1;
6352 /* Indicate that we have not instantiated virtual registers yet. */
6353 virtuals_instantiated = 0;
6355 /* Indicate that we want CONCATs now. */
6356 generating_concat_p = 1;
6358 /* Indicate we have no need of a frame pointer yet. */
6359 frame_pointer_needed = 0;
6361 /* By default assume not stdarg. */
6362 current_function_stdarg = 0;
6364 /* We haven't made any trampolines for this function yet. */
6365 trampoline_list = 0;
6367 init_pending_stack_adjust ();
6368 inhibit_defer_pop = 0;
6370 current_function_outgoing_args_size = 0;
6372 current_function_funcdef_no = funcdef_no++;
6374 cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
6376 cfun->max_jumptable_ents = 0;
6378 (*lang_hooks.function.init) (cfun);
6379 if (init_machine_status)
6380 cfun->machine = (*init_machine_status) ();
6383 /* Initialize the rtl expansion mechanism so that we can do simple things
6384 like generate sequences. This is used to provide a context during global
6385 initialization of some passes. */
6386 void
6387 init_dummy_function_start (void)
6389 prepare_function_start ();
6392 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
6393 and initialize static variables for generating RTL for the statements
6394 of the function. */
6396 void
6397 init_function_start (tree subr)
6399 prepare_function_start ();
6401 current_function_name = (*lang_hooks.decl_printable_name) (subr, 2);
6402 cfun->decl = subr;
6404 /* Nonzero if this is a nested function that uses a static chain. */
6406 current_function_needs_context
6407 = (decl_function_context (current_function_decl) != 0
6408 && ! DECL_NO_STATIC_CHAIN (current_function_decl));
6410 /* Within function body, compute a type's size as soon it is laid out. */
6411 immediate_size_expand++;
6413 /* Prevent ever trying to delete the first instruction of a
6414 function. Also tell final how to output a linenum before the
6415 function prologue. Note linenums could be missing, e.g. when
6416 compiling a Java .class file. */
6417 if (DECL_SOURCE_LINE (subr))
6418 emit_line_note (DECL_SOURCE_LOCATION (subr));
6420 /* Make sure first insn is a note even if we don't want linenums.
6421 This makes sure the first insn will never be deleted.
6422 Also, final expects a note to appear there. */
6423 emit_note (NOTE_INSN_DELETED);
6425 /* Set flags used by final.c. */
6426 if (aggregate_value_p (DECL_RESULT (subr)))
6428 #ifdef PCC_STATIC_STRUCT_RETURN
6429 current_function_returns_pcc_struct = 1;
6430 #endif
6431 current_function_returns_struct = 1;
6434 /* Warn if this value is an aggregate type,
6435 regardless of which calling convention we are using for it. */
6436 if (warn_aggregate_return
6437 && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
6438 warning ("function returns an aggregate");
6440 current_function_returns_pointer
6441 = POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
6444 /* Make sure all values used by the optimization passes have sane
6445 defaults. */
6446 void
6447 init_function_for_compilation (void)
6449 reg_renumber = 0;
6451 /* No prologue/epilogue insns yet. */
6452 VARRAY_GROW (prologue, 0);
6453 VARRAY_GROW (epilogue, 0);
6454 VARRAY_GROW (sibcall_epilogue, 0);
6457 /* Expand a call to __main at the beginning of a possible main function. */
6459 #if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
6460 #undef HAS_INIT_SECTION
6461 #define HAS_INIT_SECTION
6462 #endif
6464 void
6465 expand_main_function (void)
6467 #ifdef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
6468 if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN)
6470 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
6471 rtx tmp, seq;
6473 start_sequence ();
6474 /* Forcibly align the stack. */
6475 #ifdef STACK_GROWS_DOWNWARD
6476 tmp = expand_simple_binop (Pmode, AND, stack_pointer_rtx, GEN_INT(-align),
6477 stack_pointer_rtx, 1, OPTAB_WIDEN);
6478 #else
6479 tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
6480 GEN_INT (align - 1), NULL_RTX, 1, OPTAB_WIDEN);
6481 tmp = expand_simple_binop (Pmode, AND, tmp, GEN_INT (-align),
6482 stack_pointer_rtx, 1, OPTAB_WIDEN);
6483 #endif
6484 if (tmp != stack_pointer_rtx)
6485 emit_move_insn (stack_pointer_rtx, tmp);
6487 /* Enlist allocate_dynamic_stack_space to pick up the pieces. */
6488 tmp = force_reg (Pmode, const0_rtx);
6489 allocate_dynamic_stack_space (tmp, NULL_RTX, BIGGEST_ALIGNMENT);
6490 seq = get_insns ();
6491 end_sequence ();
6493 for (tmp = get_last_insn (); tmp; tmp = PREV_INSN (tmp))
6494 if (NOTE_P (tmp) && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_FUNCTION_BEG)
6495 break;
6496 if (tmp)
6497 emit_insn_before (seq, tmp);
6498 else
6499 emit_insn (seq);
6501 #endif
6503 #ifndef HAS_INIT_SECTION
6504 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
6505 #endif
6508 /* The PENDING_SIZES represent the sizes of variable-sized types.
6509 Create RTL for the various sizes now (using temporary variables),
6510 so that we can refer to the sizes from the RTL we are generating
6511 for the current function. The PENDING_SIZES are a TREE_LIST. The
6512 TREE_VALUE of each node is a SAVE_EXPR. */
6514 void
6515 expand_pending_sizes (tree pending_sizes)
6517 tree tem;
6519 /* Evaluate now the sizes of any types declared among the arguments. */
6520 for (tem = pending_sizes; tem; tem = TREE_CHAIN (tem))
6522 expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode, 0);
6523 /* Flush the queue in case this parameter declaration has
6524 side-effects. */
6525 emit_queue ();
6529 /* Start the RTL for a new function, and set variables used for
6530 emitting RTL.
6531 SUBR is the FUNCTION_DECL node.
6532 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
6533 the function's parameters, which must be run at any return statement. */
6535 void
6536 expand_function_start (tree subr, int parms_have_cleanups)
6538 tree tem;
6539 rtx last_ptr = NULL_RTX;
6541 /* Make sure volatile mem refs aren't considered
6542 valid operands of arithmetic insns. */
6543 init_recog_no_volatile ();
6545 current_function_instrument_entry_exit
6546 = (flag_instrument_function_entry_exit
6547 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6549 current_function_profile
6550 = (profile_flag
6551 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6553 current_function_limit_stack
6554 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
6556 /* If function gets a static chain arg, store it in the stack frame.
6557 Do this first, so it gets the first stack slot offset. */
6558 if (current_function_needs_context)
6560 last_ptr = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
6562 /* Delay copying static chain if it is not a register to avoid
6563 conflicts with regs used for parameters. */
6564 if (! SMALL_REGISTER_CLASSES
6565 || GET_CODE (static_chain_incoming_rtx) == REG)
6566 emit_move_insn (last_ptr, static_chain_incoming_rtx);
6569 /* If the parameters of this function need cleaning up, get a label
6570 for the beginning of the code which executes those cleanups. This must
6571 be done before doing anything with return_label. */
6572 if (parms_have_cleanups)
6573 cleanup_label = gen_label_rtx ();
6574 else
6575 cleanup_label = 0;
6577 /* Make the label for return statements to jump to. Do not special
6578 case machines with special return instructions -- they will be
6579 handled later during jump, ifcvt, or epilogue creation. */
6580 return_label = gen_label_rtx ();
6582 /* Initialize rtx used to return the value. */
6583 /* Do this before assign_parms so that we copy the struct value address
6584 before any library calls that assign parms might generate. */
6586 /* Decide whether to return the value in memory or in a register. */
6587 if (aggregate_value_p (DECL_RESULT (subr)))
6589 /* Returning something that won't go in a register. */
6590 rtx value_address = 0;
6592 #ifdef PCC_STATIC_STRUCT_RETURN
6593 if (current_function_returns_pcc_struct)
6595 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
6596 value_address = assemble_static_space (size);
6598 else
6599 #endif
6601 /* Expect to be passed the address of a place to store the value.
6602 If it is passed as an argument, assign_parms will take care of
6603 it. */
6604 if (struct_value_incoming_rtx)
6606 value_address = gen_reg_rtx (Pmode);
6607 emit_move_insn (value_address, struct_value_incoming_rtx);
6610 if (value_address)
6612 rtx x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), value_address);
6613 set_mem_attributes (x, DECL_RESULT (subr), 1);
6614 SET_DECL_RTL (DECL_RESULT (subr), x);
6617 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
6618 /* If return mode is void, this decl rtl should not be used. */
6619 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
6620 else
6622 /* Compute the return values into a pseudo reg, which we will copy
6623 into the true return register after the cleanups are done. */
6625 /* In order to figure out what mode to use for the pseudo, we
6626 figure out what the mode of the eventual return register will
6627 actually be, and use that. */
6628 rtx hard_reg
6629 = hard_function_value (TREE_TYPE (DECL_RESULT (subr)),
6630 subr, 1);
6632 /* Structures that are returned in registers are not aggregate_value_p,
6633 so we may see a PARALLEL or a REG. */
6634 if (REG_P (hard_reg))
6635 SET_DECL_RTL (DECL_RESULT (subr), gen_reg_rtx (GET_MODE (hard_reg)));
6636 else if (GET_CODE (hard_reg) == PARALLEL)
6637 SET_DECL_RTL (DECL_RESULT (subr), gen_group_rtx (hard_reg));
6638 else
6639 abort ();
6641 /* Set DECL_REGISTER flag so that expand_function_end will copy the
6642 result to the real return register(s). */
6643 DECL_REGISTER (DECL_RESULT (subr)) = 1;
6646 /* Initialize rtx for parameters and local variables.
6647 In some cases this requires emitting insns. */
6649 assign_parms (subr);
6651 /* Copy the static chain now if it wasn't a register. The delay is to
6652 avoid conflicts with the parameter passing registers. */
6654 if (SMALL_REGISTER_CLASSES && current_function_needs_context)
6655 if (GET_CODE (static_chain_incoming_rtx) != REG)
6656 emit_move_insn (last_ptr, static_chain_incoming_rtx);
6658 /* The following was moved from init_function_start.
6659 The move is supposed to make sdb output more accurate. */
6660 /* Indicate the beginning of the function body,
6661 as opposed to parm setup. */
6662 emit_note (NOTE_INSN_FUNCTION_BEG);
6664 if (GET_CODE (get_last_insn ()) != NOTE)
6665 emit_note (NOTE_INSN_DELETED);
6666 parm_birth_insn = get_last_insn ();
6668 context_display = 0;
6669 if (current_function_needs_context)
6671 /* Fetch static chain values for containing functions. */
6672 tem = decl_function_context (current_function_decl);
6673 /* Copy the static chain pointer into a pseudo. If we have
6674 small register classes, copy the value from memory if
6675 static_chain_incoming_rtx is a REG. */
6676 if (tem)
6678 /* If the static chain originally came in a register, put it back
6679 there, then move it out in the next insn. The reason for
6680 this peculiar code is to satisfy function integration. */
6681 if (SMALL_REGISTER_CLASSES
6682 && GET_CODE (static_chain_incoming_rtx) == REG)
6683 emit_move_insn (static_chain_incoming_rtx, last_ptr);
6684 last_ptr = copy_to_reg (static_chain_incoming_rtx);
6687 while (tem)
6689 tree rtlexp = make_node (RTL_EXPR);
6691 RTL_EXPR_RTL (rtlexp) = last_ptr;
6692 context_display = tree_cons (tem, rtlexp, context_display);
6693 tem = decl_function_context (tem);
6694 if (tem == 0)
6695 break;
6696 /* Chain thru stack frames, assuming pointer to next lexical frame
6697 is found at the place we always store it. */
6698 #ifdef FRAME_GROWS_DOWNWARD
6699 last_ptr = plus_constant (last_ptr,
6700 -(HOST_WIDE_INT) GET_MODE_SIZE (Pmode));
6701 #endif
6702 last_ptr = gen_rtx_MEM (Pmode, memory_address (Pmode, last_ptr));
6703 set_mem_alias_set (last_ptr, get_frame_alias_set ());
6704 last_ptr = copy_to_reg (last_ptr);
6706 /* If we are not optimizing, ensure that we know that this
6707 piece of context is live over the entire function. */
6708 if (! optimize)
6709 save_expr_regs = gen_rtx_EXPR_LIST (VOIDmode, last_ptr,
6710 save_expr_regs);
6714 if (current_function_instrument_entry_exit)
6716 rtx fun = DECL_RTL (current_function_decl);
6717 if (GET_CODE (fun) == MEM)
6718 fun = XEXP (fun, 0);
6719 else
6720 abort ();
6721 emit_library_call (profile_function_entry_libfunc, LCT_NORMAL, VOIDmode,
6722 2, fun, Pmode,
6723 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6725 hard_frame_pointer_rtx),
6726 Pmode);
6729 if (current_function_profile)
6731 #ifdef PROFILE_HOOK
6732 PROFILE_HOOK (current_function_funcdef_no);
6733 #endif
6736 /* After the display initializations is where the tail-recursion label
6737 should go, if we end up needing one. Ensure we have a NOTE here
6738 since some things (like trampolines) get placed before this. */
6739 tail_recursion_reentry = emit_note (NOTE_INSN_DELETED);
6741 /* Evaluate now the sizes of any types declared among the arguments. */
6742 expand_pending_sizes (nreverse (get_pending_sizes ()));
6744 /* Make sure there is a line number after the function entry setup code. */
6745 force_next_line_note ();
6748 /* Undo the effects of init_dummy_function_start. */
6749 void
6750 expand_dummy_function_end (void)
6752 /* End any sequences that failed to be closed due to syntax errors. */
6753 while (in_sequence_p ())
6754 end_sequence ();
6756 /* Outside function body, can't compute type's actual size
6757 until next function's body starts. */
6759 free_after_parsing (cfun);
6760 free_after_compilation (cfun);
6761 cfun = 0;
6764 /* Call DOIT for each hard register used as a return value from
6765 the current function. */
6767 void
6768 diddle_return_value (void (*doit) (rtx, void *), void *arg)
6770 rtx outgoing = current_function_return_rtx;
6772 if (! outgoing)
6773 return;
6775 if (GET_CODE (outgoing) == REG)
6776 (*doit) (outgoing, arg);
6777 else if (GET_CODE (outgoing) == PARALLEL)
6779 int i;
6781 for (i = 0; i < XVECLEN (outgoing, 0); i++)
6783 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
6785 if (GET_CODE (x) == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
6786 (*doit) (x, arg);
6791 static void
6792 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
6794 emit_insn (gen_rtx_CLOBBER (VOIDmode, reg));
6797 void
6798 clobber_return_register (void)
6800 diddle_return_value (do_clobber_return_reg, NULL);
6802 /* In case we do use pseudo to return value, clobber it too. */
6803 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6805 tree decl_result = DECL_RESULT (current_function_decl);
6806 rtx decl_rtl = DECL_RTL (decl_result);
6807 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
6809 do_clobber_return_reg (decl_rtl, NULL);
6814 static void
6815 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
6817 emit_insn (gen_rtx_USE (VOIDmode, reg));
6820 void
6821 use_return_register (void)
6823 diddle_return_value (do_use_return_reg, NULL);
6826 static GTY(()) rtx initial_trampoline;
6828 /* Generate RTL for the end of the current function. */
6830 void
6831 expand_function_end (void)
6833 tree link;
6834 rtx clobber_after;
6836 finish_expr_for_function ();
6838 /* If arg_pointer_save_area was referenced only from a nested
6839 function, we will not have initialized it yet. Do that now. */
6840 if (arg_pointer_save_area && ! cfun->arg_pointer_save_area_init)
6841 get_arg_pointer_save_area (cfun);
6843 #ifdef NON_SAVING_SETJMP
6844 /* Don't put any variables in registers if we call setjmp
6845 on a machine that fails to restore the registers. */
6846 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
6848 if (DECL_INITIAL (current_function_decl) != error_mark_node)
6849 setjmp_protect (DECL_INITIAL (current_function_decl));
6851 setjmp_protect_args ();
6853 #endif
6855 /* Initialize any trampolines required by this function. */
6856 for (link = trampoline_list; link; link = TREE_CHAIN (link))
6858 tree function = TREE_PURPOSE (link);
6859 rtx context ATTRIBUTE_UNUSED = lookup_static_chain (function);
6860 rtx tramp = RTL_EXPR_RTL (TREE_VALUE (link));
6861 #ifdef TRAMPOLINE_TEMPLATE
6862 rtx blktramp;
6863 #endif
6864 rtx seq;
6866 #ifdef TRAMPOLINE_TEMPLATE
6867 /* First make sure this compilation has a template for
6868 initializing trampolines. */
6869 if (initial_trampoline == 0)
6871 initial_trampoline
6872 = gen_rtx_MEM (BLKmode, assemble_trampoline_template ());
6873 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
6875 #endif
6877 /* Generate insns to initialize the trampoline. */
6878 start_sequence ();
6879 tramp = round_trampoline_addr (XEXP (tramp, 0));
6880 #ifdef TRAMPOLINE_TEMPLATE
6881 blktramp = replace_equiv_address (initial_trampoline, tramp);
6882 emit_block_move (blktramp, initial_trampoline,
6883 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
6884 #endif
6885 trampolines_created = 1;
6886 INITIALIZE_TRAMPOLINE (tramp, XEXP (DECL_RTL (function), 0), context);
6887 seq = get_insns ();
6888 end_sequence ();
6890 /* Put those insns at entry to the containing function (this one). */
6891 emit_insn_before (seq, tail_recursion_reentry);
6894 /* If we are doing stack checking and this function makes calls,
6895 do a stack probe at the start of the function to ensure we have enough
6896 space for another stack frame. */
6897 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
6899 rtx insn, seq;
6901 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6902 if (GET_CODE (insn) == CALL_INSN)
6904 start_sequence ();
6905 probe_stack_range (STACK_CHECK_PROTECT,
6906 GEN_INT (STACK_CHECK_MAX_FRAME_SIZE));
6907 seq = get_insns ();
6908 end_sequence ();
6909 emit_insn_before (seq, tail_recursion_reentry);
6910 break;
6914 /* Possibly warn about unused parameters. */
6915 if (warn_unused_parameter)
6917 tree decl;
6919 for (decl = DECL_ARGUMENTS (current_function_decl);
6920 decl; decl = TREE_CHAIN (decl))
6921 if (! TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
6922 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
6923 warning ("%Hunused parameter '%D'",
6924 &DECL_SOURCE_LOCATION (decl), decl);
6927 /* Delete handlers for nonlocal gotos if nothing uses them. */
6928 if (nonlocal_goto_handler_slots != 0
6929 && ! current_function_has_nonlocal_label)
6930 delete_handlers ();
6932 /* End any sequences that failed to be closed due to syntax errors. */
6933 while (in_sequence_p ())
6934 end_sequence ();
6936 /* Outside function body, can't compute type's actual size
6937 until next function's body starts. */
6938 immediate_size_expand--;
6940 clear_pending_stack_adjust ();
6941 do_pending_stack_adjust ();
6943 /* Mark the end of the function body.
6944 If control reaches this insn, the function can drop through
6945 without returning a value. */
6946 emit_note (NOTE_INSN_FUNCTION_END);
6948 /* Must mark the last line number note in the function, so that the test
6949 coverage code can avoid counting the last line twice. This just tells
6950 the code to ignore the immediately following line note, since there
6951 already exists a copy of this note somewhere above. This line number
6952 note is still needed for debugging though, so we can't delete it. */
6953 if (flag_test_coverage)
6954 emit_note (NOTE_INSN_REPEATED_LINE_NUMBER);
6956 /* Output a linenumber for the end of the function.
6957 SDB depends on this. */
6958 force_next_line_note ();
6959 emit_line_note (input_location);
6961 /* Before the return label (if any), clobber the return
6962 registers so that they are not propagated live to the rest of
6963 the function. This can only happen with functions that drop
6964 through; if there had been a return statement, there would
6965 have either been a return rtx, or a jump to the return label.
6967 We delay actual code generation after the current_function_value_rtx
6968 is computed. */
6969 clobber_after = get_last_insn ();
6971 /* Output the label for the actual return from the function,
6972 if one is expected. This happens either because a function epilogue
6973 is used instead of a return instruction, or because a return was done
6974 with a goto in order to run local cleanups, or because of pcc-style
6975 structure returning. */
6976 if (return_label)
6977 emit_label (return_label);
6979 if (current_function_instrument_entry_exit)
6981 rtx fun = DECL_RTL (current_function_decl);
6982 if (GET_CODE (fun) == MEM)
6983 fun = XEXP (fun, 0);
6984 else
6985 abort ();
6986 emit_library_call (profile_function_exit_libfunc, LCT_NORMAL, VOIDmode,
6987 2, fun, Pmode,
6988 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6990 hard_frame_pointer_rtx),
6991 Pmode);
6994 /* Let except.c know where it should emit the call to unregister
6995 the function context for sjlj exceptions. */
6996 if (flag_exceptions && USING_SJLJ_EXCEPTIONS)
6997 sjlj_emit_function_exit_after (get_last_insn ());
6999 /* If we had calls to alloca, and this machine needs
7000 an accurate stack pointer to exit the function,
7001 insert some code to save and restore the stack pointer. */
7002 #ifdef EXIT_IGNORE_STACK
7003 if (! EXIT_IGNORE_STACK)
7004 #endif
7005 if (current_function_calls_alloca)
7007 rtx tem = 0;
7009 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
7010 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
7013 /* If scalar return value was computed in a pseudo-reg, or was a named
7014 return value that got dumped to the stack, copy that to the hard
7015 return register. */
7016 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
7018 tree decl_result = DECL_RESULT (current_function_decl);
7019 rtx decl_rtl = DECL_RTL (decl_result);
7021 if (REG_P (decl_rtl)
7022 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
7023 : DECL_REGISTER (decl_result))
7025 rtx real_decl_rtl = current_function_return_rtx;
7027 /* This should be set in assign_parms. */
7028 if (! REG_FUNCTION_VALUE_P (real_decl_rtl))
7029 abort ();
7031 /* If this is a BLKmode structure being returned in registers,
7032 then use the mode computed in expand_return. Note that if
7033 decl_rtl is memory, then its mode may have been changed,
7034 but that current_function_return_rtx has not. */
7035 if (GET_MODE (real_decl_rtl) == BLKmode)
7036 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
7038 /* If a named return value dumped decl_return to memory, then
7039 we may need to re-do the PROMOTE_MODE signed/unsigned
7040 extension. */
7041 if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
7043 int unsignedp = TREE_UNSIGNED (TREE_TYPE (decl_result));
7045 #ifdef PROMOTE_FUNCTION_RETURN
7046 promote_mode (TREE_TYPE (decl_result), GET_MODE (decl_rtl),
7047 &unsignedp, 1);
7048 #endif
7050 convert_move (real_decl_rtl, decl_rtl, unsignedp);
7052 else if (GET_CODE (real_decl_rtl) == PARALLEL)
7054 /* If expand_function_start has created a PARALLEL for decl_rtl,
7055 move the result to the real return registers. Otherwise, do
7056 a group load from decl_rtl for a named return. */
7057 if (GET_CODE (decl_rtl) == PARALLEL)
7058 emit_group_move (real_decl_rtl, decl_rtl);
7059 else
7060 emit_group_load (real_decl_rtl, decl_rtl,
7061 TREE_TYPE (decl_result),
7062 int_size_in_bytes (TREE_TYPE (decl_result)));
7064 else
7065 emit_move_insn (real_decl_rtl, decl_rtl);
7069 /* If returning a structure, arrange to return the address of the value
7070 in a place where debuggers expect to find it.
7072 If returning a structure PCC style,
7073 the caller also depends on this value.
7074 And current_function_returns_pcc_struct is not necessarily set. */
7075 if (current_function_returns_struct
7076 || current_function_returns_pcc_struct)
7078 rtx value_address
7079 = XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
7080 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
7081 #ifdef FUNCTION_OUTGOING_VALUE
7082 rtx outgoing
7083 = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
7084 current_function_decl);
7085 #else
7086 rtx outgoing
7087 = FUNCTION_VALUE (build_pointer_type (type), current_function_decl);
7088 #endif
7090 /* Mark this as a function return value so integrate will delete the
7091 assignment and USE below when inlining this function. */
7092 REG_FUNCTION_VALUE_P (outgoing) = 1;
7094 #ifdef POINTERS_EXTEND_UNSIGNED
7095 /* The address may be ptr_mode and OUTGOING may be Pmode. */
7096 if (GET_MODE (outgoing) != GET_MODE (value_address))
7097 value_address = convert_memory_address (GET_MODE (outgoing),
7098 value_address);
7099 #endif
7101 emit_move_insn (outgoing, value_address);
7103 /* Show return register used to hold result (in this case the address
7104 of the result. */
7105 current_function_return_rtx = outgoing;
7108 /* If this is an implementation of throw, do what's necessary to
7109 communicate between __builtin_eh_return and the epilogue. */
7110 expand_eh_return ();
7112 /* Emit the actual code to clobber return register. */
7114 rtx seq, after;
7116 start_sequence ();
7117 clobber_return_register ();
7118 seq = get_insns ();
7119 end_sequence ();
7121 after = emit_insn_after (seq, clobber_after);
7123 if (clobber_after != after)
7124 cfun->x_clobber_return_insn = after;
7127 /* ??? This should no longer be necessary since stupid is no longer with
7128 us, but there are some parts of the compiler (eg reload_combine, and
7129 sh mach_dep_reorg) that still try and compute their own lifetime info
7130 instead of using the general framework. */
7131 use_return_register ();
7133 /* Fix up any gotos that jumped out to the outermost
7134 binding level of the function.
7135 Must follow emitting RETURN_LABEL. */
7137 /* If you have any cleanups to do at this point,
7138 and they need to create temporary variables,
7139 then you will lose. */
7140 expand_fixups (get_insns ());
7144 get_arg_pointer_save_area (struct function *f)
7146 rtx ret = f->x_arg_pointer_save_area;
7148 if (! ret)
7150 ret = assign_stack_local_1 (Pmode, GET_MODE_SIZE (Pmode), 0, f);
7151 f->x_arg_pointer_save_area = ret;
7154 if (f == cfun && ! f->arg_pointer_save_area_init)
7156 rtx seq;
7158 /* Save the arg pointer at the beginning of the function. The
7159 generated stack slot may not be a valid memory address, so we
7160 have to check it and fix it if necessary. */
7161 start_sequence ();
7162 emit_move_insn (validize_mem (ret), virtual_incoming_args_rtx);
7163 seq = get_insns ();
7164 end_sequence ();
7166 push_topmost_sequence ();
7167 emit_insn_after (seq, get_insns ());
7168 pop_topmost_sequence ();
7171 return ret;
7174 /* Extend a vector that records the INSN_UIDs of INSNS
7175 (a list of one or more insns). */
7177 static void
7178 record_insns (rtx insns, varray_type *vecp)
7180 int i, len;
7181 rtx tmp;
7183 tmp = insns;
7184 len = 0;
7185 while (tmp != NULL_RTX)
7187 len++;
7188 tmp = NEXT_INSN (tmp);
7191 i = VARRAY_SIZE (*vecp);
7192 VARRAY_GROW (*vecp, i + len);
7193 tmp = insns;
7194 while (tmp != NULL_RTX)
7196 VARRAY_INT (*vecp, i) = INSN_UID (tmp);
7197 i++;
7198 tmp = NEXT_INSN (tmp);
7202 /* Set the specified locator to the insn chain. */
7203 static void
7204 set_insn_locators (rtx insn, int loc)
7206 while (insn != NULL_RTX)
7208 if (INSN_P (insn))
7209 INSN_LOCATOR (insn) = loc;
7210 insn = NEXT_INSN (insn);
7214 /* Determine how many INSN_UIDs in VEC are part of INSN. Because we can
7215 be running after reorg, SEQUENCE rtl is possible. */
7217 static int
7218 contains (rtx insn, varray_type vec)
7220 int i, j;
7222 if (GET_CODE (insn) == INSN
7223 && GET_CODE (PATTERN (insn)) == SEQUENCE)
7225 int count = 0;
7226 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
7227 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7228 if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == VARRAY_INT (vec, j))
7229 count++;
7230 return count;
7232 else
7234 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7235 if (INSN_UID (insn) == VARRAY_INT (vec, j))
7236 return 1;
7238 return 0;
7242 prologue_epilogue_contains (rtx insn)
7244 if (contains (insn, prologue))
7245 return 1;
7246 if (contains (insn, epilogue))
7247 return 1;
7248 return 0;
7252 sibcall_epilogue_contains (rtx insn)
7254 if (sibcall_epilogue)
7255 return contains (insn, sibcall_epilogue);
7256 return 0;
7259 #ifdef HAVE_return
7260 /* Insert gen_return at the end of block BB. This also means updating
7261 block_for_insn appropriately. */
7263 static void
7264 emit_return_into_block (basic_block bb, rtx line_note)
7266 emit_jump_insn_after (gen_return (), bb->end);
7267 if (line_note)
7268 emit_note_copy_after (line_note, PREV_INSN (bb->end));
7270 #endif /* HAVE_return */
7272 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
7274 /* These functions convert the epilogue into a variant that does not modify the
7275 stack pointer. This is used in cases where a function returns an object
7276 whose size is not known until it is computed. The called function leaves the
7277 object on the stack, leaves the stack depressed, and returns a pointer to
7278 the object.
7280 What we need to do is track all modifications and references to the stack
7281 pointer, deleting the modifications and changing the references to point to
7282 the location the stack pointer would have pointed to had the modifications
7283 taken place.
7285 These functions need to be portable so we need to make as few assumptions
7286 about the epilogue as we can. However, the epilogue basically contains
7287 three things: instructions to reset the stack pointer, instructions to
7288 reload registers, possibly including the frame pointer, and an
7289 instruction to return to the caller.
7291 If we can't be sure of what a relevant epilogue insn is doing, we abort.
7292 We also make no attempt to validate the insns we make since if they are
7293 invalid, we probably can't do anything valid. The intent is that these
7294 routines get "smarter" as more and more machines start to use them and
7295 they try operating on different epilogues.
7297 We use the following structure to track what the part of the epilogue that
7298 we've already processed has done. We keep two copies of the SP equivalence,
7299 one for use during the insn we are processing and one for use in the next
7300 insn. The difference is because one part of a PARALLEL may adjust SP
7301 and the other may use it. */
7303 struct epi_info
7305 rtx sp_equiv_reg; /* REG that SP is set from, perhaps SP. */
7306 HOST_WIDE_INT sp_offset; /* Offset from SP_EQUIV_REG of present SP. */
7307 rtx new_sp_equiv_reg; /* REG to be used at end of insn. */
7308 HOST_WIDE_INT new_sp_offset; /* Offset to be used at end of insn. */
7309 rtx equiv_reg_src; /* If nonzero, the value that SP_EQUIV_REG
7310 should be set to once we no longer need
7311 its value. */
7314 static void handle_epilogue_set (rtx, struct epi_info *);
7315 static void emit_equiv_load (struct epi_info *);
7317 /* Modify INSN, a list of one or more insns that is part of the epilogue, to
7318 no modifications to the stack pointer. Return the new list of insns. */
7320 static rtx
7321 keep_stack_depressed (rtx insns)
7323 int j;
7324 struct epi_info info;
7325 rtx insn, next;
7327 /* If the epilogue is just a single instruction, it ust be OK as is. */
7329 if (NEXT_INSN (insns) == NULL_RTX)
7330 return insns;
7332 /* Otherwise, start a sequence, initialize the information we have, and
7333 process all the insns we were given. */
7334 start_sequence ();
7336 info.sp_equiv_reg = stack_pointer_rtx;
7337 info.sp_offset = 0;
7338 info.equiv_reg_src = 0;
7340 insn = insns;
7341 next = NULL_RTX;
7342 while (insn != NULL_RTX)
7344 next = NEXT_INSN (insn);
7346 if (!INSN_P (insn))
7348 add_insn (insn);
7349 insn = next;
7350 continue;
7353 /* If this insn references the register that SP is equivalent to and
7354 we have a pending load to that register, we must force out the load
7355 first and then indicate we no longer know what SP's equivalent is. */
7356 if (info.equiv_reg_src != 0
7357 && reg_referenced_p (info.sp_equiv_reg, PATTERN (insn)))
7359 emit_equiv_load (&info);
7360 info.sp_equiv_reg = 0;
7363 info.new_sp_equiv_reg = info.sp_equiv_reg;
7364 info.new_sp_offset = info.sp_offset;
7366 /* If this is a (RETURN) and the return address is on the stack,
7367 update the address and change to an indirect jump. */
7368 if (GET_CODE (PATTERN (insn)) == RETURN
7369 || (GET_CODE (PATTERN (insn)) == PARALLEL
7370 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == RETURN))
7372 rtx retaddr = INCOMING_RETURN_ADDR_RTX;
7373 rtx base = 0;
7374 HOST_WIDE_INT offset = 0;
7375 rtx jump_insn, jump_set;
7377 /* If the return address is in a register, we can emit the insn
7378 unchanged. Otherwise, it must be a MEM and we see what the
7379 base register and offset are. In any case, we have to emit any
7380 pending load to the equivalent reg of SP, if any. */
7381 if (GET_CODE (retaddr) == REG)
7383 emit_equiv_load (&info);
7384 add_insn (insn);
7385 insn = next;
7386 continue;
7388 else if (GET_CODE (retaddr) == MEM
7389 && GET_CODE (XEXP (retaddr, 0)) == REG)
7390 base = gen_rtx_REG (Pmode, REGNO (XEXP (retaddr, 0))), offset = 0;
7391 else if (GET_CODE (retaddr) == MEM
7392 && GET_CODE (XEXP (retaddr, 0)) == PLUS
7393 && GET_CODE (XEXP (XEXP (retaddr, 0), 0)) == REG
7394 && GET_CODE (XEXP (XEXP (retaddr, 0), 1)) == CONST_INT)
7396 base = gen_rtx_REG (Pmode, REGNO (XEXP (XEXP (retaddr, 0), 0)));
7397 offset = INTVAL (XEXP (XEXP (retaddr, 0), 1));
7399 else
7400 abort ();
7402 /* If the base of the location containing the return pointer
7403 is SP, we must update it with the replacement address. Otherwise,
7404 just build the necessary MEM. */
7405 retaddr = plus_constant (base, offset);
7406 if (base == stack_pointer_rtx)
7407 retaddr = simplify_replace_rtx (retaddr, stack_pointer_rtx,
7408 plus_constant (info.sp_equiv_reg,
7409 info.sp_offset));
7411 retaddr = gen_rtx_MEM (Pmode, retaddr);
7413 /* If there is a pending load to the equivalent register for SP
7414 and we reference that register, we must load our address into
7415 a scratch register and then do that load. */
7416 if (info.equiv_reg_src
7417 && reg_overlap_mentioned_p (info.equiv_reg_src, retaddr))
7419 unsigned int regno;
7420 rtx reg;
7422 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
7423 if (HARD_REGNO_MODE_OK (regno, Pmode)
7424 && !fixed_regs[regno]
7425 && TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)
7426 && !REGNO_REG_SET_P (EXIT_BLOCK_PTR->global_live_at_start,
7427 regno)
7428 && !refers_to_regno_p (regno,
7429 regno + HARD_REGNO_NREGS (regno,
7430 Pmode),
7431 info.equiv_reg_src, NULL))
7432 break;
7434 if (regno == FIRST_PSEUDO_REGISTER)
7435 abort ();
7437 reg = gen_rtx_REG (Pmode, regno);
7438 emit_move_insn (reg, retaddr);
7439 retaddr = reg;
7442 emit_equiv_load (&info);
7443 jump_insn = emit_jump_insn (gen_indirect_jump (retaddr));
7445 /* Show the SET in the above insn is a RETURN. */
7446 jump_set = single_set (jump_insn);
7447 if (jump_set == 0)
7448 abort ();
7449 else
7450 SET_IS_RETURN_P (jump_set) = 1;
7453 /* If SP is not mentioned in the pattern and its equivalent register, if
7454 any, is not modified, just emit it. Otherwise, if neither is set,
7455 replace the reference to SP and emit the insn. If none of those are
7456 true, handle each SET individually. */
7457 else if (!reg_mentioned_p (stack_pointer_rtx, PATTERN (insn))
7458 && (info.sp_equiv_reg == stack_pointer_rtx
7459 || !reg_set_p (info.sp_equiv_reg, insn)))
7460 add_insn (insn);
7461 else if (! reg_set_p (stack_pointer_rtx, insn)
7462 && (info.sp_equiv_reg == stack_pointer_rtx
7463 || !reg_set_p (info.sp_equiv_reg, insn)))
7465 if (! validate_replace_rtx (stack_pointer_rtx,
7466 plus_constant (info.sp_equiv_reg,
7467 info.sp_offset),
7468 insn))
7469 abort ();
7471 add_insn (insn);
7473 else if (GET_CODE (PATTERN (insn)) == SET)
7474 handle_epilogue_set (PATTERN (insn), &info);
7475 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
7477 for (j = 0; j < XVECLEN (PATTERN (insn), 0); j++)
7478 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET)
7479 handle_epilogue_set (XVECEXP (PATTERN (insn), 0, j), &info);
7481 else
7482 add_insn (insn);
7484 info.sp_equiv_reg = info.new_sp_equiv_reg;
7485 info.sp_offset = info.new_sp_offset;
7487 insn = next;
7490 insns = get_insns ();
7491 end_sequence ();
7492 return insns;
7495 /* SET is a SET from an insn in the epilogue. P is a pointer to the epi_info
7496 structure that contains information about what we've seen so far. We
7497 process this SET by either updating that data or by emitting one or
7498 more insns. */
7500 static void
7501 handle_epilogue_set (rtx set, struct epi_info *p)
7503 /* First handle the case where we are setting SP. Record what it is being
7504 set from. If unknown, abort. */
7505 if (reg_set_p (stack_pointer_rtx, set))
7507 if (SET_DEST (set) != stack_pointer_rtx)
7508 abort ();
7510 if (GET_CODE (SET_SRC (set)) == PLUS
7511 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
7513 p->new_sp_equiv_reg = XEXP (SET_SRC (set), 0);
7514 p->new_sp_offset = INTVAL (XEXP (SET_SRC (set), 1));
7516 else
7517 p->new_sp_equiv_reg = SET_SRC (set), p->new_sp_offset = 0;
7519 /* If we are adjusting SP, we adjust from the old data. */
7520 if (p->new_sp_equiv_reg == stack_pointer_rtx)
7522 p->new_sp_equiv_reg = p->sp_equiv_reg;
7523 p->new_sp_offset += p->sp_offset;
7526 if (p->new_sp_equiv_reg == 0 || GET_CODE (p->new_sp_equiv_reg) != REG)
7527 abort ();
7529 return;
7532 /* Next handle the case where we are setting SP's equivalent register.
7533 If we already have a value to set it to, abort. We could update, but
7534 there seems little point in handling that case. Note that we have
7535 to allow for the case where we are setting the register set in
7536 the previous part of a PARALLEL inside a single insn. But use the
7537 old offset for any updates within this insn. */
7538 else if (p->new_sp_equiv_reg != 0 && reg_set_p (p->new_sp_equiv_reg, set))
7540 if (!rtx_equal_p (p->new_sp_equiv_reg, SET_DEST (set))
7541 || p->equiv_reg_src != 0)
7542 abort ();
7543 else
7544 p->equiv_reg_src
7545 = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7546 plus_constant (p->sp_equiv_reg,
7547 p->sp_offset));
7550 /* Otherwise, replace any references to SP in the insn to its new value
7551 and emit the insn. */
7552 else
7554 SET_SRC (set) = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7555 plus_constant (p->sp_equiv_reg,
7556 p->sp_offset));
7557 SET_DEST (set) = simplify_replace_rtx (SET_DEST (set), stack_pointer_rtx,
7558 plus_constant (p->sp_equiv_reg,
7559 p->sp_offset));
7560 emit_insn (set);
7564 /* Emit an insn to do the load shown in p->equiv_reg_src, if needed. */
7566 static void
7567 emit_equiv_load (struct epi_info *p)
7569 if (p->equiv_reg_src != 0)
7570 emit_move_insn (p->sp_equiv_reg, p->equiv_reg_src);
7572 p->equiv_reg_src = 0;
7574 #endif
7576 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
7577 this into place with notes indicating where the prologue ends and where
7578 the epilogue begins. Update the basic block information when possible. */
7580 void
7581 thread_prologue_and_epilogue_insns (rtx f ATTRIBUTE_UNUSED)
7583 int inserted = 0;
7584 edge e;
7585 #if defined (HAVE_sibcall_epilogue) || defined (HAVE_epilogue) || defined (HAVE_return) || defined (HAVE_prologue)
7586 rtx seq;
7587 #endif
7588 #ifdef HAVE_prologue
7589 rtx prologue_end = NULL_RTX;
7590 #endif
7591 #if defined (HAVE_epilogue) || defined(HAVE_return)
7592 rtx epilogue_end = NULL_RTX;
7593 #endif
7595 #ifdef HAVE_prologue
7596 if (HAVE_prologue)
7598 start_sequence ();
7599 seq = gen_prologue ();
7600 emit_insn (seq);
7602 /* Retain a map of the prologue insns. */
7603 record_insns (seq, &prologue);
7604 prologue_end = emit_note (NOTE_INSN_PROLOGUE_END);
7606 seq = get_insns ();
7607 end_sequence ();
7608 set_insn_locators (seq, prologue_locator);
7610 /* Can't deal with multiple successors of the entry block
7611 at the moment. Function should always have at least one
7612 entry point. */
7613 if (!ENTRY_BLOCK_PTR->succ || ENTRY_BLOCK_PTR->succ->succ_next)
7614 abort ();
7616 insert_insn_on_edge (seq, ENTRY_BLOCK_PTR->succ);
7617 inserted = 1;
7619 #endif
7621 /* If the exit block has no non-fake predecessors, we don't need
7622 an epilogue. */
7623 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7624 if ((e->flags & EDGE_FAKE) == 0)
7625 break;
7626 if (e == NULL)
7627 goto epilogue_done;
7629 #ifdef HAVE_return
7630 if (optimize && HAVE_return)
7632 /* If we're allowed to generate a simple return instruction,
7633 then by definition we don't need a full epilogue. Examine
7634 the block that falls through to EXIT. If it does not
7635 contain any code, examine its predecessors and try to
7636 emit (conditional) return instructions. */
7638 basic_block last;
7639 edge e_next;
7640 rtx label;
7642 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7643 if (e->flags & EDGE_FALLTHRU)
7644 break;
7645 if (e == NULL)
7646 goto epilogue_done;
7647 last = e->src;
7649 /* Verify that there are no active instructions in the last block. */
7650 label = last->end;
7651 while (label && GET_CODE (label) != CODE_LABEL)
7653 if (active_insn_p (label))
7654 break;
7655 label = PREV_INSN (label);
7658 if (last->head == label && GET_CODE (label) == CODE_LABEL)
7660 rtx epilogue_line_note = NULL_RTX;
7662 /* Locate the line number associated with the closing brace,
7663 if we can find one. */
7664 for (seq = get_last_insn ();
7665 seq && ! active_insn_p (seq);
7666 seq = PREV_INSN (seq))
7667 if (GET_CODE (seq) == NOTE && NOTE_LINE_NUMBER (seq) > 0)
7669 epilogue_line_note = seq;
7670 break;
7673 for (e = last->pred; e; e = e_next)
7675 basic_block bb = e->src;
7676 rtx jump;
7678 e_next = e->pred_next;
7679 if (bb == ENTRY_BLOCK_PTR)
7680 continue;
7682 jump = bb->end;
7683 if ((GET_CODE (jump) != JUMP_INSN) || JUMP_LABEL (jump) != label)
7684 continue;
7686 /* If we have an unconditional jump, we can replace that
7687 with a simple return instruction. */
7688 if (simplejump_p (jump))
7690 emit_return_into_block (bb, epilogue_line_note);
7691 delete_insn (jump);
7694 /* If we have a conditional jump, we can try to replace
7695 that with a conditional return instruction. */
7696 else if (condjump_p (jump))
7698 if (! redirect_jump (jump, 0, 0))
7699 continue;
7701 /* If this block has only one successor, it both jumps
7702 and falls through to the fallthru block, so we can't
7703 delete the edge. */
7704 if (bb->succ->succ_next == NULL)
7705 continue;
7707 else
7708 continue;
7710 /* Fix up the CFG for the successful change we just made. */
7711 redirect_edge_succ (e, EXIT_BLOCK_PTR);
7714 /* Emit a return insn for the exit fallthru block. Whether
7715 this is still reachable will be determined later. */
7717 emit_barrier_after (last->end);
7718 emit_return_into_block (last, epilogue_line_note);
7719 epilogue_end = last->end;
7720 last->succ->flags &= ~EDGE_FALLTHRU;
7721 goto epilogue_done;
7724 #endif
7725 #ifdef HAVE_epilogue
7726 if (HAVE_epilogue)
7728 /* Find the edge that falls through to EXIT. Other edges may exist
7729 due to RETURN instructions, but those don't need epilogues.
7730 There really shouldn't be a mixture -- either all should have
7731 been converted or none, however... */
7733 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7734 if (e->flags & EDGE_FALLTHRU)
7735 break;
7736 if (e == NULL)
7737 goto epilogue_done;
7739 start_sequence ();
7740 epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
7742 seq = gen_epilogue ();
7744 #ifdef INCOMING_RETURN_ADDR_RTX
7745 /* If this function returns with the stack depressed and we can support
7746 it, massage the epilogue to actually do that. */
7747 if (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
7748 && TYPE_RETURNS_STACK_DEPRESSED (TREE_TYPE (current_function_decl)))
7749 seq = keep_stack_depressed (seq);
7750 #endif
7752 emit_jump_insn (seq);
7754 /* Retain a map of the epilogue insns. */
7755 record_insns (seq, &epilogue);
7756 set_insn_locators (seq, epilogue_locator);
7758 seq = get_insns ();
7759 end_sequence ();
7761 insert_insn_on_edge (seq, e);
7762 inserted = 1;
7764 #endif
7765 epilogue_done:
7767 if (inserted)
7768 commit_edge_insertions ();
7770 #ifdef HAVE_sibcall_epilogue
7771 /* Emit sibling epilogues before any sibling call sites. */
7772 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7774 basic_block bb = e->src;
7775 rtx insn = bb->end;
7776 rtx i;
7777 rtx newinsn;
7779 if (GET_CODE (insn) != CALL_INSN
7780 || ! SIBLING_CALL_P (insn))
7781 continue;
7783 start_sequence ();
7784 emit_insn (gen_sibcall_epilogue ());
7785 seq = get_insns ();
7786 end_sequence ();
7788 /* Retain a map of the epilogue insns. Used in life analysis to
7789 avoid getting rid of sibcall epilogue insns. Do this before we
7790 actually emit the sequence. */
7791 record_insns (seq, &sibcall_epilogue);
7792 set_insn_locators (seq, epilogue_locator);
7794 i = PREV_INSN (insn);
7795 newinsn = emit_insn_before (seq, insn);
7797 #endif
7799 #ifdef HAVE_prologue
7800 if (prologue_end)
7802 rtx insn, prev;
7804 /* GDB handles `break f' by setting a breakpoint on the first
7805 line note after the prologue. Which means (1) that if
7806 there are line number notes before where we inserted the
7807 prologue we should move them, and (2) we should generate a
7808 note before the end of the first basic block, if there isn't
7809 one already there.
7811 ??? This behavior is completely broken when dealing with
7812 multiple entry functions. We simply place the note always
7813 into first basic block and let alternate entry points
7814 to be missed.
7817 for (insn = prologue_end; insn; insn = prev)
7819 prev = PREV_INSN (insn);
7820 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7822 /* Note that we cannot reorder the first insn in the
7823 chain, since rest_of_compilation relies on that
7824 remaining constant. */
7825 if (prev == NULL)
7826 break;
7827 reorder_insns (insn, insn, prologue_end);
7831 /* Find the last line number note in the first block. */
7832 for (insn = ENTRY_BLOCK_PTR->next_bb->end;
7833 insn != prologue_end && insn;
7834 insn = PREV_INSN (insn))
7835 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7836 break;
7838 /* If we didn't find one, make a copy of the first line number
7839 we run across. */
7840 if (! insn)
7842 for (insn = next_active_insn (prologue_end);
7843 insn;
7844 insn = PREV_INSN (insn))
7845 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7847 emit_note_copy_after (insn, prologue_end);
7848 break;
7852 #endif
7853 #ifdef HAVE_epilogue
7854 if (epilogue_end)
7856 rtx insn, next;
7858 /* Similarly, move any line notes that appear after the epilogue.
7859 There is no need, however, to be quite so anal about the existence
7860 of such a note. */
7861 for (insn = epilogue_end; insn; insn = next)
7863 next = NEXT_INSN (insn);
7864 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7865 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
7868 #endif
7871 /* Reposition the prologue-end and epilogue-begin notes after instruction
7872 scheduling and delayed branch scheduling. */
7874 void
7875 reposition_prologue_and_epilogue_notes (rtx f ATTRIBUTE_UNUSED)
7877 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
7878 rtx insn, last, note;
7879 int len;
7881 if ((len = VARRAY_SIZE (prologue)) > 0)
7883 last = 0, note = 0;
7885 /* Scan from the beginning until we reach the last prologue insn.
7886 We apparently can't depend on basic_block_{head,end} after
7887 reorg has run. */
7888 for (insn = f; insn; insn = NEXT_INSN (insn))
7890 if (GET_CODE (insn) == NOTE)
7892 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
7893 note = insn;
7895 else if (contains (insn, prologue))
7897 last = insn;
7898 if (--len == 0)
7899 break;
7903 if (last)
7905 /* Find the prologue-end note if we haven't already, and
7906 move it to just after the last prologue insn. */
7907 if (note == 0)
7909 for (note = last; (note = NEXT_INSN (note));)
7910 if (GET_CODE (note) == NOTE
7911 && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
7912 break;
7915 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
7916 if (GET_CODE (last) == CODE_LABEL)
7917 last = NEXT_INSN (last);
7918 reorder_insns (note, note, last);
7922 if ((len = VARRAY_SIZE (epilogue)) > 0)
7924 last = 0, note = 0;
7926 /* Scan from the end until we reach the first epilogue insn.
7927 We apparently can't depend on basic_block_{head,end} after
7928 reorg has run. */
7929 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
7931 if (GET_CODE (insn) == NOTE)
7933 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
7934 note = insn;
7936 else if (contains (insn, epilogue))
7938 last = insn;
7939 if (--len == 0)
7940 break;
7944 if (last)
7946 /* Find the epilogue-begin note if we haven't already, and
7947 move it to just before the first epilogue insn. */
7948 if (note == 0)
7950 for (note = insn; (note = PREV_INSN (note));)
7951 if (GET_CODE (note) == NOTE
7952 && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
7953 break;
7956 if (PREV_INSN (last) != note)
7957 reorder_insns (note, note, PREV_INSN (last));
7960 #endif /* HAVE_prologue or HAVE_epilogue */
7963 /* Called once, at initialization, to initialize function.c. */
7965 void
7966 init_function_once (void)
7968 VARRAY_INT_INIT (prologue, 0, "prologue");
7969 VARRAY_INT_INIT (epilogue, 0, "epilogue");
7970 VARRAY_INT_INIT (sibcall_epilogue, 0, "sibcall_epilogue");
7973 #include "gt-function.h"