Revert:
[official-gcc.git] / gcc / function.c
blob581c0834d44c5b0741166f7f2722a2d6c2e9a6b4
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, 2004, 2005, 2006
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This file handles the generation of rtl code from tree structure
24 at the level of the function as a whole.
25 It creates the rtl expressions for parameters and auto variables
26 and has full responsibility for allocating stack slots.
28 `expand_function_start' is called at the beginning of a function,
29 before the function body is parsed, and `expand_function_end' is
30 called after parsing the body.
32 Call `assign_stack_local' to allocate a stack slot for a local variable.
33 This is usually done during the RTL generation for the function body,
34 but it can also be done in the reload pass when a pseudo-register does
35 not get a hard register. */
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "rtl.h"
42 #include "tree.h"
43 #include "flags.h"
44 #include "except.h"
45 #include "function.h"
46 #include "expr.h"
47 #include "optabs.h"
48 #include "libfuncs.h"
49 #include "regs.h"
50 #include "hard-reg-set.h"
51 #include "insn-config.h"
52 #include "recog.h"
53 #include "output.h"
54 #include "basic-block.h"
55 #include "toplev.h"
56 #include "hashtab.h"
57 #include "ggc.h"
58 #include "tm_p.h"
59 #include "integrate.h"
60 #include "langhooks.h"
61 #include "target.h"
62 #include "cfglayout.h"
63 #include "tree-gimple.h"
64 #include "tree-pass.h"
65 #include "predict.h"
66 #include "vecprim.h"
68 #ifndef LOCAL_ALIGNMENT
69 #define LOCAL_ALIGNMENT(TYPE, ALIGNMENT) ALIGNMENT
70 #endif
72 #ifndef STACK_ALIGNMENT_NEEDED
73 #define STACK_ALIGNMENT_NEEDED 1
74 #endif
76 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
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 /* Nonzero if function being compiled doesn't contain any calls
96 (ignoring the prologue and epilogue). This is set prior to
97 local register allocation and is valid for the remaining
98 compiler passes. */
99 int current_function_is_leaf;
101 /* Nonzero if function being compiled doesn't modify the stack pointer
102 (ignoring the prologue and epilogue). This is only valid after
103 life_analysis has run. */
104 int current_function_sp_is_unchanging;
106 /* Nonzero if the function being compiled is a leaf function which only
107 uses leaf registers. This is valid after reload (specifically after
108 sched2) and is useful only if the port defines LEAF_REGISTERS. */
109 int current_function_uses_only_leaf_regs;
111 /* Nonzero once virtual register instantiation has been done.
112 assign_stack_local uses frame_pointer_rtx when this is nonzero.
113 calls.c:emit_library_call_value_1 uses it to set up
114 post-instantiation libcalls. */
115 int virtuals_instantiated;
117 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
118 static GTY(()) int funcdef_no;
120 /* These variables hold pointers to functions to create and destroy
121 target specific, per-function data structures. */
122 struct machine_function * (*init_machine_status) (void);
124 /* The currently compiled function. */
125 struct function *cfun = 0;
127 /* These arrays record the INSN_UIDs of the prologue and epilogue insns. */
128 static VEC(int,heap) *prologue;
129 static VEC(int,heap) *epilogue;
131 /* Array of INSN_UIDs to hold the INSN_UIDs for each sibcall epilogue
132 in this function. */
133 static VEC(int,heap) *sibcall_epilogue;
135 /* In order to evaluate some expressions, such as function calls returning
136 structures in memory, we need to temporarily allocate stack locations.
137 We record each allocated temporary in the following structure.
139 Associated with each temporary slot is a nesting level. When we pop up
140 one level, all temporaries associated with the previous level are freed.
141 Normally, all temporaries are freed after the execution of the statement
142 in which they were created. However, if we are inside a ({...}) grouping,
143 the result may be in a temporary and hence must be preserved. If the
144 result could be in a temporary, we preserve it if we can determine which
145 one it is in. If we cannot determine which temporary may contain the
146 result, all temporaries are preserved. A temporary is preserved by
147 pretending it was allocated at the previous nesting level.
149 Automatic variables are also assigned temporary slots, at the nesting
150 level where they are defined. They are marked a "kept" so that
151 free_temp_slots will not free them. */
153 struct temp_slot GTY(())
155 /* Points to next temporary slot. */
156 struct temp_slot *next;
157 /* Points to previous temporary slot. */
158 struct temp_slot *prev;
160 /* The rtx to used to reference the slot. */
161 rtx slot;
162 /* The rtx used to represent the address if not the address of the
163 slot above. May be an EXPR_LIST if multiple addresses exist. */
164 rtx address;
165 /* The alignment (in bits) of the slot. */
166 unsigned int align;
167 /* The size, in units, of the slot. */
168 HOST_WIDE_INT size;
169 /* The type of the object in the slot, or zero if it doesn't correspond
170 to a type. We use this to determine whether a slot can be reused.
171 It can be reused if objects of the type of the new slot will always
172 conflict with objects of the type of the old slot. */
173 tree type;
174 /* Nonzero if this temporary is currently in use. */
175 char in_use;
176 /* Nonzero if this temporary has its address taken. */
177 char addr_taken;
178 /* Nesting level at which this slot is being used. */
179 int level;
180 /* Nonzero if this should survive a call to free_temp_slots. */
181 int keep;
182 /* The offset of the slot from the frame_pointer, including extra space
183 for alignment. This info is for combine_temp_slots. */
184 HOST_WIDE_INT base_offset;
185 /* The size of the slot, including extra space for alignment. This
186 info is for combine_temp_slots. */
187 HOST_WIDE_INT full_size;
190 /* Forward declarations. */
192 static rtx assign_stack_local_1 (enum machine_mode, HOST_WIDE_INT, int,
193 struct function *);
194 static struct temp_slot *find_temp_slot_from_address (rtx);
195 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
196 static void pad_below (struct args_size *, enum machine_mode, tree);
197 static void reorder_blocks_1 (rtx, tree, VEC(tree,heap) **);
198 static int all_blocks (tree, tree *);
199 static tree *get_block_vector (tree, int *);
200 extern tree debug_find_var_in_block_tree (tree, tree);
201 /* We always define `record_insns' even if it's not used so that we
202 can always export `prologue_epilogue_contains'. */
203 static void record_insns (rtx, VEC(int,heap) **) ATTRIBUTE_UNUSED;
204 static int contains (rtx, VEC(int,heap) **);
205 #ifdef HAVE_return
206 static void emit_return_into_block (basic_block);
207 #endif
208 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
209 static rtx keep_stack_depressed (rtx);
210 #endif
211 static void prepare_function_start (tree);
212 static void do_clobber_return_reg (rtx, void *);
213 static void do_use_return_reg (rtx, void *);
214 static void set_insn_locators (rtx, int) ATTRIBUTE_UNUSED;
216 /* Pointer to chain of `struct function' for containing functions. */
217 struct function *outer_function_chain;
219 /* Given a function decl for a containing function,
220 return the `struct function' for it. */
222 struct function *
223 find_function_data (tree decl)
225 struct function *p;
227 for (p = outer_function_chain; p; p = p->outer)
228 if (p->decl == decl)
229 return p;
231 gcc_unreachable ();
234 /* Save the current context for compilation of a nested function.
235 This is called from language-specific code. The caller should use
236 the enter_nested langhook to save any language-specific state,
237 since this function knows only about language-independent
238 variables. */
240 void
241 push_function_context_to (tree context ATTRIBUTE_UNUSED)
243 struct function *p;
245 if (cfun == 0)
246 init_dummy_function_start ();
247 p = cfun;
249 p->outer = outer_function_chain;
250 outer_function_chain = p;
252 lang_hooks.function.enter_nested (p);
254 cfun = 0;
257 void
258 push_function_context (void)
260 push_function_context_to (current_function_decl);
263 /* Restore the last saved context, at the end of a nested function.
264 This function is called from language-specific code. */
266 void
267 pop_function_context_from (tree context ATTRIBUTE_UNUSED)
269 struct function *p = outer_function_chain;
271 cfun = p;
272 outer_function_chain = p->outer;
274 current_function_decl = p->decl;
276 lang_hooks.function.leave_nested (p);
278 /* Reset variables that have known state during rtx generation. */
279 virtuals_instantiated = 0;
280 generating_concat_p = 1;
283 void
284 pop_function_context (void)
286 pop_function_context_from (current_function_decl);
289 /* Clear out all parts of the state in F that can safely be discarded
290 after the function has been parsed, but not compiled, to let
291 garbage collection reclaim the memory. */
293 void
294 free_after_parsing (struct function *f)
296 /* f->expr->forced_labels is used by code generation. */
297 /* f->emit->regno_reg_rtx is used by code generation. */
298 /* f->varasm is used by code generation. */
299 /* f->eh->eh_return_stub_label is used by code generation. */
301 lang_hooks.function.final (f);
304 /* Clear out all parts of the state in F that can safely be discarded
305 after the function has been compiled, to let garbage collection
306 reclaim the memory. */
308 void
309 free_after_compilation (struct function *f)
311 VEC_free (int, heap, prologue);
312 VEC_free (int, heap, epilogue);
313 VEC_free (int, heap, sibcall_epilogue);
315 f->eh = NULL;
316 f->expr = NULL;
317 f->emit = NULL;
318 f->varasm = NULL;
319 f->machine = NULL;
320 f->cfg = NULL;
322 f->x_avail_temp_slots = NULL;
323 f->x_used_temp_slots = NULL;
324 f->arg_offset_rtx = NULL;
325 f->return_rtx = NULL;
326 f->internal_arg_pointer = NULL;
327 f->x_nonlocal_goto_handler_labels = NULL;
328 f->x_return_label = NULL;
329 f->x_naked_return_label = NULL;
330 f->x_stack_slot_list = NULL;
331 f->x_stack_check_probe_note = NULL;
332 f->x_arg_pointer_save_area = NULL;
333 f->x_parm_birth_insn = NULL;
334 f->epilogue_delay_list = NULL;
337 /* Allocate fixed slots in the stack frame of the current function. */
339 /* Return size needed for stack frame based on slots so far allocated in
340 function F.
341 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
342 the caller may have to do that. */
344 static HOST_WIDE_INT
345 get_func_frame_size (struct function *f)
347 if (FRAME_GROWS_DOWNWARD)
348 return -f->x_frame_offset;
349 else
350 return f->x_frame_offset;
353 /* Return size needed for stack frame based on slots so far allocated.
354 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
355 the caller may have to do that. */
357 HOST_WIDE_INT
358 get_frame_size (void)
360 return get_func_frame_size (cfun);
363 /* Issue an error message and return TRUE if frame OFFSET overflows in
364 the signed target pointer arithmetics for function FUNC. Otherwise
365 return FALSE. */
367 bool
368 frame_offset_overflow (HOST_WIDE_INT offset, tree func)
370 unsigned HOST_WIDE_INT size = FRAME_GROWS_DOWNWARD ? -offset : offset;
372 if (size > ((unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (Pmode) - 1))
373 /* Leave room for the fixed part of the frame. */
374 - 64 * UNITS_PER_WORD)
376 error ("%Jtotal size of local objects too large", func);
377 return TRUE;
380 return FALSE;
383 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
384 with machine mode MODE.
386 ALIGN controls the amount of alignment for the address of the slot:
387 0 means according to MODE,
388 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
389 -2 means use BITS_PER_UNIT,
390 positive specifies alignment boundary in bits.
392 We do not round to stack_boundary here.
394 FUNCTION specifies the function to allocate in. */
396 static rtx
397 assign_stack_local_1 (enum machine_mode mode, HOST_WIDE_INT size, int align,
398 struct function *function)
400 rtx x, addr;
401 int bigend_correction = 0;
402 unsigned int alignment;
403 int frame_off, frame_alignment, frame_phase;
405 if (align == 0)
407 tree type;
409 if (mode == BLKmode)
410 alignment = BIGGEST_ALIGNMENT;
411 else
412 alignment = GET_MODE_ALIGNMENT (mode);
414 /* Allow the target to (possibly) increase the alignment of this
415 stack slot. */
416 type = lang_hooks.types.type_for_mode (mode, 0);
417 if (type)
418 alignment = LOCAL_ALIGNMENT (type, alignment);
420 alignment /= BITS_PER_UNIT;
422 else if (align == -1)
424 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
425 size = CEIL_ROUND (size, alignment);
427 else if (align == -2)
428 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
429 else
430 alignment = align / BITS_PER_UNIT;
432 if (FRAME_GROWS_DOWNWARD)
433 function->x_frame_offset -= size;
435 /* Ignore alignment we can't do with expected alignment of the boundary. */
436 if (alignment * BITS_PER_UNIT > PREFERRED_STACK_BOUNDARY)
437 alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
439 if (function->stack_alignment_needed < alignment * BITS_PER_UNIT)
440 function->stack_alignment_needed = alignment * BITS_PER_UNIT;
442 /* Calculate how many bytes the start of local variables is off from
443 stack alignment. */
444 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
445 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
446 frame_phase = frame_off ? frame_alignment - frame_off : 0;
448 /* Round the frame offset to the specified alignment. The default is
449 to always honor requests to align the stack but a port may choose to
450 do its own stack alignment by defining STACK_ALIGNMENT_NEEDED. */
451 if (STACK_ALIGNMENT_NEEDED
452 || mode != BLKmode
453 || size != 0)
455 /* We must be careful here, since FRAME_OFFSET might be negative and
456 division with a negative dividend isn't as well defined as we might
457 like. So we instead assume that ALIGNMENT is a power of two and
458 use logical operations which are unambiguous. */
459 if (FRAME_GROWS_DOWNWARD)
460 function->x_frame_offset
461 = (FLOOR_ROUND (function->x_frame_offset - frame_phase,
462 (unsigned HOST_WIDE_INT) alignment)
463 + frame_phase);
464 else
465 function->x_frame_offset
466 = (CEIL_ROUND (function->x_frame_offset - frame_phase,
467 (unsigned HOST_WIDE_INT) alignment)
468 + frame_phase);
471 /* On a big-endian machine, if we are allocating more space than we will use,
472 use the least significant bytes of those that are allocated. */
473 if (BYTES_BIG_ENDIAN && mode != BLKmode && GET_MODE_SIZE (mode) < size)
474 bigend_correction = size - GET_MODE_SIZE (mode);
476 /* If we have already instantiated virtual registers, return the actual
477 address relative to the frame pointer. */
478 if (function == cfun && virtuals_instantiated)
479 addr = plus_constant (frame_pointer_rtx,
480 trunc_int_for_mode
481 (frame_offset + bigend_correction
482 + STARTING_FRAME_OFFSET, Pmode));
483 else
484 addr = plus_constant (virtual_stack_vars_rtx,
485 trunc_int_for_mode
486 (function->x_frame_offset + bigend_correction,
487 Pmode));
489 if (!FRAME_GROWS_DOWNWARD)
490 function->x_frame_offset += size;
492 x = gen_rtx_MEM (mode, addr);
493 MEM_NOTRAP_P (x) = 1;
495 function->x_stack_slot_list
496 = gen_rtx_EXPR_LIST (VOIDmode, x, function->x_stack_slot_list);
498 if (frame_offset_overflow (function->x_frame_offset, function->decl))
499 function->x_frame_offset = 0;
501 return x;
504 /* Wrapper around assign_stack_local_1; assign a local stack slot for the
505 current function. */
508 assign_stack_local (enum machine_mode mode, HOST_WIDE_INT size, int align)
510 return assign_stack_local_1 (mode, size, align, cfun);
514 /* Removes temporary slot TEMP from LIST. */
516 static void
517 cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
519 if (temp->next)
520 temp->next->prev = temp->prev;
521 if (temp->prev)
522 temp->prev->next = temp->next;
523 else
524 *list = temp->next;
526 temp->prev = temp->next = NULL;
529 /* Inserts temporary slot TEMP to LIST. */
531 static void
532 insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
534 temp->next = *list;
535 if (*list)
536 (*list)->prev = temp;
537 temp->prev = NULL;
538 *list = temp;
541 /* Returns the list of used temp slots at LEVEL. */
543 static struct temp_slot **
544 temp_slots_at_level (int level)
546 if (level >= (int) VEC_length (temp_slot_p, used_temp_slots))
548 size_t old_length = VEC_length (temp_slot_p, used_temp_slots);
549 temp_slot_p *p;
551 VEC_safe_grow (temp_slot_p, gc, used_temp_slots, level + 1);
552 p = VEC_address (temp_slot_p, used_temp_slots);
553 memset (&p[old_length], 0,
554 sizeof (temp_slot_p) * (level + 1 - old_length));
557 return &(VEC_address (temp_slot_p, used_temp_slots)[level]);
560 /* Returns the maximal temporary slot level. */
562 static int
563 max_slot_level (void)
565 if (!used_temp_slots)
566 return -1;
568 return VEC_length (temp_slot_p, used_temp_slots) - 1;
571 /* Moves temporary slot TEMP to LEVEL. */
573 static void
574 move_slot_to_level (struct temp_slot *temp, int level)
576 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
577 insert_slot_to_list (temp, temp_slots_at_level (level));
578 temp->level = level;
581 /* Make temporary slot TEMP available. */
583 static void
584 make_slot_available (struct temp_slot *temp)
586 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
587 insert_slot_to_list (temp, &avail_temp_slots);
588 temp->in_use = 0;
589 temp->level = -1;
592 /* Allocate a temporary stack slot and record it for possible later
593 reuse.
595 MODE is the machine mode to be given to the returned rtx.
597 SIZE is the size in units of the space required. We do no rounding here
598 since assign_stack_local will do any required rounding.
600 KEEP is 1 if this slot is to be retained after a call to
601 free_temp_slots. Automatic variables for a block are allocated
602 with this flag. KEEP values of 2 or 3 were needed respectively
603 for variables whose lifetime is controlled by CLEANUP_POINT_EXPRs
604 or for SAVE_EXPRs, but they are now unused.
606 TYPE is the type that will be used for the stack slot. */
609 assign_stack_temp_for_type (enum machine_mode mode, HOST_WIDE_INT size,
610 int keep, tree type)
612 unsigned int align;
613 struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
614 rtx slot;
616 /* If SIZE is -1 it means that somebody tried to allocate a temporary
617 of a variable size. */
618 gcc_assert (size != -1);
620 /* These are now unused. */
621 gcc_assert (keep <= 1);
623 if (mode == BLKmode)
624 align = BIGGEST_ALIGNMENT;
625 else
626 align = GET_MODE_ALIGNMENT (mode);
628 if (! type)
629 type = lang_hooks.types.type_for_mode (mode, 0);
631 if (type)
632 align = LOCAL_ALIGNMENT (type, align);
634 /* Try to find an available, already-allocated temporary of the proper
635 mode which meets the size and alignment requirements. Choose the
636 smallest one with the closest alignment.
638 If assign_stack_temp is called outside of the tree->rtl expansion,
639 we cannot reuse the stack slots (that may still refer to
640 VIRTUAL_STACK_VARS_REGNUM). */
641 if (!virtuals_instantiated)
643 for (p = avail_temp_slots; p; p = p->next)
645 if (p->align >= align && p->size >= size
646 && GET_MODE (p->slot) == mode
647 && objects_must_conflict_p (p->type, type)
648 && (best_p == 0 || best_p->size > p->size
649 || (best_p->size == p->size && best_p->align > p->align)))
651 if (p->align == align && p->size == size)
653 selected = p;
654 cut_slot_from_list (selected, &avail_temp_slots);
655 best_p = 0;
656 break;
658 best_p = p;
663 /* Make our best, if any, the one to use. */
664 if (best_p)
666 selected = best_p;
667 cut_slot_from_list (selected, &avail_temp_slots);
669 /* If there are enough aligned bytes left over, make them into a new
670 temp_slot so that the extra bytes don't get wasted. Do this only
671 for BLKmode slots, so that we can be sure of the alignment. */
672 if (GET_MODE (best_p->slot) == BLKmode)
674 int alignment = best_p->align / BITS_PER_UNIT;
675 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
677 if (best_p->size - rounded_size >= alignment)
679 p = ggc_alloc (sizeof (struct temp_slot));
680 p->in_use = p->addr_taken = 0;
681 p->size = best_p->size - rounded_size;
682 p->base_offset = best_p->base_offset + rounded_size;
683 p->full_size = best_p->full_size - rounded_size;
684 p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
685 p->align = best_p->align;
686 p->address = 0;
687 p->type = best_p->type;
688 insert_slot_to_list (p, &avail_temp_slots);
690 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
691 stack_slot_list);
693 best_p->size = rounded_size;
694 best_p->full_size = rounded_size;
699 /* If we still didn't find one, make a new temporary. */
700 if (selected == 0)
702 HOST_WIDE_INT frame_offset_old = frame_offset;
704 p = ggc_alloc (sizeof (struct temp_slot));
706 /* We are passing an explicit alignment request to assign_stack_local.
707 One side effect of that is assign_stack_local will not round SIZE
708 to ensure the frame offset remains suitably aligned.
710 So for requests which depended on the rounding of SIZE, we go ahead
711 and round it now. We also make sure ALIGNMENT is at least
712 BIGGEST_ALIGNMENT. */
713 gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
714 p->slot = assign_stack_local (mode,
715 (mode == BLKmode
716 ? CEIL_ROUND (size, (int) align / BITS_PER_UNIT)
717 : size),
718 align);
720 p->align = align;
722 /* The following slot size computation is necessary because we don't
723 know the actual size of the temporary slot until assign_stack_local
724 has performed all the frame alignment and size rounding for the
725 requested temporary. Note that extra space added for alignment
726 can be either above or below this stack slot depending on which
727 way the frame grows. We include the extra space if and only if it
728 is above this slot. */
729 if (FRAME_GROWS_DOWNWARD)
730 p->size = frame_offset_old - frame_offset;
731 else
732 p->size = size;
734 /* Now define the fields used by combine_temp_slots. */
735 if (FRAME_GROWS_DOWNWARD)
737 p->base_offset = frame_offset;
738 p->full_size = frame_offset_old - frame_offset;
740 else
742 p->base_offset = frame_offset_old;
743 p->full_size = frame_offset - frame_offset_old;
745 p->address = 0;
747 selected = p;
750 p = selected;
751 p->in_use = 1;
752 p->addr_taken = 0;
753 p->type = type;
754 p->level = temp_slot_level;
755 p->keep = keep;
757 pp = temp_slots_at_level (p->level);
758 insert_slot_to_list (p, pp);
760 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
761 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
762 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
764 /* If we know the alias set for the memory that will be used, use
765 it. If there's no TYPE, then we don't know anything about the
766 alias set for the memory. */
767 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
768 set_mem_align (slot, align);
770 /* If a type is specified, set the relevant flags. */
771 if (type != 0)
773 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
774 MEM_SET_IN_STRUCT_P (slot, AGGREGATE_TYPE_P (type));
776 MEM_NOTRAP_P (slot) = 1;
778 return slot;
781 /* Allocate a temporary stack slot and record it for possible later
782 reuse. First three arguments are same as in preceding function. */
785 assign_stack_temp (enum machine_mode mode, HOST_WIDE_INT size, int keep)
787 return assign_stack_temp_for_type (mode, size, keep, NULL_TREE);
790 /* Assign a temporary.
791 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
792 and so that should be used in error messages. In either case, we
793 allocate of the given type.
794 KEEP is as for assign_stack_temp.
795 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
796 it is 0 if a register is OK.
797 DONT_PROMOTE is 1 if we should not promote values in register
798 to wider modes. */
801 assign_temp (tree type_or_decl, int keep, int memory_required,
802 int dont_promote ATTRIBUTE_UNUSED)
804 tree type, decl;
805 enum machine_mode mode;
806 #ifdef PROMOTE_MODE
807 int unsignedp;
808 #endif
810 if (DECL_P (type_or_decl))
811 decl = type_or_decl, type = TREE_TYPE (decl);
812 else
813 decl = NULL, type = type_or_decl;
815 mode = TYPE_MODE (type);
816 #ifdef PROMOTE_MODE
817 unsignedp = TYPE_UNSIGNED (type);
818 #endif
820 if (mode == BLKmode || memory_required)
822 HOST_WIDE_INT size = int_size_in_bytes (type);
823 rtx tmp;
825 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
826 problems with allocating the stack space. */
827 if (size == 0)
828 size = 1;
830 /* Unfortunately, we don't yet know how to allocate variable-sized
831 temporaries. However, sometimes we can find a fixed upper limit on
832 the size, so try that instead. */
833 else if (size == -1)
834 size = max_int_size_in_bytes (type);
836 /* The size of the temporary may be too large to fit into an integer. */
837 /* ??? Not sure this should happen except for user silliness, so limit
838 this to things that aren't compiler-generated temporaries. The
839 rest of the time we'll die in assign_stack_temp_for_type. */
840 if (decl && size == -1
841 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
843 error ("size of variable %q+D is too large", decl);
844 size = 1;
847 tmp = assign_stack_temp_for_type (mode, size, keep, type);
848 return tmp;
851 #ifdef PROMOTE_MODE
852 if (! dont_promote)
853 mode = promote_mode (type, mode, &unsignedp, 0);
854 #endif
856 return gen_reg_rtx (mode);
859 /* Combine temporary stack slots which are adjacent on the stack.
861 This allows for better use of already allocated stack space. This is only
862 done for BLKmode slots because we can be sure that we won't have alignment
863 problems in this case. */
865 static void
866 combine_temp_slots (void)
868 struct temp_slot *p, *q, *next, *next_q;
869 int num_slots;
871 /* We can't combine slots, because the information about which slot
872 is in which alias set will be lost. */
873 if (flag_strict_aliasing)
874 return;
876 /* If there are a lot of temp slots, don't do anything unless
877 high levels of optimization. */
878 if (! flag_expensive_optimizations)
879 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
880 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
881 return;
883 for (p = avail_temp_slots; p; p = next)
885 int delete_p = 0;
887 next = p->next;
889 if (GET_MODE (p->slot) != BLKmode)
890 continue;
892 for (q = p->next; q; q = next_q)
894 int delete_q = 0;
896 next_q = q->next;
898 if (GET_MODE (q->slot) != BLKmode)
899 continue;
901 if (p->base_offset + p->full_size == q->base_offset)
903 /* Q comes after P; combine Q into P. */
904 p->size += q->size;
905 p->full_size += q->full_size;
906 delete_q = 1;
908 else if (q->base_offset + q->full_size == p->base_offset)
910 /* P comes after Q; combine P into Q. */
911 q->size += p->size;
912 q->full_size += p->full_size;
913 delete_p = 1;
914 break;
916 if (delete_q)
917 cut_slot_from_list (q, &avail_temp_slots);
920 /* Either delete P or advance past it. */
921 if (delete_p)
922 cut_slot_from_list (p, &avail_temp_slots);
926 /* Find the temp slot corresponding to the object at address X. */
928 static struct temp_slot *
929 find_temp_slot_from_address (rtx x)
931 struct temp_slot *p;
932 rtx next;
933 int i;
935 for (i = max_slot_level (); i >= 0; i--)
936 for (p = *temp_slots_at_level (i); p; p = p->next)
938 if (XEXP (p->slot, 0) == x
939 || p->address == x
940 || (GET_CODE (x) == PLUS
941 && XEXP (x, 0) == virtual_stack_vars_rtx
942 && GET_CODE (XEXP (x, 1)) == CONST_INT
943 && INTVAL (XEXP (x, 1)) >= p->base_offset
944 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size))
945 return p;
947 else if (p->address != 0 && GET_CODE (p->address) == EXPR_LIST)
948 for (next = p->address; next; next = XEXP (next, 1))
949 if (XEXP (next, 0) == x)
950 return p;
953 /* If we have a sum involving a register, see if it points to a temp
954 slot. */
955 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
956 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
957 return p;
958 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
959 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
960 return p;
962 return 0;
965 /* Indicate that NEW is an alternate way of referring to the temp slot
966 that previously was known by OLD. */
968 void
969 update_temp_slot_address (rtx old, rtx new)
971 struct temp_slot *p;
973 if (rtx_equal_p (old, new))
974 return;
976 p = find_temp_slot_from_address (old);
978 /* If we didn't find one, see if both OLD is a PLUS. If so, and NEW
979 is a register, see if one operand of the PLUS is a temporary
980 location. If so, NEW points into it. Otherwise, if both OLD and
981 NEW are a PLUS and if there is a register in common between them.
982 If so, try a recursive call on those values. */
983 if (p == 0)
985 if (GET_CODE (old) != PLUS)
986 return;
988 if (REG_P (new))
990 update_temp_slot_address (XEXP (old, 0), new);
991 update_temp_slot_address (XEXP (old, 1), new);
992 return;
994 else if (GET_CODE (new) != PLUS)
995 return;
997 if (rtx_equal_p (XEXP (old, 0), XEXP (new, 0)))
998 update_temp_slot_address (XEXP (old, 1), XEXP (new, 1));
999 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 0)))
1000 update_temp_slot_address (XEXP (old, 0), XEXP (new, 1));
1001 else if (rtx_equal_p (XEXP (old, 0), XEXP (new, 1)))
1002 update_temp_slot_address (XEXP (old, 1), XEXP (new, 0));
1003 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 1)))
1004 update_temp_slot_address (XEXP (old, 0), XEXP (new, 0));
1006 return;
1009 /* Otherwise add an alias for the temp's address. */
1010 else if (p->address == 0)
1011 p->address = new;
1012 else
1014 if (GET_CODE (p->address) != EXPR_LIST)
1015 p->address = gen_rtx_EXPR_LIST (VOIDmode, p->address, NULL_RTX);
1017 p->address = gen_rtx_EXPR_LIST (VOIDmode, new, p->address);
1021 /* If X could be a reference to a temporary slot, mark the fact that its
1022 address was taken. */
1024 void
1025 mark_temp_addr_taken (rtx x)
1027 struct temp_slot *p;
1029 if (x == 0)
1030 return;
1032 /* If X is not in memory or is at a constant address, it cannot be in
1033 a temporary slot. */
1034 if (!MEM_P (x) || CONSTANT_P (XEXP (x, 0)))
1035 return;
1037 p = find_temp_slot_from_address (XEXP (x, 0));
1038 if (p != 0)
1039 p->addr_taken = 1;
1042 /* If X could be a reference to a temporary slot, mark that slot as
1043 belonging to the to one level higher than the current level. If X
1044 matched one of our slots, just mark that one. Otherwise, we can't
1045 easily predict which it is, so upgrade all of them. Kept slots
1046 need not be touched.
1048 This is called when an ({...}) construct occurs and a statement
1049 returns a value in memory. */
1051 void
1052 preserve_temp_slots (rtx x)
1054 struct temp_slot *p = 0, *next;
1056 /* If there is no result, we still might have some objects whose address
1057 were taken, so we need to make sure they stay around. */
1058 if (x == 0)
1060 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1062 next = p->next;
1064 if (p->addr_taken)
1065 move_slot_to_level (p, temp_slot_level - 1);
1068 return;
1071 /* If X is a register that is being used as a pointer, see if we have
1072 a temporary slot we know it points to. To be consistent with
1073 the code below, we really should preserve all non-kept slots
1074 if we can't find a match, but that seems to be much too costly. */
1075 if (REG_P (x) && REG_POINTER (x))
1076 p = find_temp_slot_from_address (x);
1078 /* If X is not in memory or is at a constant address, it cannot be in
1079 a temporary slot, but it can contain something whose address was
1080 taken. */
1081 if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1083 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1085 next = p->next;
1087 if (p->addr_taken)
1088 move_slot_to_level (p, temp_slot_level - 1);
1091 return;
1094 /* First see if we can find a match. */
1095 if (p == 0)
1096 p = find_temp_slot_from_address (XEXP (x, 0));
1098 if (p != 0)
1100 /* Move everything at our level whose address was taken to our new
1101 level in case we used its address. */
1102 struct temp_slot *q;
1104 if (p->level == temp_slot_level)
1106 for (q = *temp_slots_at_level (temp_slot_level); q; q = next)
1108 next = q->next;
1110 if (p != q && q->addr_taken)
1111 move_slot_to_level (q, temp_slot_level - 1);
1114 move_slot_to_level (p, temp_slot_level - 1);
1115 p->addr_taken = 0;
1117 return;
1120 /* Otherwise, preserve all non-kept slots at this level. */
1121 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1123 next = p->next;
1125 if (!p->keep)
1126 move_slot_to_level (p, temp_slot_level - 1);
1130 /* Free all temporaries used so far. This is normally called at the
1131 end of generating code for a statement. */
1133 void
1134 free_temp_slots (void)
1136 struct temp_slot *p, *next;
1138 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1140 next = p->next;
1142 if (!p->keep)
1143 make_slot_available (p);
1146 combine_temp_slots ();
1149 /* Push deeper into the nesting level for stack temporaries. */
1151 void
1152 push_temp_slots (void)
1154 temp_slot_level++;
1157 /* Pop a temporary nesting level. All slots in use in the current level
1158 are freed. */
1160 void
1161 pop_temp_slots (void)
1163 struct temp_slot *p, *next;
1165 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1167 next = p->next;
1168 make_slot_available (p);
1171 combine_temp_slots ();
1173 temp_slot_level--;
1176 /* Initialize temporary slots. */
1178 void
1179 init_temp_slots (void)
1181 /* We have not allocated any temporaries yet. */
1182 avail_temp_slots = 0;
1183 used_temp_slots = 0;
1184 temp_slot_level = 0;
1187 /* These routines are responsible for converting virtual register references
1188 to the actual hard register references once RTL generation is complete.
1190 The following four variables are used for communication between the
1191 routines. They contain the offsets of the virtual registers from their
1192 respective hard registers. */
1194 static int in_arg_offset;
1195 static int var_offset;
1196 static int dynamic_offset;
1197 static int out_arg_offset;
1198 static int cfa_offset;
1200 /* In most machines, the stack pointer register is equivalent to the bottom
1201 of the stack. */
1203 #ifndef STACK_POINTER_OFFSET
1204 #define STACK_POINTER_OFFSET 0
1205 #endif
1207 /* If not defined, pick an appropriate default for the offset of dynamically
1208 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1209 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
1211 #ifndef STACK_DYNAMIC_OFFSET
1213 /* The bottom of the stack points to the actual arguments. If
1214 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1215 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1216 stack space for register parameters is not pushed by the caller, but
1217 rather part of the fixed stack areas and hence not included in
1218 `current_function_outgoing_args_size'. Nevertheless, we must allow
1219 for it when allocating stack dynamic objects. */
1221 #if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
1222 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1223 ((ACCUMULATE_OUTGOING_ARGS \
1224 ? (current_function_outgoing_args_size + REG_PARM_STACK_SPACE (FNDECL)) : 0)\
1225 + (STACK_POINTER_OFFSET)) \
1227 #else
1228 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1229 ((ACCUMULATE_OUTGOING_ARGS ? current_function_outgoing_args_size : 0) \
1230 + (STACK_POINTER_OFFSET))
1231 #endif
1232 #endif
1235 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1236 is a virtual register, return the equivalent hard register and set the
1237 offset indirectly through the pointer. Otherwise, return 0. */
1239 static rtx
1240 instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
1242 rtx new;
1243 HOST_WIDE_INT offset;
1245 if (x == virtual_incoming_args_rtx)
1246 new = arg_pointer_rtx, offset = in_arg_offset;
1247 else if (x == virtual_stack_vars_rtx)
1248 new = frame_pointer_rtx, offset = var_offset;
1249 else if (x == virtual_stack_dynamic_rtx)
1250 new = stack_pointer_rtx, offset = dynamic_offset;
1251 else if (x == virtual_outgoing_args_rtx)
1252 new = stack_pointer_rtx, offset = out_arg_offset;
1253 else if (x == virtual_cfa_rtx)
1255 #ifdef FRAME_POINTER_CFA_OFFSET
1256 new = frame_pointer_rtx;
1257 #else
1258 new = arg_pointer_rtx;
1259 #endif
1260 offset = cfa_offset;
1262 else
1263 return NULL_RTX;
1265 *poffset = offset;
1266 return new;
1269 /* A subroutine of instantiate_virtual_regs, called via for_each_rtx.
1270 Instantiate any virtual registers present inside of *LOC. The expression
1271 is simplified, as much as possible, but is not to be considered "valid"
1272 in any sense implied by the target. If any change is made, set CHANGED
1273 to true. */
1275 static int
1276 instantiate_virtual_regs_in_rtx (rtx *loc, void *data)
1278 HOST_WIDE_INT offset;
1279 bool *changed = (bool *) data;
1280 rtx x, new;
1282 x = *loc;
1283 if (x == 0)
1284 return 0;
1286 switch (GET_CODE (x))
1288 case REG:
1289 new = instantiate_new_reg (x, &offset);
1290 if (new)
1292 *loc = plus_constant (new, offset);
1293 if (changed)
1294 *changed = true;
1296 return -1;
1298 case PLUS:
1299 new = instantiate_new_reg (XEXP (x, 0), &offset);
1300 if (new)
1302 new = plus_constant (new, offset);
1303 *loc = simplify_gen_binary (PLUS, GET_MODE (x), new, XEXP (x, 1));
1304 if (changed)
1305 *changed = true;
1306 return -1;
1309 /* FIXME -- from old code */
1310 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1311 we can commute the PLUS and SUBREG because pointers into the
1312 frame are well-behaved. */
1313 break;
1315 default:
1316 break;
1319 return 0;
1322 /* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1323 matches the predicate for insn CODE operand OPERAND. */
1325 static int
1326 safe_insn_predicate (int code, int operand, rtx x)
1328 const struct insn_operand_data *op_data;
1330 if (code < 0)
1331 return true;
1333 op_data = &insn_data[code].operand[operand];
1334 if (op_data->predicate == NULL)
1335 return true;
1337 return op_data->predicate (x, op_data->mode);
1340 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1341 registers present inside of insn. The result will be a valid insn. */
1343 static void
1344 instantiate_virtual_regs_in_insn (rtx insn)
1346 HOST_WIDE_INT offset;
1347 int insn_code, i;
1348 bool any_change = false;
1349 rtx set, new, x, seq;
1351 /* There are some special cases to be handled first. */
1352 set = single_set (insn);
1353 if (set)
1355 /* We're allowed to assign to a virtual register. This is interpreted
1356 to mean that the underlying register gets assigned the inverse
1357 transformation. This is used, for example, in the handling of
1358 non-local gotos. */
1359 new = instantiate_new_reg (SET_DEST (set), &offset);
1360 if (new)
1362 start_sequence ();
1364 for_each_rtx (&SET_SRC (set), instantiate_virtual_regs_in_rtx, NULL);
1365 x = simplify_gen_binary (PLUS, GET_MODE (new), SET_SRC (set),
1366 GEN_INT (-offset));
1367 x = force_operand (x, new);
1368 if (x != new)
1369 emit_move_insn (new, x);
1371 seq = get_insns ();
1372 end_sequence ();
1374 emit_insn_before (seq, insn);
1375 delete_insn (insn);
1376 return;
1379 /* Handle a straight copy from a virtual register by generating a
1380 new add insn. The difference between this and falling through
1381 to the generic case is avoiding a new pseudo and eliminating a
1382 move insn in the initial rtl stream. */
1383 new = instantiate_new_reg (SET_SRC (set), &offset);
1384 if (new && offset != 0
1385 && REG_P (SET_DEST (set))
1386 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1388 start_sequence ();
1390 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS,
1391 new, GEN_INT (offset), SET_DEST (set),
1392 1, OPTAB_LIB_WIDEN);
1393 if (x != SET_DEST (set))
1394 emit_move_insn (SET_DEST (set), x);
1396 seq = get_insns ();
1397 end_sequence ();
1399 emit_insn_before (seq, insn);
1400 delete_insn (insn);
1401 return;
1404 extract_insn (insn);
1405 insn_code = INSN_CODE (insn);
1407 /* Handle a plus involving a virtual register by determining if the
1408 operands remain valid if they're modified in place. */
1409 if (GET_CODE (SET_SRC (set)) == PLUS
1410 && recog_data.n_operands >= 3
1411 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1412 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1413 && GET_CODE (recog_data.operand[2]) == CONST_INT
1414 && (new = instantiate_new_reg (recog_data.operand[1], &offset)))
1416 offset += INTVAL (recog_data.operand[2]);
1418 /* If the sum is zero, then replace with a plain move. */
1419 if (offset == 0
1420 && REG_P (SET_DEST (set))
1421 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1423 start_sequence ();
1424 emit_move_insn (SET_DEST (set), new);
1425 seq = get_insns ();
1426 end_sequence ();
1428 emit_insn_before (seq, insn);
1429 delete_insn (insn);
1430 return;
1433 x = gen_int_mode (offset, recog_data.operand_mode[2]);
1435 /* Using validate_change and apply_change_group here leaves
1436 recog_data in an invalid state. Since we know exactly what
1437 we want to check, do those two by hand. */
1438 if (safe_insn_predicate (insn_code, 1, new)
1439 && safe_insn_predicate (insn_code, 2, x))
1441 *recog_data.operand_loc[1] = recog_data.operand[1] = new;
1442 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1443 any_change = true;
1445 /* Fall through into the regular operand fixup loop in
1446 order to take care of operands other than 1 and 2. */
1450 else
1452 extract_insn (insn);
1453 insn_code = INSN_CODE (insn);
1456 /* In the general case, we expect virtual registers to appear only in
1457 operands, and then only as either bare registers or inside memories. */
1458 for (i = 0; i < recog_data.n_operands; ++i)
1460 x = recog_data.operand[i];
1461 switch (GET_CODE (x))
1463 case MEM:
1465 rtx addr = XEXP (x, 0);
1466 bool changed = false;
1468 for_each_rtx (&addr, instantiate_virtual_regs_in_rtx, &changed);
1469 if (!changed)
1470 continue;
1472 start_sequence ();
1473 x = replace_equiv_address (x, addr);
1474 seq = get_insns ();
1475 end_sequence ();
1476 if (seq)
1477 emit_insn_before (seq, insn);
1479 break;
1481 case REG:
1482 new = instantiate_new_reg (x, &offset);
1483 if (new == NULL)
1484 continue;
1485 if (offset == 0)
1486 x = new;
1487 else
1489 start_sequence ();
1491 /* Careful, special mode predicates may have stuff in
1492 insn_data[insn_code].operand[i].mode that isn't useful
1493 to us for computing a new value. */
1494 /* ??? Recognize address_operand and/or "p" constraints
1495 to see if (plus new offset) is a valid before we put
1496 this through expand_simple_binop. */
1497 x = expand_simple_binop (GET_MODE (x), PLUS, new,
1498 GEN_INT (offset), NULL_RTX,
1499 1, OPTAB_LIB_WIDEN);
1500 seq = get_insns ();
1501 end_sequence ();
1502 emit_insn_before (seq, insn);
1504 break;
1506 case SUBREG:
1507 new = instantiate_new_reg (SUBREG_REG (x), &offset);
1508 if (new == NULL)
1509 continue;
1510 if (offset != 0)
1512 start_sequence ();
1513 new = expand_simple_binop (GET_MODE (new), PLUS, new,
1514 GEN_INT (offset), NULL_RTX,
1515 1, OPTAB_LIB_WIDEN);
1516 seq = get_insns ();
1517 end_sequence ();
1518 emit_insn_before (seq, insn);
1520 x = simplify_gen_subreg (recog_data.operand_mode[i], new,
1521 GET_MODE (new), SUBREG_BYTE (x));
1522 break;
1524 default:
1525 continue;
1528 /* At this point, X contains the new value for the operand.
1529 Validate the new value vs the insn predicate. Note that
1530 asm insns will have insn_code -1 here. */
1531 if (!safe_insn_predicate (insn_code, i, x))
1533 start_sequence ();
1534 x = force_reg (insn_data[insn_code].operand[i].mode, x);
1535 seq = get_insns ();
1536 end_sequence ();
1537 if (seq)
1538 emit_insn_before (seq, insn);
1541 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1542 any_change = true;
1545 if (any_change)
1547 /* Propagate operand changes into the duplicates. */
1548 for (i = 0; i < recog_data.n_dups; ++i)
1549 *recog_data.dup_loc[i]
1550 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1552 /* Force re-recognition of the instruction for validation. */
1553 INSN_CODE (insn) = -1;
1556 if (asm_noperands (PATTERN (insn)) >= 0)
1558 if (!check_asm_operands (PATTERN (insn)))
1560 error_for_asm (insn, "impossible constraint in %<asm%>");
1561 delete_insn (insn);
1564 else
1566 if (recog_memoized (insn) < 0)
1567 fatal_insn_not_found (insn);
1571 /* Subroutine of instantiate_decls. Given RTL representing a decl,
1572 do any instantiation required. */
1574 static void
1575 instantiate_decl (rtx x)
1577 rtx addr;
1579 if (x == 0)
1580 return;
1582 /* If this is a CONCAT, recurse for the pieces. */
1583 if (GET_CODE (x) == CONCAT)
1585 instantiate_decl (XEXP (x, 0));
1586 instantiate_decl (XEXP (x, 1));
1587 return;
1590 /* If this is not a MEM, no need to do anything. Similarly if the
1591 address is a constant or a register that is not a virtual register. */
1592 if (!MEM_P (x))
1593 return;
1595 addr = XEXP (x, 0);
1596 if (CONSTANT_P (addr)
1597 || (REG_P (addr)
1598 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
1599 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
1600 return;
1602 for_each_rtx (&XEXP (x, 0), instantiate_virtual_regs_in_rtx, NULL);
1605 /* Helper for instantiate_decls called via walk_tree: Process all decls
1606 in the given DECL_VALUE_EXPR. */
1608 static tree
1609 instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1611 tree t = *tp;
1612 if (! EXPR_P (t) && ! GIMPLE_STMT_P (t))
1614 *walk_subtrees = 0;
1615 if (DECL_P (t) && DECL_RTL_SET_P (t))
1616 instantiate_decl (DECL_RTL (t));
1618 return NULL;
1621 /* Subroutine of instantiate_decls: Process all decls in the given
1622 BLOCK node and all its subblocks. */
1624 static void
1625 instantiate_decls_1 (tree let)
1627 tree t;
1629 for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
1631 if (DECL_RTL_SET_P (t))
1632 instantiate_decl (DECL_RTL (t));
1633 if (TREE_CODE (t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (t))
1635 tree v = DECL_VALUE_EXPR (t);
1636 walk_tree (&v, instantiate_expr, NULL, NULL);
1640 /* Process all subblocks. */
1641 for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
1642 instantiate_decls_1 (t);
1645 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
1646 all virtual registers in their DECL_RTL's. */
1648 static void
1649 instantiate_decls (tree fndecl)
1651 tree decl;
1653 /* Process all parameters of the function. */
1654 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
1656 instantiate_decl (DECL_RTL (decl));
1657 instantiate_decl (DECL_INCOMING_RTL (decl));
1658 if (DECL_HAS_VALUE_EXPR_P (decl))
1660 tree v = DECL_VALUE_EXPR (decl);
1661 walk_tree (&v, instantiate_expr, NULL, NULL);
1665 /* Now process all variables defined in the function or its subblocks. */
1666 instantiate_decls_1 (DECL_INITIAL (fndecl));
1669 /* Pass through the INSNS of function FNDECL and convert virtual register
1670 references to hard register references. */
1672 static unsigned int
1673 instantiate_virtual_regs (void)
1675 rtx insn;
1677 /* Compute the offsets to use for this function. */
1678 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1679 var_offset = STARTING_FRAME_OFFSET;
1680 dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
1681 out_arg_offset = STACK_POINTER_OFFSET;
1682 #ifdef FRAME_POINTER_CFA_OFFSET
1683 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1684 #else
1685 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1686 #endif
1688 /* Initialize recognition, indicating that volatile is OK. */
1689 init_recog ();
1691 /* Scan through all the insns, instantiating every virtual register still
1692 present. */
1693 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1694 if (INSN_P (insn))
1696 /* These patterns in the instruction stream can never be recognized.
1697 Fortunately, they shouldn't contain virtual registers either. */
1698 if (GET_CODE (PATTERN (insn)) == USE
1699 || GET_CODE (PATTERN (insn)) == CLOBBER
1700 || GET_CODE (PATTERN (insn)) == ADDR_VEC
1701 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
1702 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
1703 continue;
1705 instantiate_virtual_regs_in_insn (insn);
1707 if (INSN_DELETED_P (insn))
1708 continue;
1710 for_each_rtx (&REG_NOTES (insn), instantiate_virtual_regs_in_rtx, NULL);
1712 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
1713 if (GET_CODE (insn) == CALL_INSN)
1714 for_each_rtx (&CALL_INSN_FUNCTION_USAGE (insn),
1715 instantiate_virtual_regs_in_rtx, NULL);
1718 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
1719 instantiate_decls (current_function_decl);
1721 /* Indicate that, from now on, assign_stack_local should use
1722 frame_pointer_rtx. */
1723 virtuals_instantiated = 1;
1724 return 0;
1727 struct tree_opt_pass pass_instantiate_virtual_regs =
1729 "vregs", /* name */
1730 NULL, /* gate */
1731 instantiate_virtual_regs, /* execute */
1732 NULL, /* sub */
1733 NULL, /* next */
1734 0, /* static_pass_number */
1735 0, /* tv_id */
1736 0, /* properties_required */
1737 0, /* properties_provided */
1738 0, /* properties_destroyed */
1739 0, /* todo_flags_start */
1740 TODO_dump_func, /* todo_flags_finish */
1741 0 /* letter */
1745 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
1746 This means a type for which function calls must pass an address to the
1747 function or get an address back from the function.
1748 EXP may be a type node or an expression (whose type is tested). */
1751 aggregate_value_p (tree exp, tree fntype)
1753 int i, regno, nregs;
1754 rtx reg;
1756 tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
1758 /* DECL node associated with FNTYPE when relevant, which we might need to
1759 check for by-invisible-reference returns, typically for CALL_EXPR input
1760 EXPressions. */
1761 tree fndecl = NULL_TREE;
1763 if (fntype)
1764 switch (TREE_CODE (fntype))
1766 case CALL_EXPR:
1767 fndecl = get_callee_fndecl (fntype);
1768 fntype = fndecl ? TREE_TYPE (fndecl) : 0;
1769 break;
1770 case FUNCTION_DECL:
1771 fndecl = fntype;
1772 fntype = TREE_TYPE (fndecl);
1773 break;
1774 case FUNCTION_TYPE:
1775 case METHOD_TYPE:
1776 break;
1777 case IDENTIFIER_NODE:
1778 fntype = 0;
1779 break;
1780 default:
1781 /* We don't expect other rtl types here. */
1782 gcc_unreachable ();
1785 if (TREE_CODE (type) == VOID_TYPE)
1786 return 0;
1788 /* If the front end has decided that this needs to be passed by
1789 reference, do so. */
1790 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
1791 && DECL_BY_REFERENCE (exp))
1792 return 1;
1794 /* If the EXPression is a CALL_EXPR, honor DECL_BY_REFERENCE set on the
1795 called function RESULT_DECL, meaning the function returns in memory by
1796 invisible reference. This check lets front-ends not set TREE_ADDRESSABLE
1797 on the function type, which used to be the way to request such a return
1798 mechanism but might now be causing troubles at gimplification time if
1799 temporaries with the function type need to be created. */
1800 if (TREE_CODE (exp) == CALL_EXPR && fndecl && DECL_RESULT (fndecl)
1801 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
1802 return 1;
1804 if (targetm.calls.return_in_memory (type, fntype))
1805 return 1;
1806 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
1807 and thus can't be returned in registers. */
1808 if (TREE_ADDRESSABLE (type))
1809 return 1;
1810 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
1811 return 1;
1812 /* Make sure we have suitable call-clobbered regs to return
1813 the value in; if not, we must return it in memory. */
1814 reg = hard_function_value (type, 0, fntype, 0);
1816 /* If we have something other than a REG (e.g. a PARALLEL), then assume
1817 it is OK. */
1818 if (!REG_P (reg))
1819 return 0;
1821 regno = REGNO (reg);
1822 nregs = hard_regno_nregs[regno][TYPE_MODE (type)];
1823 for (i = 0; i < nregs; i++)
1824 if (! call_used_regs[regno + i])
1825 return 1;
1826 return 0;
1829 /* Return true if we should assign DECL a pseudo register; false if it
1830 should live on the local stack. */
1832 bool
1833 use_register_for_decl (tree decl)
1835 /* Honor volatile. */
1836 if (TREE_SIDE_EFFECTS (decl))
1837 return false;
1839 /* Honor addressability. */
1840 if (TREE_ADDRESSABLE (decl))
1841 return false;
1843 /* Only register-like things go in registers. */
1844 if (DECL_MODE (decl) == BLKmode)
1845 return false;
1847 /* If -ffloat-store specified, don't put explicit float variables
1848 into registers. */
1849 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
1850 propagates values across these stores, and it probably shouldn't. */
1851 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
1852 return false;
1854 /* If we're not interested in tracking debugging information for
1855 this decl, then we can certainly put it in a register. */
1856 if (DECL_IGNORED_P (decl))
1857 return true;
1859 return (optimize || DECL_REGISTER (decl));
1862 /* Return true if TYPE should be passed by invisible reference. */
1864 bool
1865 pass_by_reference (CUMULATIVE_ARGS *ca, enum machine_mode mode,
1866 tree type, bool named_arg)
1868 if (type)
1870 /* If this type contains non-trivial constructors, then it is
1871 forbidden for the middle-end to create any new copies. */
1872 if (TREE_ADDRESSABLE (type))
1873 return true;
1875 /* GCC post 3.4 passes *all* variable sized types by reference. */
1876 if (!TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1877 return true;
1880 return targetm.calls.pass_by_reference (ca, mode, type, named_arg);
1883 /* Return true if TYPE, which is passed by reference, should be callee
1884 copied instead of caller copied. */
1886 bool
1887 reference_callee_copied (CUMULATIVE_ARGS *ca, enum machine_mode mode,
1888 tree type, bool named_arg)
1890 if (type && TREE_ADDRESSABLE (type))
1891 return false;
1892 return targetm.calls.callee_copies (ca, mode, type, named_arg);
1895 /* Structures to communicate between the subroutines of assign_parms.
1896 The first holds data persistent across all parameters, the second
1897 is cleared out for each parameter. */
1899 struct assign_parm_data_all
1901 CUMULATIVE_ARGS args_so_far;
1902 struct args_size stack_args_size;
1903 tree function_result_decl;
1904 tree orig_fnargs;
1905 rtx conversion_insns;
1906 HOST_WIDE_INT pretend_args_size;
1907 HOST_WIDE_INT extra_pretend_bytes;
1908 int reg_parm_stack_space;
1911 struct assign_parm_data_one
1913 tree nominal_type;
1914 tree passed_type;
1915 rtx entry_parm;
1916 rtx stack_parm;
1917 enum machine_mode nominal_mode;
1918 enum machine_mode passed_mode;
1919 enum machine_mode promoted_mode;
1920 struct locate_and_pad_arg_data locate;
1921 int partial;
1922 BOOL_BITFIELD named_arg : 1;
1923 BOOL_BITFIELD passed_pointer : 1;
1924 BOOL_BITFIELD on_stack : 1;
1925 BOOL_BITFIELD loaded_in_reg : 1;
1928 /* A subroutine of assign_parms. Initialize ALL. */
1930 static void
1931 assign_parms_initialize_all (struct assign_parm_data_all *all)
1933 tree fntype;
1935 memset (all, 0, sizeof (*all));
1937 fntype = TREE_TYPE (current_function_decl);
1939 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
1940 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far, fntype, NULL_RTX);
1941 #else
1942 INIT_CUMULATIVE_ARGS (all->args_so_far, fntype, NULL_RTX,
1943 current_function_decl, -1);
1944 #endif
1946 #ifdef REG_PARM_STACK_SPACE
1947 all->reg_parm_stack_space = REG_PARM_STACK_SPACE (current_function_decl);
1948 #endif
1951 /* If ARGS contains entries with complex types, split the entry into two
1952 entries of the component type. Return a new list of substitutions are
1953 needed, else the old list. */
1955 static tree
1956 split_complex_args (tree args)
1958 tree p;
1960 /* Before allocating memory, check for the common case of no complex. */
1961 for (p = args; p; p = TREE_CHAIN (p))
1963 tree type = TREE_TYPE (p);
1964 if (TREE_CODE (type) == COMPLEX_TYPE
1965 && targetm.calls.split_complex_arg (type))
1966 goto found;
1968 return args;
1970 found:
1971 args = copy_list (args);
1973 for (p = args; p; p = TREE_CHAIN (p))
1975 tree type = TREE_TYPE (p);
1976 if (TREE_CODE (type) == COMPLEX_TYPE
1977 && targetm.calls.split_complex_arg (type))
1979 tree decl;
1980 tree subtype = TREE_TYPE (type);
1981 bool addressable = TREE_ADDRESSABLE (p);
1983 /* Rewrite the PARM_DECL's type with its component. */
1984 TREE_TYPE (p) = subtype;
1985 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
1986 DECL_MODE (p) = VOIDmode;
1987 DECL_SIZE (p) = NULL;
1988 DECL_SIZE_UNIT (p) = NULL;
1989 /* If this arg must go in memory, put it in a pseudo here.
1990 We can't allow it to go in memory as per normal parms,
1991 because the usual place might not have the imag part
1992 adjacent to the real part. */
1993 DECL_ARTIFICIAL (p) = addressable;
1994 DECL_IGNORED_P (p) = addressable;
1995 TREE_ADDRESSABLE (p) = 0;
1996 layout_decl (p, 0);
1998 /* Build a second synthetic decl. */
1999 decl = build_decl (PARM_DECL, NULL_TREE, subtype);
2000 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2001 DECL_ARTIFICIAL (decl) = addressable;
2002 DECL_IGNORED_P (decl) = addressable;
2003 layout_decl (decl, 0);
2005 /* Splice it in; skip the new decl. */
2006 TREE_CHAIN (decl) = TREE_CHAIN (p);
2007 TREE_CHAIN (p) = decl;
2008 p = decl;
2012 return args;
2015 /* A subroutine of assign_parms. Adjust the parameter list to incorporate
2016 the hidden struct return argument, and (abi willing) complex args.
2017 Return the new parameter list. */
2019 static tree
2020 assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2022 tree fndecl = current_function_decl;
2023 tree fntype = TREE_TYPE (fndecl);
2024 tree fnargs = DECL_ARGUMENTS (fndecl);
2026 /* If struct value address is treated as the first argument, make it so. */
2027 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
2028 && ! current_function_returns_pcc_struct
2029 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2031 tree type = build_pointer_type (TREE_TYPE (fntype));
2032 tree decl;
2034 decl = build_decl (PARM_DECL, NULL_TREE, type);
2035 DECL_ARG_TYPE (decl) = type;
2036 DECL_ARTIFICIAL (decl) = 1;
2037 DECL_IGNORED_P (decl) = 1;
2039 TREE_CHAIN (decl) = fnargs;
2040 fnargs = decl;
2041 all->function_result_decl = decl;
2044 all->orig_fnargs = fnargs;
2046 /* If the target wants to split complex arguments into scalars, do so. */
2047 if (targetm.calls.split_complex_arg)
2048 fnargs = split_complex_args (fnargs);
2050 return fnargs;
2053 /* A subroutine of assign_parms. Examine PARM and pull out type and mode
2054 data for the parameter. Incorporate ABI specifics such as pass-by-
2055 reference and type promotion. */
2057 static void
2058 assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2059 struct assign_parm_data_one *data)
2061 tree nominal_type, passed_type;
2062 enum machine_mode nominal_mode, passed_mode, promoted_mode;
2064 memset (data, 0, sizeof (*data));
2066 /* NAMED_ARG is a mis-nomer. We really mean 'non-varadic'. */
2067 if (!current_function_stdarg)
2068 data->named_arg = 1; /* No varadic parms. */
2069 else if (TREE_CHAIN (parm))
2070 data->named_arg = 1; /* Not the last non-varadic parm. */
2071 else if (targetm.calls.strict_argument_naming (&all->args_so_far))
2072 data->named_arg = 1; /* Only varadic ones are unnamed. */
2073 else
2074 data->named_arg = 0; /* Treat as varadic. */
2076 nominal_type = TREE_TYPE (parm);
2077 passed_type = DECL_ARG_TYPE (parm);
2079 /* Look out for errors propagating this far. Also, if the parameter's
2080 type is void then its value doesn't matter. */
2081 if (TREE_TYPE (parm) == error_mark_node
2082 /* This can happen after weird syntax errors
2083 or if an enum type is defined among the parms. */
2084 || TREE_CODE (parm) != PARM_DECL
2085 || passed_type == NULL
2086 || VOID_TYPE_P (nominal_type))
2088 nominal_type = passed_type = void_type_node;
2089 nominal_mode = passed_mode = promoted_mode = VOIDmode;
2090 goto egress;
2093 /* Find mode of arg as it is passed, and mode of arg as it should be
2094 during execution of this function. */
2095 passed_mode = TYPE_MODE (passed_type);
2096 nominal_mode = TYPE_MODE (nominal_type);
2098 /* If the parm is to be passed as a transparent union, use the type of
2099 the first field for the tests below. We have already verified that
2100 the modes are the same. */
2101 if (TREE_CODE (passed_type) == UNION_TYPE
2102 && TYPE_TRANSPARENT_UNION (passed_type))
2103 passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
2105 /* See if this arg was passed by invisible reference. */
2106 if (pass_by_reference (&all->args_so_far, passed_mode,
2107 passed_type, data->named_arg))
2109 passed_type = nominal_type = build_pointer_type (passed_type);
2110 data->passed_pointer = true;
2111 passed_mode = nominal_mode = Pmode;
2114 /* Find mode as it is passed by the ABI. */
2115 promoted_mode = passed_mode;
2116 if (targetm.calls.promote_function_args (TREE_TYPE (current_function_decl)))
2118 int unsignedp = TYPE_UNSIGNED (passed_type);
2119 promoted_mode = promote_mode (passed_type, promoted_mode,
2120 &unsignedp, 1);
2123 egress:
2124 data->nominal_type = nominal_type;
2125 data->passed_type = passed_type;
2126 data->nominal_mode = nominal_mode;
2127 data->passed_mode = passed_mode;
2128 data->promoted_mode = promoted_mode;
2131 /* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
2133 static void
2134 assign_parms_setup_varargs (struct assign_parm_data_all *all,
2135 struct assign_parm_data_one *data, bool no_rtl)
2137 int varargs_pretend_bytes = 0;
2139 targetm.calls.setup_incoming_varargs (&all->args_so_far,
2140 data->promoted_mode,
2141 data->passed_type,
2142 &varargs_pretend_bytes, no_rtl);
2144 /* If the back-end has requested extra stack space, record how much is
2145 needed. Do not change pretend_args_size otherwise since it may be
2146 nonzero from an earlier partial argument. */
2147 if (varargs_pretend_bytes > 0)
2148 all->pretend_args_size = varargs_pretend_bytes;
2151 /* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2152 the incoming location of the current parameter. */
2154 static void
2155 assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2156 struct assign_parm_data_one *data)
2158 HOST_WIDE_INT pretend_bytes = 0;
2159 rtx entry_parm;
2160 bool in_regs;
2162 if (data->promoted_mode == VOIDmode)
2164 data->entry_parm = data->stack_parm = const0_rtx;
2165 return;
2168 #ifdef FUNCTION_INCOMING_ARG
2169 entry_parm = FUNCTION_INCOMING_ARG (all->args_so_far, data->promoted_mode,
2170 data->passed_type, data->named_arg);
2171 #else
2172 entry_parm = FUNCTION_ARG (all->args_so_far, data->promoted_mode,
2173 data->passed_type, data->named_arg);
2174 #endif
2176 if (entry_parm == 0)
2177 data->promoted_mode = data->passed_mode;
2179 /* Determine parm's home in the stack, in case it arrives in the stack
2180 or we should pretend it did. Compute the stack position and rtx where
2181 the argument arrives and its size.
2183 There is one complexity here: If this was a parameter that would
2184 have been passed in registers, but wasn't only because it is
2185 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2186 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2187 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2188 as it was the previous time. */
2189 in_regs = entry_parm != 0;
2190 #ifdef STACK_PARMS_IN_REG_PARM_AREA
2191 in_regs = true;
2192 #endif
2193 if (!in_regs && !data->named_arg)
2195 if (targetm.calls.pretend_outgoing_varargs_named (&all->args_so_far))
2197 rtx tem;
2198 #ifdef FUNCTION_INCOMING_ARG
2199 tem = FUNCTION_INCOMING_ARG (all->args_so_far, data->promoted_mode,
2200 data->passed_type, true);
2201 #else
2202 tem = FUNCTION_ARG (all->args_so_far, data->promoted_mode,
2203 data->passed_type, true);
2204 #endif
2205 in_regs = tem != NULL;
2209 /* If this parameter was passed both in registers and in the stack, use
2210 the copy on the stack. */
2211 if (targetm.calls.must_pass_in_stack (data->promoted_mode,
2212 data->passed_type))
2213 entry_parm = 0;
2215 if (entry_parm)
2217 int partial;
2219 partial = targetm.calls.arg_partial_bytes (&all->args_so_far,
2220 data->promoted_mode,
2221 data->passed_type,
2222 data->named_arg);
2223 data->partial = partial;
2225 /* The caller might already have allocated stack space for the
2226 register parameters. */
2227 if (partial != 0 && all->reg_parm_stack_space == 0)
2229 /* Part of this argument is passed in registers and part
2230 is passed on the stack. Ask the prologue code to extend
2231 the stack part so that we can recreate the full value.
2233 PRETEND_BYTES is the size of the registers we need to store.
2234 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2235 stack space that the prologue should allocate.
2237 Internally, gcc assumes that the argument pointer is aligned
2238 to STACK_BOUNDARY bits. This is used both for alignment
2239 optimizations (see init_emit) and to locate arguments that are
2240 aligned to more than PARM_BOUNDARY bits. We must preserve this
2241 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2242 a stack boundary. */
2244 /* We assume at most one partial arg, and it must be the first
2245 argument on the stack. */
2246 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2248 pretend_bytes = partial;
2249 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2251 /* We want to align relative to the actual stack pointer, so
2252 don't include this in the stack size until later. */
2253 all->extra_pretend_bytes = all->pretend_args_size;
2257 locate_and_pad_parm (data->promoted_mode, data->passed_type, in_regs,
2258 entry_parm ? data->partial : 0, current_function_decl,
2259 &all->stack_args_size, &data->locate);
2261 /* Adjust offsets to include the pretend args. */
2262 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2263 data->locate.slot_offset.constant += pretend_bytes;
2264 data->locate.offset.constant += pretend_bytes;
2266 data->entry_parm = entry_parm;
2269 /* A subroutine of assign_parms. If there is actually space on the stack
2270 for this parm, count it in stack_args_size and return true. */
2272 static bool
2273 assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2274 struct assign_parm_data_one *data)
2276 /* Trivially true if we've no incoming register. */
2277 if (data->entry_parm == NULL)
2279 /* Also true if we're partially in registers and partially not,
2280 since we've arranged to drop the entire argument on the stack. */
2281 else if (data->partial != 0)
2283 /* Also true if the target says that it's passed in both registers
2284 and on the stack. */
2285 else if (GET_CODE (data->entry_parm) == PARALLEL
2286 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2288 /* Also true if the target says that there's stack allocated for
2289 all register parameters. */
2290 else if (all->reg_parm_stack_space > 0)
2292 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2293 else
2294 return false;
2296 all->stack_args_size.constant += data->locate.size.constant;
2297 if (data->locate.size.var)
2298 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2300 return true;
2303 /* A subroutine of assign_parms. Given that this parameter is allocated
2304 stack space by the ABI, find it. */
2306 static void
2307 assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2309 rtx offset_rtx, stack_parm;
2310 unsigned int align, boundary;
2312 /* If we're passing this arg using a reg, make its stack home the
2313 aligned stack slot. */
2314 if (data->entry_parm)
2315 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2316 else
2317 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2319 stack_parm = current_function_internal_arg_pointer;
2320 if (offset_rtx != const0_rtx)
2321 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2322 stack_parm = gen_rtx_MEM (data->promoted_mode, stack_parm);
2324 set_mem_attributes (stack_parm, parm, 1);
2326 boundary = data->locate.boundary;
2327 align = BITS_PER_UNIT;
2329 /* If we're padding upward, we know that the alignment of the slot
2330 is FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2331 intentionally forcing upward padding. Otherwise we have to come
2332 up with a guess at the alignment based on OFFSET_RTX. */
2333 if (data->locate.where_pad != downward || data->entry_parm)
2334 align = boundary;
2335 else if (GET_CODE (offset_rtx) == CONST_INT)
2337 align = INTVAL (offset_rtx) * BITS_PER_UNIT | boundary;
2338 align = align & -align;
2340 set_mem_align (stack_parm, align);
2342 if (data->entry_parm)
2343 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2345 data->stack_parm = stack_parm;
2348 /* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2349 always valid and contiguous. */
2351 static void
2352 assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2354 rtx entry_parm = data->entry_parm;
2355 rtx stack_parm = data->stack_parm;
2357 /* If this parm was passed part in regs and part in memory, pretend it
2358 arrived entirely in memory by pushing the register-part onto the stack.
2359 In the special case of a DImode or DFmode that is split, we could put
2360 it together in a pseudoreg directly, but for now that's not worth
2361 bothering with. */
2362 if (data->partial != 0)
2364 /* Handle calls that pass values in multiple non-contiguous
2365 locations. The Irix 6 ABI has examples of this. */
2366 if (GET_CODE (entry_parm) == PARALLEL)
2367 emit_group_store (validize_mem (stack_parm), entry_parm,
2368 data->passed_type,
2369 int_size_in_bytes (data->passed_type));
2370 else
2372 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2373 move_block_from_reg (REGNO (entry_parm), validize_mem (stack_parm),
2374 data->partial / UNITS_PER_WORD);
2377 entry_parm = stack_parm;
2380 /* If we didn't decide this parm came in a register, by default it came
2381 on the stack. */
2382 else if (entry_parm == NULL)
2383 entry_parm = stack_parm;
2385 /* When an argument is passed in multiple locations, we can't make use
2386 of this information, but we can save some copying if the whole argument
2387 is passed in a single register. */
2388 else if (GET_CODE (entry_parm) == PARALLEL
2389 && data->nominal_mode != BLKmode
2390 && data->passed_mode != BLKmode)
2392 size_t i, len = XVECLEN (entry_parm, 0);
2394 for (i = 0; i < len; i++)
2395 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2396 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2397 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2398 == data->passed_mode)
2399 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2401 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2402 break;
2406 data->entry_parm = entry_parm;
2409 /* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2410 always valid and properly aligned. */
2412 static void
2413 assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2415 rtx stack_parm = data->stack_parm;
2417 /* If we can't trust the parm stack slot to be aligned enough for its
2418 ultimate type, don't use that slot after entry. We'll make another
2419 stack slot, if we need one. */
2420 if (stack_parm
2421 && ((STRICT_ALIGNMENT
2422 && GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm))
2423 || (data->nominal_type
2424 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2425 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2426 stack_parm = NULL;
2428 /* If parm was passed in memory, and we need to convert it on entry,
2429 don't store it back in that same slot. */
2430 else if (data->entry_parm == stack_parm
2431 && data->nominal_mode != BLKmode
2432 && data->nominal_mode != data->passed_mode)
2433 stack_parm = NULL;
2435 /* If stack protection is in effect for this function, don't leave any
2436 pointers in their passed stack slots. */
2437 else if (cfun->stack_protect_guard
2438 && (flag_stack_protect == 2
2439 || data->passed_pointer
2440 || POINTER_TYPE_P (data->nominal_type)))
2441 stack_parm = NULL;
2443 data->stack_parm = stack_parm;
2446 /* A subroutine of assign_parms. Return true if the current parameter
2447 should be stored as a BLKmode in the current frame. */
2449 static bool
2450 assign_parm_setup_block_p (struct assign_parm_data_one *data)
2452 if (data->nominal_mode == BLKmode)
2453 return true;
2454 if (GET_CODE (data->entry_parm) == PARALLEL)
2455 return true;
2457 #ifdef BLOCK_REG_PADDING
2458 /* Only assign_parm_setup_block knows how to deal with register arguments
2459 that are padded at the least significant end. */
2460 if (REG_P (data->entry_parm)
2461 && GET_MODE_SIZE (data->promoted_mode) < UNITS_PER_WORD
2462 && (BLOCK_REG_PADDING (data->passed_mode, data->passed_type, 1)
2463 == (BYTES_BIG_ENDIAN ? upward : downward)))
2464 return true;
2465 #endif
2467 return false;
2470 /* A subroutine of assign_parms. Arrange for the parameter to be
2471 present and valid in DATA->STACK_RTL. */
2473 static void
2474 assign_parm_setup_block (struct assign_parm_data_all *all,
2475 tree parm, struct assign_parm_data_one *data)
2477 rtx entry_parm = data->entry_parm;
2478 rtx stack_parm = data->stack_parm;
2479 HOST_WIDE_INT size;
2480 HOST_WIDE_INT size_stored;
2481 rtx orig_entry_parm = entry_parm;
2483 if (GET_CODE (entry_parm) == PARALLEL)
2484 entry_parm = emit_group_move_into_temps (entry_parm);
2486 /* If we've a non-block object that's nevertheless passed in parts,
2487 reconstitute it in register operations rather than on the stack. */
2488 if (GET_CODE (entry_parm) == PARALLEL
2489 && data->nominal_mode != BLKmode)
2491 rtx elt0 = XEXP (XVECEXP (orig_entry_parm, 0, 0), 0);
2493 if ((XVECLEN (entry_parm, 0) > 1
2494 || hard_regno_nregs[REGNO (elt0)][GET_MODE (elt0)] > 1)
2495 && use_register_for_decl (parm))
2497 rtx parmreg = gen_reg_rtx (data->nominal_mode);
2499 push_to_sequence (all->conversion_insns);
2501 /* For values returned in multiple registers, handle possible
2502 incompatible calls to emit_group_store.
2504 For example, the following would be invalid, and would have to
2505 be fixed by the conditional below:
2507 emit_group_store ((reg:SF), (parallel:DF))
2508 emit_group_store ((reg:SI), (parallel:DI))
2510 An example of this are doubles in e500 v2:
2511 (parallel:DF (expr_list (reg:SI) (const_int 0))
2512 (expr_list (reg:SI) (const_int 4))). */
2513 if (data->nominal_mode != data->passed_mode)
2515 rtx t = gen_reg_rtx (GET_MODE (entry_parm));
2516 emit_group_store (t, entry_parm, NULL_TREE,
2517 GET_MODE_SIZE (GET_MODE (entry_parm)));
2518 convert_move (parmreg, t, 0);
2520 else
2521 emit_group_store (parmreg, entry_parm, data->nominal_type,
2522 int_size_in_bytes (data->nominal_type));
2524 all->conversion_insns = get_insns ();
2525 end_sequence ();
2527 SET_DECL_RTL (parm, parmreg);
2528 return;
2532 size = int_size_in_bytes (data->passed_type);
2533 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2534 if (stack_parm == 0)
2536 DECL_ALIGN (parm) = MAX (DECL_ALIGN (parm), BITS_PER_WORD);
2537 stack_parm = assign_stack_local (BLKmode, size_stored,
2538 DECL_ALIGN (parm));
2539 if (GET_MODE_SIZE (GET_MODE (entry_parm)) == size)
2540 PUT_MODE (stack_parm, GET_MODE (entry_parm));
2541 set_mem_attributes (stack_parm, parm, 1);
2544 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2545 calls that pass values in multiple non-contiguous locations. */
2546 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2548 rtx mem;
2550 /* Note that we will be storing an integral number of words.
2551 So we have to be careful to ensure that we allocate an
2552 integral number of words. We do this above when we call
2553 assign_stack_local if space was not allocated in the argument
2554 list. If it was, this will not work if PARM_BOUNDARY is not
2555 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2556 if it becomes a problem. Exception is when BLKmode arrives
2557 with arguments not conforming to word_mode. */
2559 if (data->stack_parm == 0)
2561 else if (GET_CODE (entry_parm) == PARALLEL)
2563 else
2564 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2566 mem = validize_mem (stack_parm);
2568 /* Handle values in multiple non-contiguous locations. */
2569 if (GET_CODE (entry_parm) == PARALLEL)
2571 push_to_sequence (all->conversion_insns);
2572 emit_group_store (mem, entry_parm, data->passed_type, size);
2573 all->conversion_insns = get_insns ();
2574 end_sequence ();
2577 else if (size == 0)
2580 /* If SIZE is that of a mode no bigger than a word, just use
2581 that mode's store operation. */
2582 else if (size <= UNITS_PER_WORD)
2584 enum machine_mode mode
2585 = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2587 if (mode != BLKmode
2588 #ifdef BLOCK_REG_PADDING
2589 && (size == UNITS_PER_WORD
2590 || (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2591 != (BYTES_BIG_ENDIAN ? upward : downward)))
2592 #endif
2595 rtx reg = gen_rtx_REG (mode, REGNO (entry_parm));
2596 emit_move_insn (change_address (mem, mode, 0), reg);
2599 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
2600 machine must be aligned to the left before storing
2601 to memory. Note that the previous test doesn't
2602 handle all cases (e.g. SIZE == 3). */
2603 else if (size != UNITS_PER_WORD
2604 #ifdef BLOCK_REG_PADDING
2605 && (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2606 == downward)
2607 #else
2608 && BYTES_BIG_ENDIAN
2609 #endif
2612 rtx tem, x;
2613 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2614 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2616 x = expand_shift (LSHIFT_EXPR, word_mode, reg,
2617 build_int_cst (NULL_TREE, by),
2618 NULL_RTX, 1);
2619 tem = change_address (mem, word_mode, 0);
2620 emit_move_insn (tem, x);
2622 else
2623 move_block_from_reg (REGNO (entry_parm), mem,
2624 size_stored / UNITS_PER_WORD);
2626 else
2627 move_block_from_reg (REGNO (entry_parm), mem,
2628 size_stored / UNITS_PER_WORD);
2630 else if (data->stack_parm == 0)
2632 push_to_sequence (all->conversion_insns);
2633 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
2634 BLOCK_OP_NORMAL);
2635 all->conversion_insns = get_insns ();
2636 end_sequence ();
2639 data->stack_parm = stack_parm;
2640 SET_DECL_RTL (parm, stack_parm);
2643 /* A subroutine of assign_parms. Allocate a pseudo to hold the current
2644 parameter. Get it there. Perform all ABI specified conversions. */
2646 static void
2647 assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
2648 struct assign_parm_data_one *data)
2650 rtx parmreg;
2651 enum machine_mode promoted_nominal_mode;
2652 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
2653 bool did_conversion = false;
2655 /* Store the parm in a pseudoregister during the function, but we may
2656 need to do it in a wider mode. */
2658 /* This is not really promoting for a call. However we need to be
2659 consistent with assign_parm_find_data_types and expand_expr_real_1. */
2660 promoted_nominal_mode
2661 = promote_mode (data->nominal_type, data->nominal_mode, &unsignedp, 1);
2663 parmreg = gen_reg_rtx (promoted_nominal_mode);
2665 if (!DECL_ARTIFICIAL (parm))
2666 mark_user_reg (parmreg);
2668 /* If this was an item that we received a pointer to,
2669 set DECL_RTL appropriately. */
2670 if (data->passed_pointer)
2672 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->passed_type)), parmreg);
2673 set_mem_attributes (x, parm, 1);
2674 SET_DECL_RTL (parm, x);
2676 else
2677 SET_DECL_RTL (parm, parmreg);
2679 /* Copy the value into the register. */
2680 if (data->nominal_mode != data->passed_mode
2681 || promoted_nominal_mode != data->promoted_mode)
2683 int save_tree_used;
2685 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
2686 mode, by the caller. We now have to convert it to
2687 NOMINAL_MODE, if different. However, PARMREG may be in
2688 a different mode than NOMINAL_MODE if it is being stored
2689 promoted.
2691 If ENTRY_PARM is a hard register, it might be in a register
2692 not valid for operating in its mode (e.g., an odd-numbered
2693 register for a DFmode). In that case, moves are the only
2694 thing valid, so we can't do a convert from there. This
2695 occurs when the calling sequence allow such misaligned
2696 usages.
2698 In addition, the conversion may involve a call, which could
2699 clobber parameters which haven't been copied to pseudo
2700 registers yet. Therefore, we must first copy the parm to
2701 a pseudo reg here, and save the conversion until after all
2702 parameters have been moved. */
2704 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
2706 emit_move_insn (tempreg, validize_mem (data->entry_parm));
2708 push_to_sequence (all->conversion_insns);
2709 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
2711 if (GET_CODE (tempreg) == SUBREG
2712 && GET_MODE (tempreg) == data->nominal_mode
2713 && REG_P (SUBREG_REG (tempreg))
2714 && data->nominal_mode == data->passed_mode
2715 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm)
2716 && GET_MODE_SIZE (GET_MODE (tempreg))
2717 < GET_MODE_SIZE (GET_MODE (data->entry_parm)))
2719 /* The argument is already sign/zero extended, so note it
2720 into the subreg. */
2721 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
2722 SUBREG_PROMOTED_UNSIGNED_SET (tempreg, unsignedp);
2725 /* TREE_USED gets set erroneously during expand_assignment. */
2726 save_tree_used = TREE_USED (parm);
2727 expand_assignment (parm, make_tree (data->nominal_type, tempreg));
2728 TREE_USED (parm) = save_tree_used;
2729 all->conversion_insns = get_insns ();
2730 end_sequence ();
2732 did_conversion = true;
2734 else
2735 emit_move_insn (parmreg, validize_mem (data->entry_parm));
2737 /* If we were passed a pointer but the actual value can safely live
2738 in a register, put it in one. */
2739 if (data->passed_pointer
2740 && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
2741 /* If by-reference argument was promoted, demote it. */
2742 && (TYPE_MODE (TREE_TYPE (parm)) != GET_MODE (DECL_RTL (parm))
2743 || use_register_for_decl (parm)))
2745 /* We can't use nominal_mode, because it will have been set to
2746 Pmode above. We must use the actual mode of the parm. */
2747 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
2748 mark_user_reg (parmreg);
2750 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
2752 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
2753 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
2755 push_to_sequence (all->conversion_insns);
2756 emit_move_insn (tempreg, DECL_RTL (parm));
2757 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
2758 emit_move_insn (parmreg, tempreg);
2759 all->conversion_insns = get_insns ();
2760 end_sequence ();
2762 did_conversion = true;
2764 else
2765 emit_move_insn (parmreg, DECL_RTL (parm));
2767 SET_DECL_RTL (parm, parmreg);
2769 /* STACK_PARM is the pointer, not the parm, and PARMREG is
2770 now the parm. */
2771 data->stack_parm = NULL;
2774 /* Mark the register as eliminable if we did no conversion and it was
2775 copied from memory at a fixed offset, and the arg pointer was not
2776 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
2777 offset formed an invalid address, such memory-equivalences as we
2778 make here would screw up life analysis for it. */
2779 if (data->nominal_mode == data->passed_mode
2780 && !did_conversion
2781 && data->stack_parm != 0
2782 && MEM_P (data->stack_parm)
2783 && data->locate.offset.var == 0
2784 && reg_mentioned_p (virtual_incoming_args_rtx,
2785 XEXP (data->stack_parm, 0)))
2787 rtx linsn = get_last_insn ();
2788 rtx sinsn, set;
2790 /* Mark complex types separately. */
2791 if (GET_CODE (parmreg) == CONCAT)
2793 enum machine_mode submode
2794 = GET_MODE_INNER (GET_MODE (parmreg));
2795 int regnor = REGNO (XEXP (parmreg, 0));
2796 int regnoi = REGNO (XEXP (parmreg, 1));
2797 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
2798 rtx stacki = adjust_address_nv (data->stack_parm, submode,
2799 GET_MODE_SIZE (submode));
2801 /* Scan backwards for the set of the real and
2802 imaginary parts. */
2803 for (sinsn = linsn; sinsn != 0;
2804 sinsn = prev_nonnote_insn (sinsn))
2806 set = single_set (sinsn);
2807 if (set == 0)
2808 continue;
2810 if (SET_DEST (set) == regno_reg_rtx [regnoi])
2811 REG_NOTES (sinsn)
2812 = gen_rtx_EXPR_LIST (REG_EQUIV, stacki,
2813 REG_NOTES (sinsn));
2814 else if (SET_DEST (set) == regno_reg_rtx [regnor])
2815 REG_NOTES (sinsn)
2816 = gen_rtx_EXPR_LIST (REG_EQUIV, stackr,
2817 REG_NOTES (sinsn));
2820 else if ((set = single_set (linsn)) != 0
2821 && SET_DEST (set) == parmreg)
2822 REG_NOTES (linsn)
2823 = gen_rtx_EXPR_LIST (REG_EQUIV,
2824 data->stack_parm, REG_NOTES (linsn));
2827 /* For pointer data type, suggest pointer register. */
2828 if (POINTER_TYPE_P (TREE_TYPE (parm)))
2829 mark_reg_pointer (parmreg,
2830 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
2833 /* A subroutine of assign_parms. Allocate stack space to hold the current
2834 parameter. Get it there. Perform all ABI specified conversions. */
2836 static void
2837 assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
2838 struct assign_parm_data_one *data)
2840 /* Value must be stored in the stack slot STACK_PARM during function
2841 execution. */
2842 bool to_conversion = false;
2844 if (data->promoted_mode != data->nominal_mode)
2846 /* Conversion is required. */
2847 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
2849 emit_move_insn (tempreg, validize_mem (data->entry_parm));
2851 push_to_sequence (all->conversion_insns);
2852 to_conversion = true;
2854 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
2855 TYPE_UNSIGNED (TREE_TYPE (parm)));
2857 if (data->stack_parm)
2858 /* ??? This may need a big-endian conversion on sparc64. */
2859 data->stack_parm
2860 = adjust_address (data->stack_parm, data->nominal_mode, 0);
2863 if (data->entry_parm != data->stack_parm)
2865 rtx src, dest;
2867 if (data->stack_parm == 0)
2869 data->stack_parm
2870 = assign_stack_local (GET_MODE (data->entry_parm),
2871 GET_MODE_SIZE (GET_MODE (data->entry_parm)),
2872 TYPE_ALIGN (data->passed_type));
2873 set_mem_attributes (data->stack_parm, parm, 1);
2876 dest = validize_mem (data->stack_parm);
2877 src = validize_mem (data->entry_parm);
2879 if (MEM_P (src))
2881 /* Use a block move to handle potentially misaligned entry_parm. */
2882 if (!to_conversion)
2883 push_to_sequence (all->conversion_insns);
2884 to_conversion = true;
2886 emit_block_move (dest, src,
2887 GEN_INT (int_size_in_bytes (data->passed_type)),
2888 BLOCK_OP_NORMAL);
2890 else
2891 emit_move_insn (dest, src);
2894 if (to_conversion)
2896 all->conversion_insns = get_insns ();
2897 end_sequence ();
2900 SET_DECL_RTL (parm, data->stack_parm);
2903 /* A subroutine of assign_parms. If the ABI splits complex arguments, then
2904 undo the frobbing that we did in assign_parms_augmented_arg_list. */
2906 static void
2907 assign_parms_unsplit_complex (struct assign_parm_data_all *all, tree fnargs)
2909 tree parm;
2910 tree orig_fnargs = all->orig_fnargs;
2912 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm))
2914 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
2915 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
2917 rtx tmp, real, imag;
2918 enum machine_mode inner = GET_MODE_INNER (DECL_MODE (parm));
2920 real = DECL_RTL (fnargs);
2921 imag = DECL_RTL (TREE_CHAIN (fnargs));
2922 if (inner != GET_MODE (real))
2924 real = gen_lowpart_SUBREG (inner, real);
2925 imag = gen_lowpart_SUBREG (inner, imag);
2928 if (TREE_ADDRESSABLE (parm))
2930 rtx rmem, imem;
2931 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
2933 /* split_complex_arg put the real and imag parts in
2934 pseudos. Move them to memory. */
2935 tmp = assign_stack_local (DECL_MODE (parm), size,
2936 TYPE_ALIGN (TREE_TYPE (parm)));
2937 set_mem_attributes (tmp, parm, 1);
2938 rmem = adjust_address_nv (tmp, inner, 0);
2939 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
2940 push_to_sequence (all->conversion_insns);
2941 emit_move_insn (rmem, real);
2942 emit_move_insn (imem, imag);
2943 all->conversion_insns = get_insns ();
2944 end_sequence ();
2946 else
2947 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
2948 SET_DECL_RTL (parm, tmp);
2950 real = DECL_INCOMING_RTL (fnargs);
2951 imag = DECL_INCOMING_RTL (TREE_CHAIN (fnargs));
2952 if (inner != GET_MODE (real))
2954 real = gen_lowpart_SUBREG (inner, real);
2955 imag = gen_lowpart_SUBREG (inner, imag);
2957 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
2958 set_decl_incoming_rtl (parm, tmp);
2959 fnargs = TREE_CHAIN (fnargs);
2961 else
2963 SET_DECL_RTL (parm, DECL_RTL (fnargs));
2964 set_decl_incoming_rtl (parm, DECL_INCOMING_RTL (fnargs));
2966 /* Set MEM_EXPR to the original decl, i.e. to PARM,
2967 instead of the copy of decl, i.e. FNARGS. */
2968 if (DECL_INCOMING_RTL (parm) && MEM_P (DECL_INCOMING_RTL (parm)))
2969 set_mem_expr (DECL_INCOMING_RTL (parm), parm);
2972 fnargs = TREE_CHAIN (fnargs);
2976 /* Assign RTL expressions to the function's parameters. This may involve
2977 copying them into registers and using those registers as the DECL_RTL. */
2979 static void
2980 assign_parms (tree fndecl)
2982 struct assign_parm_data_all all;
2983 tree fnargs, parm;
2985 current_function_internal_arg_pointer
2986 = targetm.calls.internal_arg_pointer ();
2988 assign_parms_initialize_all (&all);
2989 fnargs = assign_parms_augmented_arg_list (&all);
2991 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
2993 struct assign_parm_data_one data;
2995 /* Extract the type of PARM; adjust it according to ABI. */
2996 assign_parm_find_data_types (&all, parm, &data);
2998 /* Early out for errors and void parameters. */
2999 if (data.passed_mode == VOIDmode)
3001 SET_DECL_RTL (parm, const0_rtx);
3002 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3003 continue;
3006 if (current_function_stdarg && !TREE_CHAIN (parm))
3007 assign_parms_setup_varargs (&all, &data, false);
3009 /* Find out where the parameter arrives in this function. */
3010 assign_parm_find_entry_rtl (&all, &data);
3012 /* Find out where stack space for this parameter might be. */
3013 if (assign_parm_is_stack_parm (&all, &data))
3015 assign_parm_find_stack_rtl (parm, &data);
3016 assign_parm_adjust_entry_rtl (&data);
3019 /* Record permanently how this parm was passed. */
3020 set_decl_incoming_rtl (parm, data.entry_parm);
3022 /* Update info on where next arg arrives in registers. */
3023 FUNCTION_ARG_ADVANCE (all.args_so_far, data.promoted_mode,
3024 data.passed_type, data.named_arg);
3026 assign_parm_adjust_stack_rtl (&data);
3028 if (assign_parm_setup_block_p (&data))
3029 assign_parm_setup_block (&all, parm, &data);
3030 else if (data.passed_pointer || use_register_for_decl (parm))
3031 assign_parm_setup_reg (&all, parm, &data);
3032 else
3033 assign_parm_setup_stack (&all, parm, &data);
3036 if (targetm.calls.split_complex_arg && fnargs != all.orig_fnargs)
3037 assign_parms_unsplit_complex (&all, fnargs);
3039 /* Output all parameter conversion instructions (possibly including calls)
3040 now that all parameters have been copied out of hard registers. */
3041 emit_insn (all.conversion_insns);
3043 /* If we are receiving a struct value address as the first argument, set up
3044 the RTL for the function result. As this might require code to convert
3045 the transmitted address to Pmode, we do this here to ensure that possible
3046 preliminary conversions of the address have been emitted already. */
3047 if (all.function_result_decl)
3049 tree result = DECL_RESULT (current_function_decl);
3050 rtx addr = DECL_RTL (all.function_result_decl);
3051 rtx x;
3053 if (DECL_BY_REFERENCE (result))
3054 x = addr;
3055 else
3057 addr = convert_memory_address (Pmode, addr);
3058 x = gen_rtx_MEM (DECL_MODE (result), addr);
3059 set_mem_attributes (x, result, 1);
3061 SET_DECL_RTL (result, x);
3064 /* We have aligned all the args, so add space for the pretend args. */
3065 current_function_pretend_args_size = all.pretend_args_size;
3066 all.stack_args_size.constant += all.extra_pretend_bytes;
3067 current_function_args_size = all.stack_args_size.constant;
3069 /* Adjust function incoming argument size for alignment and
3070 minimum length. */
3072 #ifdef REG_PARM_STACK_SPACE
3073 current_function_args_size = MAX (current_function_args_size,
3074 REG_PARM_STACK_SPACE (fndecl));
3075 #endif
3077 current_function_args_size = CEIL_ROUND (current_function_args_size,
3078 PARM_BOUNDARY / BITS_PER_UNIT);
3080 #ifdef ARGS_GROW_DOWNWARD
3081 current_function_arg_offset_rtx
3082 = (all.stack_args_size.var == 0 ? GEN_INT (-all.stack_args_size.constant)
3083 : expand_expr (size_diffop (all.stack_args_size.var,
3084 size_int (-all.stack_args_size.constant)),
3085 NULL_RTX, VOIDmode, 0));
3086 #else
3087 current_function_arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3088 #endif
3090 /* See how many bytes, if any, of its args a function should try to pop
3091 on return. */
3093 current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
3094 current_function_args_size);
3096 /* For stdarg.h function, save info about
3097 regs and stack space used by the named args. */
3099 current_function_args_info = all.args_so_far;
3101 /* Set the rtx used for the function return value. Put this in its
3102 own variable so any optimizers that need this information don't have
3103 to include tree.h. Do this here so it gets done when an inlined
3104 function gets output. */
3106 current_function_return_rtx
3107 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3108 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3110 /* If scalar return value was computed in a pseudo-reg, or was a named
3111 return value that got dumped to the stack, copy that to the hard
3112 return register. */
3113 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3115 tree decl_result = DECL_RESULT (fndecl);
3116 rtx decl_rtl = DECL_RTL (decl_result);
3118 if (REG_P (decl_rtl)
3119 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3120 : DECL_REGISTER (decl_result))
3122 rtx real_decl_rtl;
3124 real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3125 fndecl, true);
3126 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3127 /* The delay slot scheduler assumes that current_function_return_rtx
3128 holds the hard register containing the return value, not a
3129 temporary pseudo. */
3130 current_function_return_rtx = real_decl_rtl;
3135 /* A subroutine of gimplify_parameters, invoked via walk_tree.
3136 For all seen types, gimplify their sizes. */
3138 static tree
3139 gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3141 tree t = *tp;
3143 *walk_subtrees = 0;
3144 if (TYPE_P (t))
3146 if (POINTER_TYPE_P (t))
3147 *walk_subtrees = 1;
3148 else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3149 && !TYPE_SIZES_GIMPLIFIED (t))
3151 gimplify_type_sizes (t, (tree *) data);
3152 *walk_subtrees = 1;
3156 return NULL;
3159 /* Gimplify the parameter list for current_function_decl. This involves
3160 evaluating SAVE_EXPRs of variable sized parameters and generating code
3161 to implement callee-copies reference parameters. Returns a list of
3162 statements to add to the beginning of the function, or NULL if nothing
3163 to do. */
3165 tree
3166 gimplify_parameters (void)
3168 struct assign_parm_data_all all;
3169 tree fnargs, parm, stmts = NULL;
3171 assign_parms_initialize_all (&all);
3172 fnargs = assign_parms_augmented_arg_list (&all);
3174 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
3176 struct assign_parm_data_one data;
3178 /* Extract the type of PARM; adjust it according to ABI. */
3179 assign_parm_find_data_types (&all, parm, &data);
3181 /* Early out for errors and void parameters. */
3182 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3183 continue;
3185 /* Update info on where next arg arrives in registers. */
3186 FUNCTION_ARG_ADVANCE (all.args_so_far, data.promoted_mode,
3187 data.passed_type, data.named_arg);
3189 /* ??? Once upon a time variable_size stuffed parameter list
3190 SAVE_EXPRs (amongst others) onto a pending sizes list. This
3191 turned out to be less than manageable in the gimple world.
3192 Now we have to hunt them down ourselves. */
3193 walk_tree_without_duplicates (&data.passed_type,
3194 gimplify_parm_type, &stmts);
3196 if (!TREE_CONSTANT (DECL_SIZE (parm)))
3198 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3199 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3202 if (data.passed_pointer)
3204 tree type = TREE_TYPE (data.passed_type);
3205 if (reference_callee_copied (&all.args_so_far, TYPE_MODE (type),
3206 type, data.named_arg))
3208 tree local, t;
3210 /* For constant sized objects, this is trivial; for
3211 variable-sized objects, we have to play games. */
3212 if (TREE_CONSTANT (DECL_SIZE (parm)))
3214 local = create_tmp_var (type, get_name (parm));
3215 DECL_IGNORED_P (local) = 0;
3217 else
3219 tree ptr_type, addr, args;
3221 ptr_type = build_pointer_type (type);
3222 addr = create_tmp_var (ptr_type, get_name (parm));
3223 DECL_IGNORED_P (addr) = 0;
3224 local = build_fold_indirect_ref (addr);
3226 args = tree_cons (NULL, DECL_SIZE_UNIT (parm), NULL);
3227 t = built_in_decls[BUILT_IN_ALLOCA];
3228 t = build_function_call_expr (t, args);
3229 t = fold_convert (ptr_type, t);
3230 t = build2 (GIMPLE_MODIFY_STMT, void_type_node, addr, t);
3231 gimplify_and_add (t, &stmts);
3234 t = build2 (GIMPLE_MODIFY_STMT, void_type_node, local, parm);
3235 gimplify_and_add (t, &stmts);
3237 SET_DECL_VALUE_EXPR (parm, local);
3238 DECL_HAS_VALUE_EXPR_P (parm) = 1;
3243 return stmts;
3246 /* Indicate whether REGNO is an incoming argument to the current function
3247 that was promoted to a wider mode. If so, return the RTX for the
3248 register (to get its mode). PMODE and PUNSIGNEDP are set to the mode
3249 that REGNO is promoted from and whether the promotion was signed or
3250 unsigned. */
3253 promoted_input_arg (unsigned int regno, enum machine_mode *pmode, int *punsignedp)
3255 tree arg;
3257 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
3258 arg = TREE_CHAIN (arg))
3259 if (REG_P (DECL_INCOMING_RTL (arg))
3260 && REGNO (DECL_INCOMING_RTL (arg)) == regno
3261 && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
3263 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
3264 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (arg));
3266 mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
3267 if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
3268 && mode != DECL_MODE (arg))
3270 *pmode = DECL_MODE (arg);
3271 *punsignedp = unsignedp;
3272 return DECL_INCOMING_RTL (arg);
3276 return 0;
3280 /* Compute the size and offset from the start of the stacked arguments for a
3281 parm passed in mode PASSED_MODE and with type TYPE.
3283 INITIAL_OFFSET_PTR points to the current offset into the stacked
3284 arguments.
3286 The starting offset and size for this parm are returned in
3287 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
3288 nonzero, the offset is that of stack slot, which is returned in
3289 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
3290 padding required from the initial offset ptr to the stack slot.
3292 IN_REGS is nonzero if the argument will be passed in registers. It will
3293 never be set if REG_PARM_STACK_SPACE is not defined.
3295 FNDECL is the function in which the argument was defined.
3297 There are two types of rounding that are done. The first, controlled by
3298 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
3299 list to be aligned to the specific boundary (in bits). This rounding
3300 affects the initial and starting offsets, but not the argument size.
3302 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
3303 optionally rounds the size of the parm to PARM_BOUNDARY. The
3304 initial offset is not affected by this rounding, while the size always
3305 is and the starting offset may be. */
3307 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
3308 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
3309 callers pass in the total size of args so far as
3310 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
3312 void
3313 locate_and_pad_parm (enum machine_mode passed_mode, tree type, int in_regs,
3314 int partial, tree fndecl ATTRIBUTE_UNUSED,
3315 struct args_size *initial_offset_ptr,
3316 struct locate_and_pad_arg_data *locate)
3318 tree sizetree;
3319 enum direction where_pad;
3320 unsigned int boundary;
3321 int reg_parm_stack_space = 0;
3322 int part_size_in_regs;
3324 #ifdef REG_PARM_STACK_SPACE
3325 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
3327 /* If we have found a stack parm before we reach the end of the
3328 area reserved for registers, skip that area. */
3329 if (! in_regs)
3331 if (reg_parm_stack_space > 0)
3333 if (initial_offset_ptr->var)
3335 initial_offset_ptr->var
3336 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
3337 ssize_int (reg_parm_stack_space));
3338 initial_offset_ptr->constant = 0;
3340 else if (initial_offset_ptr->constant < reg_parm_stack_space)
3341 initial_offset_ptr->constant = reg_parm_stack_space;
3344 #endif /* REG_PARM_STACK_SPACE */
3346 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
3348 sizetree
3349 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
3350 where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
3351 boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
3352 locate->where_pad = where_pad;
3353 locate->boundary = boundary;
3355 /* Remember if the outgoing parameter requires extra alignment on the
3356 calling function side. */
3357 if (boundary > PREFERRED_STACK_BOUNDARY)
3358 boundary = PREFERRED_STACK_BOUNDARY;
3359 if (cfun->stack_alignment_needed < boundary)
3360 cfun->stack_alignment_needed = boundary;
3362 #ifdef ARGS_GROW_DOWNWARD
3363 locate->slot_offset.constant = -initial_offset_ptr->constant;
3364 if (initial_offset_ptr->var)
3365 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
3366 initial_offset_ptr->var);
3369 tree s2 = sizetree;
3370 if (where_pad != none
3371 && (!host_integerp (sizetree, 1)
3372 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
3373 s2 = round_up (s2, PARM_BOUNDARY / BITS_PER_UNIT);
3374 SUB_PARM_SIZE (locate->slot_offset, s2);
3377 locate->slot_offset.constant += part_size_in_regs;
3379 if (!in_regs
3380 #ifdef REG_PARM_STACK_SPACE
3381 || REG_PARM_STACK_SPACE (fndecl) > 0
3382 #endif
3384 pad_to_arg_alignment (&locate->slot_offset, boundary,
3385 &locate->alignment_pad);
3387 locate->size.constant = (-initial_offset_ptr->constant
3388 - locate->slot_offset.constant);
3389 if (initial_offset_ptr->var)
3390 locate->size.var = size_binop (MINUS_EXPR,
3391 size_binop (MINUS_EXPR,
3392 ssize_int (0),
3393 initial_offset_ptr->var),
3394 locate->slot_offset.var);
3396 /* Pad_below needs the pre-rounded size to know how much to pad
3397 below. */
3398 locate->offset = locate->slot_offset;
3399 if (where_pad == downward)
3400 pad_below (&locate->offset, passed_mode, sizetree);
3402 #else /* !ARGS_GROW_DOWNWARD */
3403 if (!in_regs
3404 #ifdef REG_PARM_STACK_SPACE
3405 || REG_PARM_STACK_SPACE (fndecl) > 0
3406 #endif
3408 pad_to_arg_alignment (initial_offset_ptr, boundary,
3409 &locate->alignment_pad);
3410 locate->slot_offset = *initial_offset_ptr;
3412 #ifdef PUSH_ROUNDING
3413 if (passed_mode != BLKmode)
3414 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
3415 #endif
3417 /* Pad_below needs the pre-rounded size to know how much to pad below
3418 so this must be done before rounding up. */
3419 locate->offset = locate->slot_offset;
3420 if (where_pad == downward)
3421 pad_below (&locate->offset, passed_mode, sizetree);
3423 if (where_pad != none
3424 && (!host_integerp (sizetree, 1)
3425 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
3426 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
3428 ADD_PARM_SIZE (locate->size, sizetree);
3430 locate->size.constant -= part_size_in_regs;
3431 #endif /* ARGS_GROW_DOWNWARD */
3434 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
3435 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
3437 static void
3438 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
3439 struct args_size *alignment_pad)
3441 tree save_var = NULL_TREE;
3442 HOST_WIDE_INT save_constant = 0;
3443 int boundary_in_bytes = boundary / BITS_PER_UNIT;
3444 HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
3446 #ifdef SPARC_STACK_BOUNDARY_HACK
3447 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
3448 the real alignment of %sp. However, when it does this, the
3449 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
3450 if (SPARC_STACK_BOUNDARY_HACK)
3451 sp_offset = 0;
3452 #endif
3454 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
3456 save_var = offset_ptr->var;
3457 save_constant = offset_ptr->constant;
3460 alignment_pad->var = NULL_TREE;
3461 alignment_pad->constant = 0;
3463 if (boundary > BITS_PER_UNIT)
3465 if (offset_ptr->var)
3467 tree sp_offset_tree = ssize_int (sp_offset);
3468 tree offset = size_binop (PLUS_EXPR,
3469 ARGS_SIZE_TREE (*offset_ptr),
3470 sp_offset_tree);
3471 #ifdef ARGS_GROW_DOWNWARD
3472 tree rounded = round_down (offset, boundary / BITS_PER_UNIT);
3473 #else
3474 tree rounded = round_up (offset, boundary / BITS_PER_UNIT);
3475 #endif
3477 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
3478 /* ARGS_SIZE_TREE includes constant term. */
3479 offset_ptr->constant = 0;
3480 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
3481 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
3482 save_var);
3484 else
3486 offset_ptr->constant = -sp_offset +
3487 #ifdef ARGS_GROW_DOWNWARD
3488 FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3489 #else
3490 CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3491 #endif
3492 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
3493 alignment_pad->constant = offset_ptr->constant - save_constant;
3498 static void
3499 pad_below (struct args_size *offset_ptr, enum machine_mode passed_mode, tree sizetree)
3501 if (passed_mode != BLKmode)
3503 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
3504 offset_ptr->constant
3505 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
3506 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
3507 - GET_MODE_SIZE (passed_mode));
3509 else
3511 if (TREE_CODE (sizetree) != INTEGER_CST
3512 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
3514 /* Round the size up to multiple of PARM_BOUNDARY bits. */
3515 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
3516 /* Add it in. */
3517 ADD_PARM_SIZE (*offset_ptr, s2);
3518 SUB_PARM_SIZE (*offset_ptr, sizetree);
3523 /* Walk the tree of blocks describing the binding levels within a function
3524 and warn about variables the might be killed by setjmp or vfork.
3525 This is done after calling flow_analysis and before global_alloc
3526 clobbers the pseudo-regs to hard regs. */
3528 void
3529 setjmp_vars_warning (tree block)
3531 tree decl, sub;
3533 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
3535 if (TREE_CODE (decl) == VAR_DECL
3536 && DECL_RTL_SET_P (decl)
3537 && REG_P (DECL_RTL (decl))
3538 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
3539 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
3540 " %<longjmp%> or %<vfork%>", decl);
3543 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
3544 setjmp_vars_warning (sub);
3547 /* Do the appropriate part of setjmp_vars_warning
3548 but for arguments instead of local variables. */
3550 void
3551 setjmp_args_warning (void)
3553 tree decl;
3554 for (decl = DECL_ARGUMENTS (current_function_decl);
3555 decl; decl = TREE_CHAIN (decl))
3556 if (DECL_RTL (decl) != 0
3557 && REG_P (DECL_RTL (decl))
3558 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
3559 warning (OPT_Wclobbered,
3560 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
3561 decl);
3565 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
3566 and create duplicate blocks. */
3567 /* ??? Need an option to either create block fragments or to create
3568 abstract origin duplicates of a source block. It really depends
3569 on what optimization has been performed. */
3571 void
3572 reorder_blocks (void)
3574 tree block = DECL_INITIAL (current_function_decl);
3575 VEC(tree,heap) *block_stack;
3577 if (block == NULL_TREE)
3578 return;
3580 block_stack = VEC_alloc (tree, heap, 10);
3582 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
3583 clear_block_marks (block);
3585 /* Prune the old trees away, so that they don't get in the way. */
3586 BLOCK_SUBBLOCKS (block) = NULL_TREE;
3587 BLOCK_CHAIN (block) = NULL_TREE;
3589 /* Recreate the block tree from the note nesting. */
3590 reorder_blocks_1 (get_insns (), block, &block_stack);
3591 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
3593 VEC_free (tree, heap, block_stack);
3596 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
3598 void
3599 clear_block_marks (tree block)
3601 while (block)
3603 TREE_ASM_WRITTEN (block) = 0;
3604 clear_block_marks (BLOCK_SUBBLOCKS (block));
3605 block = BLOCK_CHAIN (block);
3609 static void
3610 reorder_blocks_1 (rtx insns, tree current_block, VEC(tree,heap) **p_block_stack)
3612 rtx insn;
3614 for (insn = insns; insn; insn = NEXT_INSN (insn))
3616 if (NOTE_P (insn))
3618 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
3620 tree block = NOTE_BLOCK (insn);
3621 tree origin;
3623 origin = (BLOCK_FRAGMENT_ORIGIN (block)
3624 ? BLOCK_FRAGMENT_ORIGIN (block)
3625 : block);
3627 /* If we have seen this block before, that means it now
3628 spans multiple address regions. Create a new fragment. */
3629 if (TREE_ASM_WRITTEN (block))
3631 tree new_block = copy_node (block);
3633 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
3634 BLOCK_FRAGMENT_CHAIN (new_block)
3635 = BLOCK_FRAGMENT_CHAIN (origin);
3636 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
3638 NOTE_BLOCK (insn) = new_block;
3639 block = new_block;
3642 BLOCK_SUBBLOCKS (block) = 0;
3643 TREE_ASM_WRITTEN (block) = 1;
3644 /* When there's only one block for the entire function,
3645 current_block == block and we mustn't do this, it
3646 will cause infinite recursion. */
3647 if (block != current_block)
3649 if (block != origin)
3650 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block);
3652 BLOCK_SUPERCONTEXT (block) = current_block;
3653 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
3654 BLOCK_SUBBLOCKS (current_block) = block;
3655 current_block = origin;
3657 VEC_safe_push (tree, heap, *p_block_stack, block);
3659 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
3661 NOTE_BLOCK (insn) = VEC_pop (tree, *p_block_stack);
3662 BLOCK_SUBBLOCKS (current_block)
3663 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
3664 current_block = BLOCK_SUPERCONTEXT (current_block);
3670 /* Reverse the order of elements in the chain T of blocks,
3671 and return the new head of the chain (old last element). */
3673 tree
3674 blocks_nreverse (tree t)
3676 tree prev = 0, decl, next;
3677 for (decl = t; decl; decl = next)
3679 next = BLOCK_CHAIN (decl);
3680 BLOCK_CHAIN (decl) = prev;
3681 prev = decl;
3683 return prev;
3686 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
3687 non-NULL, list them all into VECTOR, in a depth-first preorder
3688 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
3689 blocks. */
3691 static int
3692 all_blocks (tree block, tree *vector)
3694 int n_blocks = 0;
3696 while (block)
3698 TREE_ASM_WRITTEN (block) = 0;
3700 /* Record this block. */
3701 if (vector)
3702 vector[n_blocks] = block;
3704 ++n_blocks;
3706 /* Record the subblocks, and their subblocks... */
3707 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
3708 vector ? vector + n_blocks : 0);
3709 block = BLOCK_CHAIN (block);
3712 return n_blocks;
3715 /* Return a vector containing all the blocks rooted at BLOCK. The
3716 number of elements in the vector is stored in N_BLOCKS_P. The
3717 vector is dynamically allocated; it is the caller's responsibility
3718 to call `free' on the pointer returned. */
3720 static tree *
3721 get_block_vector (tree block, int *n_blocks_p)
3723 tree *block_vector;
3725 *n_blocks_p = all_blocks (block, NULL);
3726 block_vector = XNEWVEC (tree, *n_blocks_p);
3727 all_blocks (block, block_vector);
3729 return block_vector;
3732 static GTY(()) int next_block_index = 2;
3734 /* Set BLOCK_NUMBER for all the blocks in FN. */
3736 void
3737 number_blocks (tree fn)
3739 int i;
3740 int n_blocks;
3741 tree *block_vector;
3743 /* For SDB and XCOFF debugging output, we start numbering the blocks
3744 from 1 within each function, rather than keeping a running
3745 count. */
3746 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
3747 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
3748 next_block_index = 1;
3749 #endif
3751 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
3753 /* The top-level BLOCK isn't numbered at all. */
3754 for (i = 1; i < n_blocks; ++i)
3755 /* We number the blocks from two. */
3756 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
3758 free (block_vector);
3760 return;
3763 /* If VAR is present in a subblock of BLOCK, return the subblock. */
3765 tree
3766 debug_find_var_in_block_tree (tree var, tree block)
3768 tree t;
3770 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
3771 if (t == var)
3772 return block;
3774 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
3776 tree ret = debug_find_var_in_block_tree (var, t);
3777 if (ret)
3778 return ret;
3781 return NULL_TREE;
3784 /* Allocate a function structure for FNDECL and set its contents
3785 to the defaults. */
3787 void
3788 allocate_struct_function (tree fndecl)
3790 tree result;
3791 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
3793 cfun = ggc_alloc_cleared (sizeof (struct function));
3795 cfun->stack_alignment_needed = STACK_BOUNDARY;
3796 cfun->preferred_stack_boundary = STACK_BOUNDARY;
3798 current_function_funcdef_no = funcdef_no++;
3800 cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
3802 init_eh_for_function ();
3804 lang_hooks.function.init (cfun);
3805 if (init_machine_status)
3806 cfun->machine = (*init_machine_status) ();
3808 if (fndecl == NULL)
3809 return;
3811 DECL_STRUCT_FUNCTION (fndecl) = cfun;
3812 cfun->decl = fndecl;
3814 result = DECL_RESULT (fndecl);
3815 if (aggregate_value_p (result, fndecl))
3817 #ifdef PCC_STATIC_STRUCT_RETURN
3818 current_function_returns_pcc_struct = 1;
3819 #endif
3820 current_function_returns_struct = 1;
3823 current_function_returns_pointer = POINTER_TYPE_P (TREE_TYPE (result));
3825 current_function_stdarg
3826 = (fntype
3827 && TYPE_ARG_TYPES (fntype) != 0
3828 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3829 != void_type_node));
3831 /* Assume all registers in stdarg functions need to be saved. */
3832 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
3833 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
3836 /* Reset cfun, and other non-struct-function variables to defaults as
3837 appropriate for emitting rtl at the start of a function. */
3839 static void
3840 prepare_function_start (tree fndecl)
3842 if (fndecl && DECL_STRUCT_FUNCTION (fndecl))
3843 cfun = DECL_STRUCT_FUNCTION (fndecl);
3844 else
3845 allocate_struct_function (fndecl);
3846 init_emit ();
3847 init_varasm_status (cfun);
3848 init_expr ();
3850 cse_not_expected = ! optimize;
3852 /* Caller save not needed yet. */
3853 caller_save_needed = 0;
3855 /* We haven't done register allocation yet. */
3856 reg_renumber = 0;
3858 /* Indicate that we have not instantiated virtual registers yet. */
3859 virtuals_instantiated = 0;
3861 /* Indicate that we want CONCATs now. */
3862 generating_concat_p = 1;
3864 /* Indicate we have no need of a frame pointer yet. */
3865 frame_pointer_needed = 0;
3868 /* Initialize the rtl expansion mechanism so that we can do simple things
3869 like generate sequences. This is used to provide a context during global
3870 initialization of some passes. */
3871 void
3872 init_dummy_function_start (void)
3874 prepare_function_start (NULL);
3877 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
3878 and initialize static variables for generating RTL for the statements
3879 of the function. */
3881 void
3882 init_function_start (tree subr)
3884 prepare_function_start (subr);
3886 /* Prevent ever trying to delete the first instruction of a
3887 function. Also tell final how to output a linenum before the
3888 function prologue. Note linenums could be missing, e.g. when
3889 compiling a Java .class file. */
3890 if (! DECL_IS_BUILTIN (subr))
3891 emit_line_note (DECL_SOURCE_LOCATION (subr));
3893 /* Make sure first insn is a note even if we don't want linenums.
3894 This makes sure the first insn will never be deleted.
3895 Also, final expects a note to appear there. */
3896 emit_note (NOTE_INSN_DELETED);
3898 /* Warn if this value is an aggregate type,
3899 regardless of which calling convention we are using for it. */
3900 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
3901 warning (OPT_Waggregate_return, "function returns an aggregate");
3904 /* Make sure all values used by the optimization passes have sane
3905 defaults. */
3906 unsigned int
3907 init_function_for_compilation (void)
3909 reg_renumber = 0;
3911 /* No prologue/epilogue insns yet. Make sure that these vectors are
3912 empty. */
3913 gcc_assert (VEC_length (int, prologue) == 0);
3914 gcc_assert (VEC_length (int, epilogue) == 0);
3915 gcc_assert (VEC_length (int, sibcall_epilogue) == 0);
3916 return 0;
3919 struct tree_opt_pass pass_init_function =
3921 NULL, /* name */
3922 NULL, /* gate */
3923 init_function_for_compilation, /* execute */
3924 NULL, /* sub */
3925 NULL, /* next */
3926 0, /* static_pass_number */
3927 0, /* tv_id */
3928 0, /* properties_required */
3929 0, /* properties_provided */
3930 0, /* properties_destroyed */
3931 0, /* todo_flags_start */
3932 0, /* todo_flags_finish */
3933 0 /* letter */
3937 void
3938 expand_main_function (void)
3940 #if (defined(INVOKE__main) \
3941 || (!defined(HAS_INIT_SECTION) \
3942 && !defined(INIT_SECTION_ASM_OP) \
3943 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
3944 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
3945 #endif
3948 /* Expand code to initialize the stack_protect_guard. This is invoked at
3949 the beginning of a function to be protected. */
3951 #ifndef HAVE_stack_protect_set
3952 # define HAVE_stack_protect_set 0
3953 # define gen_stack_protect_set(x,y) (gcc_unreachable (), NULL_RTX)
3954 #endif
3956 void
3957 stack_protect_prologue (void)
3959 tree guard_decl = targetm.stack_protect_guard ();
3960 rtx x, y;
3962 /* Avoid expand_expr here, because we don't want guard_decl pulled
3963 into registers unless absolutely necessary. And we know that
3964 cfun->stack_protect_guard is a local stack slot, so this skips
3965 all the fluff. */
3966 x = validize_mem (DECL_RTL (cfun->stack_protect_guard));
3967 y = validize_mem (DECL_RTL (guard_decl));
3969 /* Allow the target to copy from Y to X without leaking Y into a
3970 register. */
3971 if (HAVE_stack_protect_set)
3973 rtx insn = gen_stack_protect_set (x, y);
3974 if (insn)
3976 emit_insn (insn);
3977 return;
3981 /* Otherwise do a straight move. */
3982 emit_move_insn (x, y);
3985 /* Expand code to verify the stack_protect_guard. This is invoked at
3986 the end of a function to be protected. */
3988 #ifndef HAVE_stack_protect_test
3989 # define HAVE_stack_protect_test 0
3990 # define gen_stack_protect_test(x, y, z) (gcc_unreachable (), NULL_RTX)
3991 #endif
3993 void
3994 stack_protect_epilogue (void)
3996 tree guard_decl = targetm.stack_protect_guard ();
3997 rtx label = gen_label_rtx ();
3998 rtx x, y, tmp;
4000 /* Avoid expand_expr here, because we don't want guard_decl pulled
4001 into registers unless absolutely necessary. And we know that
4002 cfun->stack_protect_guard is a local stack slot, so this skips
4003 all the fluff. */
4004 x = validize_mem (DECL_RTL (cfun->stack_protect_guard));
4005 y = validize_mem (DECL_RTL (guard_decl));
4007 /* Allow the target to compare Y with X without leaking either into
4008 a register. */
4009 switch (HAVE_stack_protect_test != 0)
4011 case 1:
4012 tmp = gen_stack_protect_test (x, y, label);
4013 if (tmp)
4015 emit_insn (tmp);
4016 break;
4018 /* FALLTHRU */
4020 default:
4021 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
4022 break;
4025 /* The noreturn predictor has been moved to the tree level. The rtl-level
4026 predictors estimate this branch about 20%, which isn't enough to get
4027 things moved out of line. Since this is the only extant case of adding
4028 a noreturn function at the rtl level, it doesn't seem worth doing ought
4029 except adding the prediction by hand. */
4030 tmp = get_last_insn ();
4031 if (JUMP_P (tmp))
4032 predict_insn_def (tmp, PRED_NORETURN, TAKEN);
4034 expand_expr_stmt (targetm.stack_protect_fail ());
4035 emit_label (label);
4038 /* Start the RTL for a new function, and set variables used for
4039 emitting RTL.
4040 SUBR is the FUNCTION_DECL node.
4041 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
4042 the function's parameters, which must be run at any return statement. */
4044 void
4045 expand_function_start (tree subr)
4047 /* Make sure volatile mem refs aren't considered
4048 valid operands of arithmetic insns. */
4049 init_recog_no_volatile ();
4051 current_function_profile
4052 = (profile_flag
4053 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
4055 current_function_limit_stack
4056 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
4058 /* Make the label for return statements to jump to. Do not special
4059 case machines with special return instructions -- they will be
4060 handled later during jump, ifcvt, or epilogue creation. */
4061 return_label = gen_label_rtx ();
4063 /* Initialize rtx used to return the value. */
4064 /* Do this before assign_parms so that we copy the struct value address
4065 before any library calls that assign parms might generate. */
4067 /* Decide whether to return the value in memory or in a register. */
4068 if (aggregate_value_p (DECL_RESULT (subr), subr))
4070 /* Returning something that won't go in a register. */
4071 rtx value_address = 0;
4073 #ifdef PCC_STATIC_STRUCT_RETURN
4074 if (current_function_returns_pcc_struct)
4076 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
4077 value_address = assemble_static_space (size);
4079 else
4080 #endif
4082 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
4083 /* Expect to be passed the address of a place to store the value.
4084 If it is passed as an argument, assign_parms will take care of
4085 it. */
4086 if (sv)
4088 value_address = gen_reg_rtx (Pmode);
4089 emit_move_insn (value_address, sv);
4092 if (value_address)
4094 rtx x = value_address;
4095 if (!DECL_BY_REFERENCE (DECL_RESULT (subr)))
4097 x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), x);
4098 set_mem_attributes (x, DECL_RESULT (subr), 1);
4100 SET_DECL_RTL (DECL_RESULT (subr), x);
4103 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
4104 /* If return mode is void, this decl rtl should not be used. */
4105 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
4106 else
4108 /* Compute the return values into a pseudo reg, which we will copy
4109 into the true return register after the cleanups are done. */
4110 tree return_type = TREE_TYPE (DECL_RESULT (subr));
4111 if (TYPE_MODE (return_type) != BLKmode
4112 && targetm.calls.return_in_msb (return_type))
4113 /* expand_function_end will insert the appropriate padding in
4114 this case. Use the return value's natural (unpadded) mode
4115 within the function proper. */
4116 SET_DECL_RTL (DECL_RESULT (subr),
4117 gen_reg_rtx (TYPE_MODE (return_type)));
4118 else
4120 /* In order to figure out what mode to use for the pseudo, we
4121 figure out what the mode of the eventual return register will
4122 actually be, and use that. */
4123 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
4125 /* Structures that are returned in registers are not
4126 aggregate_value_p, so we may see a PARALLEL or a REG. */
4127 if (REG_P (hard_reg))
4128 SET_DECL_RTL (DECL_RESULT (subr),
4129 gen_reg_rtx (GET_MODE (hard_reg)));
4130 else
4132 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
4133 SET_DECL_RTL (DECL_RESULT (subr), gen_group_rtx (hard_reg));
4137 /* Set DECL_REGISTER flag so that expand_function_end will copy the
4138 result to the real return register(s). */
4139 DECL_REGISTER (DECL_RESULT (subr)) = 1;
4142 /* Initialize rtx for parameters and local variables.
4143 In some cases this requires emitting insns. */
4144 assign_parms (subr);
4146 /* If function gets a static chain arg, store it. */
4147 if (cfun->static_chain_decl)
4149 tree parm = cfun->static_chain_decl;
4150 rtx local = gen_reg_rtx (Pmode);
4152 set_decl_incoming_rtl (parm, static_chain_incoming_rtx);
4153 SET_DECL_RTL (parm, local);
4154 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4156 emit_move_insn (local, static_chain_incoming_rtx);
4159 /* If the function receives a non-local goto, then store the
4160 bits we need to restore the frame pointer. */
4161 if (cfun->nonlocal_goto_save_area)
4163 tree t_save;
4164 rtx r_save;
4166 /* ??? We need to do this save early. Unfortunately here is
4167 before the frame variable gets declared. Help out... */
4168 expand_var (TREE_OPERAND (cfun->nonlocal_goto_save_area, 0));
4170 t_save = build4 (ARRAY_REF, ptr_type_node,
4171 cfun->nonlocal_goto_save_area,
4172 integer_zero_node, NULL_TREE, NULL_TREE);
4173 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
4174 r_save = convert_memory_address (Pmode, r_save);
4176 emit_move_insn (r_save, virtual_stack_vars_rtx);
4177 update_nonlocal_goto_save_area ();
4180 /* The following was moved from init_function_start.
4181 The move is supposed to make sdb output more accurate. */
4182 /* Indicate the beginning of the function body,
4183 as opposed to parm setup. */
4184 emit_note (NOTE_INSN_FUNCTION_BEG);
4186 gcc_assert (NOTE_P (get_last_insn ()));
4188 parm_birth_insn = get_last_insn ();
4190 if (current_function_profile)
4192 #ifdef PROFILE_HOOK
4193 PROFILE_HOOK (current_function_funcdef_no);
4194 #endif
4197 /* After the display initializations is where the stack checking
4198 probe should go. */
4199 if(flag_stack_check)
4200 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
4202 /* Make sure there is a line number after the function entry setup code. */
4203 force_next_line_note ();
4206 /* Undo the effects of init_dummy_function_start. */
4207 void
4208 expand_dummy_function_end (void)
4210 /* End any sequences that failed to be closed due to syntax errors. */
4211 while (in_sequence_p ())
4212 end_sequence ();
4214 /* Outside function body, can't compute type's actual size
4215 until next function's body starts. */
4217 free_after_parsing (cfun);
4218 free_after_compilation (cfun);
4219 cfun = 0;
4222 /* Call DOIT for each hard register used as a return value from
4223 the current function. */
4225 void
4226 diddle_return_value (void (*doit) (rtx, void *), void *arg)
4228 rtx outgoing = current_function_return_rtx;
4230 if (! outgoing)
4231 return;
4233 if (REG_P (outgoing))
4234 (*doit) (outgoing, arg);
4235 else if (GET_CODE (outgoing) == PARALLEL)
4237 int i;
4239 for (i = 0; i < XVECLEN (outgoing, 0); i++)
4241 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
4243 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
4244 (*doit) (x, arg);
4249 static void
4250 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4252 emit_insn (gen_rtx_CLOBBER (VOIDmode, reg));
4255 void
4256 clobber_return_register (void)
4258 diddle_return_value (do_clobber_return_reg, NULL);
4260 /* In case we do use pseudo to return value, clobber it too. */
4261 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
4263 tree decl_result = DECL_RESULT (current_function_decl);
4264 rtx decl_rtl = DECL_RTL (decl_result);
4265 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
4267 do_clobber_return_reg (decl_rtl, NULL);
4272 static void
4273 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4275 emit_insn (gen_rtx_USE (VOIDmode, reg));
4278 static void
4279 use_return_register (void)
4281 diddle_return_value (do_use_return_reg, NULL);
4284 /* Possibly warn about unused parameters. */
4285 void
4286 do_warn_unused_parameter (tree fn)
4288 tree decl;
4290 for (decl = DECL_ARGUMENTS (fn);
4291 decl; decl = TREE_CHAIN (decl))
4292 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
4293 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl))
4294 warning (OPT_Wunused_parameter, "unused parameter %q+D", decl);
4297 static GTY(()) rtx initial_trampoline;
4299 /* Generate RTL for the end of the current function. */
4301 void
4302 expand_function_end (void)
4304 rtx clobber_after;
4306 /* If arg_pointer_save_area was referenced only from a nested
4307 function, we will not have initialized it yet. Do that now. */
4308 if (arg_pointer_save_area && ! cfun->arg_pointer_save_area_init)
4309 get_arg_pointer_save_area (cfun);
4311 /* If we are doing stack checking and this function makes calls,
4312 do a stack probe at the start of the function to ensure we have enough
4313 space for another stack frame. */
4314 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
4316 rtx insn, seq;
4318 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4319 if (CALL_P (insn))
4321 start_sequence ();
4322 probe_stack_range (STACK_CHECK_PROTECT,
4323 GEN_INT (STACK_CHECK_MAX_FRAME_SIZE));
4324 seq = get_insns ();
4325 end_sequence ();
4326 emit_insn_before (seq, stack_check_probe_note);
4327 break;
4331 /* Possibly warn about unused parameters.
4332 When frontend does unit-at-a-time, the warning is already
4333 issued at finalization time. */
4334 if (warn_unused_parameter
4335 && !lang_hooks.callgraph.expand_function)
4336 do_warn_unused_parameter (current_function_decl);
4338 /* End any sequences that failed to be closed due to syntax errors. */
4339 while (in_sequence_p ())
4340 end_sequence ();
4342 clear_pending_stack_adjust ();
4343 do_pending_stack_adjust ();
4345 /* Output a linenumber for the end of the function.
4346 SDB depends on this. */
4347 force_next_line_note ();
4348 emit_line_note (input_location);
4350 /* Before the return label (if any), clobber the return
4351 registers so that they are not propagated live to the rest of
4352 the function. This can only happen with functions that drop
4353 through; if there had been a return statement, there would
4354 have either been a return rtx, or a jump to the return label.
4356 We delay actual code generation after the current_function_value_rtx
4357 is computed. */
4358 clobber_after = get_last_insn ();
4360 /* Output the label for the actual return from the function. */
4361 emit_label (return_label);
4363 if (USING_SJLJ_EXCEPTIONS)
4365 /* Let except.c know where it should emit the call to unregister
4366 the function context for sjlj exceptions. */
4367 if (flag_exceptions)
4368 sjlj_emit_function_exit_after (get_last_insn ());
4370 else
4372 /* @@@ This is a kludge. We want to ensure that instructions that
4373 may trap are not moved into the epilogue by scheduling, because
4374 we don't always emit unwind information for the epilogue.
4375 However, not all machine descriptions define a blockage insn, so
4376 emit an ASM_INPUT to act as one. */
4377 if (flag_non_call_exceptions)
4378 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
4381 /* If this is an implementation of throw, do what's necessary to
4382 communicate between __builtin_eh_return and the epilogue. */
4383 expand_eh_return ();
4385 /* If scalar return value was computed in a pseudo-reg, or was a named
4386 return value that got dumped to the stack, copy that to the hard
4387 return register. */
4388 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
4390 tree decl_result = DECL_RESULT (current_function_decl);
4391 rtx decl_rtl = DECL_RTL (decl_result);
4393 if (REG_P (decl_rtl)
4394 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
4395 : DECL_REGISTER (decl_result))
4397 rtx real_decl_rtl = current_function_return_rtx;
4399 /* This should be set in assign_parms. */
4400 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
4402 /* If this is a BLKmode structure being returned in registers,
4403 then use the mode computed in expand_return. Note that if
4404 decl_rtl is memory, then its mode may have been changed,
4405 but that current_function_return_rtx has not. */
4406 if (GET_MODE (real_decl_rtl) == BLKmode)
4407 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
4409 /* If a non-BLKmode return value should be padded at the least
4410 significant end of the register, shift it left by the appropriate
4411 amount. BLKmode results are handled using the group load/store
4412 machinery. */
4413 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
4414 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
4416 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
4417 REGNO (real_decl_rtl)),
4418 decl_rtl);
4419 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
4421 /* If a named return value dumped decl_return to memory, then
4422 we may need to re-do the PROMOTE_MODE signed/unsigned
4423 extension. */
4424 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
4426 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
4428 if (targetm.calls.promote_function_return (TREE_TYPE (current_function_decl)))
4429 promote_mode (TREE_TYPE (decl_result), GET_MODE (decl_rtl),
4430 &unsignedp, 1);
4432 convert_move (real_decl_rtl, decl_rtl, unsignedp);
4434 else if (GET_CODE (real_decl_rtl) == PARALLEL)
4436 /* If expand_function_start has created a PARALLEL for decl_rtl,
4437 move the result to the real return registers. Otherwise, do
4438 a group load from decl_rtl for a named return. */
4439 if (GET_CODE (decl_rtl) == PARALLEL)
4440 emit_group_move (real_decl_rtl, decl_rtl);
4441 else
4442 emit_group_load (real_decl_rtl, decl_rtl,
4443 TREE_TYPE (decl_result),
4444 int_size_in_bytes (TREE_TYPE (decl_result)));
4446 /* In the case of complex integer modes smaller than a word, we'll
4447 need to generate some non-trivial bitfield insertions. Do that
4448 on a pseudo and not the hard register. */
4449 else if (GET_CODE (decl_rtl) == CONCAT
4450 && GET_MODE_CLASS (GET_MODE (decl_rtl)) == MODE_COMPLEX_INT
4451 && GET_MODE_BITSIZE (GET_MODE (decl_rtl)) <= BITS_PER_WORD)
4453 int old_generating_concat_p;
4454 rtx tmp;
4456 old_generating_concat_p = generating_concat_p;
4457 generating_concat_p = 0;
4458 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
4459 generating_concat_p = old_generating_concat_p;
4461 emit_move_insn (tmp, decl_rtl);
4462 emit_move_insn (real_decl_rtl, tmp);
4464 else
4465 emit_move_insn (real_decl_rtl, decl_rtl);
4469 /* If returning a structure, arrange to return the address of the value
4470 in a place where debuggers expect to find it.
4472 If returning a structure PCC style,
4473 the caller also depends on this value.
4474 And current_function_returns_pcc_struct is not necessarily set. */
4475 if (current_function_returns_struct
4476 || current_function_returns_pcc_struct)
4478 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
4479 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
4480 rtx outgoing;
4482 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
4483 type = TREE_TYPE (type);
4484 else
4485 value_address = XEXP (value_address, 0);
4487 outgoing = targetm.calls.function_value (build_pointer_type (type),
4488 current_function_decl, true);
4490 /* Mark this as a function return value so integrate will delete the
4491 assignment and USE below when inlining this function. */
4492 REG_FUNCTION_VALUE_P (outgoing) = 1;
4494 /* The address may be ptr_mode and OUTGOING may be Pmode. */
4495 value_address = convert_memory_address (GET_MODE (outgoing),
4496 value_address);
4498 emit_move_insn (outgoing, value_address);
4500 /* Show return register used to hold result (in this case the address
4501 of the result. */
4502 current_function_return_rtx = outgoing;
4505 /* Emit the actual code to clobber return register. */
4507 rtx seq;
4509 start_sequence ();
4510 clobber_return_register ();
4511 expand_naked_return ();
4512 seq = get_insns ();
4513 end_sequence ();
4515 emit_insn_after (seq, clobber_after);
4518 /* Output the label for the naked return from the function. */
4519 emit_label (naked_return_label);
4521 /* If stack protection is enabled for this function, check the guard. */
4522 if (cfun->stack_protect_guard)
4523 stack_protect_epilogue ();
4525 /* If we had calls to alloca, and this machine needs
4526 an accurate stack pointer to exit the function,
4527 insert some code to save and restore the stack pointer. */
4528 if (! EXIT_IGNORE_STACK
4529 && current_function_calls_alloca)
4531 rtx tem = 0;
4533 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
4534 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
4537 /* ??? This should no longer be necessary since stupid is no longer with
4538 us, but there are some parts of the compiler (eg reload_combine, and
4539 sh mach_dep_reorg) that still try and compute their own lifetime info
4540 instead of using the general framework. */
4541 use_return_register ();
4545 get_arg_pointer_save_area (struct function *f)
4547 rtx ret = f->x_arg_pointer_save_area;
4549 if (! ret)
4551 ret = assign_stack_local_1 (Pmode, GET_MODE_SIZE (Pmode), 0, f);
4552 f->x_arg_pointer_save_area = ret;
4555 if (f == cfun && ! f->arg_pointer_save_area_init)
4557 rtx seq;
4559 /* Save the arg pointer at the beginning of the function. The
4560 generated stack slot may not be a valid memory address, so we
4561 have to check it and fix it if necessary. */
4562 start_sequence ();
4563 emit_move_insn (validize_mem (ret), virtual_incoming_args_rtx);
4564 seq = get_insns ();
4565 end_sequence ();
4567 push_topmost_sequence ();
4568 emit_insn_after (seq, entry_of_function ());
4569 pop_topmost_sequence ();
4572 return ret;
4575 /* Extend a vector that records the INSN_UIDs of INSNS
4576 (a list of one or more insns). */
4578 static void
4579 record_insns (rtx insns, VEC(int,heap) **vecp)
4581 rtx tmp;
4583 for (tmp = insns; tmp != NULL_RTX; tmp = NEXT_INSN (tmp))
4584 VEC_safe_push (int, heap, *vecp, INSN_UID (tmp));
4587 /* Set the locator of the insn chain starting at INSN to LOC. */
4588 static void
4589 set_insn_locators (rtx insn, int loc)
4591 while (insn != NULL_RTX)
4593 if (INSN_P (insn))
4594 INSN_LOCATOR (insn) = loc;
4595 insn = NEXT_INSN (insn);
4599 /* Determine how many INSN_UIDs in VEC are part of INSN. Because we can
4600 be running after reorg, SEQUENCE rtl is possible. */
4602 static int
4603 contains (rtx insn, VEC(int,heap) **vec)
4605 int i, j;
4607 if (NONJUMP_INSN_P (insn)
4608 && GET_CODE (PATTERN (insn)) == SEQUENCE)
4610 int count = 0;
4611 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
4612 for (j = VEC_length (int, *vec) - 1; j >= 0; --j)
4613 if (INSN_UID (XVECEXP (PATTERN (insn), 0, i))
4614 == VEC_index (int, *vec, j))
4615 count++;
4616 return count;
4618 else
4620 for (j = VEC_length (int, *vec) - 1; j >= 0; --j)
4621 if (INSN_UID (insn) == VEC_index (int, *vec, j))
4622 return 1;
4624 return 0;
4628 prologue_epilogue_contains (rtx insn)
4630 if (contains (insn, &prologue))
4631 return 1;
4632 if (contains (insn, &epilogue))
4633 return 1;
4634 return 0;
4638 sibcall_epilogue_contains (rtx insn)
4640 if (sibcall_epilogue)
4641 return contains (insn, &sibcall_epilogue);
4642 return 0;
4645 #ifdef HAVE_return
4646 /* Insert gen_return at the end of block BB. This also means updating
4647 block_for_insn appropriately. */
4649 static void
4650 emit_return_into_block (basic_block bb)
4652 emit_jump_insn_after (gen_return (), BB_END (bb));
4654 #endif /* HAVE_return */
4656 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
4658 /* These functions convert the epilogue into a variant that does not
4659 modify the stack pointer. This is used in cases where a function
4660 returns an object whose size is not known until it is computed.
4661 The called function leaves the object on the stack, leaves the
4662 stack depressed, and returns a pointer to the object.
4664 What we need to do is track all modifications and references to the
4665 stack pointer, deleting the modifications and changing the
4666 references to point to the location the stack pointer would have
4667 pointed to had the modifications taken place.
4669 These functions need to be portable so we need to make as few
4670 assumptions about the epilogue as we can. However, the epilogue
4671 basically contains three things: instructions to reset the stack
4672 pointer, instructions to reload registers, possibly including the
4673 frame pointer, and an instruction to return to the caller.
4675 We must be sure of what a relevant epilogue insn is doing. We also
4676 make no attempt to validate the insns we make since if they are
4677 invalid, we probably can't do anything valid. The intent is that
4678 these routines get "smarter" as more and more machines start to use
4679 them and they try operating on different epilogues.
4681 We use the following structure to track what the part of the
4682 epilogue that we've already processed has done. We keep two copies
4683 of the SP equivalence, one for use during the insn we are
4684 processing and one for use in the next insn. The difference is
4685 because one part of a PARALLEL may adjust SP and the other may use
4686 it. */
4688 struct epi_info
4690 rtx sp_equiv_reg; /* REG that SP is set from, perhaps SP. */
4691 HOST_WIDE_INT sp_offset; /* Offset from SP_EQUIV_REG of present SP. */
4692 rtx new_sp_equiv_reg; /* REG to be used at end of insn. */
4693 HOST_WIDE_INT new_sp_offset; /* Offset to be used at end of insn. */
4694 rtx equiv_reg_src; /* If nonzero, the value that SP_EQUIV_REG
4695 should be set to once we no longer need
4696 its value. */
4697 rtx const_equiv[FIRST_PSEUDO_REGISTER]; /* Any known constant equivalences
4698 for registers. */
4701 static void handle_epilogue_set (rtx, struct epi_info *);
4702 static void update_epilogue_consts (rtx, rtx, void *);
4703 static void emit_equiv_load (struct epi_info *);
4705 /* Modify INSN, a list of one or more insns that is part of the epilogue, to
4706 no modifications to the stack pointer. Return the new list of insns. */
4708 static rtx
4709 keep_stack_depressed (rtx insns)
4711 int j;
4712 struct epi_info info;
4713 rtx insn, next;
4715 /* If the epilogue is just a single instruction, it must be OK as is. */
4716 if (NEXT_INSN (insns) == NULL_RTX)
4717 return insns;
4719 /* Otherwise, start a sequence, initialize the information we have, and
4720 process all the insns we were given. */
4721 start_sequence ();
4723 info.sp_equiv_reg = stack_pointer_rtx;
4724 info.sp_offset = 0;
4725 info.equiv_reg_src = 0;
4727 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
4728 info.const_equiv[j] = 0;
4730 insn = insns;
4731 next = NULL_RTX;
4732 while (insn != NULL_RTX)
4734 next = NEXT_INSN (insn);
4736 if (!INSN_P (insn))
4738 add_insn (insn);
4739 insn = next;
4740 continue;
4743 /* If this insn references the register that SP is equivalent to and
4744 we have a pending load to that register, we must force out the load
4745 first and then indicate we no longer know what SP's equivalent is. */
4746 if (info.equiv_reg_src != 0
4747 && reg_referenced_p (info.sp_equiv_reg, PATTERN (insn)))
4749 emit_equiv_load (&info);
4750 info.sp_equiv_reg = 0;
4753 info.new_sp_equiv_reg = info.sp_equiv_reg;
4754 info.new_sp_offset = info.sp_offset;
4756 /* If this is a (RETURN) and the return address is on the stack,
4757 update the address and change to an indirect jump. */
4758 if (GET_CODE (PATTERN (insn)) == RETURN
4759 || (GET_CODE (PATTERN (insn)) == PARALLEL
4760 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == RETURN))
4762 rtx retaddr = INCOMING_RETURN_ADDR_RTX;
4763 rtx base = 0;
4764 HOST_WIDE_INT offset = 0;
4765 rtx jump_insn, jump_set;
4767 /* If the return address is in a register, we can emit the insn
4768 unchanged. Otherwise, it must be a MEM and we see what the
4769 base register and offset are. In any case, we have to emit any
4770 pending load to the equivalent reg of SP, if any. */
4771 if (REG_P (retaddr))
4773 emit_equiv_load (&info);
4774 add_insn (insn);
4775 insn = next;
4776 continue;
4778 else
4780 rtx ret_ptr;
4781 gcc_assert (MEM_P (retaddr));
4783 ret_ptr = XEXP (retaddr, 0);
4785 if (REG_P (ret_ptr))
4787 base = gen_rtx_REG (Pmode, REGNO (ret_ptr));
4788 offset = 0;
4790 else
4792 gcc_assert (GET_CODE (ret_ptr) == PLUS
4793 && REG_P (XEXP (ret_ptr, 0))
4794 && GET_CODE (XEXP (ret_ptr, 1)) == CONST_INT);
4795 base = gen_rtx_REG (Pmode, REGNO (XEXP (ret_ptr, 0)));
4796 offset = INTVAL (XEXP (ret_ptr, 1));
4800 /* If the base of the location containing the return pointer
4801 is SP, we must update it with the replacement address. Otherwise,
4802 just build the necessary MEM. */
4803 retaddr = plus_constant (base, offset);
4804 if (base == stack_pointer_rtx)
4805 retaddr = simplify_replace_rtx (retaddr, stack_pointer_rtx,
4806 plus_constant (info.sp_equiv_reg,
4807 info.sp_offset));
4809 retaddr = gen_rtx_MEM (Pmode, retaddr);
4810 MEM_NOTRAP_P (retaddr) = 1;
4812 /* If there is a pending load to the equivalent register for SP
4813 and we reference that register, we must load our address into
4814 a scratch register and then do that load. */
4815 if (info.equiv_reg_src
4816 && reg_overlap_mentioned_p (info.equiv_reg_src, retaddr))
4818 unsigned int regno;
4819 rtx reg;
4821 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4822 if (HARD_REGNO_MODE_OK (regno, Pmode)
4823 && !fixed_regs[regno]
4824 && TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)
4825 && !REGNO_REG_SET_P
4826 (EXIT_BLOCK_PTR->il.rtl->global_live_at_start, regno)
4827 && !refers_to_regno_p (regno,
4828 regno + hard_regno_nregs[regno]
4829 [Pmode],
4830 info.equiv_reg_src, NULL)
4831 && info.const_equiv[regno] == 0)
4832 break;
4834 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
4836 reg = gen_rtx_REG (Pmode, regno);
4837 emit_move_insn (reg, retaddr);
4838 retaddr = reg;
4841 emit_equiv_load (&info);
4842 jump_insn = emit_jump_insn (gen_indirect_jump (retaddr));
4844 /* Show the SET in the above insn is a RETURN. */
4845 jump_set = single_set (jump_insn);
4846 gcc_assert (jump_set);
4847 SET_IS_RETURN_P (jump_set) = 1;
4850 /* If SP is not mentioned in the pattern and its equivalent register, if
4851 any, is not modified, just emit it. Otherwise, if neither is set,
4852 replace the reference to SP and emit the insn. If none of those are
4853 true, handle each SET individually. */
4854 else if (!reg_mentioned_p (stack_pointer_rtx, PATTERN (insn))
4855 && (info.sp_equiv_reg == stack_pointer_rtx
4856 || !reg_set_p (info.sp_equiv_reg, insn)))
4857 add_insn (insn);
4858 else if (! reg_set_p (stack_pointer_rtx, insn)
4859 && (info.sp_equiv_reg == stack_pointer_rtx
4860 || !reg_set_p (info.sp_equiv_reg, insn)))
4862 int changed;
4864 changed = validate_replace_rtx (stack_pointer_rtx,
4865 plus_constant (info.sp_equiv_reg,
4866 info.sp_offset),
4867 insn);
4868 gcc_assert (changed);
4870 add_insn (insn);
4872 else if (GET_CODE (PATTERN (insn)) == SET)
4873 handle_epilogue_set (PATTERN (insn), &info);
4874 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
4876 for (j = 0; j < XVECLEN (PATTERN (insn), 0); j++)
4877 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET)
4878 handle_epilogue_set (XVECEXP (PATTERN (insn), 0, j), &info);
4880 else
4881 add_insn (insn);
4883 info.sp_equiv_reg = info.new_sp_equiv_reg;
4884 info.sp_offset = info.new_sp_offset;
4886 /* Now update any constants this insn sets. */
4887 note_stores (PATTERN (insn), update_epilogue_consts, &info);
4888 insn = next;
4891 insns = get_insns ();
4892 end_sequence ();
4893 return insns;
4896 /* SET is a SET from an insn in the epilogue. P is a pointer to the epi_info
4897 structure that contains information about what we've seen so far. We
4898 process this SET by either updating that data or by emitting one or
4899 more insns. */
4901 static void
4902 handle_epilogue_set (rtx set, struct epi_info *p)
4904 /* First handle the case where we are setting SP. Record what it is being
4905 set from, which we must be able to determine */
4906 if (reg_set_p (stack_pointer_rtx, set))
4908 gcc_assert (SET_DEST (set) == stack_pointer_rtx);
4910 if (GET_CODE (SET_SRC (set)) == PLUS)
4912 p->new_sp_equiv_reg = XEXP (SET_SRC (set), 0);
4913 if (GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
4914 p->new_sp_offset = INTVAL (XEXP (SET_SRC (set), 1));
4915 else
4917 gcc_assert (REG_P (XEXP (SET_SRC (set), 1))
4918 && (REGNO (XEXP (SET_SRC (set), 1))
4919 < FIRST_PSEUDO_REGISTER)
4920 && p->const_equiv[REGNO (XEXP (SET_SRC (set), 1))]);
4921 p->new_sp_offset
4922 = INTVAL (p->const_equiv[REGNO (XEXP (SET_SRC (set), 1))]);
4925 else
4926 p->new_sp_equiv_reg = SET_SRC (set), p->new_sp_offset = 0;
4928 /* If we are adjusting SP, we adjust from the old data. */
4929 if (p->new_sp_equiv_reg == stack_pointer_rtx)
4931 p->new_sp_equiv_reg = p->sp_equiv_reg;
4932 p->new_sp_offset += p->sp_offset;
4935 gcc_assert (p->new_sp_equiv_reg && REG_P (p->new_sp_equiv_reg));
4937 return;
4940 /* Next handle the case where we are setting SP's equivalent
4941 register. We must not already have a value to set it to. We
4942 could update, but there seems little point in handling that case.
4943 Note that we have to allow for the case where we are setting the
4944 register set in the previous part of a PARALLEL inside a single
4945 insn. But use the old offset for any updates within this insn.
4946 We must allow for the case where the register is being set in a
4947 different (usually wider) mode than Pmode). */
4948 else if (p->new_sp_equiv_reg != 0 && reg_set_p (p->new_sp_equiv_reg, set))
4950 gcc_assert (!p->equiv_reg_src
4951 && REG_P (p->new_sp_equiv_reg)
4952 && REG_P (SET_DEST (set))
4953 && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (set)))
4954 <= BITS_PER_WORD)
4955 && REGNO (p->new_sp_equiv_reg) == REGNO (SET_DEST (set)));
4956 p->equiv_reg_src
4957 = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
4958 plus_constant (p->sp_equiv_reg,
4959 p->sp_offset));
4962 /* Otherwise, replace any references to SP in the insn to its new value
4963 and emit the insn. */
4964 else
4966 SET_SRC (set) = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
4967 plus_constant (p->sp_equiv_reg,
4968 p->sp_offset));
4969 SET_DEST (set) = simplify_replace_rtx (SET_DEST (set), stack_pointer_rtx,
4970 plus_constant (p->sp_equiv_reg,
4971 p->sp_offset));
4972 emit_insn (set);
4976 /* Update the tracking information for registers set to constants. */
4978 static void
4979 update_epilogue_consts (rtx dest, rtx x, void *data)
4981 struct epi_info *p = (struct epi_info *) data;
4982 rtx new;
4984 if (!REG_P (dest) || REGNO (dest) >= FIRST_PSEUDO_REGISTER)
4985 return;
4987 /* If we are either clobbering a register or doing a partial set,
4988 show we don't know the value. */
4989 else if (GET_CODE (x) == CLOBBER || ! rtx_equal_p (dest, SET_DEST (x)))
4990 p->const_equiv[REGNO (dest)] = 0;
4992 /* If we are setting it to a constant, record that constant. */
4993 else if (GET_CODE (SET_SRC (x)) == CONST_INT)
4994 p->const_equiv[REGNO (dest)] = SET_SRC (x);
4996 /* If this is a binary operation between a register we have been tracking
4997 and a constant, see if we can compute a new constant value. */
4998 else if (ARITHMETIC_P (SET_SRC (x))
4999 && REG_P (XEXP (SET_SRC (x), 0))
5000 && REGNO (XEXP (SET_SRC (x), 0)) < FIRST_PSEUDO_REGISTER
5001 && p->const_equiv[REGNO (XEXP (SET_SRC (x), 0))] != 0
5002 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
5003 && 0 != (new = simplify_binary_operation
5004 (GET_CODE (SET_SRC (x)), GET_MODE (dest),
5005 p->const_equiv[REGNO (XEXP (SET_SRC (x), 0))],
5006 XEXP (SET_SRC (x), 1)))
5007 && GET_CODE (new) == CONST_INT)
5008 p->const_equiv[REGNO (dest)] = new;
5010 /* Otherwise, we can't do anything with this value. */
5011 else
5012 p->const_equiv[REGNO (dest)] = 0;
5015 /* Emit an insn to do the load shown in p->equiv_reg_src, if needed. */
5017 static void
5018 emit_equiv_load (struct epi_info *p)
5020 if (p->equiv_reg_src != 0)
5022 rtx dest = p->sp_equiv_reg;
5024 if (GET_MODE (p->equiv_reg_src) != GET_MODE (dest))
5025 dest = gen_rtx_REG (GET_MODE (p->equiv_reg_src),
5026 REGNO (p->sp_equiv_reg));
5028 emit_move_insn (dest, p->equiv_reg_src);
5029 p->equiv_reg_src = 0;
5032 #endif
5034 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
5035 this into place with notes indicating where the prologue ends and where
5036 the epilogue begins. Update the basic block information when possible. */
5038 void
5039 thread_prologue_and_epilogue_insns (rtx f ATTRIBUTE_UNUSED)
5041 int inserted = 0;
5042 edge e;
5043 #if defined (HAVE_sibcall_epilogue) || defined (HAVE_epilogue) || defined (HAVE_return) || defined (HAVE_prologue)
5044 rtx seq;
5045 #endif
5046 #ifdef HAVE_prologue
5047 rtx prologue_end = NULL_RTX;
5048 #endif
5049 #if defined (HAVE_epilogue) || defined(HAVE_return)
5050 rtx epilogue_end = NULL_RTX;
5051 #endif
5052 edge_iterator ei;
5054 #ifdef HAVE_prologue
5055 if (HAVE_prologue)
5057 start_sequence ();
5058 seq = gen_prologue ();
5059 emit_insn (seq);
5061 /* Retain a map of the prologue insns. */
5062 record_insns (seq, &prologue);
5063 prologue_end = emit_note (NOTE_INSN_PROLOGUE_END);
5065 seq = get_insns ();
5066 end_sequence ();
5067 set_insn_locators (seq, prologue_locator);
5069 /* Can't deal with multiple successors of the entry block
5070 at the moment. Function should always have at least one
5071 entry point. */
5072 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR));
5074 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
5075 inserted = 1;
5077 #endif
5079 /* If the exit block has no non-fake predecessors, we don't need
5080 an epilogue. */
5081 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5082 if ((e->flags & EDGE_FAKE) == 0)
5083 break;
5084 if (e == NULL)
5085 goto epilogue_done;
5087 #ifdef HAVE_return
5088 if (optimize && HAVE_return)
5090 /* If we're allowed to generate a simple return instruction,
5091 then by definition we don't need a full epilogue. Examine
5092 the block that falls through to EXIT. If it does not
5093 contain any code, examine its predecessors and try to
5094 emit (conditional) return instructions. */
5096 basic_block last;
5097 rtx label;
5099 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5100 if (e->flags & EDGE_FALLTHRU)
5101 break;
5102 if (e == NULL)
5103 goto epilogue_done;
5104 last = e->src;
5106 /* Verify that there are no active instructions in the last block. */
5107 label = BB_END (last);
5108 while (label && !LABEL_P (label))
5110 if (active_insn_p (label))
5111 break;
5112 label = PREV_INSN (label);
5115 if (BB_HEAD (last) == label && LABEL_P (label))
5117 edge_iterator ei2;
5119 for (ei2 = ei_start (last->preds); (e = ei_safe_edge (ei2)); )
5121 basic_block bb = e->src;
5122 rtx jump;
5124 if (bb == ENTRY_BLOCK_PTR)
5126 ei_next (&ei2);
5127 continue;
5130 jump = BB_END (bb);
5131 if (!JUMP_P (jump) || JUMP_LABEL (jump) != label)
5133 ei_next (&ei2);
5134 continue;
5137 /* If we have an unconditional jump, we can replace that
5138 with a simple return instruction. */
5139 if (simplejump_p (jump))
5141 emit_return_into_block (bb);
5142 delete_insn (jump);
5145 /* If we have a conditional jump, we can try to replace
5146 that with a conditional return instruction. */
5147 else if (condjump_p (jump))
5149 if (! redirect_jump (jump, 0, 0))
5151 ei_next (&ei2);
5152 continue;
5155 /* If this block has only one successor, it both jumps
5156 and falls through to the fallthru block, so we can't
5157 delete the edge. */
5158 if (single_succ_p (bb))
5160 ei_next (&ei2);
5161 continue;
5164 else
5166 ei_next (&ei2);
5167 continue;
5170 /* Fix up the CFG for the successful change we just made. */
5171 redirect_edge_succ (e, EXIT_BLOCK_PTR);
5174 /* Emit a return insn for the exit fallthru block. Whether
5175 this is still reachable will be determined later. */
5177 emit_barrier_after (BB_END (last));
5178 emit_return_into_block (last);
5179 epilogue_end = BB_END (last);
5180 single_succ_edge (last)->flags &= ~EDGE_FALLTHRU;
5181 goto epilogue_done;
5184 #endif
5185 /* Find the edge that falls through to EXIT. Other edges may exist
5186 due to RETURN instructions, but those don't need epilogues.
5187 There really shouldn't be a mixture -- either all should have
5188 been converted or none, however... */
5190 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5191 if (e->flags & EDGE_FALLTHRU)
5192 break;
5193 if (e == NULL)
5194 goto epilogue_done;
5196 #ifdef HAVE_epilogue
5197 if (HAVE_epilogue)
5199 start_sequence ();
5200 epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
5202 seq = gen_epilogue ();
5204 #ifdef INCOMING_RETURN_ADDR_RTX
5205 /* If this function returns with the stack depressed and we can support
5206 it, massage the epilogue to actually do that. */
5207 if (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
5208 && TYPE_RETURNS_STACK_DEPRESSED (TREE_TYPE (current_function_decl)))
5209 seq = keep_stack_depressed (seq);
5210 #endif
5212 emit_jump_insn (seq);
5214 /* Retain a map of the epilogue insns. */
5215 record_insns (seq, &epilogue);
5216 set_insn_locators (seq, epilogue_locator);
5218 seq = get_insns ();
5219 end_sequence ();
5221 insert_insn_on_edge (seq, e);
5222 inserted = 1;
5224 else
5225 #endif
5227 basic_block cur_bb;
5229 if (! next_active_insn (BB_END (e->src)))
5230 goto epilogue_done;
5231 /* We have a fall-through edge to the exit block, the source is not
5232 at the end of the function, and there will be an assembler epilogue
5233 at the end of the function.
5234 We can't use force_nonfallthru here, because that would try to
5235 use return. Inserting a jump 'by hand' is extremely messy, so
5236 we take advantage of cfg_layout_finalize using
5237 fixup_fallthru_exit_predecessor. */
5238 cfg_layout_initialize (0);
5239 FOR_EACH_BB (cur_bb)
5240 if (cur_bb->index >= NUM_FIXED_BLOCKS
5241 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
5242 cur_bb->aux = cur_bb->next_bb;
5243 cfg_layout_finalize ();
5245 epilogue_done:
5247 if (inserted)
5248 commit_edge_insertions ();
5250 #ifdef HAVE_sibcall_epilogue
5251 /* Emit sibling epilogues before any sibling call sites. */
5252 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
5254 basic_block bb = e->src;
5255 rtx insn = BB_END (bb);
5257 if (!CALL_P (insn)
5258 || ! SIBLING_CALL_P (insn))
5260 ei_next (&ei);
5261 continue;
5264 start_sequence ();
5265 emit_insn (gen_sibcall_epilogue ());
5266 seq = get_insns ();
5267 end_sequence ();
5269 /* Retain a map of the epilogue insns. Used in life analysis to
5270 avoid getting rid of sibcall epilogue insns. Do this before we
5271 actually emit the sequence. */
5272 record_insns (seq, &sibcall_epilogue);
5273 set_insn_locators (seq, epilogue_locator);
5275 emit_insn_before (seq, insn);
5276 ei_next (&ei);
5278 #endif
5280 #ifdef HAVE_epilogue
5281 if (epilogue_end)
5283 rtx insn, next;
5285 /* Similarly, move any line notes that appear after the epilogue.
5286 There is no need, however, to be quite so anal about the existence
5287 of such a note. Also possibly move
5288 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
5289 info generation. */
5290 for (insn = epilogue_end; insn; insn = next)
5292 next = NEXT_INSN (insn);
5293 if (NOTE_P (insn)
5294 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG))
5295 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
5298 #endif
5301 /* Reposition the prologue-end and epilogue-begin notes after instruction
5302 scheduling and delayed branch scheduling. */
5304 void
5305 reposition_prologue_and_epilogue_notes (rtx f ATTRIBUTE_UNUSED)
5307 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
5308 rtx insn, last, note;
5309 int len;
5311 if ((len = VEC_length (int, prologue)) > 0)
5313 last = 0, note = 0;
5315 /* Scan from the beginning until we reach the last prologue insn.
5316 We apparently can't depend on basic_block_{head,end} after
5317 reorg has run. */
5318 for (insn = f; insn; insn = NEXT_INSN (insn))
5320 if (NOTE_P (insn))
5322 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
5323 note = insn;
5325 else if (contains (insn, &prologue))
5327 last = insn;
5328 if (--len == 0)
5329 break;
5333 if (last)
5335 /* Find the prologue-end note if we haven't already, and
5336 move it to just after the last prologue insn. */
5337 if (note == 0)
5339 for (note = last; (note = NEXT_INSN (note));)
5340 if (NOTE_P (note)
5341 && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
5342 break;
5345 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
5346 if (LABEL_P (last))
5347 last = NEXT_INSN (last);
5348 reorder_insns (note, note, last);
5352 if ((len = VEC_length (int, epilogue)) > 0)
5354 last = 0, note = 0;
5356 /* Scan from the end until we reach the first epilogue insn.
5357 We apparently can't depend on basic_block_{head,end} after
5358 reorg has run. */
5359 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
5361 if (NOTE_P (insn))
5363 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
5364 note = insn;
5366 else if (contains (insn, &epilogue))
5368 last = insn;
5369 if (--len == 0)
5370 break;
5374 if (last)
5376 /* Find the epilogue-begin note if we haven't already, and
5377 move it to just before the first epilogue insn. */
5378 if (note == 0)
5380 for (note = insn; (note = PREV_INSN (note));)
5381 if (NOTE_P (note)
5382 && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
5383 break;
5386 if (PREV_INSN (last) != note)
5387 reorder_insns (note, note, PREV_INSN (last));
5390 #endif /* HAVE_prologue or HAVE_epilogue */
5393 /* Resets insn_block_boundaries array. */
5395 void
5396 reset_block_changes (void)
5398 cfun->ib_boundaries_block = VEC_alloc (tree, gc, 100);
5399 VEC_quick_push (tree, cfun->ib_boundaries_block, NULL_TREE);
5402 /* Record the boundary for BLOCK. */
5403 void
5404 record_block_change (tree block)
5406 int i, n;
5407 tree last_block;
5409 if (!block)
5410 return;
5412 if(!cfun->ib_boundaries_block)
5413 return;
5415 last_block = VEC_pop (tree, cfun->ib_boundaries_block);
5416 n = get_max_uid ();
5417 for (i = VEC_length (tree, cfun->ib_boundaries_block); i < n; i++)
5418 VEC_safe_push (tree, gc, cfun->ib_boundaries_block, last_block);
5420 VEC_safe_push (tree, gc, cfun->ib_boundaries_block, block);
5423 /* Finishes record of boundaries. */
5424 void
5425 finalize_block_changes (void)
5427 record_block_change (DECL_INITIAL (current_function_decl));
5430 /* For INSN return the BLOCK it belongs to. */
5431 void
5432 check_block_change (rtx insn, tree *block)
5434 unsigned uid = INSN_UID (insn);
5436 if (uid >= VEC_length (tree, cfun->ib_boundaries_block))
5437 return;
5439 *block = VEC_index (tree, cfun->ib_boundaries_block, uid);
5442 /* Releases the ib_boundaries_block records. */
5443 void
5444 free_block_changes (void)
5446 VEC_free (tree, gc, cfun->ib_boundaries_block);
5449 /* Returns the name of the current function. */
5450 const char *
5451 current_function_name (void)
5453 return lang_hooks.decl_printable_name (cfun->decl, 2);
5457 static unsigned int
5458 rest_of_handle_check_leaf_regs (void)
5460 #ifdef LEAF_REGISTERS
5461 current_function_uses_only_leaf_regs
5462 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
5463 #endif
5464 return 0;
5467 /* Insert a TYPE into the used types hash table of CFUN. */
5468 static void
5469 used_types_insert_helper (tree type, struct function *func)
5471 if (type != NULL && func != NULL)
5473 void **slot;
5475 if (func->used_types_hash == NULL)
5476 func->used_types_hash = htab_create_ggc (37, htab_hash_pointer,
5477 htab_eq_pointer, NULL);
5478 slot = htab_find_slot (func->used_types_hash, type, INSERT);
5479 if (*slot == NULL)
5480 *slot = type;
5484 /* Given a type, insert it into the used hash table in cfun. */
5485 void
5486 used_types_insert (tree t)
5488 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
5489 t = TREE_TYPE (t);
5490 t = TYPE_MAIN_VARIANT (t);
5491 if (debug_info_level > DINFO_LEVEL_NONE)
5492 used_types_insert_helper (t, cfun);
5495 struct tree_opt_pass pass_leaf_regs =
5497 NULL, /* name */
5498 NULL, /* gate */
5499 rest_of_handle_check_leaf_regs, /* execute */
5500 NULL, /* sub */
5501 NULL, /* next */
5502 0, /* static_pass_number */
5503 0, /* tv_id */
5504 0, /* properties_required */
5505 0, /* properties_provided */
5506 0, /* properties_destroyed */
5507 0, /* todo_flags_start */
5508 0, /* todo_flags_finish */
5509 0 /* letter */
5513 #include "gt-function.h"