* arm.h (TARGET_CPU_CPP_BUILTINS): Remove Maverick support.
[official-gcc.git] / gcc / function.c
blob518d52401c5ade0bf7893978723960d49410164a
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, 2007, 2008, 2009,
4 2010, 2011, 2012 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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file handles the generation of rtl code from tree structure
23 at the level of the function as a whole.
24 It creates the rtl expressions for parameters and auto variables
25 and has full responsibility for allocating stack slots.
27 `expand_function_start' is called at the beginning of a function,
28 before the function body is parsed, and `expand_function_end' is
29 called after parsing the body.
31 Call `assign_stack_local' to allocate a stack slot for a local variable.
32 This is usually done during the RTL generation for the function body,
33 but it can also be done in the reload pass when a pseudo-register does
34 not get a hard register. */
36 #include "config.h"
37 #include "system.h"
38 #include "coretypes.h"
39 #include "tm.h"
40 #include "rtl-error.h"
41 #include "tree.h"
42 #include "flags.h"
43 #include "except.h"
44 #include "function.h"
45 #include "expr.h"
46 #include "optabs.h"
47 #include "libfuncs.h"
48 #include "regs.h"
49 #include "hard-reg-set.h"
50 #include "insn-config.h"
51 #include "recog.h"
52 #include "output.h"
53 #include "basic-block.h"
54 #include "hashtab.h"
55 #include "ggc.h"
56 #include "tm_p.h"
57 #include "langhooks.h"
58 #include "target.h"
59 #include "common/common-target.h"
60 #include "gimple.h"
61 #include "tree-pass.h"
62 #include "predict.h"
63 #include "df.h"
64 #include "timevar.h"
65 #include "vecprim.h"
66 #include "params.h"
67 #include "bb-reorder.h"
69 /* So we can assign to cfun in this file. */
70 #undef cfun
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 once virtual register instantiation has been done.
96 assign_stack_local uses frame_pointer_rtx when this is nonzero.
97 calls.c:emit_library_call_value_1 uses it to set up
98 post-instantiation libcalls. */
99 int virtuals_instantiated;
101 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
102 static GTY(()) int funcdef_no;
104 /* These variables hold pointers to functions to create and destroy
105 target specific, per-function data structures. */
106 struct machine_function * (*init_machine_status) (void);
108 /* The currently compiled function. */
109 struct function *cfun = 0;
111 /* These hashes record the prologue and epilogue insns. */
112 static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
113 htab_t prologue_insn_hash;
114 static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
115 htab_t epilogue_insn_hash;
118 htab_t types_used_by_vars_hash = NULL;
119 VEC(tree,gc) *types_used_by_cur_var_decl;
121 /* Forward declarations. */
123 static struct temp_slot *find_temp_slot_from_address (rtx);
124 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
125 static void pad_below (struct args_size *, enum machine_mode, tree);
126 static void reorder_blocks_1 (rtx, tree, VEC(tree,heap) **);
127 static int all_blocks (tree, tree *);
128 static tree *get_block_vector (tree, int *);
129 extern tree debug_find_var_in_block_tree (tree, tree);
130 /* We always define `record_insns' even if it's not used so that we
131 can always export `prologue_epilogue_contains'. */
132 static void record_insns (rtx, rtx, htab_t *) ATTRIBUTE_UNUSED;
133 static bool contains (const_rtx, htab_t);
134 static void prepare_function_start (void);
135 static void do_clobber_return_reg (rtx, void *);
136 static void do_use_return_reg (rtx, void *);
137 static void set_insn_locators (rtx, int) ATTRIBUTE_UNUSED;
139 /* Stack of nested functions. */
140 /* Keep track of the cfun stack. */
142 typedef struct function *function_p;
144 DEF_VEC_P(function_p);
145 DEF_VEC_ALLOC_P(function_p,heap);
146 static VEC(function_p,heap) *function_context_stack;
148 /* Save the current context for compilation of a nested function.
149 This is called from language-specific code. */
151 void
152 push_function_context (void)
154 if (cfun == 0)
155 allocate_struct_function (NULL, false);
157 VEC_safe_push (function_p, heap, function_context_stack, cfun);
158 set_cfun (NULL);
161 /* Restore the last saved context, at the end of a nested function.
162 This function is called from language-specific code. */
164 void
165 pop_function_context (void)
167 struct function *p = VEC_pop (function_p, function_context_stack);
168 set_cfun (p);
169 current_function_decl = p->decl;
171 /* Reset variables that have known state during rtx generation. */
172 virtuals_instantiated = 0;
173 generating_concat_p = 1;
176 /* Clear out all parts of the state in F that can safely be discarded
177 after the function has been parsed, but not compiled, to let
178 garbage collection reclaim the memory. */
180 void
181 free_after_parsing (struct function *f)
183 f->language = 0;
186 /* Clear out all parts of the state in F that can safely be discarded
187 after the function has been compiled, to let garbage collection
188 reclaim the memory. */
190 void
191 free_after_compilation (struct function *f)
193 prologue_insn_hash = NULL;
194 epilogue_insn_hash = NULL;
196 free (crtl->emit.regno_pointer_align);
198 memset (crtl, 0, sizeof (struct rtl_data));
199 f->eh = NULL;
200 f->machine = NULL;
201 f->cfg = NULL;
203 regno_reg_rtx = NULL;
204 insn_locators_free ();
207 /* Return size needed for stack frame based on slots so far allocated.
208 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
209 the caller may have to do that. */
211 HOST_WIDE_INT
212 get_frame_size (void)
214 if (FRAME_GROWS_DOWNWARD)
215 return -frame_offset;
216 else
217 return frame_offset;
220 /* Issue an error message and return TRUE if frame OFFSET overflows in
221 the signed target pointer arithmetics for function FUNC. Otherwise
222 return FALSE. */
224 bool
225 frame_offset_overflow (HOST_WIDE_INT offset, tree func)
227 unsigned HOST_WIDE_INT size = FRAME_GROWS_DOWNWARD ? -offset : offset;
229 if (size > ((unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (Pmode) - 1))
230 /* Leave room for the fixed part of the frame. */
231 - 64 * UNITS_PER_WORD)
233 error_at (DECL_SOURCE_LOCATION (func),
234 "total size of local objects too large");
235 return TRUE;
238 return FALSE;
241 /* Return stack slot alignment in bits for TYPE and MODE. */
243 static unsigned int
244 get_stack_local_alignment (tree type, enum machine_mode mode)
246 unsigned int alignment;
248 if (mode == BLKmode)
249 alignment = BIGGEST_ALIGNMENT;
250 else
251 alignment = GET_MODE_ALIGNMENT (mode);
253 /* Allow the frond-end to (possibly) increase the alignment of this
254 stack slot. */
255 if (! type)
256 type = lang_hooks.types.type_for_mode (mode, 0);
258 return STACK_SLOT_ALIGNMENT (type, mode, alignment);
261 /* Determine whether it is possible to fit a stack slot of size SIZE and
262 alignment ALIGNMENT into an area in the stack frame that starts at
263 frame offset START and has a length of LENGTH. If so, store the frame
264 offset to be used for the stack slot in *POFFSET and return true;
265 return false otherwise. This function will extend the frame size when
266 given a start/length pair that lies at the end of the frame. */
268 static bool
269 try_fit_stack_local (HOST_WIDE_INT start, HOST_WIDE_INT length,
270 HOST_WIDE_INT size, unsigned int alignment,
271 HOST_WIDE_INT *poffset)
273 HOST_WIDE_INT this_frame_offset;
274 int frame_off, frame_alignment, frame_phase;
276 /* Calculate how many bytes the start of local variables is off from
277 stack alignment. */
278 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
279 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
280 frame_phase = frame_off ? frame_alignment - frame_off : 0;
282 /* Round the frame offset to the specified alignment. */
284 /* We must be careful here, since FRAME_OFFSET might be negative and
285 division with a negative dividend isn't as well defined as we might
286 like. So we instead assume that ALIGNMENT is a power of two and
287 use logical operations which are unambiguous. */
288 if (FRAME_GROWS_DOWNWARD)
289 this_frame_offset
290 = (FLOOR_ROUND (start + length - size - frame_phase,
291 (unsigned HOST_WIDE_INT) alignment)
292 + frame_phase);
293 else
294 this_frame_offset
295 = (CEIL_ROUND (start - frame_phase,
296 (unsigned HOST_WIDE_INT) alignment)
297 + frame_phase);
299 /* See if it fits. If this space is at the edge of the frame,
300 consider extending the frame to make it fit. Our caller relies on
301 this when allocating a new slot. */
302 if (frame_offset == start && this_frame_offset < frame_offset)
303 frame_offset = this_frame_offset;
304 else if (this_frame_offset < start)
305 return false;
306 else if (start + length == frame_offset
307 && this_frame_offset + size > start + length)
308 frame_offset = this_frame_offset + size;
309 else if (this_frame_offset + size > start + length)
310 return false;
312 *poffset = this_frame_offset;
313 return true;
316 /* Create a new frame_space structure describing free space in the stack
317 frame beginning at START and ending at END, and chain it into the
318 function's frame_space_list. */
320 static void
321 add_frame_space (HOST_WIDE_INT start, HOST_WIDE_INT end)
323 struct frame_space *space = ggc_alloc_frame_space ();
324 space->next = crtl->frame_space_list;
325 crtl->frame_space_list = space;
326 space->start = start;
327 space->length = end - start;
330 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
331 with machine mode MODE.
333 ALIGN controls the amount of alignment for the address of the slot:
334 0 means according to MODE,
335 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
336 -2 means use BITS_PER_UNIT,
337 positive specifies alignment boundary in bits.
339 KIND has ASLK_REDUCE_ALIGN bit set if it is OK to reduce
340 alignment and ASLK_RECORD_PAD bit set if we should remember
341 extra space we allocated for alignment purposes. When we are
342 called from assign_stack_temp_for_type, it is not set so we don't
343 track the same stack slot in two independent lists.
345 We do not round to stack_boundary here. */
348 assign_stack_local_1 (enum machine_mode mode, HOST_WIDE_INT size,
349 int align, int kind)
351 rtx x, addr;
352 int bigend_correction = 0;
353 HOST_WIDE_INT slot_offset = 0, old_frame_offset;
354 unsigned int alignment, alignment_in_bits;
356 if (align == 0)
358 alignment = get_stack_local_alignment (NULL, mode);
359 alignment /= BITS_PER_UNIT;
361 else if (align == -1)
363 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
364 size = CEIL_ROUND (size, alignment);
366 else if (align == -2)
367 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
368 else
369 alignment = align / BITS_PER_UNIT;
371 alignment_in_bits = alignment * BITS_PER_UNIT;
373 /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
374 if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
376 alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
377 alignment = alignment_in_bits / BITS_PER_UNIT;
380 if (SUPPORTS_STACK_ALIGNMENT)
382 if (crtl->stack_alignment_estimated < alignment_in_bits)
384 if (!crtl->stack_realign_processed)
385 crtl->stack_alignment_estimated = alignment_in_bits;
386 else
388 /* If stack is realigned and stack alignment value
389 hasn't been finalized, it is OK not to increase
390 stack_alignment_estimated. The bigger alignment
391 requirement is recorded in stack_alignment_needed
392 below. */
393 gcc_assert (!crtl->stack_realign_finalized);
394 if (!crtl->stack_realign_needed)
396 /* It is OK to reduce the alignment as long as the
397 requested size is 0 or the estimated stack
398 alignment >= mode alignment. */
399 gcc_assert ((kind & ASLK_REDUCE_ALIGN)
400 || size == 0
401 || (crtl->stack_alignment_estimated
402 >= GET_MODE_ALIGNMENT (mode)));
403 alignment_in_bits = crtl->stack_alignment_estimated;
404 alignment = alignment_in_bits / BITS_PER_UNIT;
410 if (crtl->stack_alignment_needed < alignment_in_bits)
411 crtl->stack_alignment_needed = alignment_in_bits;
412 if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
413 crtl->max_used_stack_slot_alignment = alignment_in_bits;
415 if (mode != BLKmode || size != 0)
417 if (kind & ASLK_RECORD_PAD)
419 struct frame_space **psp;
421 for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
423 struct frame_space *space = *psp;
424 if (!try_fit_stack_local (space->start, space->length, size,
425 alignment, &slot_offset))
426 continue;
427 *psp = space->next;
428 if (slot_offset > space->start)
429 add_frame_space (space->start, slot_offset);
430 if (slot_offset + size < space->start + space->length)
431 add_frame_space (slot_offset + size,
432 space->start + space->length);
433 goto found_space;
437 else if (!STACK_ALIGNMENT_NEEDED)
439 slot_offset = frame_offset;
440 goto found_space;
443 old_frame_offset = frame_offset;
445 if (FRAME_GROWS_DOWNWARD)
447 frame_offset -= size;
448 try_fit_stack_local (frame_offset, size, size, alignment, &slot_offset);
450 if (kind & ASLK_RECORD_PAD)
452 if (slot_offset > frame_offset)
453 add_frame_space (frame_offset, slot_offset);
454 if (slot_offset + size < old_frame_offset)
455 add_frame_space (slot_offset + size, old_frame_offset);
458 else
460 frame_offset += size;
461 try_fit_stack_local (old_frame_offset, size, size, alignment, &slot_offset);
463 if (kind & ASLK_RECORD_PAD)
465 if (slot_offset > old_frame_offset)
466 add_frame_space (old_frame_offset, slot_offset);
467 if (slot_offset + size < frame_offset)
468 add_frame_space (slot_offset + size, frame_offset);
472 found_space:
473 /* On a big-endian machine, if we are allocating more space than we will use,
474 use the least significant bytes of those that are allocated. */
475 if (BYTES_BIG_ENDIAN && mode != BLKmode && GET_MODE_SIZE (mode) < size)
476 bigend_correction = size - GET_MODE_SIZE (mode);
478 /* If we have already instantiated virtual registers, return the actual
479 address relative to the frame pointer. */
480 if (virtuals_instantiated)
481 addr = plus_constant (Pmode, frame_pointer_rtx,
482 trunc_int_for_mode
483 (slot_offset + bigend_correction
484 + STARTING_FRAME_OFFSET, Pmode));
485 else
486 addr = plus_constant (Pmode, virtual_stack_vars_rtx,
487 trunc_int_for_mode
488 (slot_offset + bigend_correction,
489 Pmode));
491 x = gen_rtx_MEM (mode, addr);
492 set_mem_align (x, alignment_in_bits);
493 MEM_NOTRAP_P (x) = 1;
495 stack_slot_list
496 = gen_rtx_EXPR_LIST (VOIDmode, x, stack_slot_list);
498 if (frame_offset_overflow (frame_offset, current_function_decl))
499 frame_offset = 0;
501 return x;
504 /* Wrap up assign_stack_local_1 with last parameter as false. */
507 assign_stack_local (enum machine_mode mode, HOST_WIDE_INT size, int align)
509 return assign_stack_local_1 (mode, size, align, ASLK_RECORD_PAD);
512 /* In order to evaluate some expressions, such as function calls returning
513 structures in memory, we need to temporarily allocate stack locations.
514 We record each allocated temporary in the following structure.
516 Associated with each temporary slot is a nesting level. When we pop up
517 one level, all temporaries associated with the previous level are freed.
518 Normally, all temporaries are freed after the execution of the statement
519 in which they were created. However, if we are inside a ({...}) grouping,
520 the result may be in a temporary and hence must be preserved. If the
521 result could be in a temporary, we preserve it if we can determine which
522 one it is in. If we cannot determine which temporary may contain the
523 result, all temporaries are preserved. A temporary is preserved by
524 pretending it was allocated at the previous nesting level. */
526 struct GTY(()) temp_slot {
527 /* Points to next temporary slot. */
528 struct temp_slot *next;
529 /* Points to previous temporary slot. */
530 struct temp_slot *prev;
531 /* The rtx to used to reference the slot. */
532 rtx slot;
533 /* The size, in units, of the slot. */
534 HOST_WIDE_INT size;
535 /* The type of the object in the slot, or zero if it doesn't correspond
536 to a type. We use this to determine whether a slot can be reused.
537 It can be reused if objects of the type of the new slot will always
538 conflict with objects of the type of the old slot. */
539 tree type;
540 /* The alignment (in bits) of the slot. */
541 unsigned int align;
542 /* Nonzero if this temporary is currently in use. */
543 char in_use;
544 /* Nesting level at which this slot is being used. */
545 int level;
546 /* The offset of the slot from the frame_pointer, including extra space
547 for alignment. This info is for combine_temp_slots. */
548 HOST_WIDE_INT base_offset;
549 /* The size of the slot, including extra space for alignment. This
550 info is for combine_temp_slots. */
551 HOST_WIDE_INT full_size;
554 /* A table of addresses that represent a stack slot. The table is a mapping
555 from address RTXen to a temp slot. */
556 static GTY((param_is(struct temp_slot_address_entry))) htab_t temp_slot_address_table;
557 static size_t n_temp_slots_in_use;
559 /* Entry for the above hash table. */
560 struct GTY(()) temp_slot_address_entry {
561 hashval_t hash;
562 rtx address;
563 struct temp_slot *temp_slot;
566 /* Removes temporary slot TEMP from LIST. */
568 static void
569 cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
571 if (temp->next)
572 temp->next->prev = temp->prev;
573 if (temp->prev)
574 temp->prev->next = temp->next;
575 else
576 *list = temp->next;
578 temp->prev = temp->next = NULL;
581 /* Inserts temporary slot TEMP to LIST. */
583 static void
584 insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
586 temp->next = *list;
587 if (*list)
588 (*list)->prev = temp;
589 temp->prev = NULL;
590 *list = temp;
593 /* Returns the list of used temp slots at LEVEL. */
595 static struct temp_slot **
596 temp_slots_at_level (int level)
598 if (level >= (int) VEC_length (temp_slot_p, used_temp_slots))
599 VEC_safe_grow_cleared (temp_slot_p, gc, used_temp_slots, level + 1);
601 return &(VEC_address (temp_slot_p, used_temp_slots)[level]);
604 /* Returns the maximal temporary slot level. */
606 static int
607 max_slot_level (void)
609 if (!used_temp_slots)
610 return -1;
612 return VEC_length (temp_slot_p, used_temp_slots) - 1;
615 /* Moves temporary slot TEMP to LEVEL. */
617 static void
618 move_slot_to_level (struct temp_slot *temp, int level)
620 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
621 insert_slot_to_list (temp, temp_slots_at_level (level));
622 temp->level = level;
625 /* Make temporary slot TEMP available. */
627 static void
628 make_slot_available (struct temp_slot *temp)
630 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
631 insert_slot_to_list (temp, &avail_temp_slots);
632 temp->in_use = 0;
633 temp->level = -1;
634 n_temp_slots_in_use--;
637 /* Compute the hash value for an address -> temp slot mapping.
638 The value is cached on the mapping entry. */
639 static hashval_t
640 temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
642 int do_not_record = 0;
643 return hash_rtx (t->address, GET_MODE (t->address),
644 &do_not_record, NULL, false);
647 /* Return the hash value for an address -> temp slot mapping. */
648 static hashval_t
649 temp_slot_address_hash (const void *p)
651 const struct temp_slot_address_entry *t;
652 t = (const struct temp_slot_address_entry *) p;
653 return t->hash;
656 /* Compare two address -> temp slot mapping entries. */
657 static int
658 temp_slot_address_eq (const void *p1, const void *p2)
660 const struct temp_slot_address_entry *t1, *t2;
661 t1 = (const struct temp_slot_address_entry *) p1;
662 t2 = (const struct temp_slot_address_entry *) p2;
663 return exp_equiv_p (t1->address, t2->address, 0, true);
666 /* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping. */
667 static void
668 insert_temp_slot_address (rtx address, struct temp_slot *temp_slot)
670 void **slot;
671 struct temp_slot_address_entry *t = ggc_alloc_temp_slot_address_entry ();
672 t->address = address;
673 t->temp_slot = temp_slot;
674 t->hash = temp_slot_address_compute_hash (t);
675 slot = htab_find_slot_with_hash (temp_slot_address_table, t, t->hash, INSERT);
676 *slot = t;
679 /* Remove an address -> temp slot mapping entry if the temp slot is
680 not in use anymore. Callback for remove_unused_temp_slot_addresses. */
681 static int
682 remove_unused_temp_slot_addresses_1 (void **slot, void *data ATTRIBUTE_UNUSED)
684 const struct temp_slot_address_entry *t;
685 t = (const struct temp_slot_address_entry *) *slot;
686 if (! t->temp_slot->in_use)
687 htab_clear_slot (temp_slot_address_table, slot);
688 return 1;
691 /* Remove all mappings of addresses to unused temp slots. */
692 static void
693 remove_unused_temp_slot_addresses (void)
695 /* Use quicker clearing if there aren't any active temp slots. */
696 if (n_temp_slots_in_use)
697 htab_traverse (temp_slot_address_table,
698 remove_unused_temp_slot_addresses_1,
699 NULL);
700 else
701 htab_empty (temp_slot_address_table);
704 /* Find the temp slot corresponding to the object at address X. */
706 static struct temp_slot *
707 find_temp_slot_from_address (rtx x)
709 struct temp_slot *p;
710 struct temp_slot_address_entry tmp, *t;
712 /* First try the easy way:
713 See if X exists in the address -> temp slot mapping. */
714 tmp.address = x;
715 tmp.temp_slot = NULL;
716 tmp.hash = temp_slot_address_compute_hash (&tmp);
717 t = (struct temp_slot_address_entry *)
718 htab_find_with_hash (temp_slot_address_table, &tmp, tmp.hash);
719 if (t)
720 return t->temp_slot;
722 /* If we have a sum involving a register, see if it points to a temp
723 slot. */
724 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
725 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
726 return p;
727 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
728 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
729 return p;
731 /* Last resort: Address is a virtual stack var address. */
732 if (GET_CODE (x) == PLUS
733 && XEXP (x, 0) == virtual_stack_vars_rtx
734 && CONST_INT_P (XEXP (x, 1)))
736 int i;
737 for (i = max_slot_level (); i >= 0; i--)
738 for (p = *temp_slots_at_level (i); p; p = p->next)
740 if (INTVAL (XEXP (x, 1)) >= p->base_offset
741 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size)
742 return p;
746 return NULL;
749 /* Allocate a temporary stack slot and record it for possible later
750 reuse.
752 MODE is the machine mode to be given to the returned rtx.
754 SIZE is the size in units of the space required. We do no rounding here
755 since assign_stack_local will do any required rounding.
757 TYPE is the type that will be used for the stack slot. */
760 assign_stack_temp_for_type (enum machine_mode mode, HOST_WIDE_INT size,
761 tree type)
763 unsigned int align;
764 struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
765 rtx slot;
767 /* If SIZE is -1 it means that somebody tried to allocate a temporary
768 of a variable size. */
769 gcc_assert (size != -1);
771 align = get_stack_local_alignment (type, mode);
773 /* Try to find an available, already-allocated temporary of the proper
774 mode which meets the size and alignment requirements. Choose the
775 smallest one with the closest alignment.
777 If assign_stack_temp is called outside of the tree->rtl expansion,
778 we cannot reuse the stack slots (that may still refer to
779 VIRTUAL_STACK_VARS_REGNUM). */
780 if (!virtuals_instantiated)
782 for (p = avail_temp_slots; p; p = p->next)
784 if (p->align >= align && p->size >= size
785 && GET_MODE (p->slot) == mode
786 && objects_must_conflict_p (p->type, type)
787 && (best_p == 0 || best_p->size > p->size
788 || (best_p->size == p->size && best_p->align > p->align)))
790 if (p->align == align && p->size == size)
792 selected = p;
793 cut_slot_from_list (selected, &avail_temp_slots);
794 best_p = 0;
795 break;
797 best_p = p;
802 /* Make our best, if any, the one to use. */
803 if (best_p)
805 selected = best_p;
806 cut_slot_from_list (selected, &avail_temp_slots);
808 /* If there are enough aligned bytes left over, make them into a new
809 temp_slot so that the extra bytes don't get wasted. Do this only
810 for BLKmode slots, so that we can be sure of the alignment. */
811 if (GET_MODE (best_p->slot) == BLKmode)
813 int alignment = best_p->align / BITS_PER_UNIT;
814 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
816 if (best_p->size - rounded_size >= alignment)
818 p = ggc_alloc_temp_slot ();
819 p->in_use = 0;
820 p->size = best_p->size - rounded_size;
821 p->base_offset = best_p->base_offset + rounded_size;
822 p->full_size = best_p->full_size - rounded_size;
823 p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
824 p->align = best_p->align;
825 p->type = best_p->type;
826 insert_slot_to_list (p, &avail_temp_slots);
828 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
829 stack_slot_list);
831 best_p->size = rounded_size;
832 best_p->full_size = rounded_size;
837 /* If we still didn't find one, make a new temporary. */
838 if (selected == 0)
840 HOST_WIDE_INT frame_offset_old = frame_offset;
842 p = ggc_alloc_temp_slot ();
844 /* We are passing an explicit alignment request to assign_stack_local.
845 One side effect of that is assign_stack_local will not round SIZE
846 to ensure the frame offset remains suitably aligned.
848 So for requests which depended on the rounding of SIZE, we go ahead
849 and round it now. We also make sure ALIGNMENT is at least
850 BIGGEST_ALIGNMENT. */
851 gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
852 p->slot = assign_stack_local_1 (mode,
853 (mode == BLKmode
854 ? CEIL_ROUND (size,
855 (int) align
856 / BITS_PER_UNIT)
857 : size),
858 align, 0);
860 p->align = align;
862 /* The following slot size computation is necessary because we don't
863 know the actual size of the temporary slot until assign_stack_local
864 has performed all the frame alignment and size rounding for the
865 requested temporary. Note that extra space added for alignment
866 can be either above or below this stack slot depending on which
867 way the frame grows. We include the extra space if and only if it
868 is above this slot. */
869 if (FRAME_GROWS_DOWNWARD)
870 p->size = frame_offset_old - frame_offset;
871 else
872 p->size = size;
874 /* Now define the fields used by combine_temp_slots. */
875 if (FRAME_GROWS_DOWNWARD)
877 p->base_offset = frame_offset;
878 p->full_size = frame_offset_old - frame_offset;
880 else
882 p->base_offset = frame_offset_old;
883 p->full_size = frame_offset - frame_offset_old;
886 selected = p;
889 p = selected;
890 p->in_use = 1;
891 p->type = type;
892 p->level = temp_slot_level;
893 n_temp_slots_in_use++;
895 pp = temp_slots_at_level (p->level);
896 insert_slot_to_list (p, pp);
897 insert_temp_slot_address (XEXP (p->slot, 0), p);
899 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
900 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
901 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
903 /* If we know the alias set for the memory that will be used, use
904 it. If there's no TYPE, then we don't know anything about the
905 alias set for the memory. */
906 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
907 set_mem_align (slot, align);
909 /* If a type is specified, set the relevant flags. */
910 if (type != 0)
911 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
912 MEM_NOTRAP_P (slot) = 1;
914 return slot;
917 /* Allocate a temporary stack slot and record it for possible later
918 reuse. First two arguments are same as in preceding function. */
921 assign_stack_temp (enum machine_mode mode, HOST_WIDE_INT size)
923 return assign_stack_temp_for_type (mode, size, NULL_TREE);
926 /* Assign a temporary.
927 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
928 and so that should be used in error messages. In either case, we
929 allocate of the given type.
930 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
931 it is 0 if a register is OK.
932 DONT_PROMOTE is 1 if we should not promote values in register
933 to wider modes. */
936 assign_temp (tree type_or_decl, int memory_required,
937 int dont_promote ATTRIBUTE_UNUSED)
939 tree type, decl;
940 enum machine_mode mode;
941 #ifdef PROMOTE_MODE
942 int unsignedp;
943 #endif
945 if (DECL_P (type_or_decl))
946 decl = type_or_decl, type = TREE_TYPE (decl);
947 else
948 decl = NULL, type = type_or_decl;
950 mode = TYPE_MODE (type);
951 #ifdef PROMOTE_MODE
952 unsignedp = TYPE_UNSIGNED (type);
953 #endif
955 if (mode == BLKmode || memory_required)
957 HOST_WIDE_INT size = int_size_in_bytes (type);
958 rtx tmp;
960 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
961 problems with allocating the stack space. */
962 if (size == 0)
963 size = 1;
965 /* Unfortunately, we don't yet know how to allocate variable-sized
966 temporaries. However, sometimes we can find a fixed upper limit on
967 the size, so try that instead. */
968 else if (size == -1)
969 size = max_int_size_in_bytes (type);
971 /* The size of the temporary may be too large to fit into an integer. */
972 /* ??? Not sure this should happen except for user silliness, so limit
973 this to things that aren't compiler-generated temporaries. The
974 rest of the time we'll die in assign_stack_temp_for_type. */
975 if (decl && size == -1
976 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
978 error ("size of variable %q+D is too large", decl);
979 size = 1;
982 tmp = assign_stack_temp_for_type (mode, size, type);
983 return tmp;
986 #ifdef PROMOTE_MODE
987 if (! dont_promote)
988 mode = promote_mode (type, mode, &unsignedp);
989 #endif
991 return gen_reg_rtx (mode);
994 /* Combine temporary stack slots which are adjacent on the stack.
996 This allows for better use of already allocated stack space. This is only
997 done for BLKmode slots because we can be sure that we won't have alignment
998 problems in this case. */
1000 static void
1001 combine_temp_slots (void)
1003 struct temp_slot *p, *q, *next, *next_q;
1004 int num_slots;
1006 /* We can't combine slots, because the information about which slot
1007 is in which alias set will be lost. */
1008 if (flag_strict_aliasing)
1009 return;
1011 /* If there are a lot of temp slots, don't do anything unless
1012 high levels of optimization. */
1013 if (! flag_expensive_optimizations)
1014 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
1015 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
1016 return;
1018 for (p = avail_temp_slots; p; p = next)
1020 int delete_p = 0;
1022 next = p->next;
1024 if (GET_MODE (p->slot) != BLKmode)
1025 continue;
1027 for (q = p->next; q; q = next_q)
1029 int delete_q = 0;
1031 next_q = q->next;
1033 if (GET_MODE (q->slot) != BLKmode)
1034 continue;
1036 if (p->base_offset + p->full_size == q->base_offset)
1038 /* Q comes after P; combine Q into P. */
1039 p->size += q->size;
1040 p->full_size += q->full_size;
1041 delete_q = 1;
1043 else if (q->base_offset + q->full_size == p->base_offset)
1045 /* P comes after Q; combine P into Q. */
1046 q->size += p->size;
1047 q->full_size += p->full_size;
1048 delete_p = 1;
1049 break;
1051 if (delete_q)
1052 cut_slot_from_list (q, &avail_temp_slots);
1055 /* Either delete P or advance past it. */
1056 if (delete_p)
1057 cut_slot_from_list (p, &avail_temp_slots);
1061 /* Indicate that NEW_RTX is an alternate way of referring to the temp
1062 slot that previously was known by OLD_RTX. */
1064 void
1065 update_temp_slot_address (rtx old_rtx, rtx new_rtx)
1067 struct temp_slot *p;
1069 if (rtx_equal_p (old_rtx, new_rtx))
1070 return;
1072 p = find_temp_slot_from_address (old_rtx);
1074 /* If we didn't find one, see if both OLD_RTX is a PLUS. If so, and
1075 NEW_RTX is a register, see if one operand of the PLUS is a
1076 temporary location. If so, NEW_RTX points into it. Otherwise,
1077 if both OLD_RTX and NEW_RTX are a PLUS and if there is a register
1078 in common between them. If so, try a recursive call on those
1079 values. */
1080 if (p == 0)
1082 if (GET_CODE (old_rtx) != PLUS)
1083 return;
1085 if (REG_P (new_rtx))
1087 update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1088 update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
1089 return;
1091 else if (GET_CODE (new_rtx) != PLUS)
1092 return;
1094 if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 0)))
1095 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 1));
1096 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 0)))
1097 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 1));
1098 else if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 1)))
1099 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 0));
1100 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 1)))
1101 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 0));
1103 return;
1106 /* Otherwise add an alias for the temp's address. */
1107 insert_temp_slot_address (new_rtx, p);
1110 /* If X could be a reference to a temporary slot, mark that slot as
1111 belonging to the to one level higher than the current level. If X
1112 matched one of our slots, just mark that one. Otherwise, we can't
1113 easily predict which it is, so upgrade all of them.
1115 This is called when an ({...}) construct occurs and a statement
1116 returns a value in memory. */
1118 void
1119 preserve_temp_slots (rtx x)
1121 struct temp_slot *p = 0, *next;
1123 if (x == 0)
1124 return;
1126 /* If X is a register that is being used as a pointer, see if we have
1127 a temporary slot we know it points to. */
1128 if (REG_P (x) && REG_POINTER (x))
1129 p = find_temp_slot_from_address (x);
1131 /* If X is not in memory or is at a constant address, it cannot be in
1132 a temporary slot. */
1133 if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1134 return;
1136 /* First see if we can find a match. */
1137 if (p == 0)
1138 p = find_temp_slot_from_address (XEXP (x, 0));
1140 if (p != 0)
1142 if (p->level == temp_slot_level)
1143 move_slot_to_level (p, temp_slot_level - 1);
1144 return;
1147 /* Otherwise, preserve all non-kept slots at this level. */
1148 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1150 next = p->next;
1151 move_slot_to_level (p, temp_slot_level - 1);
1155 /* Free all temporaries used so far. This is normally called at the
1156 end of generating code for a statement. */
1158 void
1159 free_temp_slots (void)
1161 struct temp_slot *p, *next;
1162 bool some_available = false;
1164 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1166 next = p->next;
1167 make_slot_available (p);
1168 some_available = true;
1171 if (some_available)
1173 remove_unused_temp_slot_addresses ();
1174 combine_temp_slots ();
1178 /* Push deeper into the nesting level for stack temporaries. */
1180 void
1181 push_temp_slots (void)
1183 temp_slot_level++;
1186 /* Pop a temporary nesting level. All slots in use in the current level
1187 are freed. */
1189 void
1190 pop_temp_slots (void)
1192 free_temp_slots ();
1193 temp_slot_level--;
1196 /* Initialize temporary slots. */
1198 void
1199 init_temp_slots (void)
1201 /* We have not allocated any temporaries yet. */
1202 avail_temp_slots = 0;
1203 used_temp_slots = 0;
1204 temp_slot_level = 0;
1205 n_temp_slots_in_use = 0;
1207 /* Set up the table to map addresses to temp slots. */
1208 if (! temp_slot_address_table)
1209 temp_slot_address_table = htab_create_ggc (32,
1210 temp_slot_address_hash,
1211 temp_slot_address_eq,
1212 NULL);
1213 else
1214 htab_empty (temp_slot_address_table);
1217 /* Functions and data structures to keep track of the values hard regs
1218 had at the start of the function. */
1220 /* Private type used by get_hard_reg_initial_reg, get_hard_reg_initial_val,
1221 and has_hard_reg_initial_val.. */
1222 typedef struct GTY(()) initial_value_pair {
1223 rtx hard_reg;
1224 rtx pseudo;
1225 } initial_value_pair;
1226 /* ??? This could be a VEC but there is currently no way to define an
1227 opaque VEC type. This could be worked around by defining struct
1228 initial_value_pair in function.h. */
1229 typedef struct GTY(()) initial_value_struct {
1230 int num_entries;
1231 int max_entries;
1232 initial_value_pair * GTY ((length ("%h.num_entries"))) entries;
1233 } initial_value_struct;
1235 /* If a pseudo represents an initial hard reg (or expression), return
1236 it, else return NULL_RTX. */
1239 get_hard_reg_initial_reg (rtx reg)
1241 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1242 int i;
1244 if (ivs == 0)
1245 return NULL_RTX;
1247 for (i = 0; i < ivs->num_entries; i++)
1248 if (rtx_equal_p (ivs->entries[i].pseudo, reg))
1249 return ivs->entries[i].hard_reg;
1251 return NULL_RTX;
1254 /* Make sure that there's a pseudo register of mode MODE that stores the
1255 initial value of hard register REGNO. Return an rtx for such a pseudo. */
1258 get_hard_reg_initial_val (enum machine_mode mode, unsigned int regno)
1260 struct initial_value_struct *ivs;
1261 rtx rv;
1263 rv = has_hard_reg_initial_val (mode, regno);
1264 if (rv)
1265 return rv;
1267 ivs = crtl->hard_reg_initial_vals;
1268 if (ivs == 0)
1270 ivs = ggc_alloc_initial_value_struct ();
1271 ivs->num_entries = 0;
1272 ivs->max_entries = 5;
1273 ivs->entries = ggc_alloc_vec_initial_value_pair (5);
1274 crtl->hard_reg_initial_vals = ivs;
1277 if (ivs->num_entries >= ivs->max_entries)
1279 ivs->max_entries += 5;
1280 ivs->entries = GGC_RESIZEVEC (initial_value_pair, ivs->entries,
1281 ivs->max_entries);
1284 ivs->entries[ivs->num_entries].hard_reg = gen_rtx_REG (mode, regno);
1285 ivs->entries[ivs->num_entries].pseudo = gen_reg_rtx (mode);
1287 return ivs->entries[ivs->num_entries++].pseudo;
1290 /* See if get_hard_reg_initial_val has been used to create a pseudo
1291 for the initial value of hard register REGNO in mode MODE. Return
1292 the associated pseudo if so, otherwise return NULL. */
1295 has_hard_reg_initial_val (enum machine_mode mode, unsigned int regno)
1297 struct initial_value_struct *ivs;
1298 int i;
1300 ivs = crtl->hard_reg_initial_vals;
1301 if (ivs != 0)
1302 for (i = 0; i < ivs->num_entries; i++)
1303 if (GET_MODE (ivs->entries[i].hard_reg) == mode
1304 && REGNO (ivs->entries[i].hard_reg) == regno)
1305 return ivs->entries[i].pseudo;
1307 return NULL_RTX;
1310 unsigned int
1311 emit_initial_value_sets (void)
1313 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1314 int i;
1315 rtx seq;
1317 if (ivs == 0)
1318 return 0;
1320 start_sequence ();
1321 for (i = 0; i < ivs->num_entries; i++)
1322 emit_move_insn (ivs->entries[i].pseudo, ivs->entries[i].hard_reg);
1323 seq = get_insns ();
1324 end_sequence ();
1326 emit_insn_at_entry (seq);
1327 return 0;
1330 /* Return the hardreg-pseudoreg initial values pair entry I and
1331 TRUE if I is a valid entry, or FALSE if I is not a valid entry. */
1332 bool
1333 initial_value_entry (int i, rtx *hreg, rtx *preg)
1335 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1336 if (!ivs || i >= ivs->num_entries)
1337 return false;
1339 *hreg = ivs->entries[i].hard_reg;
1340 *preg = ivs->entries[i].pseudo;
1341 return true;
1344 /* These routines are responsible for converting virtual register references
1345 to the actual hard register references once RTL generation is complete.
1347 The following four variables are used for communication between the
1348 routines. They contain the offsets of the virtual registers from their
1349 respective hard registers. */
1351 static int in_arg_offset;
1352 static int var_offset;
1353 static int dynamic_offset;
1354 static int out_arg_offset;
1355 static int cfa_offset;
1357 /* In most machines, the stack pointer register is equivalent to the bottom
1358 of the stack. */
1360 #ifndef STACK_POINTER_OFFSET
1361 #define STACK_POINTER_OFFSET 0
1362 #endif
1364 /* If not defined, pick an appropriate default for the offset of dynamically
1365 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1366 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
1368 #ifndef STACK_DYNAMIC_OFFSET
1370 /* The bottom of the stack points to the actual arguments. If
1371 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1372 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1373 stack space for register parameters is not pushed by the caller, but
1374 rather part of the fixed stack areas and hence not included in
1375 `crtl->outgoing_args_size'. Nevertheless, we must allow
1376 for it when allocating stack dynamic objects. */
1378 #if defined(REG_PARM_STACK_SPACE)
1379 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1380 ((ACCUMULATE_OUTGOING_ARGS \
1381 ? (crtl->outgoing_args_size \
1382 + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1383 : REG_PARM_STACK_SPACE (FNDECL))) \
1384 : 0) + (STACK_POINTER_OFFSET))
1385 #else
1386 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1387 ((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0) \
1388 + (STACK_POINTER_OFFSET))
1389 #endif
1390 #endif
1393 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1394 is a virtual register, return the equivalent hard register and set the
1395 offset indirectly through the pointer. Otherwise, return 0. */
1397 static rtx
1398 instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
1400 rtx new_rtx;
1401 HOST_WIDE_INT offset;
1403 if (x == virtual_incoming_args_rtx)
1405 if (stack_realign_drap)
1407 /* Replace virtual_incoming_args_rtx with internal arg
1408 pointer if DRAP is used to realign stack. */
1409 new_rtx = crtl->args.internal_arg_pointer;
1410 offset = 0;
1412 else
1413 new_rtx = arg_pointer_rtx, offset = in_arg_offset;
1415 else if (x == virtual_stack_vars_rtx)
1416 new_rtx = frame_pointer_rtx, offset = var_offset;
1417 else if (x == virtual_stack_dynamic_rtx)
1418 new_rtx = stack_pointer_rtx, offset = dynamic_offset;
1419 else if (x == virtual_outgoing_args_rtx)
1420 new_rtx = stack_pointer_rtx, offset = out_arg_offset;
1421 else if (x == virtual_cfa_rtx)
1423 #ifdef FRAME_POINTER_CFA_OFFSET
1424 new_rtx = frame_pointer_rtx;
1425 #else
1426 new_rtx = arg_pointer_rtx;
1427 #endif
1428 offset = cfa_offset;
1430 else if (x == virtual_preferred_stack_boundary_rtx)
1432 new_rtx = GEN_INT (crtl->preferred_stack_boundary / BITS_PER_UNIT);
1433 offset = 0;
1435 else
1436 return NULL_RTX;
1438 *poffset = offset;
1439 return new_rtx;
1442 /* A subroutine of instantiate_virtual_regs, called via for_each_rtx.
1443 Instantiate any virtual registers present inside of *LOC. The expression
1444 is simplified, as much as possible, but is not to be considered "valid"
1445 in any sense implied by the target. If any change is made, set CHANGED
1446 to true. */
1448 static int
1449 instantiate_virtual_regs_in_rtx (rtx *loc, void *data)
1451 HOST_WIDE_INT offset;
1452 bool *changed = (bool *) data;
1453 rtx x, new_rtx;
1455 x = *loc;
1456 if (x == 0)
1457 return 0;
1459 switch (GET_CODE (x))
1461 case REG:
1462 new_rtx = instantiate_new_reg (x, &offset);
1463 if (new_rtx)
1465 *loc = plus_constant (GET_MODE (x), new_rtx, offset);
1466 if (changed)
1467 *changed = true;
1469 return -1;
1471 case PLUS:
1472 new_rtx = instantiate_new_reg (XEXP (x, 0), &offset);
1473 if (new_rtx)
1475 new_rtx = plus_constant (GET_MODE (x), new_rtx, offset);
1476 *loc = simplify_gen_binary (PLUS, GET_MODE (x), new_rtx, XEXP (x, 1));
1477 if (changed)
1478 *changed = true;
1479 return -1;
1482 /* FIXME -- from old code */
1483 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1484 we can commute the PLUS and SUBREG because pointers into the
1485 frame are well-behaved. */
1486 break;
1488 default:
1489 break;
1492 return 0;
1495 /* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1496 matches the predicate for insn CODE operand OPERAND. */
1498 static int
1499 safe_insn_predicate (int code, int operand, rtx x)
1501 return code < 0 || insn_operand_matches ((enum insn_code) code, operand, x);
1504 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1505 registers present inside of insn. The result will be a valid insn. */
1507 static void
1508 instantiate_virtual_regs_in_insn (rtx insn)
1510 HOST_WIDE_INT offset;
1511 int insn_code, i;
1512 bool any_change = false;
1513 rtx set, new_rtx, x, seq;
1515 /* There are some special cases to be handled first. */
1516 set = single_set (insn);
1517 if (set)
1519 /* We're allowed to assign to a virtual register. This is interpreted
1520 to mean that the underlying register gets assigned the inverse
1521 transformation. This is used, for example, in the handling of
1522 non-local gotos. */
1523 new_rtx = instantiate_new_reg (SET_DEST (set), &offset);
1524 if (new_rtx)
1526 start_sequence ();
1528 for_each_rtx (&SET_SRC (set), instantiate_virtual_regs_in_rtx, NULL);
1529 x = simplify_gen_binary (PLUS, GET_MODE (new_rtx), SET_SRC (set),
1530 GEN_INT (-offset));
1531 x = force_operand (x, new_rtx);
1532 if (x != new_rtx)
1533 emit_move_insn (new_rtx, x);
1535 seq = get_insns ();
1536 end_sequence ();
1538 emit_insn_before (seq, insn);
1539 delete_insn (insn);
1540 return;
1543 /* Handle a straight copy from a virtual register by generating a
1544 new add insn. The difference between this and falling through
1545 to the generic case is avoiding a new pseudo and eliminating a
1546 move insn in the initial rtl stream. */
1547 new_rtx = instantiate_new_reg (SET_SRC (set), &offset);
1548 if (new_rtx && offset != 0
1549 && REG_P (SET_DEST (set))
1550 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1552 start_sequence ();
1554 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS,
1555 new_rtx, GEN_INT (offset), SET_DEST (set),
1556 1, OPTAB_LIB_WIDEN);
1557 if (x != SET_DEST (set))
1558 emit_move_insn (SET_DEST (set), x);
1560 seq = get_insns ();
1561 end_sequence ();
1563 emit_insn_before (seq, insn);
1564 delete_insn (insn);
1565 return;
1568 extract_insn (insn);
1569 insn_code = INSN_CODE (insn);
1571 /* Handle a plus involving a virtual register by determining if the
1572 operands remain valid if they're modified in place. */
1573 if (GET_CODE (SET_SRC (set)) == PLUS
1574 && recog_data.n_operands >= 3
1575 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1576 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1577 && CONST_INT_P (recog_data.operand[2])
1578 && (new_rtx = instantiate_new_reg (recog_data.operand[1], &offset)))
1580 offset += INTVAL (recog_data.operand[2]);
1582 /* If the sum is zero, then replace with a plain move. */
1583 if (offset == 0
1584 && REG_P (SET_DEST (set))
1585 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1587 start_sequence ();
1588 emit_move_insn (SET_DEST (set), new_rtx);
1589 seq = get_insns ();
1590 end_sequence ();
1592 emit_insn_before (seq, insn);
1593 delete_insn (insn);
1594 return;
1597 x = gen_int_mode (offset, recog_data.operand_mode[2]);
1599 /* Using validate_change and apply_change_group here leaves
1600 recog_data in an invalid state. Since we know exactly what
1601 we want to check, do those two by hand. */
1602 if (safe_insn_predicate (insn_code, 1, new_rtx)
1603 && safe_insn_predicate (insn_code, 2, x))
1605 *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
1606 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1607 any_change = true;
1609 /* Fall through into the regular operand fixup loop in
1610 order to take care of operands other than 1 and 2. */
1614 else
1616 extract_insn (insn);
1617 insn_code = INSN_CODE (insn);
1620 /* In the general case, we expect virtual registers to appear only in
1621 operands, and then only as either bare registers or inside memories. */
1622 for (i = 0; i < recog_data.n_operands; ++i)
1624 x = recog_data.operand[i];
1625 switch (GET_CODE (x))
1627 case MEM:
1629 rtx addr = XEXP (x, 0);
1630 bool changed = false;
1632 for_each_rtx (&addr, instantiate_virtual_regs_in_rtx, &changed);
1633 if (!changed)
1634 continue;
1636 start_sequence ();
1637 x = replace_equiv_address (x, addr);
1638 /* It may happen that the address with the virtual reg
1639 was valid (e.g. based on the virtual stack reg, which might
1640 be acceptable to the predicates with all offsets), whereas
1641 the address now isn't anymore, for instance when the address
1642 is still offsetted, but the base reg isn't virtual-stack-reg
1643 anymore. Below we would do a force_reg on the whole operand,
1644 but this insn might actually only accept memory. Hence,
1645 before doing that last resort, try to reload the address into
1646 a register, so this operand stays a MEM. */
1647 if (!safe_insn_predicate (insn_code, i, x))
1649 addr = force_reg (GET_MODE (addr), addr);
1650 x = replace_equiv_address (x, addr);
1652 seq = get_insns ();
1653 end_sequence ();
1654 if (seq)
1655 emit_insn_before (seq, insn);
1657 break;
1659 case REG:
1660 new_rtx = instantiate_new_reg (x, &offset);
1661 if (new_rtx == NULL)
1662 continue;
1663 if (offset == 0)
1664 x = new_rtx;
1665 else
1667 start_sequence ();
1669 /* Careful, special mode predicates may have stuff in
1670 insn_data[insn_code].operand[i].mode that isn't useful
1671 to us for computing a new value. */
1672 /* ??? Recognize address_operand and/or "p" constraints
1673 to see if (plus new offset) is a valid before we put
1674 this through expand_simple_binop. */
1675 x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
1676 GEN_INT (offset), NULL_RTX,
1677 1, OPTAB_LIB_WIDEN);
1678 seq = get_insns ();
1679 end_sequence ();
1680 emit_insn_before (seq, insn);
1682 break;
1684 case SUBREG:
1685 new_rtx = instantiate_new_reg (SUBREG_REG (x), &offset);
1686 if (new_rtx == NULL)
1687 continue;
1688 if (offset != 0)
1690 start_sequence ();
1691 new_rtx = expand_simple_binop (GET_MODE (new_rtx), PLUS, new_rtx,
1692 GEN_INT (offset), NULL_RTX,
1693 1, OPTAB_LIB_WIDEN);
1694 seq = get_insns ();
1695 end_sequence ();
1696 emit_insn_before (seq, insn);
1698 x = simplify_gen_subreg (recog_data.operand_mode[i], new_rtx,
1699 GET_MODE (new_rtx), SUBREG_BYTE (x));
1700 gcc_assert (x);
1701 break;
1703 default:
1704 continue;
1707 /* At this point, X contains the new value for the operand.
1708 Validate the new value vs the insn predicate. Note that
1709 asm insns will have insn_code -1 here. */
1710 if (!safe_insn_predicate (insn_code, i, x))
1712 start_sequence ();
1713 if (REG_P (x))
1715 gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1716 x = copy_to_reg (x);
1718 else
1719 x = force_reg (insn_data[insn_code].operand[i].mode, x);
1720 seq = get_insns ();
1721 end_sequence ();
1722 if (seq)
1723 emit_insn_before (seq, insn);
1726 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1727 any_change = true;
1730 if (any_change)
1732 /* Propagate operand changes into the duplicates. */
1733 for (i = 0; i < recog_data.n_dups; ++i)
1734 *recog_data.dup_loc[i]
1735 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1737 /* Force re-recognition of the instruction for validation. */
1738 INSN_CODE (insn) = -1;
1741 if (asm_noperands (PATTERN (insn)) >= 0)
1743 if (!check_asm_operands (PATTERN (insn)))
1745 error_for_asm (insn, "impossible constraint in %<asm%>");
1746 delete_insn_and_edges (insn);
1749 else
1751 if (recog_memoized (insn) < 0)
1752 fatal_insn_not_found (insn);
1756 /* Subroutine of instantiate_decls. Given RTL representing a decl,
1757 do any instantiation required. */
1759 void
1760 instantiate_decl_rtl (rtx x)
1762 rtx addr;
1764 if (x == 0)
1765 return;
1767 /* If this is a CONCAT, recurse for the pieces. */
1768 if (GET_CODE (x) == CONCAT)
1770 instantiate_decl_rtl (XEXP (x, 0));
1771 instantiate_decl_rtl (XEXP (x, 1));
1772 return;
1775 /* If this is not a MEM, no need to do anything. Similarly if the
1776 address is a constant or a register that is not a virtual register. */
1777 if (!MEM_P (x))
1778 return;
1780 addr = XEXP (x, 0);
1781 if (CONSTANT_P (addr)
1782 || (REG_P (addr)
1783 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
1784 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
1785 return;
1787 for_each_rtx (&XEXP (x, 0), instantiate_virtual_regs_in_rtx, NULL);
1790 /* Helper for instantiate_decls called via walk_tree: Process all decls
1791 in the given DECL_VALUE_EXPR. */
1793 static tree
1794 instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1796 tree t = *tp;
1797 if (! EXPR_P (t))
1799 *walk_subtrees = 0;
1800 if (DECL_P (t))
1802 if (DECL_RTL_SET_P (t))
1803 instantiate_decl_rtl (DECL_RTL (t));
1804 if (TREE_CODE (t) == PARM_DECL && DECL_NAMELESS (t)
1805 && DECL_INCOMING_RTL (t))
1806 instantiate_decl_rtl (DECL_INCOMING_RTL (t));
1807 if ((TREE_CODE (t) == VAR_DECL
1808 || TREE_CODE (t) == RESULT_DECL)
1809 && DECL_HAS_VALUE_EXPR_P (t))
1811 tree v = DECL_VALUE_EXPR (t);
1812 walk_tree (&v, instantiate_expr, NULL, NULL);
1816 return NULL;
1819 /* Subroutine of instantiate_decls: Process all decls in the given
1820 BLOCK node and all its subblocks. */
1822 static void
1823 instantiate_decls_1 (tree let)
1825 tree t;
1827 for (t = BLOCK_VARS (let); t; t = DECL_CHAIN (t))
1829 if (DECL_RTL_SET_P (t))
1830 instantiate_decl_rtl (DECL_RTL (t));
1831 if (TREE_CODE (t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (t))
1833 tree v = DECL_VALUE_EXPR (t);
1834 walk_tree (&v, instantiate_expr, NULL, NULL);
1838 /* Process all subblocks. */
1839 for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
1840 instantiate_decls_1 (t);
1843 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
1844 all virtual registers in their DECL_RTL's. */
1846 static void
1847 instantiate_decls (tree fndecl)
1849 tree decl;
1850 unsigned ix;
1852 /* Process all parameters of the function. */
1853 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = DECL_CHAIN (decl))
1855 instantiate_decl_rtl (DECL_RTL (decl));
1856 instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
1857 if (DECL_HAS_VALUE_EXPR_P (decl))
1859 tree v = DECL_VALUE_EXPR (decl);
1860 walk_tree (&v, instantiate_expr, NULL, NULL);
1864 if ((decl = DECL_RESULT (fndecl))
1865 && TREE_CODE (decl) == RESULT_DECL)
1867 if (DECL_RTL_SET_P (decl))
1868 instantiate_decl_rtl (DECL_RTL (decl));
1869 if (DECL_HAS_VALUE_EXPR_P (decl))
1871 tree v = DECL_VALUE_EXPR (decl);
1872 walk_tree (&v, instantiate_expr, NULL, NULL);
1876 /* Now process all variables defined in the function or its subblocks. */
1877 instantiate_decls_1 (DECL_INITIAL (fndecl));
1879 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
1880 if (DECL_RTL_SET_P (decl))
1881 instantiate_decl_rtl (DECL_RTL (decl));
1882 VEC_free (tree, gc, cfun->local_decls);
1885 /* Pass through the INSNS of function FNDECL and convert virtual register
1886 references to hard register references. */
1888 static unsigned int
1889 instantiate_virtual_regs (void)
1891 rtx insn;
1893 /* Compute the offsets to use for this function. */
1894 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1895 var_offset = STARTING_FRAME_OFFSET;
1896 dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
1897 out_arg_offset = STACK_POINTER_OFFSET;
1898 #ifdef FRAME_POINTER_CFA_OFFSET
1899 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1900 #else
1901 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1902 #endif
1904 /* Initialize recognition, indicating that volatile is OK. */
1905 init_recog ();
1907 /* Scan through all the insns, instantiating every virtual register still
1908 present. */
1909 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1910 if (INSN_P (insn))
1912 /* These patterns in the instruction stream can never be recognized.
1913 Fortunately, they shouldn't contain virtual registers either. */
1914 if (GET_CODE (PATTERN (insn)) == USE
1915 || GET_CODE (PATTERN (insn)) == CLOBBER
1916 || GET_CODE (PATTERN (insn)) == ADDR_VEC
1917 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
1918 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
1919 continue;
1920 else if (DEBUG_INSN_P (insn))
1921 for_each_rtx (&INSN_VAR_LOCATION (insn),
1922 instantiate_virtual_regs_in_rtx, NULL);
1923 else
1924 instantiate_virtual_regs_in_insn (insn);
1926 if (INSN_DELETED_P (insn))
1927 continue;
1929 for_each_rtx (&REG_NOTES (insn), instantiate_virtual_regs_in_rtx, NULL);
1931 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
1932 if (CALL_P (insn))
1933 for_each_rtx (&CALL_INSN_FUNCTION_USAGE (insn),
1934 instantiate_virtual_regs_in_rtx, NULL);
1937 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
1938 instantiate_decls (current_function_decl);
1940 targetm.instantiate_decls ();
1942 /* Indicate that, from now on, assign_stack_local should use
1943 frame_pointer_rtx. */
1944 virtuals_instantiated = 1;
1946 return 0;
1949 struct rtl_opt_pass pass_instantiate_virtual_regs =
1952 RTL_PASS,
1953 "vregs", /* name */
1954 NULL, /* gate */
1955 instantiate_virtual_regs, /* execute */
1956 NULL, /* sub */
1957 NULL, /* next */
1958 0, /* static_pass_number */
1959 TV_NONE, /* tv_id */
1960 0, /* properties_required */
1961 0, /* properties_provided */
1962 0, /* properties_destroyed */
1963 0, /* todo_flags_start */
1964 0 /* todo_flags_finish */
1969 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
1970 This means a type for which function calls must pass an address to the
1971 function or get an address back from the function.
1972 EXP may be a type node or an expression (whose type is tested). */
1975 aggregate_value_p (const_tree exp, const_tree fntype)
1977 const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
1978 int i, regno, nregs;
1979 rtx reg;
1981 if (fntype)
1982 switch (TREE_CODE (fntype))
1984 case CALL_EXPR:
1986 tree fndecl = get_callee_fndecl (fntype);
1987 fntype = (fndecl
1988 ? TREE_TYPE (fndecl)
1989 : TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype))));
1991 break;
1992 case FUNCTION_DECL:
1993 fntype = TREE_TYPE (fntype);
1994 break;
1995 case FUNCTION_TYPE:
1996 case METHOD_TYPE:
1997 break;
1998 case IDENTIFIER_NODE:
1999 fntype = NULL_TREE;
2000 break;
2001 default:
2002 /* We don't expect other tree types here. */
2003 gcc_unreachable ();
2006 if (VOID_TYPE_P (type))
2007 return 0;
2009 /* If a record should be passed the same as its first (and only) member
2010 don't pass it as an aggregate. */
2011 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2012 return aggregate_value_p (first_field (type), fntype);
2014 /* If the front end has decided that this needs to be passed by
2015 reference, do so. */
2016 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
2017 && DECL_BY_REFERENCE (exp))
2018 return 1;
2020 /* Function types that are TREE_ADDRESSABLE force return in memory. */
2021 if (fntype && TREE_ADDRESSABLE (fntype))
2022 return 1;
2024 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
2025 and thus can't be returned in registers. */
2026 if (TREE_ADDRESSABLE (type))
2027 return 1;
2029 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
2030 return 1;
2032 if (targetm.calls.return_in_memory (type, fntype))
2033 return 1;
2035 /* Make sure we have suitable call-clobbered regs to return
2036 the value in; if not, we must return it in memory. */
2037 reg = hard_function_value (type, 0, fntype, 0);
2039 /* If we have something other than a REG (e.g. a PARALLEL), then assume
2040 it is OK. */
2041 if (!REG_P (reg))
2042 return 0;
2044 regno = REGNO (reg);
2045 nregs = hard_regno_nregs[regno][TYPE_MODE (type)];
2046 for (i = 0; i < nregs; i++)
2047 if (! call_used_regs[regno + i])
2048 return 1;
2050 return 0;
2053 /* Return true if we should assign DECL a pseudo register; false if it
2054 should live on the local stack. */
2056 bool
2057 use_register_for_decl (const_tree decl)
2059 if (!targetm.calls.allocate_stack_slots_for_args())
2060 return true;
2062 /* Honor volatile. */
2063 if (TREE_SIDE_EFFECTS (decl))
2064 return false;
2066 /* Honor addressability. */
2067 if (TREE_ADDRESSABLE (decl))
2068 return false;
2070 /* Only register-like things go in registers. */
2071 if (DECL_MODE (decl) == BLKmode)
2072 return false;
2074 /* If -ffloat-store specified, don't put explicit float variables
2075 into registers. */
2076 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2077 propagates values across these stores, and it probably shouldn't. */
2078 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2079 return false;
2081 /* If we're not interested in tracking debugging information for
2082 this decl, then we can certainly put it in a register. */
2083 if (DECL_IGNORED_P (decl))
2084 return true;
2086 if (optimize)
2087 return true;
2089 if (!DECL_REGISTER (decl))
2090 return false;
2092 switch (TREE_CODE (TREE_TYPE (decl)))
2094 case RECORD_TYPE:
2095 case UNION_TYPE:
2096 case QUAL_UNION_TYPE:
2097 /* When not optimizing, disregard register keyword for variables with
2098 types containing methods, otherwise the methods won't be callable
2099 from the debugger. */
2100 if (TYPE_METHODS (TREE_TYPE (decl)))
2101 return false;
2102 break;
2103 default:
2104 break;
2107 return true;
2110 /* Return true if TYPE should be passed by invisible reference. */
2112 bool
2113 pass_by_reference (CUMULATIVE_ARGS *ca, enum machine_mode mode,
2114 tree type, bool named_arg)
2116 if (type)
2118 /* If this type contains non-trivial constructors, then it is
2119 forbidden for the middle-end to create any new copies. */
2120 if (TREE_ADDRESSABLE (type))
2121 return true;
2123 /* GCC post 3.4 passes *all* variable sized types by reference. */
2124 if (!TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2125 return true;
2127 /* If a record type should be passed the same as its first (and only)
2128 member, use the type and mode of that member. */
2129 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2131 type = TREE_TYPE (first_field (type));
2132 mode = TYPE_MODE (type);
2136 return targetm.calls.pass_by_reference (pack_cumulative_args (ca), mode,
2137 type, named_arg);
2140 /* Return true if TYPE, which is passed by reference, should be callee
2141 copied instead of caller copied. */
2143 bool
2144 reference_callee_copied (CUMULATIVE_ARGS *ca, enum machine_mode mode,
2145 tree type, bool named_arg)
2147 if (type && TREE_ADDRESSABLE (type))
2148 return false;
2149 return targetm.calls.callee_copies (pack_cumulative_args (ca), mode, type,
2150 named_arg);
2153 /* Structures to communicate between the subroutines of assign_parms.
2154 The first holds data persistent across all parameters, the second
2155 is cleared out for each parameter. */
2157 struct assign_parm_data_all
2159 /* When INIT_CUMULATIVE_ARGS gets revamped, allocating CUMULATIVE_ARGS
2160 should become a job of the target or otherwise encapsulated. */
2161 CUMULATIVE_ARGS args_so_far_v;
2162 cumulative_args_t args_so_far;
2163 struct args_size stack_args_size;
2164 tree function_result_decl;
2165 tree orig_fnargs;
2166 rtx first_conversion_insn;
2167 rtx last_conversion_insn;
2168 HOST_WIDE_INT pretend_args_size;
2169 HOST_WIDE_INT extra_pretend_bytes;
2170 int reg_parm_stack_space;
2173 struct assign_parm_data_one
2175 tree nominal_type;
2176 tree passed_type;
2177 rtx entry_parm;
2178 rtx stack_parm;
2179 enum machine_mode nominal_mode;
2180 enum machine_mode passed_mode;
2181 enum machine_mode promoted_mode;
2182 struct locate_and_pad_arg_data locate;
2183 int partial;
2184 BOOL_BITFIELD named_arg : 1;
2185 BOOL_BITFIELD passed_pointer : 1;
2186 BOOL_BITFIELD on_stack : 1;
2187 BOOL_BITFIELD loaded_in_reg : 1;
2190 /* A subroutine of assign_parms. Initialize ALL. */
2192 static void
2193 assign_parms_initialize_all (struct assign_parm_data_all *all)
2195 tree fntype ATTRIBUTE_UNUSED;
2197 memset (all, 0, sizeof (*all));
2199 fntype = TREE_TYPE (current_function_decl);
2201 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
2202 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far_v, fntype, NULL_RTX);
2203 #else
2204 INIT_CUMULATIVE_ARGS (all->args_so_far_v, fntype, NULL_RTX,
2205 current_function_decl, -1);
2206 #endif
2207 all->args_so_far = pack_cumulative_args (&all->args_so_far_v);
2209 #ifdef REG_PARM_STACK_SPACE
2210 all->reg_parm_stack_space = REG_PARM_STACK_SPACE (current_function_decl);
2211 #endif
2214 /* If ARGS contains entries with complex types, split the entry into two
2215 entries of the component type. Return a new list of substitutions are
2216 needed, else the old list. */
2218 static void
2219 split_complex_args (VEC(tree, heap) **args)
2221 unsigned i;
2222 tree p;
2224 FOR_EACH_VEC_ELT (tree, *args, i, p)
2226 tree type = TREE_TYPE (p);
2227 if (TREE_CODE (type) == COMPLEX_TYPE
2228 && targetm.calls.split_complex_arg (type))
2230 tree decl;
2231 tree subtype = TREE_TYPE (type);
2232 bool addressable = TREE_ADDRESSABLE (p);
2234 /* Rewrite the PARM_DECL's type with its component. */
2235 p = copy_node (p);
2236 TREE_TYPE (p) = subtype;
2237 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2238 DECL_MODE (p) = VOIDmode;
2239 DECL_SIZE (p) = NULL;
2240 DECL_SIZE_UNIT (p) = NULL;
2241 /* If this arg must go in memory, put it in a pseudo here.
2242 We can't allow it to go in memory as per normal parms,
2243 because the usual place might not have the imag part
2244 adjacent to the real part. */
2245 DECL_ARTIFICIAL (p) = addressable;
2246 DECL_IGNORED_P (p) = addressable;
2247 TREE_ADDRESSABLE (p) = 0;
2248 layout_decl (p, 0);
2249 VEC_replace (tree, *args, i, p);
2251 /* Build a second synthetic decl. */
2252 decl = build_decl (EXPR_LOCATION (p),
2253 PARM_DECL, NULL_TREE, subtype);
2254 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2255 DECL_ARTIFICIAL (decl) = addressable;
2256 DECL_IGNORED_P (decl) = addressable;
2257 layout_decl (decl, 0);
2258 VEC_safe_insert (tree, heap, *args, ++i, decl);
2263 /* A subroutine of assign_parms. Adjust the parameter list to incorporate
2264 the hidden struct return argument, and (abi willing) complex args.
2265 Return the new parameter list. */
2267 static VEC(tree, heap) *
2268 assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2270 tree fndecl = current_function_decl;
2271 tree fntype = TREE_TYPE (fndecl);
2272 VEC(tree, heap) *fnargs = NULL;
2273 tree arg;
2275 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
2276 VEC_safe_push (tree, heap, fnargs, arg);
2278 all->orig_fnargs = DECL_ARGUMENTS (fndecl);
2280 /* If struct value address is treated as the first argument, make it so. */
2281 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
2282 && ! cfun->returns_pcc_struct
2283 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2285 tree type = build_pointer_type (TREE_TYPE (fntype));
2286 tree decl;
2288 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2289 PARM_DECL, get_identifier (".result_ptr"), type);
2290 DECL_ARG_TYPE (decl) = type;
2291 DECL_ARTIFICIAL (decl) = 1;
2292 DECL_NAMELESS (decl) = 1;
2293 TREE_CONSTANT (decl) = 1;
2295 DECL_CHAIN (decl) = all->orig_fnargs;
2296 all->orig_fnargs = decl;
2297 VEC_safe_insert (tree, heap, fnargs, 0, decl);
2299 all->function_result_decl = decl;
2302 /* If the target wants to split complex arguments into scalars, do so. */
2303 if (targetm.calls.split_complex_arg)
2304 split_complex_args (&fnargs);
2306 return fnargs;
2309 /* A subroutine of assign_parms. Examine PARM and pull out type and mode
2310 data for the parameter. Incorporate ABI specifics such as pass-by-
2311 reference and type promotion. */
2313 static void
2314 assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2315 struct assign_parm_data_one *data)
2317 tree nominal_type, passed_type;
2318 enum machine_mode nominal_mode, passed_mode, promoted_mode;
2319 int unsignedp;
2321 memset (data, 0, sizeof (*data));
2323 /* NAMED_ARG is a misnomer. We really mean 'non-variadic'. */
2324 if (!cfun->stdarg)
2325 data->named_arg = 1; /* No variadic parms. */
2326 else if (DECL_CHAIN (parm))
2327 data->named_arg = 1; /* Not the last non-variadic parm. */
2328 else if (targetm.calls.strict_argument_naming (all->args_so_far))
2329 data->named_arg = 1; /* Only variadic ones are unnamed. */
2330 else
2331 data->named_arg = 0; /* Treat as variadic. */
2333 nominal_type = TREE_TYPE (parm);
2334 passed_type = DECL_ARG_TYPE (parm);
2336 /* Look out for errors propagating this far. Also, if the parameter's
2337 type is void then its value doesn't matter. */
2338 if (TREE_TYPE (parm) == error_mark_node
2339 /* This can happen after weird syntax errors
2340 or if an enum type is defined among the parms. */
2341 || TREE_CODE (parm) != PARM_DECL
2342 || passed_type == NULL
2343 || VOID_TYPE_P (nominal_type))
2345 nominal_type = passed_type = void_type_node;
2346 nominal_mode = passed_mode = promoted_mode = VOIDmode;
2347 goto egress;
2350 /* Find mode of arg as it is passed, and mode of arg as it should be
2351 during execution of this function. */
2352 passed_mode = TYPE_MODE (passed_type);
2353 nominal_mode = TYPE_MODE (nominal_type);
2355 /* If the parm is to be passed as a transparent union or record, use the
2356 type of the first field for the tests below. We have already verified
2357 that the modes are the same. */
2358 if ((TREE_CODE (passed_type) == UNION_TYPE
2359 || TREE_CODE (passed_type) == RECORD_TYPE)
2360 && TYPE_TRANSPARENT_AGGR (passed_type))
2361 passed_type = TREE_TYPE (first_field (passed_type));
2363 /* See if this arg was passed by invisible reference. */
2364 if (pass_by_reference (&all->args_so_far_v, passed_mode,
2365 passed_type, data->named_arg))
2367 passed_type = nominal_type = build_pointer_type (passed_type);
2368 data->passed_pointer = true;
2369 passed_mode = nominal_mode = Pmode;
2372 /* Find mode as it is passed by the ABI. */
2373 unsignedp = TYPE_UNSIGNED (passed_type);
2374 promoted_mode = promote_function_mode (passed_type, passed_mode, &unsignedp,
2375 TREE_TYPE (current_function_decl), 0);
2377 egress:
2378 data->nominal_type = nominal_type;
2379 data->passed_type = passed_type;
2380 data->nominal_mode = nominal_mode;
2381 data->passed_mode = passed_mode;
2382 data->promoted_mode = promoted_mode;
2385 /* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
2387 static void
2388 assign_parms_setup_varargs (struct assign_parm_data_all *all,
2389 struct assign_parm_data_one *data, bool no_rtl)
2391 int varargs_pretend_bytes = 0;
2393 targetm.calls.setup_incoming_varargs (all->args_so_far,
2394 data->promoted_mode,
2395 data->passed_type,
2396 &varargs_pretend_bytes, no_rtl);
2398 /* If the back-end has requested extra stack space, record how much is
2399 needed. Do not change pretend_args_size otherwise since it may be
2400 nonzero from an earlier partial argument. */
2401 if (varargs_pretend_bytes > 0)
2402 all->pretend_args_size = varargs_pretend_bytes;
2405 /* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2406 the incoming location of the current parameter. */
2408 static void
2409 assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2410 struct assign_parm_data_one *data)
2412 HOST_WIDE_INT pretend_bytes = 0;
2413 rtx entry_parm;
2414 bool in_regs;
2416 if (data->promoted_mode == VOIDmode)
2418 data->entry_parm = data->stack_parm = const0_rtx;
2419 return;
2422 entry_parm = targetm.calls.function_incoming_arg (all->args_so_far,
2423 data->promoted_mode,
2424 data->passed_type,
2425 data->named_arg);
2427 if (entry_parm == 0)
2428 data->promoted_mode = data->passed_mode;
2430 /* Determine parm's home in the stack, in case it arrives in the stack
2431 or we should pretend it did. Compute the stack position and rtx where
2432 the argument arrives and its size.
2434 There is one complexity here: If this was a parameter that would
2435 have been passed in registers, but wasn't only because it is
2436 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2437 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2438 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2439 as it was the previous time. */
2440 in_regs = entry_parm != 0;
2441 #ifdef STACK_PARMS_IN_REG_PARM_AREA
2442 in_regs = true;
2443 #endif
2444 if (!in_regs && !data->named_arg)
2446 if (targetm.calls.pretend_outgoing_varargs_named (all->args_so_far))
2448 rtx tem;
2449 tem = targetm.calls.function_incoming_arg (all->args_so_far,
2450 data->promoted_mode,
2451 data->passed_type, true);
2452 in_regs = tem != NULL;
2456 /* If this parameter was passed both in registers and in the stack, use
2457 the copy on the stack. */
2458 if (targetm.calls.must_pass_in_stack (data->promoted_mode,
2459 data->passed_type))
2460 entry_parm = 0;
2462 if (entry_parm)
2464 int partial;
2466 partial = targetm.calls.arg_partial_bytes (all->args_so_far,
2467 data->promoted_mode,
2468 data->passed_type,
2469 data->named_arg);
2470 data->partial = partial;
2472 /* The caller might already have allocated stack space for the
2473 register parameters. */
2474 if (partial != 0 && all->reg_parm_stack_space == 0)
2476 /* Part of this argument is passed in registers and part
2477 is passed on the stack. Ask the prologue code to extend
2478 the stack part so that we can recreate the full value.
2480 PRETEND_BYTES is the size of the registers we need to store.
2481 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2482 stack space that the prologue should allocate.
2484 Internally, gcc assumes that the argument pointer is aligned
2485 to STACK_BOUNDARY bits. This is used both for alignment
2486 optimizations (see init_emit) and to locate arguments that are
2487 aligned to more than PARM_BOUNDARY bits. We must preserve this
2488 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2489 a stack boundary. */
2491 /* We assume at most one partial arg, and it must be the first
2492 argument on the stack. */
2493 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2495 pretend_bytes = partial;
2496 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2498 /* We want to align relative to the actual stack pointer, so
2499 don't include this in the stack size until later. */
2500 all->extra_pretend_bytes = all->pretend_args_size;
2504 locate_and_pad_parm (data->promoted_mode, data->passed_type, in_regs,
2505 entry_parm ? data->partial : 0, current_function_decl,
2506 &all->stack_args_size, &data->locate);
2508 /* Update parm_stack_boundary if this parameter is passed in the
2509 stack. */
2510 if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2511 crtl->parm_stack_boundary = data->locate.boundary;
2513 /* Adjust offsets to include the pretend args. */
2514 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2515 data->locate.slot_offset.constant += pretend_bytes;
2516 data->locate.offset.constant += pretend_bytes;
2518 data->entry_parm = entry_parm;
2521 /* A subroutine of assign_parms. If there is actually space on the stack
2522 for this parm, count it in stack_args_size and return true. */
2524 static bool
2525 assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2526 struct assign_parm_data_one *data)
2528 /* Trivially true if we've no incoming register. */
2529 if (data->entry_parm == NULL)
2531 /* Also true if we're partially in registers and partially not,
2532 since we've arranged to drop the entire argument on the stack. */
2533 else if (data->partial != 0)
2535 /* Also true if the target says that it's passed in both registers
2536 and on the stack. */
2537 else if (GET_CODE (data->entry_parm) == PARALLEL
2538 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2540 /* Also true if the target says that there's stack allocated for
2541 all register parameters. */
2542 else if (all->reg_parm_stack_space > 0)
2544 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2545 else
2546 return false;
2548 all->stack_args_size.constant += data->locate.size.constant;
2549 if (data->locate.size.var)
2550 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2552 return true;
2555 /* A subroutine of assign_parms. Given that this parameter is allocated
2556 stack space by the ABI, find it. */
2558 static void
2559 assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2561 rtx offset_rtx, stack_parm;
2562 unsigned int align, boundary;
2564 /* If we're passing this arg using a reg, make its stack home the
2565 aligned stack slot. */
2566 if (data->entry_parm)
2567 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2568 else
2569 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2571 stack_parm = crtl->args.internal_arg_pointer;
2572 if (offset_rtx != const0_rtx)
2573 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2574 stack_parm = gen_rtx_MEM (data->promoted_mode, stack_parm);
2576 if (!data->passed_pointer)
2578 set_mem_attributes (stack_parm, parm, 1);
2579 /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2580 while promoted mode's size is needed. */
2581 if (data->promoted_mode != BLKmode
2582 && data->promoted_mode != DECL_MODE (parm))
2584 set_mem_size (stack_parm, GET_MODE_SIZE (data->promoted_mode));
2585 if (MEM_EXPR (stack_parm) && MEM_OFFSET_KNOWN_P (stack_parm))
2587 int offset = subreg_lowpart_offset (DECL_MODE (parm),
2588 data->promoted_mode);
2589 if (offset)
2590 set_mem_offset (stack_parm, MEM_OFFSET (stack_parm) - offset);
2595 boundary = data->locate.boundary;
2596 align = BITS_PER_UNIT;
2598 /* If we're padding upward, we know that the alignment of the slot
2599 is TARGET_FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2600 intentionally forcing upward padding. Otherwise we have to come
2601 up with a guess at the alignment based on OFFSET_RTX. */
2602 if (data->locate.where_pad != downward || data->entry_parm)
2603 align = boundary;
2604 else if (CONST_INT_P (offset_rtx))
2606 align = INTVAL (offset_rtx) * BITS_PER_UNIT | boundary;
2607 align = align & -align;
2609 set_mem_align (stack_parm, align);
2611 if (data->entry_parm)
2612 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2614 data->stack_parm = stack_parm;
2617 /* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2618 always valid and contiguous. */
2620 static void
2621 assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2623 rtx entry_parm = data->entry_parm;
2624 rtx stack_parm = data->stack_parm;
2626 /* If this parm was passed part in regs and part in memory, pretend it
2627 arrived entirely in memory by pushing the register-part onto the stack.
2628 In the special case of a DImode or DFmode that is split, we could put
2629 it together in a pseudoreg directly, but for now that's not worth
2630 bothering with. */
2631 if (data->partial != 0)
2633 /* Handle calls that pass values in multiple non-contiguous
2634 locations. The Irix 6 ABI has examples of this. */
2635 if (GET_CODE (entry_parm) == PARALLEL)
2636 emit_group_store (validize_mem (stack_parm), entry_parm,
2637 data->passed_type,
2638 int_size_in_bytes (data->passed_type));
2639 else
2641 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2642 move_block_from_reg (REGNO (entry_parm), validize_mem (stack_parm),
2643 data->partial / UNITS_PER_WORD);
2646 entry_parm = stack_parm;
2649 /* If we didn't decide this parm came in a register, by default it came
2650 on the stack. */
2651 else if (entry_parm == NULL)
2652 entry_parm = stack_parm;
2654 /* When an argument is passed in multiple locations, we can't make use
2655 of this information, but we can save some copying if the whole argument
2656 is passed in a single register. */
2657 else if (GET_CODE (entry_parm) == PARALLEL
2658 && data->nominal_mode != BLKmode
2659 && data->passed_mode != BLKmode)
2661 size_t i, len = XVECLEN (entry_parm, 0);
2663 for (i = 0; i < len; i++)
2664 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2665 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2666 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2667 == data->passed_mode)
2668 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2670 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2671 break;
2675 data->entry_parm = entry_parm;
2678 /* A subroutine of assign_parms. Reconstitute any values which were
2679 passed in multiple registers and would fit in a single register. */
2681 static void
2682 assign_parm_remove_parallels (struct assign_parm_data_one *data)
2684 rtx entry_parm = data->entry_parm;
2686 /* Convert the PARALLEL to a REG of the same mode as the parallel.
2687 This can be done with register operations rather than on the
2688 stack, even if we will store the reconstituted parameter on the
2689 stack later. */
2690 if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
2692 rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
2693 emit_group_store (parmreg, entry_parm, data->passed_type,
2694 GET_MODE_SIZE (GET_MODE (entry_parm)));
2695 entry_parm = parmreg;
2698 data->entry_parm = entry_parm;
2701 /* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2702 always valid and properly aligned. */
2704 static void
2705 assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2707 rtx stack_parm = data->stack_parm;
2709 /* If we can't trust the parm stack slot to be aligned enough for its
2710 ultimate type, don't use that slot after entry. We'll make another
2711 stack slot, if we need one. */
2712 if (stack_parm
2713 && ((STRICT_ALIGNMENT
2714 && GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm))
2715 || (data->nominal_type
2716 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2717 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2718 stack_parm = NULL;
2720 /* If parm was passed in memory, and we need to convert it on entry,
2721 don't store it back in that same slot. */
2722 else if (data->entry_parm == stack_parm
2723 && data->nominal_mode != BLKmode
2724 && data->nominal_mode != data->passed_mode)
2725 stack_parm = NULL;
2727 /* If stack protection is in effect for this function, don't leave any
2728 pointers in their passed stack slots. */
2729 else if (crtl->stack_protect_guard
2730 && (flag_stack_protect == 2
2731 || data->passed_pointer
2732 || POINTER_TYPE_P (data->nominal_type)))
2733 stack_parm = NULL;
2735 data->stack_parm = stack_parm;
2738 /* A subroutine of assign_parms. Return true if the current parameter
2739 should be stored as a BLKmode in the current frame. */
2741 static bool
2742 assign_parm_setup_block_p (struct assign_parm_data_one *data)
2744 if (data->nominal_mode == BLKmode)
2745 return true;
2746 if (GET_MODE (data->entry_parm) == BLKmode)
2747 return true;
2749 #ifdef BLOCK_REG_PADDING
2750 /* Only assign_parm_setup_block knows how to deal with register arguments
2751 that are padded at the least significant end. */
2752 if (REG_P (data->entry_parm)
2753 && GET_MODE_SIZE (data->promoted_mode) < UNITS_PER_WORD
2754 && (BLOCK_REG_PADDING (data->passed_mode, data->passed_type, 1)
2755 == (BYTES_BIG_ENDIAN ? upward : downward)))
2756 return true;
2757 #endif
2759 return false;
2762 /* A subroutine of assign_parms. Arrange for the parameter to be
2763 present and valid in DATA->STACK_RTL. */
2765 static void
2766 assign_parm_setup_block (struct assign_parm_data_all *all,
2767 tree parm, struct assign_parm_data_one *data)
2769 rtx entry_parm = data->entry_parm;
2770 rtx stack_parm = data->stack_parm;
2771 HOST_WIDE_INT size;
2772 HOST_WIDE_INT size_stored;
2774 if (GET_CODE (entry_parm) == PARALLEL)
2775 entry_parm = emit_group_move_into_temps (entry_parm);
2777 size = int_size_in_bytes (data->passed_type);
2778 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2779 if (stack_parm == 0)
2781 DECL_ALIGN (parm) = MAX (DECL_ALIGN (parm), BITS_PER_WORD);
2782 stack_parm = assign_stack_local (BLKmode, size_stored,
2783 DECL_ALIGN (parm));
2784 if (GET_MODE_SIZE (GET_MODE (entry_parm)) == size)
2785 PUT_MODE (stack_parm, GET_MODE (entry_parm));
2786 set_mem_attributes (stack_parm, parm, 1);
2789 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2790 calls that pass values in multiple non-contiguous locations. */
2791 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2793 rtx mem;
2795 /* Note that we will be storing an integral number of words.
2796 So we have to be careful to ensure that we allocate an
2797 integral number of words. We do this above when we call
2798 assign_stack_local if space was not allocated in the argument
2799 list. If it was, this will not work if PARM_BOUNDARY is not
2800 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2801 if it becomes a problem. Exception is when BLKmode arrives
2802 with arguments not conforming to word_mode. */
2804 if (data->stack_parm == 0)
2806 else if (GET_CODE (entry_parm) == PARALLEL)
2808 else
2809 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2811 mem = validize_mem (stack_parm);
2813 /* Handle values in multiple non-contiguous locations. */
2814 if (GET_CODE (entry_parm) == PARALLEL)
2816 push_to_sequence2 (all->first_conversion_insn,
2817 all->last_conversion_insn);
2818 emit_group_store (mem, entry_parm, data->passed_type, size);
2819 all->first_conversion_insn = get_insns ();
2820 all->last_conversion_insn = get_last_insn ();
2821 end_sequence ();
2824 else if (size == 0)
2827 /* If SIZE is that of a mode no bigger than a word, just use
2828 that mode's store operation. */
2829 else if (size <= UNITS_PER_WORD)
2831 enum machine_mode mode
2832 = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2834 if (mode != BLKmode
2835 #ifdef BLOCK_REG_PADDING
2836 && (size == UNITS_PER_WORD
2837 || (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2838 != (BYTES_BIG_ENDIAN ? upward : downward)))
2839 #endif
2842 rtx reg;
2844 /* We are really truncating a word_mode value containing
2845 SIZE bytes into a value of mode MODE. If such an
2846 operation requires no actual instructions, we can refer
2847 to the value directly in mode MODE, otherwise we must
2848 start with the register in word_mode and explicitly
2849 convert it. */
2850 if (TRULY_NOOP_TRUNCATION (size * BITS_PER_UNIT, BITS_PER_WORD))
2851 reg = gen_rtx_REG (mode, REGNO (entry_parm));
2852 else
2854 reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2855 reg = convert_to_mode (mode, copy_to_reg (reg), 1);
2857 emit_move_insn (change_address (mem, mode, 0), reg);
2860 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
2861 machine must be aligned to the left before storing
2862 to memory. Note that the previous test doesn't
2863 handle all cases (e.g. SIZE == 3). */
2864 else if (size != UNITS_PER_WORD
2865 #ifdef BLOCK_REG_PADDING
2866 && (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2867 == downward)
2868 #else
2869 && BYTES_BIG_ENDIAN
2870 #endif
2873 rtx tem, x;
2874 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2875 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2877 x = expand_shift (LSHIFT_EXPR, word_mode, reg, by, NULL_RTX, 1);
2878 tem = change_address (mem, word_mode, 0);
2879 emit_move_insn (tem, x);
2881 else
2882 move_block_from_reg (REGNO (entry_parm), mem,
2883 size_stored / UNITS_PER_WORD);
2885 else
2886 move_block_from_reg (REGNO (entry_parm), mem,
2887 size_stored / UNITS_PER_WORD);
2889 else if (data->stack_parm == 0)
2891 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
2892 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
2893 BLOCK_OP_NORMAL);
2894 all->first_conversion_insn = get_insns ();
2895 all->last_conversion_insn = get_last_insn ();
2896 end_sequence ();
2899 data->stack_parm = stack_parm;
2900 SET_DECL_RTL (parm, stack_parm);
2903 /* A subroutine of assign_parms. Allocate a pseudo to hold the current
2904 parameter. Get it there. Perform all ABI specified conversions. */
2906 static void
2907 assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
2908 struct assign_parm_data_one *data)
2910 rtx parmreg, validated_mem;
2911 rtx equiv_stack_parm;
2912 enum machine_mode promoted_nominal_mode;
2913 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
2914 bool did_conversion = false;
2915 bool need_conversion, moved;
2917 /* Store the parm in a pseudoregister during the function, but we may
2918 need to do it in a wider mode. Using 2 here makes the result
2919 consistent with promote_decl_mode and thus expand_expr_real_1. */
2920 promoted_nominal_mode
2921 = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
2922 TREE_TYPE (current_function_decl), 2);
2924 parmreg = gen_reg_rtx (promoted_nominal_mode);
2926 if (!DECL_ARTIFICIAL (parm))
2927 mark_user_reg (parmreg);
2929 /* If this was an item that we received a pointer to,
2930 set DECL_RTL appropriately. */
2931 if (data->passed_pointer)
2933 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->passed_type)), parmreg);
2934 set_mem_attributes (x, parm, 1);
2935 SET_DECL_RTL (parm, x);
2937 else
2938 SET_DECL_RTL (parm, parmreg);
2940 assign_parm_remove_parallels (data);
2942 /* Copy the value into the register, thus bridging between
2943 assign_parm_find_data_types and expand_expr_real_1. */
2945 equiv_stack_parm = data->stack_parm;
2946 validated_mem = validize_mem (data->entry_parm);
2948 need_conversion = (data->nominal_mode != data->passed_mode
2949 || promoted_nominal_mode != data->promoted_mode);
2950 moved = false;
2952 if (need_conversion
2953 && GET_MODE_CLASS (data->nominal_mode) == MODE_INT
2954 && data->nominal_mode == data->passed_mode
2955 && data->nominal_mode == GET_MODE (data->entry_parm))
2957 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
2958 mode, by the caller. We now have to convert it to
2959 NOMINAL_MODE, if different. However, PARMREG may be in
2960 a different mode than NOMINAL_MODE if it is being stored
2961 promoted.
2963 If ENTRY_PARM is a hard register, it might be in a register
2964 not valid for operating in its mode (e.g., an odd-numbered
2965 register for a DFmode). In that case, moves are the only
2966 thing valid, so we can't do a convert from there. This
2967 occurs when the calling sequence allow such misaligned
2968 usages.
2970 In addition, the conversion may involve a call, which could
2971 clobber parameters which haven't been copied to pseudo
2972 registers yet.
2974 First, we try to emit an insn which performs the necessary
2975 conversion. We verify that this insn does not clobber any
2976 hard registers. */
2978 enum insn_code icode;
2979 rtx op0, op1;
2981 icode = can_extend_p (promoted_nominal_mode, data->passed_mode,
2982 unsignedp);
2984 op0 = parmreg;
2985 op1 = validated_mem;
2986 if (icode != CODE_FOR_nothing
2987 && insn_operand_matches (icode, 0, op0)
2988 && insn_operand_matches (icode, 1, op1))
2990 enum rtx_code code = unsignedp ? ZERO_EXTEND : SIGN_EXTEND;
2991 rtx insn, insns;
2992 HARD_REG_SET hardregs;
2994 start_sequence ();
2995 insn = gen_extend_insn (op0, op1, promoted_nominal_mode,
2996 data->passed_mode, unsignedp);
2997 emit_insn (insn);
2998 insns = get_insns ();
3000 moved = true;
3001 CLEAR_HARD_REG_SET (hardregs);
3002 for (insn = insns; insn && moved; insn = NEXT_INSN (insn))
3004 if (INSN_P (insn))
3005 note_stores (PATTERN (insn), record_hard_reg_sets,
3006 &hardregs);
3007 if (!hard_reg_set_empty_p (hardregs))
3008 moved = false;
3011 end_sequence ();
3013 if (moved)
3015 emit_insn (insns);
3016 if (equiv_stack_parm != NULL_RTX)
3017 equiv_stack_parm = gen_rtx_fmt_e (code, GET_MODE (parmreg),
3018 equiv_stack_parm);
3023 if (moved)
3024 /* Nothing to do. */
3026 else if (need_conversion)
3028 /* We did not have an insn to convert directly, or the sequence
3029 generated appeared unsafe. We must first copy the parm to a
3030 pseudo reg, and save the conversion until after all
3031 parameters have been moved. */
3033 int save_tree_used;
3034 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3036 emit_move_insn (tempreg, validated_mem);
3038 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3039 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
3041 if (GET_CODE (tempreg) == SUBREG
3042 && GET_MODE (tempreg) == data->nominal_mode
3043 && REG_P (SUBREG_REG (tempreg))
3044 && data->nominal_mode == data->passed_mode
3045 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm)
3046 && GET_MODE_SIZE (GET_MODE (tempreg))
3047 < GET_MODE_SIZE (GET_MODE (data->entry_parm)))
3049 /* The argument is already sign/zero extended, so note it
3050 into the subreg. */
3051 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
3052 SUBREG_PROMOTED_UNSIGNED_SET (tempreg, unsignedp);
3055 /* TREE_USED gets set erroneously during expand_assignment. */
3056 save_tree_used = TREE_USED (parm);
3057 expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
3058 TREE_USED (parm) = save_tree_used;
3059 all->first_conversion_insn = get_insns ();
3060 all->last_conversion_insn = get_last_insn ();
3061 end_sequence ();
3063 did_conversion = true;
3065 else
3066 emit_move_insn (parmreg, validated_mem);
3068 /* If we were passed a pointer but the actual value can safely live
3069 in a register, put it in one. */
3070 if (data->passed_pointer
3071 && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
3072 /* If by-reference argument was promoted, demote it. */
3073 && (TYPE_MODE (TREE_TYPE (parm)) != GET_MODE (DECL_RTL (parm))
3074 || use_register_for_decl (parm)))
3076 /* We can't use nominal_mode, because it will have been set to
3077 Pmode above. We must use the actual mode of the parm. */
3078 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3079 mark_user_reg (parmreg);
3081 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
3083 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
3084 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
3086 push_to_sequence2 (all->first_conversion_insn,
3087 all->last_conversion_insn);
3088 emit_move_insn (tempreg, DECL_RTL (parm));
3089 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
3090 emit_move_insn (parmreg, tempreg);
3091 all->first_conversion_insn = get_insns ();
3092 all->last_conversion_insn = get_last_insn ();
3093 end_sequence ();
3095 did_conversion = true;
3097 else
3098 emit_move_insn (parmreg, DECL_RTL (parm));
3100 SET_DECL_RTL (parm, parmreg);
3102 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3103 now the parm. */
3104 data->stack_parm = NULL;
3107 /* Mark the register as eliminable if we did no conversion and it was
3108 copied from memory at a fixed offset, and the arg pointer was not
3109 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
3110 offset formed an invalid address, such memory-equivalences as we
3111 make here would screw up life analysis for it. */
3112 if (data->nominal_mode == data->passed_mode
3113 && !did_conversion
3114 && data->stack_parm != 0
3115 && MEM_P (data->stack_parm)
3116 && data->locate.offset.var == 0
3117 && reg_mentioned_p (virtual_incoming_args_rtx,
3118 XEXP (data->stack_parm, 0)))
3120 rtx linsn = get_last_insn ();
3121 rtx sinsn, set;
3123 /* Mark complex types separately. */
3124 if (GET_CODE (parmreg) == CONCAT)
3126 enum machine_mode submode
3127 = GET_MODE_INNER (GET_MODE (parmreg));
3128 int regnor = REGNO (XEXP (parmreg, 0));
3129 int regnoi = REGNO (XEXP (parmreg, 1));
3130 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3131 rtx stacki = adjust_address_nv (data->stack_parm, submode,
3132 GET_MODE_SIZE (submode));
3134 /* Scan backwards for the set of the real and
3135 imaginary parts. */
3136 for (sinsn = linsn; sinsn != 0;
3137 sinsn = prev_nonnote_insn (sinsn))
3139 set = single_set (sinsn);
3140 if (set == 0)
3141 continue;
3143 if (SET_DEST (set) == regno_reg_rtx [regnoi])
3144 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
3145 else if (SET_DEST (set) == regno_reg_rtx [regnor])
3146 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
3149 else
3150 set_dst_reg_note (linsn, REG_EQUIV, equiv_stack_parm, parmreg);
3153 /* For pointer data type, suggest pointer register. */
3154 if (POINTER_TYPE_P (TREE_TYPE (parm)))
3155 mark_reg_pointer (parmreg,
3156 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3159 /* A subroutine of assign_parms. Allocate stack space to hold the current
3160 parameter. Get it there. Perform all ABI specified conversions. */
3162 static void
3163 assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3164 struct assign_parm_data_one *data)
3166 /* Value must be stored in the stack slot STACK_PARM during function
3167 execution. */
3168 bool to_conversion = false;
3170 assign_parm_remove_parallels (data);
3172 if (data->promoted_mode != data->nominal_mode)
3174 /* Conversion is required. */
3175 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3177 emit_move_insn (tempreg, validize_mem (data->entry_parm));
3179 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3180 to_conversion = true;
3182 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3183 TYPE_UNSIGNED (TREE_TYPE (parm)));
3185 if (data->stack_parm)
3187 int offset = subreg_lowpart_offset (data->nominal_mode,
3188 GET_MODE (data->stack_parm));
3189 /* ??? This may need a big-endian conversion on sparc64. */
3190 data->stack_parm
3191 = adjust_address (data->stack_parm, data->nominal_mode, 0);
3192 if (offset && MEM_OFFSET_KNOWN_P (data->stack_parm))
3193 set_mem_offset (data->stack_parm,
3194 MEM_OFFSET (data->stack_parm) + offset);
3198 if (data->entry_parm != data->stack_parm)
3200 rtx src, dest;
3202 if (data->stack_parm == 0)
3204 int align = STACK_SLOT_ALIGNMENT (data->passed_type,
3205 GET_MODE (data->entry_parm),
3206 TYPE_ALIGN (data->passed_type));
3207 data->stack_parm
3208 = assign_stack_local (GET_MODE (data->entry_parm),
3209 GET_MODE_SIZE (GET_MODE (data->entry_parm)),
3210 align);
3211 set_mem_attributes (data->stack_parm, parm, 1);
3214 dest = validize_mem (data->stack_parm);
3215 src = validize_mem (data->entry_parm);
3217 if (MEM_P (src))
3219 /* Use a block move to handle potentially misaligned entry_parm. */
3220 if (!to_conversion)
3221 push_to_sequence2 (all->first_conversion_insn,
3222 all->last_conversion_insn);
3223 to_conversion = true;
3225 emit_block_move (dest, src,
3226 GEN_INT (int_size_in_bytes (data->passed_type)),
3227 BLOCK_OP_NORMAL);
3229 else
3230 emit_move_insn (dest, src);
3233 if (to_conversion)
3235 all->first_conversion_insn = get_insns ();
3236 all->last_conversion_insn = get_last_insn ();
3237 end_sequence ();
3240 SET_DECL_RTL (parm, data->stack_parm);
3243 /* A subroutine of assign_parms. If the ABI splits complex arguments, then
3244 undo the frobbing that we did in assign_parms_augmented_arg_list. */
3246 static void
3247 assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3248 VEC(tree, heap) *fnargs)
3250 tree parm;
3251 tree orig_fnargs = all->orig_fnargs;
3252 unsigned i = 0;
3254 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
3256 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3257 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3259 rtx tmp, real, imag;
3260 enum machine_mode inner = GET_MODE_INNER (DECL_MODE (parm));
3262 real = DECL_RTL (VEC_index (tree, fnargs, i));
3263 imag = DECL_RTL (VEC_index (tree, fnargs, i + 1));
3264 if (inner != GET_MODE (real))
3266 real = gen_lowpart_SUBREG (inner, real);
3267 imag = gen_lowpart_SUBREG (inner, imag);
3270 if (TREE_ADDRESSABLE (parm))
3272 rtx rmem, imem;
3273 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
3274 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3275 DECL_MODE (parm),
3276 TYPE_ALIGN (TREE_TYPE (parm)));
3278 /* split_complex_arg put the real and imag parts in
3279 pseudos. Move them to memory. */
3280 tmp = assign_stack_local (DECL_MODE (parm), size, align);
3281 set_mem_attributes (tmp, parm, 1);
3282 rmem = adjust_address_nv (tmp, inner, 0);
3283 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
3284 push_to_sequence2 (all->first_conversion_insn,
3285 all->last_conversion_insn);
3286 emit_move_insn (rmem, real);
3287 emit_move_insn (imem, imag);
3288 all->first_conversion_insn = get_insns ();
3289 all->last_conversion_insn = get_last_insn ();
3290 end_sequence ();
3292 else
3293 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3294 SET_DECL_RTL (parm, tmp);
3296 real = DECL_INCOMING_RTL (VEC_index (tree, fnargs, i));
3297 imag = DECL_INCOMING_RTL (VEC_index (tree, fnargs, i + 1));
3298 if (inner != GET_MODE (real))
3300 real = gen_lowpart_SUBREG (inner, real);
3301 imag = gen_lowpart_SUBREG (inner, imag);
3303 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3304 set_decl_incoming_rtl (parm, tmp, false);
3305 i++;
3310 /* Assign RTL expressions to the function's parameters. This may involve
3311 copying them into registers and using those registers as the DECL_RTL. */
3313 static void
3314 assign_parms (tree fndecl)
3316 struct assign_parm_data_all all;
3317 tree parm;
3318 VEC(tree, heap) *fnargs;
3319 unsigned i;
3321 crtl->args.internal_arg_pointer
3322 = targetm.calls.internal_arg_pointer ();
3324 assign_parms_initialize_all (&all);
3325 fnargs = assign_parms_augmented_arg_list (&all);
3327 FOR_EACH_VEC_ELT (tree, fnargs, i, parm)
3329 struct assign_parm_data_one data;
3331 /* Extract the type of PARM; adjust it according to ABI. */
3332 assign_parm_find_data_types (&all, parm, &data);
3334 /* Early out for errors and void parameters. */
3335 if (data.passed_mode == VOIDmode)
3337 SET_DECL_RTL (parm, const0_rtx);
3338 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3339 continue;
3342 /* Estimate stack alignment from parameter alignment. */
3343 if (SUPPORTS_STACK_ALIGNMENT)
3345 unsigned int align
3346 = targetm.calls.function_arg_boundary (data.promoted_mode,
3347 data.passed_type);
3348 align = MINIMUM_ALIGNMENT (data.passed_type, data.promoted_mode,
3349 align);
3350 if (TYPE_ALIGN (data.nominal_type) > align)
3351 align = MINIMUM_ALIGNMENT (data.nominal_type,
3352 TYPE_MODE (data.nominal_type),
3353 TYPE_ALIGN (data.nominal_type));
3354 if (crtl->stack_alignment_estimated < align)
3356 gcc_assert (!crtl->stack_realign_processed);
3357 crtl->stack_alignment_estimated = align;
3361 if (cfun->stdarg && !DECL_CHAIN (parm))
3362 assign_parms_setup_varargs (&all, &data, false);
3364 /* Find out where the parameter arrives in this function. */
3365 assign_parm_find_entry_rtl (&all, &data);
3367 /* Find out where stack space for this parameter might be. */
3368 if (assign_parm_is_stack_parm (&all, &data))
3370 assign_parm_find_stack_rtl (parm, &data);
3371 assign_parm_adjust_entry_rtl (&data);
3374 /* Record permanently how this parm was passed. */
3375 if (data.passed_pointer)
3377 rtx incoming_rtl
3378 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data.passed_type)),
3379 data.entry_parm);
3380 set_decl_incoming_rtl (parm, incoming_rtl, true);
3382 else
3383 set_decl_incoming_rtl (parm, data.entry_parm, false);
3385 /* Update info on where next arg arrives in registers. */
3386 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3387 data.passed_type, data.named_arg);
3389 assign_parm_adjust_stack_rtl (&data);
3391 if (assign_parm_setup_block_p (&data))
3392 assign_parm_setup_block (&all, parm, &data);
3393 else if (data.passed_pointer || use_register_for_decl (parm))
3394 assign_parm_setup_reg (&all, parm, &data);
3395 else
3396 assign_parm_setup_stack (&all, parm, &data);
3399 if (targetm.calls.split_complex_arg)
3400 assign_parms_unsplit_complex (&all, fnargs);
3402 VEC_free (tree, heap, fnargs);
3404 /* Output all parameter conversion instructions (possibly including calls)
3405 now that all parameters have been copied out of hard registers. */
3406 emit_insn (all.first_conversion_insn);
3408 /* Estimate reload stack alignment from scalar return mode. */
3409 if (SUPPORTS_STACK_ALIGNMENT)
3411 if (DECL_RESULT (fndecl))
3413 tree type = TREE_TYPE (DECL_RESULT (fndecl));
3414 enum machine_mode mode = TYPE_MODE (type);
3416 if (mode != BLKmode
3417 && mode != VOIDmode
3418 && !AGGREGATE_TYPE_P (type))
3420 unsigned int align = GET_MODE_ALIGNMENT (mode);
3421 if (crtl->stack_alignment_estimated < align)
3423 gcc_assert (!crtl->stack_realign_processed);
3424 crtl->stack_alignment_estimated = align;
3430 /* If we are receiving a struct value address as the first argument, set up
3431 the RTL for the function result. As this might require code to convert
3432 the transmitted address to Pmode, we do this here to ensure that possible
3433 preliminary conversions of the address have been emitted already. */
3434 if (all.function_result_decl)
3436 tree result = DECL_RESULT (current_function_decl);
3437 rtx addr = DECL_RTL (all.function_result_decl);
3438 rtx x;
3440 if (DECL_BY_REFERENCE (result))
3442 SET_DECL_VALUE_EXPR (result, all.function_result_decl);
3443 x = addr;
3445 else
3447 SET_DECL_VALUE_EXPR (result,
3448 build1 (INDIRECT_REF, TREE_TYPE (result),
3449 all.function_result_decl));
3450 addr = convert_memory_address (Pmode, addr);
3451 x = gen_rtx_MEM (DECL_MODE (result), addr);
3452 set_mem_attributes (x, result, 1);
3455 DECL_HAS_VALUE_EXPR_P (result) = 1;
3457 SET_DECL_RTL (result, x);
3460 /* We have aligned all the args, so add space for the pretend args. */
3461 crtl->args.pretend_args_size = all.pretend_args_size;
3462 all.stack_args_size.constant += all.extra_pretend_bytes;
3463 crtl->args.size = all.stack_args_size.constant;
3465 /* Adjust function incoming argument size for alignment and
3466 minimum length. */
3468 #ifdef REG_PARM_STACK_SPACE
3469 crtl->args.size = MAX (crtl->args.size,
3470 REG_PARM_STACK_SPACE (fndecl));
3471 #endif
3473 crtl->args.size = CEIL_ROUND (crtl->args.size,
3474 PARM_BOUNDARY / BITS_PER_UNIT);
3476 #ifdef ARGS_GROW_DOWNWARD
3477 crtl->args.arg_offset_rtx
3478 = (all.stack_args_size.var == 0 ? GEN_INT (-all.stack_args_size.constant)
3479 : expand_expr (size_diffop (all.stack_args_size.var,
3480 size_int (-all.stack_args_size.constant)),
3481 NULL_RTX, VOIDmode, EXPAND_NORMAL));
3482 #else
3483 crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3484 #endif
3486 /* See how many bytes, if any, of its args a function should try to pop
3487 on return. */
3489 crtl->args.pops_args = targetm.calls.return_pops_args (fndecl,
3490 TREE_TYPE (fndecl),
3491 crtl->args.size);
3493 /* For stdarg.h function, save info about
3494 regs and stack space used by the named args. */
3496 crtl->args.info = all.args_so_far_v;
3498 /* Set the rtx used for the function return value. Put this in its
3499 own variable so any optimizers that need this information don't have
3500 to include tree.h. Do this here so it gets done when an inlined
3501 function gets output. */
3503 crtl->return_rtx
3504 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3505 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3507 /* If scalar return value was computed in a pseudo-reg, or was a named
3508 return value that got dumped to the stack, copy that to the hard
3509 return register. */
3510 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3512 tree decl_result = DECL_RESULT (fndecl);
3513 rtx decl_rtl = DECL_RTL (decl_result);
3515 if (REG_P (decl_rtl)
3516 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3517 : DECL_REGISTER (decl_result))
3519 rtx real_decl_rtl;
3521 real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3522 fndecl, true);
3523 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3524 /* The delay slot scheduler assumes that crtl->return_rtx
3525 holds the hard register containing the return value, not a
3526 temporary pseudo. */
3527 crtl->return_rtx = real_decl_rtl;
3532 /* A subroutine of gimplify_parameters, invoked via walk_tree.
3533 For all seen types, gimplify their sizes. */
3535 static tree
3536 gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3538 tree t = *tp;
3540 *walk_subtrees = 0;
3541 if (TYPE_P (t))
3543 if (POINTER_TYPE_P (t))
3544 *walk_subtrees = 1;
3545 else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3546 && !TYPE_SIZES_GIMPLIFIED (t))
3548 gimplify_type_sizes (t, (gimple_seq *) data);
3549 *walk_subtrees = 1;
3553 return NULL;
3556 /* Gimplify the parameter list for current_function_decl. This involves
3557 evaluating SAVE_EXPRs of variable sized parameters and generating code
3558 to implement callee-copies reference parameters. Returns a sequence of
3559 statements to add to the beginning of the function. */
3561 gimple_seq
3562 gimplify_parameters (void)
3564 struct assign_parm_data_all all;
3565 tree parm;
3566 gimple_seq stmts = NULL;
3567 VEC(tree, heap) *fnargs;
3568 unsigned i;
3570 assign_parms_initialize_all (&all);
3571 fnargs = assign_parms_augmented_arg_list (&all);
3573 FOR_EACH_VEC_ELT (tree, fnargs, i, parm)
3575 struct assign_parm_data_one data;
3577 /* Extract the type of PARM; adjust it according to ABI. */
3578 assign_parm_find_data_types (&all, parm, &data);
3580 /* Early out for errors and void parameters. */
3581 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3582 continue;
3584 /* Update info on where next arg arrives in registers. */
3585 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3586 data.passed_type, data.named_arg);
3588 /* ??? Once upon a time variable_size stuffed parameter list
3589 SAVE_EXPRs (amongst others) onto a pending sizes list. This
3590 turned out to be less than manageable in the gimple world.
3591 Now we have to hunt them down ourselves. */
3592 walk_tree_without_duplicates (&data.passed_type,
3593 gimplify_parm_type, &stmts);
3595 if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
3597 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3598 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3601 if (data.passed_pointer)
3603 tree type = TREE_TYPE (data.passed_type);
3604 if (reference_callee_copied (&all.args_so_far_v, TYPE_MODE (type),
3605 type, data.named_arg))
3607 tree local, t;
3609 /* For constant-sized objects, this is trivial; for
3610 variable-sized objects, we have to play games. */
3611 if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3612 && !(flag_stack_check == GENERIC_STACK_CHECK
3613 && compare_tree_int (DECL_SIZE_UNIT (parm),
3614 STACK_CHECK_MAX_VAR_SIZE) > 0))
3616 local = create_tmp_var (type, get_name (parm));
3617 DECL_IGNORED_P (local) = 0;
3618 /* If PARM was addressable, move that flag over
3619 to the local copy, as its address will be taken,
3620 not the PARMs. Keep the parms address taken
3621 as we'll query that flag during gimplification. */
3622 if (TREE_ADDRESSABLE (parm))
3623 TREE_ADDRESSABLE (local) = 1;
3624 else if (TREE_CODE (type) == COMPLEX_TYPE
3625 || TREE_CODE (type) == VECTOR_TYPE)
3626 DECL_GIMPLE_REG_P (local) = 1;
3628 else
3630 tree ptr_type, addr;
3632 ptr_type = build_pointer_type (type);
3633 addr = create_tmp_reg (ptr_type, get_name (parm));
3634 DECL_IGNORED_P (addr) = 0;
3635 local = build_fold_indirect_ref (addr);
3637 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
3638 t = build_call_expr (t, 2, DECL_SIZE_UNIT (parm),
3639 size_int (DECL_ALIGN (parm)));
3641 /* The call has been built for a variable-sized object. */
3642 CALL_ALLOCA_FOR_VAR_P (t) = 1;
3643 t = fold_convert (ptr_type, t);
3644 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
3645 gimplify_and_add (t, &stmts);
3648 gimplify_assign (local, parm, &stmts);
3650 SET_DECL_VALUE_EXPR (parm, local);
3651 DECL_HAS_VALUE_EXPR_P (parm) = 1;
3656 VEC_free (tree, heap, fnargs);
3658 return stmts;
3661 /* Compute the size and offset from the start of the stacked arguments for a
3662 parm passed in mode PASSED_MODE and with type TYPE.
3664 INITIAL_OFFSET_PTR points to the current offset into the stacked
3665 arguments.
3667 The starting offset and size for this parm are returned in
3668 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
3669 nonzero, the offset is that of stack slot, which is returned in
3670 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
3671 padding required from the initial offset ptr to the stack slot.
3673 IN_REGS is nonzero if the argument will be passed in registers. It will
3674 never be set if REG_PARM_STACK_SPACE is not defined.
3676 FNDECL is the function in which the argument was defined.
3678 There are two types of rounding that are done. The first, controlled by
3679 TARGET_FUNCTION_ARG_BOUNDARY, forces the offset from the start of the
3680 argument list to be aligned to the specific boundary (in bits). This
3681 rounding affects the initial and starting offsets, but not the argument
3682 size.
3684 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
3685 optionally rounds the size of the parm to PARM_BOUNDARY. The
3686 initial offset is not affected by this rounding, while the size always
3687 is and the starting offset may be. */
3689 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
3690 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
3691 callers pass in the total size of args so far as
3692 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
3694 void
3695 locate_and_pad_parm (enum machine_mode passed_mode, tree type, int in_regs,
3696 int partial, tree fndecl ATTRIBUTE_UNUSED,
3697 struct args_size *initial_offset_ptr,
3698 struct locate_and_pad_arg_data *locate)
3700 tree sizetree;
3701 enum direction where_pad;
3702 unsigned int boundary, round_boundary;
3703 int reg_parm_stack_space = 0;
3704 int part_size_in_regs;
3706 #ifdef REG_PARM_STACK_SPACE
3707 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
3709 /* If we have found a stack parm before we reach the end of the
3710 area reserved for registers, skip that area. */
3711 if (! in_regs)
3713 if (reg_parm_stack_space > 0)
3715 if (initial_offset_ptr->var)
3717 initial_offset_ptr->var
3718 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
3719 ssize_int (reg_parm_stack_space));
3720 initial_offset_ptr->constant = 0;
3722 else if (initial_offset_ptr->constant < reg_parm_stack_space)
3723 initial_offset_ptr->constant = reg_parm_stack_space;
3726 #endif /* REG_PARM_STACK_SPACE */
3728 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
3730 sizetree
3731 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
3732 where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
3733 boundary = targetm.calls.function_arg_boundary (passed_mode, type);
3734 round_boundary = targetm.calls.function_arg_round_boundary (passed_mode,
3735 type);
3736 locate->where_pad = where_pad;
3738 /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT. */
3739 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
3740 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
3742 locate->boundary = boundary;
3744 if (SUPPORTS_STACK_ALIGNMENT)
3746 /* stack_alignment_estimated can't change after stack has been
3747 realigned. */
3748 if (crtl->stack_alignment_estimated < boundary)
3750 if (!crtl->stack_realign_processed)
3751 crtl->stack_alignment_estimated = boundary;
3752 else
3754 /* If stack is realigned and stack alignment value
3755 hasn't been finalized, it is OK not to increase
3756 stack_alignment_estimated. The bigger alignment
3757 requirement is recorded in stack_alignment_needed
3758 below. */
3759 gcc_assert (!crtl->stack_realign_finalized
3760 && crtl->stack_realign_needed);
3765 /* Remember if the outgoing parameter requires extra alignment on the
3766 calling function side. */
3767 if (crtl->stack_alignment_needed < boundary)
3768 crtl->stack_alignment_needed = boundary;
3769 if (crtl->preferred_stack_boundary < boundary)
3770 crtl->preferred_stack_boundary = boundary;
3772 #ifdef ARGS_GROW_DOWNWARD
3773 locate->slot_offset.constant = -initial_offset_ptr->constant;
3774 if (initial_offset_ptr->var)
3775 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
3776 initial_offset_ptr->var);
3779 tree s2 = sizetree;
3780 if (where_pad != none
3781 && (!host_integerp (sizetree, 1)
3782 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % round_boundary))
3783 s2 = round_up (s2, round_boundary / BITS_PER_UNIT);
3784 SUB_PARM_SIZE (locate->slot_offset, s2);
3787 locate->slot_offset.constant += part_size_in_regs;
3789 if (!in_regs
3790 #ifdef REG_PARM_STACK_SPACE
3791 || REG_PARM_STACK_SPACE (fndecl) > 0
3792 #endif
3794 pad_to_arg_alignment (&locate->slot_offset, boundary,
3795 &locate->alignment_pad);
3797 locate->size.constant = (-initial_offset_ptr->constant
3798 - locate->slot_offset.constant);
3799 if (initial_offset_ptr->var)
3800 locate->size.var = size_binop (MINUS_EXPR,
3801 size_binop (MINUS_EXPR,
3802 ssize_int (0),
3803 initial_offset_ptr->var),
3804 locate->slot_offset.var);
3806 /* Pad_below needs the pre-rounded size to know how much to pad
3807 below. */
3808 locate->offset = locate->slot_offset;
3809 if (where_pad == downward)
3810 pad_below (&locate->offset, passed_mode, sizetree);
3812 #else /* !ARGS_GROW_DOWNWARD */
3813 if (!in_regs
3814 #ifdef REG_PARM_STACK_SPACE
3815 || REG_PARM_STACK_SPACE (fndecl) > 0
3816 #endif
3818 pad_to_arg_alignment (initial_offset_ptr, boundary,
3819 &locate->alignment_pad);
3820 locate->slot_offset = *initial_offset_ptr;
3822 #ifdef PUSH_ROUNDING
3823 if (passed_mode != BLKmode)
3824 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
3825 #endif
3827 /* Pad_below needs the pre-rounded size to know how much to pad below
3828 so this must be done before rounding up. */
3829 locate->offset = locate->slot_offset;
3830 if (where_pad == downward)
3831 pad_below (&locate->offset, passed_mode, sizetree);
3833 if (where_pad != none
3834 && (!host_integerp (sizetree, 1)
3835 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % round_boundary))
3836 sizetree = round_up (sizetree, round_boundary / BITS_PER_UNIT);
3838 ADD_PARM_SIZE (locate->size, sizetree);
3840 locate->size.constant -= part_size_in_regs;
3841 #endif /* ARGS_GROW_DOWNWARD */
3843 #ifdef FUNCTION_ARG_OFFSET
3844 locate->offset.constant += FUNCTION_ARG_OFFSET (passed_mode, type);
3845 #endif
3848 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
3849 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
3851 static void
3852 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
3853 struct args_size *alignment_pad)
3855 tree save_var = NULL_TREE;
3856 HOST_WIDE_INT save_constant = 0;
3857 int boundary_in_bytes = boundary / BITS_PER_UNIT;
3858 HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
3860 #ifdef SPARC_STACK_BOUNDARY_HACK
3861 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
3862 the real alignment of %sp. However, when it does this, the
3863 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
3864 if (SPARC_STACK_BOUNDARY_HACK)
3865 sp_offset = 0;
3866 #endif
3868 if (boundary > PARM_BOUNDARY)
3870 save_var = offset_ptr->var;
3871 save_constant = offset_ptr->constant;
3874 alignment_pad->var = NULL_TREE;
3875 alignment_pad->constant = 0;
3877 if (boundary > BITS_PER_UNIT)
3879 if (offset_ptr->var)
3881 tree sp_offset_tree = ssize_int (sp_offset);
3882 tree offset = size_binop (PLUS_EXPR,
3883 ARGS_SIZE_TREE (*offset_ptr),
3884 sp_offset_tree);
3885 #ifdef ARGS_GROW_DOWNWARD
3886 tree rounded = round_down (offset, boundary / BITS_PER_UNIT);
3887 #else
3888 tree rounded = round_up (offset, boundary / BITS_PER_UNIT);
3889 #endif
3891 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
3892 /* ARGS_SIZE_TREE includes constant term. */
3893 offset_ptr->constant = 0;
3894 if (boundary > PARM_BOUNDARY)
3895 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
3896 save_var);
3898 else
3900 offset_ptr->constant = -sp_offset +
3901 #ifdef ARGS_GROW_DOWNWARD
3902 FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3903 #else
3904 CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3905 #endif
3906 if (boundary > PARM_BOUNDARY)
3907 alignment_pad->constant = offset_ptr->constant - save_constant;
3912 static void
3913 pad_below (struct args_size *offset_ptr, enum machine_mode passed_mode, tree sizetree)
3915 if (passed_mode != BLKmode)
3917 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
3918 offset_ptr->constant
3919 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
3920 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
3921 - GET_MODE_SIZE (passed_mode));
3923 else
3925 if (TREE_CODE (sizetree) != INTEGER_CST
3926 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
3928 /* Round the size up to multiple of PARM_BOUNDARY bits. */
3929 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
3930 /* Add it in. */
3931 ADD_PARM_SIZE (*offset_ptr, s2);
3932 SUB_PARM_SIZE (*offset_ptr, sizetree);
3938 /* True if register REGNO was alive at a place where `setjmp' was
3939 called and was set more than once or is an argument. Such regs may
3940 be clobbered by `longjmp'. */
3942 static bool
3943 regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
3945 /* There appear to be cases where some local vars never reach the
3946 backend but have bogus regnos. */
3947 if (regno >= max_reg_num ())
3948 return false;
3950 return ((REG_N_SETS (regno) > 1
3951 || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR), regno))
3952 && REGNO_REG_SET_P (setjmp_crosses, regno));
3955 /* Walk the tree of blocks describing the binding levels within a
3956 function and warn about variables the might be killed by setjmp or
3957 vfork. This is done after calling flow_analysis before register
3958 allocation since that will clobber the pseudo-regs to hard
3959 regs. */
3961 static void
3962 setjmp_vars_warning (bitmap setjmp_crosses, tree block)
3964 tree decl, sub;
3966 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
3968 if (TREE_CODE (decl) == VAR_DECL
3969 && DECL_RTL_SET_P (decl)
3970 && REG_P (DECL_RTL (decl))
3971 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
3972 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
3973 " %<longjmp%> or %<vfork%>", decl);
3976 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
3977 setjmp_vars_warning (setjmp_crosses, sub);
3980 /* Do the appropriate part of setjmp_vars_warning
3981 but for arguments instead of local variables. */
3983 static void
3984 setjmp_args_warning (bitmap setjmp_crosses)
3986 tree decl;
3987 for (decl = DECL_ARGUMENTS (current_function_decl);
3988 decl; decl = DECL_CHAIN (decl))
3989 if (DECL_RTL (decl) != 0
3990 && REG_P (DECL_RTL (decl))
3991 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
3992 warning (OPT_Wclobbered,
3993 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
3994 decl);
3997 /* Generate warning messages for variables live across setjmp. */
3999 void
4000 generate_setjmp_warnings (void)
4002 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
4004 if (n_basic_blocks == NUM_FIXED_BLOCKS
4005 || bitmap_empty_p (setjmp_crosses))
4006 return;
4008 setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
4009 setjmp_args_warning (setjmp_crosses);
4013 /* Reverse the order of elements in the fragment chain T of blocks,
4014 and return the new head of the chain (old last element).
4015 In addition to that clear BLOCK_SAME_RANGE flags when needed
4016 and adjust BLOCK_SUPERCONTEXT from the super fragment to
4017 its super fragment origin. */
4019 static tree
4020 block_fragments_nreverse (tree t)
4022 tree prev = 0, block, next, prev_super = 0;
4023 tree super = BLOCK_SUPERCONTEXT (t);
4024 if (BLOCK_FRAGMENT_ORIGIN (super))
4025 super = BLOCK_FRAGMENT_ORIGIN (super);
4026 for (block = t; block; block = next)
4028 next = BLOCK_FRAGMENT_CHAIN (block);
4029 BLOCK_FRAGMENT_CHAIN (block) = prev;
4030 if ((prev && !BLOCK_SAME_RANGE (prev))
4031 || (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (block))
4032 != prev_super))
4033 BLOCK_SAME_RANGE (block) = 0;
4034 prev_super = BLOCK_SUPERCONTEXT (block);
4035 BLOCK_SUPERCONTEXT (block) = super;
4036 prev = block;
4038 t = BLOCK_FRAGMENT_ORIGIN (t);
4039 if (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (t))
4040 != prev_super)
4041 BLOCK_SAME_RANGE (t) = 0;
4042 BLOCK_SUPERCONTEXT (t) = super;
4043 return prev;
4046 /* Reverse the order of elements in the chain T of blocks,
4047 and return the new head of the chain (old last element).
4048 Also do the same on subblocks and reverse the order of elements
4049 in BLOCK_FRAGMENT_CHAIN as well. */
4051 static tree
4052 blocks_nreverse_all (tree t)
4054 tree prev = 0, block, next;
4055 for (block = t; block; block = next)
4057 next = BLOCK_CHAIN (block);
4058 BLOCK_CHAIN (block) = prev;
4059 if (BLOCK_FRAGMENT_CHAIN (block)
4060 && BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE)
4062 BLOCK_FRAGMENT_CHAIN (block)
4063 = block_fragments_nreverse (BLOCK_FRAGMENT_CHAIN (block));
4064 if (!BLOCK_SAME_RANGE (BLOCK_FRAGMENT_CHAIN (block)))
4065 BLOCK_SAME_RANGE (block) = 0;
4067 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4068 prev = block;
4070 return prev;
4074 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
4075 and create duplicate blocks. */
4076 /* ??? Need an option to either create block fragments or to create
4077 abstract origin duplicates of a source block. It really depends
4078 on what optimization has been performed. */
4080 void
4081 reorder_blocks (void)
4083 tree block = DECL_INITIAL (current_function_decl);
4084 VEC(tree,heap) *block_stack;
4086 if (block == NULL_TREE)
4087 return;
4089 block_stack = VEC_alloc (tree, heap, 10);
4091 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
4092 clear_block_marks (block);
4094 /* Prune the old trees away, so that they don't get in the way. */
4095 BLOCK_SUBBLOCKS (block) = NULL_TREE;
4096 BLOCK_CHAIN (block) = NULL_TREE;
4098 /* Recreate the block tree from the note nesting. */
4099 reorder_blocks_1 (get_insns (), block, &block_stack);
4100 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4102 VEC_free (tree, heap, block_stack);
4105 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
4107 void
4108 clear_block_marks (tree block)
4110 while (block)
4112 TREE_ASM_WRITTEN (block) = 0;
4113 clear_block_marks (BLOCK_SUBBLOCKS (block));
4114 block = BLOCK_CHAIN (block);
4118 static void
4119 reorder_blocks_1 (rtx insns, tree current_block, VEC(tree,heap) **p_block_stack)
4121 rtx insn;
4122 tree prev_beg = NULL_TREE, prev_end = NULL_TREE;
4124 for (insn = insns; insn; insn = NEXT_INSN (insn))
4126 if (NOTE_P (insn))
4128 if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
4130 tree block = NOTE_BLOCK (insn);
4131 tree origin;
4133 gcc_assert (BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE);
4134 origin = block;
4136 if (prev_end)
4137 BLOCK_SAME_RANGE (prev_end) = 0;
4138 prev_end = NULL_TREE;
4140 /* If we have seen this block before, that means it now
4141 spans multiple address regions. Create a new fragment. */
4142 if (TREE_ASM_WRITTEN (block))
4144 tree new_block = copy_node (block);
4146 BLOCK_SAME_RANGE (new_block) = 0;
4147 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
4148 BLOCK_FRAGMENT_CHAIN (new_block)
4149 = BLOCK_FRAGMENT_CHAIN (origin);
4150 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
4152 NOTE_BLOCK (insn) = new_block;
4153 block = new_block;
4156 if (prev_beg == current_block && prev_beg)
4157 BLOCK_SAME_RANGE (block) = 1;
4159 prev_beg = origin;
4161 BLOCK_SUBBLOCKS (block) = 0;
4162 TREE_ASM_WRITTEN (block) = 1;
4163 /* When there's only one block for the entire function,
4164 current_block == block and we mustn't do this, it
4165 will cause infinite recursion. */
4166 if (block != current_block)
4168 tree super;
4169 if (block != origin)
4170 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block
4171 || BLOCK_FRAGMENT_ORIGIN (BLOCK_SUPERCONTEXT
4172 (origin))
4173 == current_block);
4174 if (VEC_empty (tree, *p_block_stack))
4175 super = current_block;
4176 else
4178 super = VEC_last (tree, *p_block_stack);
4179 gcc_assert (super == current_block
4180 || BLOCK_FRAGMENT_ORIGIN (super)
4181 == current_block);
4183 BLOCK_SUPERCONTEXT (block) = super;
4184 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4185 BLOCK_SUBBLOCKS (current_block) = block;
4186 current_block = origin;
4188 VEC_safe_push (tree, heap, *p_block_stack, block);
4190 else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
4192 NOTE_BLOCK (insn) = VEC_pop (tree, *p_block_stack);
4193 current_block = BLOCK_SUPERCONTEXT (current_block);
4194 if (BLOCK_FRAGMENT_ORIGIN (current_block))
4195 current_block = BLOCK_FRAGMENT_ORIGIN (current_block);
4196 prev_beg = NULL_TREE;
4197 prev_end = BLOCK_SAME_RANGE (NOTE_BLOCK (insn))
4198 ? NOTE_BLOCK (insn) : NULL_TREE;
4201 else
4203 prev_beg = NULL_TREE;
4204 if (prev_end)
4205 BLOCK_SAME_RANGE (prev_end) = 0;
4206 prev_end = NULL_TREE;
4211 /* Reverse the order of elements in the chain T of blocks,
4212 and return the new head of the chain (old last element). */
4214 tree
4215 blocks_nreverse (tree t)
4217 tree prev = 0, block, next;
4218 for (block = t; block; block = next)
4220 next = BLOCK_CHAIN (block);
4221 BLOCK_CHAIN (block) = prev;
4222 prev = block;
4224 return prev;
4227 /* Concatenate two chains of blocks (chained through BLOCK_CHAIN)
4228 by modifying the last node in chain 1 to point to chain 2. */
4230 tree
4231 block_chainon (tree op1, tree op2)
4233 tree t1;
4235 if (!op1)
4236 return op2;
4237 if (!op2)
4238 return op1;
4240 for (t1 = op1; BLOCK_CHAIN (t1); t1 = BLOCK_CHAIN (t1))
4241 continue;
4242 BLOCK_CHAIN (t1) = op2;
4244 #ifdef ENABLE_TREE_CHECKING
4246 tree t2;
4247 for (t2 = op2; t2; t2 = BLOCK_CHAIN (t2))
4248 gcc_assert (t2 != t1);
4250 #endif
4252 return op1;
4255 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
4256 non-NULL, list them all into VECTOR, in a depth-first preorder
4257 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
4258 blocks. */
4260 static int
4261 all_blocks (tree block, tree *vector)
4263 int n_blocks = 0;
4265 while (block)
4267 TREE_ASM_WRITTEN (block) = 0;
4269 /* Record this block. */
4270 if (vector)
4271 vector[n_blocks] = block;
4273 ++n_blocks;
4275 /* Record the subblocks, and their subblocks... */
4276 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4277 vector ? vector + n_blocks : 0);
4278 block = BLOCK_CHAIN (block);
4281 return n_blocks;
4284 /* Return a vector containing all the blocks rooted at BLOCK. The
4285 number of elements in the vector is stored in N_BLOCKS_P. The
4286 vector is dynamically allocated; it is the caller's responsibility
4287 to call `free' on the pointer returned. */
4289 static tree *
4290 get_block_vector (tree block, int *n_blocks_p)
4292 tree *block_vector;
4294 *n_blocks_p = all_blocks (block, NULL);
4295 block_vector = XNEWVEC (tree, *n_blocks_p);
4296 all_blocks (block, block_vector);
4298 return block_vector;
4301 static GTY(()) int next_block_index = 2;
4303 /* Set BLOCK_NUMBER for all the blocks in FN. */
4305 void
4306 number_blocks (tree fn)
4308 int i;
4309 int n_blocks;
4310 tree *block_vector;
4312 /* For SDB and XCOFF debugging output, we start numbering the blocks
4313 from 1 within each function, rather than keeping a running
4314 count. */
4315 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
4316 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
4317 next_block_index = 1;
4318 #endif
4320 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4322 /* The top-level BLOCK isn't numbered at all. */
4323 for (i = 1; i < n_blocks; ++i)
4324 /* We number the blocks from two. */
4325 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4327 free (block_vector);
4329 return;
4332 /* If VAR is present in a subblock of BLOCK, return the subblock. */
4334 DEBUG_FUNCTION tree
4335 debug_find_var_in_block_tree (tree var, tree block)
4337 tree t;
4339 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4340 if (t == var)
4341 return block;
4343 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4345 tree ret = debug_find_var_in_block_tree (var, t);
4346 if (ret)
4347 return ret;
4350 return NULL_TREE;
4353 /* Keep track of whether we're in a dummy function context. If we are,
4354 we don't want to invoke the set_current_function hook, because we'll
4355 get into trouble if the hook calls target_reinit () recursively or
4356 when the initial initialization is not yet complete. */
4358 static bool in_dummy_function;
4360 /* Invoke the target hook when setting cfun. Update the optimization options
4361 if the function uses different options than the default. */
4363 static void
4364 invoke_set_current_function_hook (tree fndecl)
4366 if (!in_dummy_function)
4368 tree opts = ((fndecl)
4369 ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4370 : optimization_default_node);
4372 if (!opts)
4373 opts = optimization_default_node;
4375 /* Change optimization options if needed. */
4376 if (optimization_current_node != opts)
4378 optimization_current_node = opts;
4379 cl_optimization_restore (&global_options, TREE_OPTIMIZATION (opts));
4382 targetm.set_current_function (fndecl);
4386 /* cfun should never be set directly; use this function. */
4388 void
4389 set_cfun (struct function *new_cfun)
4391 if (cfun != new_cfun)
4393 cfun = new_cfun;
4394 invoke_set_current_function_hook (new_cfun ? new_cfun->decl : NULL_TREE);
4398 /* Initialized with NOGC, making this poisonous to the garbage collector. */
4400 static VEC(function_p,heap) *cfun_stack;
4402 /* Push the current cfun onto the stack, and set cfun to new_cfun. */
4404 void
4405 push_cfun (struct function *new_cfun)
4407 VEC_safe_push (function_p, heap, cfun_stack, cfun);
4408 set_cfun (new_cfun);
4411 /* Pop cfun from the stack. */
4413 void
4414 pop_cfun (void)
4416 struct function *new_cfun = VEC_pop (function_p, cfun_stack);
4417 set_cfun (new_cfun);
4420 /* Return value of funcdef and increase it. */
4422 get_next_funcdef_no (void)
4424 return funcdef_no++;
4427 /* Return value of funcdef. */
4429 get_last_funcdef_no (void)
4431 return funcdef_no;
4434 /* Allocate a function structure for FNDECL and set its contents
4435 to the defaults. Set cfun to the newly-allocated object.
4436 Some of the helper functions invoked during initialization assume
4437 that cfun has already been set. Therefore, assign the new object
4438 directly into cfun and invoke the back end hook explicitly at the
4439 very end, rather than initializing a temporary and calling set_cfun
4440 on it.
4442 ABSTRACT_P is true if this is a function that will never be seen by
4443 the middle-end. Such functions are front-end concepts (like C++
4444 function templates) that do not correspond directly to functions
4445 placed in object files. */
4447 void
4448 allocate_struct_function (tree fndecl, bool abstract_p)
4450 tree result;
4451 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4453 cfun = ggc_alloc_cleared_function ();
4455 init_eh_for_function ();
4457 if (init_machine_status)
4458 cfun->machine = (*init_machine_status) ();
4460 #ifdef OVERRIDE_ABI_FORMAT
4461 OVERRIDE_ABI_FORMAT (fndecl);
4462 #endif
4464 invoke_set_current_function_hook (fndecl);
4466 if (fndecl != NULL_TREE)
4468 DECL_STRUCT_FUNCTION (fndecl) = cfun;
4469 cfun->decl = fndecl;
4470 current_function_funcdef_no = get_next_funcdef_no ();
4472 result = DECL_RESULT (fndecl);
4473 if (!abstract_p && aggregate_value_p (result, fndecl))
4475 #ifdef PCC_STATIC_STRUCT_RETURN
4476 cfun->returns_pcc_struct = 1;
4477 #endif
4478 cfun->returns_struct = 1;
4481 cfun->stdarg = stdarg_p (fntype);
4483 /* Assume all registers in stdarg functions need to be saved. */
4484 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4485 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4487 /* ??? This could be set on a per-function basis by the front-end
4488 but is this worth the hassle? */
4489 cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4493 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4494 instead of just setting it. */
4496 void
4497 push_struct_function (tree fndecl)
4499 VEC_safe_push (function_p, heap, cfun_stack, cfun);
4500 allocate_struct_function (fndecl, false);
4503 /* Reset crtl and other non-struct-function variables to defaults as
4504 appropriate for emitting rtl at the start of a function. */
4506 static void
4507 prepare_function_start (void)
4509 gcc_assert (!crtl->emit.x_last_insn);
4510 init_temp_slots ();
4511 init_emit ();
4512 init_varasm_status ();
4513 init_expr ();
4514 default_rtl_profile ();
4516 if (flag_stack_usage_info)
4518 cfun->su = ggc_alloc_cleared_stack_usage ();
4519 cfun->su->static_stack_size = -1;
4522 cse_not_expected = ! optimize;
4524 /* Caller save not needed yet. */
4525 caller_save_needed = 0;
4527 /* We haven't done register allocation yet. */
4528 reg_renumber = 0;
4530 /* Indicate that we have not instantiated virtual registers yet. */
4531 virtuals_instantiated = 0;
4533 /* Indicate that we want CONCATs now. */
4534 generating_concat_p = 1;
4536 /* Indicate we have no need of a frame pointer yet. */
4537 frame_pointer_needed = 0;
4540 /* Initialize the rtl expansion mechanism so that we can do simple things
4541 like generate sequences. This is used to provide a context during global
4542 initialization of some passes. You must call expand_dummy_function_end
4543 to exit this context. */
4545 void
4546 init_dummy_function_start (void)
4548 gcc_assert (!in_dummy_function);
4549 in_dummy_function = true;
4550 push_struct_function (NULL_TREE);
4551 prepare_function_start ();
4554 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4555 and initialize static variables for generating RTL for the statements
4556 of the function. */
4558 void
4559 init_function_start (tree subr)
4561 if (subr && DECL_STRUCT_FUNCTION (subr))
4562 set_cfun (DECL_STRUCT_FUNCTION (subr));
4563 else
4564 allocate_struct_function (subr, false);
4565 prepare_function_start ();
4566 decide_function_section (subr);
4568 /* Warn if this value is an aggregate type,
4569 regardless of which calling convention we are using for it. */
4570 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
4571 warning (OPT_Waggregate_return, "function returns an aggregate");
4575 void
4576 expand_main_function (void)
4578 #if (defined(INVOKE__main) \
4579 || (!defined(HAS_INIT_SECTION) \
4580 && !defined(INIT_SECTION_ASM_OP) \
4581 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
4582 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
4583 #endif
4586 /* Expand code to initialize the stack_protect_guard. This is invoked at
4587 the beginning of a function to be protected. */
4589 #ifndef HAVE_stack_protect_set
4590 # define HAVE_stack_protect_set 0
4591 # define gen_stack_protect_set(x,y) (gcc_unreachable (), NULL_RTX)
4592 #endif
4594 void
4595 stack_protect_prologue (void)
4597 tree guard_decl = targetm.stack_protect_guard ();
4598 rtx x, y;
4600 x = expand_normal (crtl->stack_protect_guard);
4601 y = expand_normal (guard_decl);
4603 /* Allow the target to copy from Y to X without leaking Y into a
4604 register. */
4605 if (HAVE_stack_protect_set)
4607 rtx insn = gen_stack_protect_set (x, y);
4608 if (insn)
4610 emit_insn (insn);
4611 return;
4615 /* Otherwise do a straight move. */
4616 emit_move_insn (x, y);
4619 /* Expand code to verify the stack_protect_guard. This is invoked at
4620 the end of a function to be protected. */
4622 #ifndef HAVE_stack_protect_test
4623 # define HAVE_stack_protect_test 0
4624 # define gen_stack_protect_test(x, y, z) (gcc_unreachable (), NULL_RTX)
4625 #endif
4627 void
4628 stack_protect_epilogue (void)
4630 tree guard_decl = targetm.stack_protect_guard ();
4631 rtx label = gen_label_rtx ();
4632 rtx x, y, tmp;
4634 x = expand_normal (crtl->stack_protect_guard);
4635 y = expand_normal (guard_decl);
4637 /* Allow the target to compare Y with X without leaking either into
4638 a register. */
4639 switch (HAVE_stack_protect_test != 0)
4641 case 1:
4642 tmp = gen_stack_protect_test (x, y, label);
4643 if (tmp)
4645 emit_insn (tmp);
4646 break;
4648 /* FALLTHRU */
4650 default:
4651 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
4652 break;
4655 /* The noreturn predictor has been moved to the tree level. The rtl-level
4656 predictors estimate this branch about 20%, which isn't enough to get
4657 things moved out of line. Since this is the only extant case of adding
4658 a noreturn function at the rtl level, it doesn't seem worth doing ought
4659 except adding the prediction by hand. */
4660 tmp = get_last_insn ();
4661 if (JUMP_P (tmp))
4662 predict_insn_def (tmp, PRED_NORETURN, TAKEN);
4664 expand_expr_stmt (targetm.stack_protect_fail ());
4665 emit_label (label);
4668 /* Start the RTL for a new function, and set variables used for
4669 emitting RTL.
4670 SUBR is the FUNCTION_DECL node.
4671 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
4672 the function's parameters, which must be run at any return statement. */
4674 void
4675 expand_function_start (tree subr)
4677 /* Make sure volatile mem refs aren't considered
4678 valid operands of arithmetic insns. */
4679 init_recog_no_volatile ();
4681 crtl->profile
4682 = (profile_flag
4683 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
4685 crtl->limit_stack
4686 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
4688 /* Make the label for return statements to jump to. Do not special
4689 case machines with special return instructions -- they will be
4690 handled later during jump, ifcvt, or epilogue creation. */
4691 return_label = gen_label_rtx ();
4693 /* Initialize rtx used to return the value. */
4694 /* Do this before assign_parms so that we copy the struct value address
4695 before any library calls that assign parms might generate. */
4697 /* Decide whether to return the value in memory or in a register. */
4698 if (aggregate_value_p (DECL_RESULT (subr), subr))
4700 /* Returning something that won't go in a register. */
4701 rtx value_address = 0;
4703 #ifdef PCC_STATIC_STRUCT_RETURN
4704 if (cfun->returns_pcc_struct)
4706 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
4707 value_address = assemble_static_space (size);
4709 else
4710 #endif
4712 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
4713 /* Expect to be passed the address of a place to store the value.
4714 If it is passed as an argument, assign_parms will take care of
4715 it. */
4716 if (sv)
4718 value_address = gen_reg_rtx (Pmode);
4719 emit_move_insn (value_address, sv);
4722 if (value_address)
4724 rtx x = value_address;
4725 if (!DECL_BY_REFERENCE (DECL_RESULT (subr)))
4727 x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), x);
4728 set_mem_attributes (x, DECL_RESULT (subr), 1);
4730 SET_DECL_RTL (DECL_RESULT (subr), x);
4733 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
4734 /* If return mode is void, this decl rtl should not be used. */
4735 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
4736 else
4738 /* Compute the return values into a pseudo reg, which we will copy
4739 into the true return register after the cleanups are done. */
4740 tree return_type = TREE_TYPE (DECL_RESULT (subr));
4741 if (TYPE_MODE (return_type) != BLKmode
4742 && targetm.calls.return_in_msb (return_type))
4743 /* expand_function_end will insert the appropriate padding in
4744 this case. Use the return value's natural (unpadded) mode
4745 within the function proper. */
4746 SET_DECL_RTL (DECL_RESULT (subr),
4747 gen_reg_rtx (TYPE_MODE (return_type)));
4748 else
4750 /* In order to figure out what mode to use for the pseudo, we
4751 figure out what the mode of the eventual return register will
4752 actually be, and use that. */
4753 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
4755 /* Structures that are returned in registers are not
4756 aggregate_value_p, so we may see a PARALLEL or a REG. */
4757 if (REG_P (hard_reg))
4758 SET_DECL_RTL (DECL_RESULT (subr),
4759 gen_reg_rtx (GET_MODE (hard_reg)));
4760 else
4762 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
4763 SET_DECL_RTL (DECL_RESULT (subr), gen_group_rtx (hard_reg));
4767 /* Set DECL_REGISTER flag so that expand_function_end will copy the
4768 result to the real return register(s). */
4769 DECL_REGISTER (DECL_RESULT (subr)) = 1;
4772 /* Initialize rtx for parameters and local variables.
4773 In some cases this requires emitting insns. */
4774 assign_parms (subr);
4776 /* If function gets a static chain arg, store it. */
4777 if (cfun->static_chain_decl)
4779 tree parm = cfun->static_chain_decl;
4780 rtx local, chain, insn;
4782 local = gen_reg_rtx (Pmode);
4783 chain = targetm.calls.static_chain (current_function_decl, true);
4785 set_decl_incoming_rtl (parm, chain, false);
4786 SET_DECL_RTL (parm, local);
4787 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4789 insn = emit_move_insn (local, chain);
4791 /* Mark the register as eliminable, similar to parameters. */
4792 if (MEM_P (chain)
4793 && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
4794 set_dst_reg_note (insn, REG_EQUIV, chain, local);
4797 /* If the function receives a non-local goto, then store the
4798 bits we need to restore the frame pointer. */
4799 if (cfun->nonlocal_goto_save_area)
4801 tree t_save;
4802 rtx r_save;
4804 tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
4805 gcc_assert (DECL_RTL_SET_P (var));
4807 t_save = build4 (ARRAY_REF,
4808 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
4809 cfun->nonlocal_goto_save_area,
4810 integer_zero_node, NULL_TREE, NULL_TREE);
4811 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
4812 gcc_assert (GET_MODE (r_save) == Pmode);
4814 emit_move_insn (r_save, targetm.builtin_setjmp_frame_value ());
4815 update_nonlocal_goto_save_area ();
4818 /* The following was moved from init_function_start.
4819 The move is supposed to make sdb output more accurate. */
4820 /* Indicate the beginning of the function body,
4821 as opposed to parm setup. */
4822 emit_note (NOTE_INSN_FUNCTION_BEG);
4824 gcc_assert (NOTE_P (get_last_insn ()));
4826 parm_birth_insn = get_last_insn ();
4828 if (crtl->profile)
4830 #ifdef PROFILE_HOOK
4831 PROFILE_HOOK (current_function_funcdef_no);
4832 #endif
4835 /* If we are doing generic stack checking, the probe should go here. */
4836 if (flag_stack_check == GENERIC_STACK_CHECK)
4837 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
4840 /* Undo the effects of init_dummy_function_start. */
4841 void
4842 expand_dummy_function_end (void)
4844 gcc_assert (in_dummy_function);
4846 /* End any sequences that failed to be closed due to syntax errors. */
4847 while (in_sequence_p ())
4848 end_sequence ();
4850 /* Outside function body, can't compute type's actual size
4851 until next function's body starts. */
4853 free_after_parsing (cfun);
4854 free_after_compilation (cfun);
4855 pop_cfun ();
4856 in_dummy_function = false;
4859 /* Call DOIT for each hard register used as a return value from
4860 the current function. */
4862 void
4863 diddle_return_value (void (*doit) (rtx, void *), void *arg)
4865 rtx outgoing = crtl->return_rtx;
4867 if (! outgoing)
4868 return;
4870 if (REG_P (outgoing))
4871 (*doit) (outgoing, arg);
4872 else if (GET_CODE (outgoing) == PARALLEL)
4874 int i;
4876 for (i = 0; i < XVECLEN (outgoing, 0); i++)
4878 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
4880 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
4881 (*doit) (x, arg);
4886 static void
4887 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4889 emit_clobber (reg);
4892 void
4893 clobber_return_register (void)
4895 diddle_return_value (do_clobber_return_reg, NULL);
4897 /* In case we do use pseudo to return value, clobber it too. */
4898 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
4900 tree decl_result = DECL_RESULT (current_function_decl);
4901 rtx decl_rtl = DECL_RTL (decl_result);
4902 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
4904 do_clobber_return_reg (decl_rtl, NULL);
4909 static void
4910 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4912 emit_use (reg);
4915 static void
4916 use_return_register (void)
4918 diddle_return_value (do_use_return_reg, NULL);
4921 /* Possibly warn about unused parameters. */
4922 void
4923 do_warn_unused_parameter (tree fn)
4925 tree decl;
4927 for (decl = DECL_ARGUMENTS (fn);
4928 decl; decl = DECL_CHAIN (decl))
4929 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
4930 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
4931 && !TREE_NO_WARNING (decl))
4932 warning (OPT_Wunused_parameter, "unused parameter %q+D", decl);
4935 static GTY(()) rtx initial_trampoline;
4937 /* Generate RTL for the end of the current function. */
4939 void
4940 expand_function_end (void)
4942 rtx clobber_after;
4944 /* If arg_pointer_save_area was referenced only from a nested
4945 function, we will not have initialized it yet. Do that now. */
4946 if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
4947 get_arg_pointer_save_area ();
4949 /* If we are doing generic stack checking and this function makes calls,
4950 do a stack probe at the start of the function to ensure we have enough
4951 space for another stack frame. */
4952 if (flag_stack_check == GENERIC_STACK_CHECK)
4954 rtx insn, seq;
4956 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4957 if (CALL_P (insn))
4959 rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
4960 start_sequence ();
4961 if (STACK_CHECK_MOVING_SP)
4962 anti_adjust_stack_and_probe (max_frame_size, true);
4963 else
4964 probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
4965 seq = get_insns ();
4966 end_sequence ();
4967 set_insn_locators (seq, prologue_locator);
4968 emit_insn_before (seq, stack_check_probe_note);
4969 break;
4973 /* End any sequences that failed to be closed due to syntax errors. */
4974 while (in_sequence_p ())
4975 end_sequence ();
4977 clear_pending_stack_adjust ();
4978 do_pending_stack_adjust ();
4980 /* Output a linenumber for the end of the function.
4981 SDB depends on this. */
4982 set_curr_insn_source_location (input_location);
4984 /* Before the return label (if any), clobber the return
4985 registers so that they are not propagated live to the rest of
4986 the function. This can only happen with functions that drop
4987 through; if there had been a return statement, there would
4988 have either been a return rtx, or a jump to the return label.
4990 We delay actual code generation after the current_function_value_rtx
4991 is computed. */
4992 clobber_after = get_last_insn ();
4994 /* Output the label for the actual return from the function. */
4995 emit_label (return_label);
4997 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
4999 /* Let except.c know where it should emit the call to unregister
5000 the function context for sjlj exceptions. */
5001 if (flag_exceptions)
5002 sjlj_emit_function_exit_after (get_last_insn ());
5004 else
5006 /* We want to ensure that instructions that may trap are not
5007 moved into the epilogue by scheduling, because we don't
5008 always emit unwind information for the epilogue. */
5009 if (cfun->can_throw_non_call_exceptions)
5010 emit_insn (gen_blockage ());
5013 /* If this is an implementation of throw, do what's necessary to
5014 communicate between __builtin_eh_return and the epilogue. */
5015 expand_eh_return ();
5017 /* If scalar return value was computed in a pseudo-reg, or was a named
5018 return value that got dumped to the stack, copy that to the hard
5019 return register. */
5020 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5022 tree decl_result = DECL_RESULT (current_function_decl);
5023 rtx decl_rtl = DECL_RTL (decl_result);
5025 if (REG_P (decl_rtl)
5026 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5027 : DECL_REGISTER (decl_result))
5029 rtx real_decl_rtl = crtl->return_rtx;
5031 /* This should be set in assign_parms. */
5032 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
5034 /* If this is a BLKmode structure being returned in registers,
5035 then use the mode computed in expand_return. Note that if
5036 decl_rtl is memory, then its mode may have been changed,
5037 but that crtl->return_rtx has not. */
5038 if (GET_MODE (real_decl_rtl) == BLKmode)
5039 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
5041 /* If a non-BLKmode return value should be padded at the least
5042 significant end of the register, shift it left by the appropriate
5043 amount. BLKmode results are handled using the group load/store
5044 machinery. */
5045 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
5046 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
5048 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
5049 REGNO (real_decl_rtl)),
5050 decl_rtl);
5051 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
5053 /* If a named return value dumped decl_return to memory, then
5054 we may need to re-do the PROMOTE_MODE signed/unsigned
5055 extension. */
5056 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
5058 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
5059 promote_function_mode (TREE_TYPE (decl_result),
5060 GET_MODE (decl_rtl), &unsignedp,
5061 TREE_TYPE (current_function_decl), 1);
5063 convert_move (real_decl_rtl, decl_rtl, unsignedp);
5065 else if (GET_CODE (real_decl_rtl) == PARALLEL)
5067 /* If expand_function_start has created a PARALLEL for decl_rtl,
5068 move the result to the real return registers. Otherwise, do
5069 a group load from decl_rtl for a named return. */
5070 if (GET_CODE (decl_rtl) == PARALLEL)
5071 emit_group_move (real_decl_rtl, decl_rtl);
5072 else
5073 emit_group_load (real_decl_rtl, decl_rtl,
5074 TREE_TYPE (decl_result),
5075 int_size_in_bytes (TREE_TYPE (decl_result)));
5077 /* In the case of complex integer modes smaller than a word, we'll
5078 need to generate some non-trivial bitfield insertions. Do that
5079 on a pseudo and not the hard register. */
5080 else if (GET_CODE (decl_rtl) == CONCAT
5081 && GET_MODE_CLASS (GET_MODE (decl_rtl)) == MODE_COMPLEX_INT
5082 && GET_MODE_BITSIZE (GET_MODE (decl_rtl)) <= BITS_PER_WORD)
5084 int old_generating_concat_p;
5085 rtx tmp;
5087 old_generating_concat_p = generating_concat_p;
5088 generating_concat_p = 0;
5089 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
5090 generating_concat_p = old_generating_concat_p;
5092 emit_move_insn (tmp, decl_rtl);
5093 emit_move_insn (real_decl_rtl, tmp);
5095 else
5096 emit_move_insn (real_decl_rtl, decl_rtl);
5100 /* If returning a structure, arrange to return the address of the value
5101 in a place where debuggers expect to find it.
5103 If returning a structure PCC style,
5104 the caller also depends on this value.
5105 And cfun->returns_pcc_struct is not necessarily set. */
5106 if (cfun->returns_struct
5107 || cfun->returns_pcc_struct)
5109 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
5110 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5111 rtx outgoing;
5113 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
5114 type = TREE_TYPE (type);
5115 else
5116 value_address = XEXP (value_address, 0);
5118 outgoing = targetm.calls.function_value (build_pointer_type (type),
5119 current_function_decl, true);
5121 /* Mark this as a function return value so integrate will delete the
5122 assignment and USE below when inlining this function. */
5123 REG_FUNCTION_VALUE_P (outgoing) = 1;
5125 /* The address may be ptr_mode and OUTGOING may be Pmode. */
5126 value_address = convert_memory_address (GET_MODE (outgoing),
5127 value_address);
5129 emit_move_insn (outgoing, value_address);
5131 /* Show return register used to hold result (in this case the address
5132 of the result. */
5133 crtl->return_rtx = outgoing;
5136 /* Emit the actual code to clobber return register. */
5138 rtx seq;
5140 start_sequence ();
5141 clobber_return_register ();
5142 seq = get_insns ();
5143 end_sequence ();
5145 emit_insn_after (seq, clobber_after);
5148 /* Output the label for the naked return from the function. */
5149 if (naked_return_label)
5150 emit_label (naked_return_label);
5152 /* @@@ This is a kludge. We want to ensure that instructions that
5153 may trap are not moved into the epilogue by scheduling, because
5154 we don't always emit unwind information for the epilogue. */
5155 if (cfun->can_throw_non_call_exceptions
5156 && targetm_common.except_unwind_info (&global_options) != UI_SJLJ)
5157 emit_insn (gen_blockage ());
5159 /* If stack protection is enabled for this function, check the guard. */
5160 if (crtl->stack_protect_guard)
5161 stack_protect_epilogue ();
5163 /* If we had calls to alloca, and this machine needs
5164 an accurate stack pointer to exit the function,
5165 insert some code to save and restore the stack pointer. */
5166 if (! EXIT_IGNORE_STACK
5167 && cfun->calls_alloca)
5169 rtx tem = 0, seq;
5171 start_sequence ();
5172 emit_stack_save (SAVE_FUNCTION, &tem);
5173 seq = get_insns ();
5174 end_sequence ();
5175 emit_insn_before (seq, parm_birth_insn);
5177 emit_stack_restore (SAVE_FUNCTION, tem);
5180 /* ??? This should no longer be necessary since stupid is no longer with
5181 us, but there are some parts of the compiler (eg reload_combine, and
5182 sh mach_dep_reorg) that still try and compute their own lifetime info
5183 instead of using the general framework. */
5184 use_return_register ();
5188 get_arg_pointer_save_area (void)
5190 rtx ret = arg_pointer_save_area;
5192 if (! ret)
5194 ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5195 arg_pointer_save_area = ret;
5198 if (! crtl->arg_pointer_save_area_init)
5200 rtx seq;
5202 /* Save the arg pointer at the beginning of the function. The
5203 generated stack slot may not be a valid memory address, so we
5204 have to check it and fix it if necessary. */
5205 start_sequence ();
5206 emit_move_insn (validize_mem (ret),
5207 crtl->args.internal_arg_pointer);
5208 seq = get_insns ();
5209 end_sequence ();
5211 push_topmost_sequence ();
5212 emit_insn_after (seq, entry_of_function ());
5213 pop_topmost_sequence ();
5215 crtl->arg_pointer_save_area_init = true;
5218 return ret;
5221 /* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
5222 for the first time. */
5224 static void
5225 record_insns (rtx insns, rtx end, htab_t *hashp)
5227 rtx tmp;
5228 htab_t hash = *hashp;
5230 if (hash == NULL)
5231 *hashp = hash
5232 = htab_create_ggc (17, htab_hash_pointer, htab_eq_pointer, NULL);
5234 for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5236 void **slot = htab_find_slot (hash, tmp, INSERT);
5237 gcc_assert (*slot == NULL);
5238 *slot = tmp;
5242 /* INSN has been duplicated or replaced by as COPY, perhaps by duplicating a
5243 basic block, splitting or peepholes. If INSN is a prologue or epilogue
5244 insn, then record COPY as well. */
5246 void
5247 maybe_copy_prologue_epilogue_insn (rtx insn, rtx copy)
5249 htab_t hash;
5250 void **slot;
5252 hash = epilogue_insn_hash;
5253 if (!hash || !htab_find (hash, insn))
5255 hash = prologue_insn_hash;
5256 if (!hash || !htab_find (hash, insn))
5257 return;
5260 slot = htab_find_slot (hash, copy, INSERT);
5261 gcc_assert (*slot == NULL);
5262 *slot = copy;
5265 /* Set the locator of the insn chain starting at INSN to LOC. */
5266 static void
5267 set_insn_locators (rtx insn, int loc)
5269 while (insn != NULL_RTX)
5271 if (INSN_P (insn))
5272 INSN_LOCATOR (insn) = loc;
5273 insn = NEXT_INSN (insn);
5277 /* Determine if any INSNs in HASH are, or are part of, INSN. Because
5278 we can be running after reorg, SEQUENCE rtl is possible. */
5280 static bool
5281 contains (const_rtx insn, htab_t hash)
5283 if (hash == NULL)
5284 return false;
5286 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5288 int i;
5289 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
5290 if (htab_find (hash, XVECEXP (PATTERN (insn), 0, i)))
5291 return true;
5292 return false;
5295 return htab_find (hash, insn) != NULL;
5299 prologue_epilogue_contains (const_rtx insn)
5301 if (contains (insn, prologue_insn_hash))
5302 return 1;
5303 if (contains (insn, epilogue_insn_hash))
5304 return 1;
5305 return 0;
5308 #ifdef HAVE_simple_return
5310 /* Return true if INSN requires the stack frame to be set up.
5311 PROLOGUE_USED contains the hard registers used in the function
5312 prologue. SET_UP_BY_PROLOGUE is the set of registers we expect the
5313 prologue to set up for the function. */
5314 bool
5315 requires_stack_frame_p (rtx insn, HARD_REG_SET prologue_used,
5316 HARD_REG_SET set_up_by_prologue)
5318 df_ref *df_rec;
5319 HARD_REG_SET hardregs;
5320 unsigned regno;
5322 if (CALL_P (insn))
5323 return !SIBLING_CALL_P (insn);
5325 /* We need a frame to get the unique CFA expected by the unwinder. */
5326 if (cfun->can_throw_non_call_exceptions && can_throw_internal (insn))
5327 return true;
5329 CLEAR_HARD_REG_SET (hardregs);
5330 for (df_rec = DF_INSN_DEFS (insn); *df_rec; df_rec++)
5332 rtx dreg = DF_REF_REG (*df_rec);
5334 if (!REG_P (dreg))
5335 continue;
5337 add_to_hard_reg_set (&hardregs, GET_MODE (dreg),
5338 REGNO (dreg));
5340 if (hard_reg_set_intersect_p (hardregs, prologue_used))
5341 return true;
5342 AND_COMPL_HARD_REG_SET (hardregs, call_used_reg_set);
5343 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5344 if (TEST_HARD_REG_BIT (hardregs, regno)
5345 && df_regs_ever_live_p (regno))
5346 return true;
5348 for (df_rec = DF_INSN_USES (insn); *df_rec; df_rec++)
5350 rtx reg = DF_REF_REG (*df_rec);
5352 if (!REG_P (reg))
5353 continue;
5355 add_to_hard_reg_set (&hardregs, GET_MODE (reg),
5356 REGNO (reg));
5358 if (hard_reg_set_intersect_p (hardregs, set_up_by_prologue))
5359 return true;
5361 return false;
5364 /* See whether BB has a single successor that uses [REGNO, END_REGNO),
5365 and if BB is its only predecessor. Return that block if so,
5366 otherwise return null. */
5368 static basic_block
5369 next_block_for_reg (basic_block bb, int regno, int end_regno)
5371 edge e, live_edge;
5372 edge_iterator ei;
5373 bitmap live;
5374 int i;
5376 live_edge = NULL;
5377 FOR_EACH_EDGE (e, ei, bb->succs)
5379 live = df_get_live_in (e->dest);
5380 for (i = regno; i < end_regno; i++)
5381 if (REGNO_REG_SET_P (live, i))
5383 if (live_edge && live_edge != e)
5384 return NULL;
5385 live_edge = e;
5389 /* We can sometimes encounter dead code. Don't try to move it
5390 into the exit block. */
5391 if (!live_edge || live_edge->dest == EXIT_BLOCK_PTR)
5392 return NULL;
5394 /* Reject targets of abnormal edges. This is needed for correctness
5395 on ports like Alpha and MIPS, whose pic_offset_table_rtx can die on
5396 exception edges even though it is generally treated as call-saved
5397 for the majority of the compilation. Moving across abnormal edges
5398 isn't going to be interesting for shrink-wrap usage anyway. */
5399 if (live_edge->flags & EDGE_ABNORMAL)
5400 return NULL;
5402 if (EDGE_COUNT (live_edge->dest->preds) > 1)
5403 return NULL;
5405 return live_edge->dest;
5408 /* Try to move INSN from BB to a successor. Return true on success.
5409 USES and DEFS are the set of registers that are used and defined
5410 after INSN in BB. */
5412 static bool
5413 move_insn_for_shrink_wrap (basic_block bb, rtx insn,
5414 const HARD_REG_SET uses,
5415 const HARD_REG_SET defs)
5417 rtx set, src, dest;
5418 bitmap live_out, live_in, bb_uses, bb_defs;
5419 unsigned int i, dregno, end_dregno, sregno, end_sregno;
5420 basic_block next_block;
5422 /* Look for a simple register copy. */
5423 set = single_set (insn);
5424 if (!set)
5425 return false;
5426 src = SET_SRC (set);
5427 dest = SET_DEST (set);
5428 if (!REG_P (dest) || !REG_P (src))
5429 return false;
5431 /* Make sure that the source register isn't defined later in BB. */
5432 sregno = REGNO (src);
5433 end_sregno = END_REGNO (src);
5434 if (overlaps_hard_reg_set_p (defs, GET_MODE (src), sregno))
5435 return false;
5437 /* Make sure that the destination register isn't referenced later in BB. */
5438 dregno = REGNO (dest);
5439 end_dregno = END_REGNO (dest);
5440 if (overlaps_hard_reg_set_p (uses, GET_MODE (dest), dregno)
5441 || overlaps_hard_reg_set_p (defs, GET_MODE (dest), dregno))
5442 return false;
5444 /* See whether there is a successor block to which we could move INSN. */
5445 next_block = next_block_for_reg (bb, dregno, end_dregno);
5446 if (!next_block)
5447 return false;
5449 /* At this point we are committed to moving INSN, but let's try to
5450 move it as far as we can. */
5453 live_out = df_get_live_out (bb);
5454 live_in = df_get_live_in (next_block);
5455 bb = next_block;
5457 /* Check whether BB uses DEST or clobbers DEST. We need to add
5458 INSN to BB if so. Either way, DEST is no longer live on entry,
5459 except for any part that overlaps SRC (next loop). */
5460 bb_uses = &DF_LR_BB_INFO (bb)->use;
5461 bb_defs = &DF_LR_BB_INFO (bb)->def;
5462 for (i = dregno; i < end_dregno; i++)
5464 if (REGNO_REG_SET_P (bb_uses, i) || REGNO_REG_SET_P (bb_defs, i))
5465 next_block = NULL;
5466 CLEAR_REGNO_REG_SET (live_out, i);
5467 CLEAR_REGNO_REG_SET (live_in, i);
5470 /* Check whether BB clobbers SRC. We need to add INSN to BB if so.
5471 Either way, SRC is now live on entry. */
5472 for (i = sregno; i < end_sregno; i++)
5474 if (REGNO_REG_SET_P (bb_defs, i))
5475 next_block = NULL;
5476 SET_REGNO_REG_SET (live_out, i);
5477 SET_REGNO_REG_SET (live_in, i);
5480 /* If we don't need to add the move to BB, look for a single
5481 successor block. */
5482 if (next_block)
5483 next_block = next_block_for_reg (next_block, dregno, end_dregno);
5485 while (next_block);
5487 /* BB now defines DEST. It only uses the parts of DEST that overlap SRC
5488 (next loop). */
5489 for (i = dregno; i < end_dregno; i++)
5491 CLEAR_REGNO_REG_SET (bb_uses, i);
5492 SET_REGNO_REG_SET (bb_defs, i);
5495 /* BB now uses SRC. */
5496 for (i = sregno; i < end_sregno; i++)
5497 SET_REGNO_REG_SET (bb_uses, i);
5499 emit_insn_after (PATTERN (insn), bb_note (bb));
5500 delete_insn (insn);
5501 return true;
5504 /* Look for register copies in the first block of the function, and move
5505 them down into successor blocks if the register is used only on one
5506 path. This exposes more opportunities for shrink-wrapping. These
5507 kinds of sets often occur when incoming argument registers are moved
5508 to call-saved registers because their values are live across one or
5509 more calls during the function. */
5511 static void
5512 prepare_shrink_wrap (basic_block entry_block)
5514 rtx insn, curr, x;
5515 HARD_REG_SET uses, defs;
5516 df_ref *ref;
5518 CLEAR_HARD_REG_SET (uses);
5519 CLEAR_HARD_REG_SET (defs);
5520 FOR_BB_INSNS_REVERSE_SAFE (entry_block, insn, curr)
5521 if (NONDEBUG_INSN_P (insn)
5522 && !move_insn_for_shrink_wrap (entry_block, insn, uses, defs))
5524 /* Add all defined registers to DEFs. */
5525 for (ref = DF_INSN_DEFS (insn); *ref; ref++)
5527 x = DF_REF_REG (*ref);
5528 if (REG_P (x) && HARD_REGISTER_P (x))
5529 SET_HARD_REG_BIT (defs, REGNO (x));
5532 /* Add all used registers to USESs. */
5533 for (ref = DF_INSN_USES (insn); *ref; ref++)
5535 x = DF_REF_REG (*ref);
5536 if (REG_P (x) && HARD_REGISTER_P (x))
5537 SET_HARD_REG_BIT (uses, REGNO (x));
5542 #endif
5544 #ifdef HAVE_return
5545 /* Insert use of return register before the end of BB. */
5547 static void
5548 emit_use_return_register_into_block (basic_block bb)
5550 rtx seq;
5551 start_sequence ();
5552 use_return_register ();
5553 seq = get_insns ();
5554 end_sequence ();
5555 emit_insn_before (seq, BB_END (bb));
5559 /* Create a return pattern, either simple_return or return, depending on
5560 simple_p. */
5562 static rtx
5563 gen_return_pattern (bool simple_p)
5565 #ifdef HAVE_simple_return
5566 return simple_p ? gen_simple_return () : gen_return ();
5567 #else
5568 gcc_assert (!simple_p);
5569 return gen_return ();
5570 #endif
5573 /* Insert an appropriate return pattern at the end of block BB. This
5574 also means updating block_for_insn appropriately. SIMPLE_P is
5575 the same as in gen_return_pattern and passed to it. */
5577 static void
5578 emit_return_into_block (bool simple_p, basic_block bb)
5580 rtx jump, pat;
5581 jump = emit_jump_insn_after (gen_return_pattern (simple_p), BB_END (bb));
5582 pat = PATTERN (jump);
5583 if (GET_CODE (pat) == PARALLEL)
5584 pat = XVECEXP (pat, 0, 0);
5585 gcc_assert (ANY_RETURN_P (pat));
5586 JUMP_LABEL (jump) = pat;
5588 #endif
5590 /* Set JUMP_LABEL for a return insn. */
5592 void
5593 set_return_jump_label (rtx returnjump)
5595 rtx pat = PATTERN (returnjump);
5596 if (GET_CODE (pat) == PARALLEL)
5597 pat = XVECEXP (pat, 0, 0);
5598 if (ANY_RETURN_P (pat))
5599 JUMP_LABEL (returnjump) = pat;
5600 else
5601 JUMP_LABEL (returnjump) = ret_rtx;
5604 #ifdef HAVE_simple_return
5605 /* Create a copy of BB instructions and insert at BEFORE. Redirect
5606 preds of BB to COPY_BB if they don't appear in NEED_PROLOGUE. */
5607 static void
5608 dup_block_and_redirect (basic_block bb, basic_block copy_bb, rtx before,
5609 bitmap_head *need_prologue)
5611 edge_iterator ei;
5612 edge e;
5613 rtx insn = BB_END (bb);
5615 /* We know BB has a single successor, so there is no need to copy a
5616 simple jump at the end of BB. */
5617 if (simplejump_p (insn))
5618 insn = PREV_INSN (insn);
5620 start_sequence ();
5621 duplicate_insn_chain (BB_HEAD (bb), insn);
5622 if (dump_file)
5624 unsigned count = 0;
5625 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5626 if (active_insn_p (insn))
5627 ++count;
5628 fprintf (dump_file, "Duplicating bb %d to bb %d, %u active insns.\n",
5629 bb->index, copy_bb->index, count);
5631 insn = get_insns ();
5632 end_sequence ();
5633 emit_insn_before (insn, before);
5635 /* Redirect all the paths that need no prologue into copy_bb. */
5636 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
5637 if (!bitmap_bit_p (need_prologue, e->src->index))
5639 redirect_edge_and_branch_force (e, copy_bb);
5640 continue;
5642 else
5643 ei_next (&ei);
5645 #endif
5647 #if defined (HAVE_return) || defined (HAVE_simple_return)
5648 /* Return true if there are any active insns between HEAD and TAIL. */
5649 static bool
5650 active_insn_between (rtx head, rtx tail)
5652 while (tail)
5654 if (active_insn_p (tail))
5655 return true;
5656 if (tail == head)
5657 return false;
5658 tail = PREV_INSN (tail);
5660 return false;
5663 /* LAST_BB is a block that exits, and empty of active instructions.
5664 Examine its predecessors for jumps that can be converted to
5665 (conditional) returns. */
5666 static VEC (edge, heap) *
5667 convert_jumps_to_returns (basic_block last_bb, bool simple_p,
5668 VEC (edge, heap) *unconverted ATTRIBUTE_UNUSED)
5670 int i;
5671 basic_block bb;
5672 rtx label;
5673 edge_iterator ei;
5674 edge e;
5675 VEC(basic_block,heap) *src_bbs;
5677 src_bbs = VEC_alloc (basic_block, heap, EDGE_COUNT (last_bb->preds));
5678 FOR_EACH_EDGE (e, ei, last_bb->preds)
5679 if (e->src != ENTRY_BLOCK_PTR)
5680 VEC_quick_push (basic_block, src_bbs, e->src);
5682 label = BB_HEAD (last_bb);
5684 FOR_EACH_VEC_ELT (basic_block, src_bbs, i, bb)
5686 rtx jump = BB_END (bb);
5688 if (!JUMP_P (jump) || JUMP_LABEL (jump) != label)
5689 continue;
5691 e = find_edge (bb, last_bb);
5693 /* If we have an unconditional jump, we can replace that
5694 with a simple return instruction. */
5695 if (simplejump_p (jump))
5697 /* The use of the return register might be present in the exit
5698 fallthru block. Either:
5699 - removing the use is safe, and we should remove the use in
5700 the exit fallthru block, or
5701 - removing the use is not safe, and we should add it here.
5702 For now, we conservatively choose the latter. Either of the
5703 2 helps in crossjumping. */
5704 emit_use_return_register_into_block (bb);
5706 emit_return_into_block (simple_p, bb);
5707 delete_insn (jump);
5710 /* If we have a conditional jump branching to the last
5711 block, we can try to replace that with a conditional
5712 return instruction. */
5713 else if (condjump_p (jump))
5715 rtx dest;
5717 if (simple_p)
5718 dest = simple_return_rtx;
5719 else
5720 dest = ret_rtx;
5721 if (!redirect_jump (jump, dest, 0))
5723 #ifdef HAVE_simple_return
5724 if (simple_p)
5726 if (dump_file)
5727 fprintf (dump_file,
5728 "Failed to redirect bb %d branch.\n", bb->index);
5729 VEC_safe_push (edge, heap, unconverted, e);
5731 #endif
5732 continue;
5735 /* See comment in simplejump_p case above. */
5736 emit_use_return_register_into_block (bb);
5738 /* If this block has only one successor, it both jumps
5739 and falls through to the fallthru block, so we can't
5740 delete the edge. */
5741 if (single_succ_p (bb))
5742 continue;
5744 else
5746 #ifdef HAVE_simple_return
5747 if (simple_p)
5749 if (dump_file)
5750 fprintf (dump_file,
5751 "Failed to redirect bb %d branch.\n", bb->index);
5752 VEC_safe_push (edge, heap, unconverted, e);
5754 #endif
5755 continue;
5758 /* Fix up the CFG for the successful change we just made. */
5759 redirect_edge_succ (e, EXIT_BLOCK_PTR);
5760 e->flags &= ~EDGE_CROSSING;
5762 VEC_free (basic_block, heap, src_bbs);
5763 return unconverted;
5766 /* Emit a return insn for the exit fallthru block. */
5767 static basic_block
5768 emit_return_for_exit (edge exit_fallthru_edge, bool simple_p)
5770 basic_block last_bb = exit_fallthru_edge->src;
5772 if (JUMP_P (BB_END (last_bb)))
5774 last_bb = split_edge (exit_fallthru_edge);
5775 exit_fallthru_edge = single_succ_edge (last_bb);
5777 emit_barrier_after (BB_END (last_bb));
5778 emit_return_into_block (simple_p, last_bb);
5779 exit_fallthru_edge->flags &= ~EDGE_FALLTHRU;
5780 return last_bb;
5782 #endif
5785 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
5786 this into place with notes indicating where the prologue ends and where
5787 the epilogue begins. Update the basic block information when possible.
5789 Notes on epilogue placement:
5790 There are several kinds of edges to the exit block:
5791 * a single fallthru edge from LAST_BB
5792 * possibly, edges from blocks containing sibcalls
5793 * possibly, fake edges from infinite loops
5795 The epilogue is always emitted on the fallthru edge from the last basic
5796 block in the function, LAST_BB, into the exit block.
5798 If LAST_BB is empty except for a label, it is the target of every
5799 other basic block in the function that ends in a return. If a
5800 target has a return or simple_return pattern (possibly with
5801 conditional variants), these basic blocks can be changed so that a
5802 return insn is emitted into them, and their target is adjusted to
5803 the real exit block.
5805 Notes on shrink wrapping: We implement a fairly conservative
5806 version of shrink-wrapping rather than the textbook one. We only
5807 generate a single prologue and a single epilogue. This is
5808 sufficient to catch a number of interesting cases involving early
5809 exits.
5811 First, we identify the blocks that require the prologue to occur before
5812 them. These are the ones that modify a call-saved register, or reference
5813 any of the stack or frame pointer registers. To simplify things, we then
5814 mark everything reachable from these blocks as also requiring a prologue.
5815 This takes care of loops automatically, and avoids the need to examine
5816 whether MEMs reference the frame, since it is sufficient to check for
5817 occurrences of the stack or frame pointer.
5819 We then compute the set of blocks for which the need for a prologue
5820 is anticipatable (borrowing terminology from the shrink-wrapping
5821 description in Muchnick's book). These are the blocks which either
5822 require a prologue themselves, or those that have only successors
5823 where the prologue is anticipatable. The prologue needs to be
5824 inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
5825 is not. For the moment, we ensure that only one such edge exists.
5827 The epilogue is placed as described above, but we make a
5828 distinction between inserting return and simple_return patterns
5829 when modifying other blocks that end in a return. Blocks that end
5830 in a sibcall omit the sibcall_epilogue if the block is not in
5831 ANTIC. */
5833 static void
5834 thread_prologue_and_epilogue_insns (void)
5836 bool inserted;
5837 #ifdef HAVE_simple_return
5838 VEC (edge, heap) *unconverted_simple_returns = NULL;
5839 bool nonempty_prologue;
5840 bitmap_head bb_flags;
5841 unsigned max_grow_size;
5842 #endif
5843 rtx returnjump;
5844 rtx seq ATTRIBUTE_UNUSED, epilogue_end ATTRIBUTE_UNUSED;
5845 rtx prologue_seq ATTRIBUTE_UNUSED, split_prologue_seq ATTRIBUTE_UNUSED;
5846 edge e, entry_edge, orig_entry_edge, exit_fallthru_edge;
5847 edge_iterator ei;
5849 df_analyze ();
5851 rtl_profile_for_bb (ENTRY_BLOCK_PTR);
5853 inserted = false;
5854 seq = NULL_RTX;
5855 epilogue_end = NULL_RTX;
5856 returnjump = NULL_RTX;
5858 /* Can't deal with multiple successors of the entry block at the
5859 moment. Function should always have at least one entry
5860 point. */
5861 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR));
5862 entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
5863 orig_entry_edge = entry_edge;
5865 split_prologue_seq = NULL_RTX;
5866 if (flag_split_stack
5867 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
5868 == NULL))
5870 #ifndef HAVE_split_stack_prologue
5871 gcc_unreachable ();
5872 #else
5873 gcc_assert (HAVE_split_stack_prologue);
5875 start_sequence ();
5876 emit_insn (gen_split_stack_prologue ());
5877 split_prologue_seq = get_insns ();
5878 end_sequence ();
5880 record_insns (split_prologue_seq, NULL, &prologue_insn_hash);
5881 set_insn_locators (split_prologue_seq, prologue_locator);
5882 #endif
5885 prologue_seq = NULL_RTX;
5886 #ifdef HAVE_prologue
5887 if (HAVE_prologue)
5889 start_sequence ();
5890 seq = gen_prologue ();
5891 emit_insn (seq);
5893 /* Insert an explicit USE for the frame pointer
5894 if the profiling is on and the frame pointer is required. */
5895 if (crtl->profile && frame_pointer_needed)
5896 emit_use (hard_frame_pointer_rtx);
5898 /* Retain a map of the prologue insns. */
5899 record_insns (seq, NULL, &prologue_insn_hash);
5900 emit_note (NOTE_INSN_PROLOGUE_END);
5902 /* Ensure that instructions are not moved into the prologue when
5903 profiling is on. The call to the profiling routine can be
5904 emitted within the live range of a call-clobbered register. */
5905 if (!targetm.profile_before_prologue () && crtl->profile)
5906 emit_insn (gen_blockage ());
5908 prologue_seq = get_insns ();
5909 end_sequence ();
5910 set_insn_locators (prologue_seq, prologue_locator);
5912 #endif
5914 #ifdef HAVE_simple_return
5915 bitmap_initialize (&bb_flags, &bitmap_default_obstack);
5917 /* Try to perform a kind of shrink-wrapping, making sure the
5918 prologue/epilogue is emitted only around those parts of the
5919 function that require it. */
5921 nonempty_prologue = false;
5922 for (seq = prologue_seq; seq; seq = NEXT_INSN (seq))
5923 if (!NOTE_P (seq) || NOTE_KIND (seq) != NOTE_INSN_PROLOGUE_END)
5925 nonempty_prologue = true;
5926 break;
5929 if (flag_shrink_wrap && HAVE_simple_return
5930 && (targetm.profile_before_prologue () || !crtl->profile)
5931 && nonempty_prologue && !crtl->calls_eh_return)
5933 HARD_REG_SET prologue_clobbered, prologue_used, live_on_edge;
5934 struct hard_reg_set_container set_up_by_prologue;
5935 rtx p_insn;
5936 VEC(basic_block, heap) *vec;
5937 basic_block bb;
5938 bitmap_head bb_antic_flags;
5939 bitmap_head bb_on_list;
5940 bitmap_head bb_tail;
5942 if (dump_file)
5943 fprintf (dump_file, "Attempting shrink-wrapping optimization.\n");
5945 /* Compute the registers set and used in the prologue. */
5946 CLEAR_HARD_REG_SET (prologue_clobbered);
5947 CLEAR_HARD_REG_SET (prologue_used);
5948 for (p_insn = prologue_seq; p_insn; p_insn = NEXT_INSN (p_insn))
5950 HARD_REG_SET this_used;
5951 if (!NONDEBUG_INSN_P (p_insn))
5952 continue;
5954 CLEAR_HARD_REG_SET (this_used);
5955 note_uses (&PATTERN (p_insn), record_hard_reg_uses,
5956 &this_used);
5957 AND_COMPL_HARD_REG_SET (this_used, prologue_clobbered);
5958 IOR_HARD_REG_SET (prologue_used, this_used);
5959 note_stores (PATTERN (p_insn), record_hard_reg_sets,
5960 &prologue_clobbered);
5963 prepare_shrink_wrap (entry_edge->dest);
5965 bitmap_initialize (&bb_antic_flags, &bitmap_default_obstack);
5966 bitmap_initialize (&bb_on_list, &bitmap_default_obstack);
5967 bitmap_initialize (&bb_tail, &bitmap_default_obstack);
5969 /* Find the set of basic blocks that require a stack frame,
5970 and blocks that are too big to be duplicated. */
5972 vec = VEC_alloc (basic_block, heap, n_basic_blocks);
5974 CLEAR_HARD_REG_SET (set_up_by_prologue.set);
5975 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
5976 STACK_POINTER_REGNUM);
5977 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode, ARG_POINTER_REGNUM);
5978 if (frame_pointer_needed)
5979 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
5980 HARD_FRAME_POINTER_REGNUM);
5981 if (pic_offset_table_rtx)
5982 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
5983 PIC_OFFSET_TABLE_REGNUM);
5984 if (stack_realign_drap && crtl->drap_reg)
5985 add_to_hard_reg_set (&set_up_by_prologue.set,
5986 GET_MODE (crtl->drap_reg),
5987 REGNO (crtl->drap_reg));
5988 if (targetm.set_up_by_prologue)
5989 targetm.set_up_by_prologue (&set_up_by_prologue);
5991 /* We don't use a different max size depending on
5992 optimize_bb_for_speed_p because increasing shrink-wrapping
5993 opportunities by duplicating tail blocks can actually result
5994 in an overall decrease in code size. */
5995 max_grow_size = get_uncond_jump_length ();
5996 max_grow_size *= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS);
5998 FOR_EACH_BB (bb)
6000 rtx insn;
6001 unsigned size = 0;
6003 FOR_BB_INSNS (bb, insn)
6004 if (NONDEBUG_INSN_P (insn))
6006 if (requires_stack_frame_p (insn, prologue_used,
6007 set_up_by_prologue.set))
6009 if (bb == entry_edge->dest)
6010 goto fail_shrinkwrap;
6011 bitmap_set_bit (&bb_flags, bb->index);
6012 VEC_quick_push (basic_block, vec, bb);
6013 break;
6015 else if (size <= max_grow_size)
6017 size += get_attr_min_length (insn);
6018 if (size > max_grow_size)
6019 bitmap_set_bit (&bb_on_list, bb->index);
6024 /* Blocks that really need a prologue, or are too big for tails. */
6025 bitmap_ior_into (&bb_on_list, &bb_flags);
6027 /* For every basic block that needs a prologue, mark all blocks
6028 reachable from it, so as to ensure they are also seen as
6029 requiring a prologue. */
6030 while (!VEC_empty (basic_block, vec))
6032 basic_block tmp_bb = VEC_pop (basic_block, vec);
6034 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
6035 if (e->dest != EXIT_BLOCK_PTR
6036 && bitmap_set_bit (&bb_flags, e->dest->index))
6037 VEC_quick_push (basic_block, vec, e->dest);
6040 /* Find the set of basic blocks that need no prologue, have a
6041 single successor, can be duplicated, meet a max size
6042 requirement, and go to the exit via like blocks. */
6043 VEC_quick_push (basic_block, vec, EXIT_BLOCK_PTR);
6044 while (!VEC_empty (basic_block, vec))
6046 basic_block tmp_bb = VEC_pop (basic_block, vec);
6048 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
6049 if (single_succ_p (e->src)
6050 && !bitmap_bit_p (&bb_on_list, e->src->index)
6051 && can_duplicate_block_p (e->src))
6053 edge pe;
6054 edge_iterator pei;
6056 /* If there is predecessor of e->src which doesn't
6057 need prologue and the edge is complex,
6058 we might not be able to redirect the branch
6059 to a copy of e->src. */
6060 FOR_EACH_EDGE (pe, pei, e->src->preds)
6061 if ((pe->flags & EDGE_COMPLEX) != 0
6062 && !bitmap_bit_p (&bb_flags, pe->src->index))
6063 break;
6064 if (pe == NULL && bitmap_set_bit (&bb_tail, e->src->index))
6065 VEC_quick_push (basic_block, vec, e->src);
6069 /* Now walk backwards from every block that is marked as needing
6070 a prologue to compute the bb_antic_flags bitmap. Exclude
6071 tail blocks; They can be duplicated to be used on paths not
6072 needing a prologue. */
6073 bitmap_clear (&bb_on_list);
6074 bitmap_and_compl (&bb_antic_flags, &bb_flags, &bb_tail);
6075 FOR_EACH_BB (bb)
6077 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
6078 continue;
6079 FOR_EACH_EDGE (e, ei, bb->preds)
6080 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
6081 && bitmap_set_bit (&bb_on_list, e->src->index))
6082 VEC_quick_push (basic_block, vec, e->src);
6084 while (!VEC_empty (basic_block, vec))
6086 basic_block tmp_bb = VEC_pop (basic_block, vec);
6087 bool all_set = true;
6089 bitmap_clear_bit (&bb_on_list, tmp_bb->index);
6090 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
6091 if (!bitmap_bit_p (&bb_antic_flags, e->dest->index))
6093 all_set = false;
6094 break;
6097 if (all_set)
6099 bitmap_set_bit (&bb_antic_flags, tmp_bb->index);
6100 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
6101 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
6102 && bitmap_set_bit (&bb_on_list, e->src->index))
6103 VEC_quick_push (basic_block, vec, e->src);
6106 /* Find exactly one edge that leads to a block in ANTIC from
6107 a block that isn't. */
6108 if (!bitmap_bit_p (&bb_antic_flags, entry_edge->dest->index))
6109 FOR_EACH_BB (bb)
6111 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
6112 continue;
6113 FOR_EACH_EDGE (e, ei, bb->preds)
6114 if (!bitmap_bit_p (&bb_antic_flags, e->src->index))
6116 if (entry_edge != orig_entry_edge)
6118 entry_edge = orig_entry_edge;
6119 if (dump_file)
6120 fprintf (dump_file, "More than one candidate edge.\n");
6121 goto fail_shrinkwrap;
6123 if (dump_file)
6124 fprintf (dump_file, "Found candidate edge for "
6125 "shrink-wrapping, %d->%d.\n", e->src->index,
6126 e->dest->index);
6127 entry_edge = e;
6131 if (entry_edge != orig_entry_edge)
6133 /* Test whether the prologue is known to clobber any register
6134 (other than FP or SP) which are live on the edge. */
6135 CLEAR_HARD_REG_BIT (prologue_clobbered, STACK_POINTER_REGNUM);
6136 if (frame_pointer_needed)
6137 CLEAR_HARD_REG_BIT (prologue_clobbered, HARD_FRAME_POINTER_REGNUM);
6138 CLEAR_HARD_REG_SET (live_on_edge);
6139 reg_set_to_hard_reg_set (&live_on_edge,
6140 df_get_live_in (entry_edge->dest));
6141 if (hard_reg_set_intersect_p (live_on_edge, prologue_clobbered))
6143 entry_edge = orig_entry_edge;
6144 if (dump_file)
6145 fprintf (dump_file,
6146 "Shrink-wrapping aborted due to clobber.\n");
6149 if (entry_edge != orig_entry_edge)
6151 crtl->shrink_wrapped = true;
6152 if (dump_file)
6153 fprintf (dump_file, "Performing shrink-wrapping.\n");
6155 /* Find tail blocks reachable from both blocks needing a
6156 prologue and blocks not needing a prologue. */
6157 if (!bitmap_empty_p (&bb_tail))
6158 FOR_EACH_BB (bb)
6160 bool some_pro, some_no_pro;
6161 if (!bitmap_bit_p (&bb_tail, bb->index))
6162 continue;
6163 some_pro = some_no_pro = false;
6164 FOR_EACH_EDGE (e, ei, bb->preds)
6166 if (bitmap_bit_p (&bb_flags, e->src->index))
6167 some_pro = true;
6168 else
6169 some_no_pro = true;
6171 if (some_pro && some_no_pro)
6172 VEC_quick_push (basic_block, vec, bb);
6173 else
6174 bitmap_clear_bit (&bb_tail, bb->index);
6176 /* Find the head of each tail. */
6177 while (!VEC_empty (basic_block, vec))
6179 basic_block tbb = VEC_pop (basic_block, vec);
6181 if (!bitmap_bit_p (&bb_tail, tbb->index))
6182 continue;
6184 while (single_succ_p (tbb))
6186 tbb = single_succ (tbb);
6187 bitmap_clear_bit (&bb_tail, tbb->index);
6190 /* Now duplicate the tails. */
6191 if (!bitmap_empty_p (&bb_tail))
6192 FOR_EACH_BB_REVERSE (bb)
6194 basic_block copy_bb, tbb;
6195 rtx insert_point;
6196 int eflags;
6198 if (!bitmap_clear_bit (&bb_tail, bb->index))
6199 continue;
6201 /* Create a copy of BB, instructions and all, for
6202 use on paths that don't need a prologue.
6203 Ideal placement of the copy is on a fall-thru edge
6204 or after a block that would jump to the copy. */
6205 FOR_EACH_EDGE (e, ei, bb->preds)
6206 if (!bitmap_bit_p (&bb_flags, e->src->index)
6207 && single_succ_p (e->src))
6208 break;
6209 if (e)
6211 copy_bb = create_basic_block (NEXT_INSN (BB_END (e->src)),
6212 NULL_RTX, e->src);
6213 BB_COPY_PARTITION (copy_bb, e->src);
6215 else
6217 /* Otherwise put the copy at the end of the function. */
6218 copy_bb = create_basic_block (NULL_RTX, NULL_RTX,
6219 EXIT_BLOCK_PTR->prev_bb);
6220 BB_COPY_PARTITION (copy_bb, bb);
6223 insert_point = emit_note_after (NOTE_INSN_DELETED,
6224 BB_END (copy_bb));
6225 emit_barrier_after (BB_END (copy_bb));
6227 tbb = bb;
6228 while (1)
6230 dup_block_and_redirect (tbb, copy_bb, insert_point,
6231 &bb_flags);
6232 tbb = single_succ (tbb);
6233 if (tbb == EXIT_BLOCK_PTR)
6234 break;
6235 e = split_block (copy_bb, PREV_INSN (insert_point));
6236 copy_bb = e->dest;
6239 /* Quiet verify_flow_info by (ab)using EDGE_FAKE.
6240 We have yet to add a simple_return to the tails,
6241 as we'd like to first convert_jumps_to_returns in
6242 case the block is no longer used after that. */
6243 eflags = EDGE_FAKE;
6244 if (CALL_P (PREV_INSN (insert_point))
6245 && SIBLING_CALL_P (PREV_INSN (insert_point)))
6246 eflags = EDGE_SIBCALL | EDGE_ABNORMAL;
6247 make_single_succ_edge (copy_bb, EXIT_BLOCK_PTR, eflags);
6249 /* verify_flow_info doesn't like a note after a
6250 sibling call. */
6251 delete_insn (insert_point);
6252 if (bitmap_empty_p (&bb_tail))
6253 break;
6257 fail_shrinkwrap:
6258 bitmap_clear (&bb_tail);
6259 bitmap_clear (&bb_antic_flags);
6260 bitmap_clear (&bb_on_list);
6261 VEC_free (basic_block, heap, vec);
6263 #endif
6265 if (split_prologue_seq != NULL_RTX)
6267 insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
6268 inserted = true;
6270 if (prologue_seq != NULL_RTX)
6272 insert_insn_on_edge (prologue_seq, entry_edge);
6273 inserted = true;
6276 /* If the exit block has no non-fake predecessors, we don't need
6277 an epilogue. */
6278 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6279 if ((e->flags & EDGE_FAKE) == 0)
6280 break;
6281 if (e == NULL)
6282 goto epilogue_done;
6284 rtl_profile_for_bb (EXIT_BLOCK_PTR);
6286 exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR->preds);
6288 /* If we're allowed to generate a simple return instruction, then by
6289 definition we don't need a full epilogue. If the last basic
6290 block before the exit block does not contain active instructions,
6291 examine its predecessors and try to emit (conditional) return
6292 instructions. */
6293 #ifdef HAVE_simple_return
6294 if (entry_edge != orig_entry_edge)
6296 if (optimize)
6298 unsigned i, last;
6300 /* convert_jumps_to_returns may add to EXIT_BLOCK_PTR->preds
6301 (but won't remove). Stop at end of current preds. */
6302 last = EDGE_COUNT (EXIT_BLOCK_PTR->preds);
6303 for (i = 0; i < last; i++)
6305 e = EDGE_I (EXIT_BLOCK_PTR->preds, i);
6306 if (LABEL_P (BB_HEAD (e->src))
6307 && !bitmap_bit_p (&bb_flags, e->src->index)
6308 && !active_insn_between (BB_HEAD (e->src), BB_END (e->src)))
6309 unconverted_simple_returns
6310 = convert_jumps_to_returns (e->src, true,
6311 unconverted_simple_returns);
6315 if (exit_fallthru_edge != NULL
6316 && EDGE_COUNT (exit_fallthru_edge->src->preds) != 0
6317 && !bitmap_bit_p (&bb_flags, exit_fallthru_edge->src->index))
6319 basic_block last_bb;
6321 last_bb = emit_return_for_exit (exit_fallthru_edge, true);
6322 returnjump = BB_END (last_bb);
6323 exit_fallthru_edge = NULL;
6326 #endif
6327 #ifdef HAVE_return
6328 if (HAVE_return)
6330 if (exit_fallthru_edge == NULL)
6331 goto epilogue_done;
6333 if (optimize)
6335 basic_block last_bb = exit_fallthru_edge->src;
6337 if (LABEL_P (BB_HEAD (last_bb))
6338 && !active_insn_between (BB_HEAD (last_bb), BB_END (last_bb)))
6339 convert_jumps_to_returns (last_bb, false, NULL);
6341 if (EDGE_COUNT (last_bb->preds) != 0
6342 && single_succ_p (last_bb))
6344 last_bb = emit_return_for_exit (exit_fallthru_edge, false);
6345 epilogue_end = returnjump = BB_END (last_bb);
6346 #ifdef HAVE_simple_return
6347 /* Emitting the return may add a basic block.
6348 Fix bb_flags for the added block. */
6349 if (last_bb != exit_fallthru_edge->src)
6350 bitmap_set_bit (&bb_flags, last_bb->index);
6351 #endif
6352 goto epilogue_done;
6356 #endif
6358 /* A small fib -- epilogue is not yet completed, but we wish to re-use
6359 this marker for the splits of EH_RETURN patterns, and nothing else
6360 uses the flag in the meantime. */
6361 epilogue_completed = 1;
6363 #ifdef HAVE_eh_return
6364 /* Find non-fallthru edges that end with EH_RETURN instructions. On
6365 some targets, these get split to a special version of the epilogue
6366 code. In order to be able to properly annotate these with unwind
6367 info, try to split them now. If we get a valid split, drop an
6368 EPILOGUE_BEG note and mark the insns as epilogue insns. */
6369 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6371 rtx prev, last, trial;
6373 if (e->flags & EDGE_FALLTHRU)
6374 continue;
6375 last = BB_END (e->src);
6376 if (!eh_returnjump_p (last))
6377 continue;
6379 prev = PREV_INSN (last);
6380 trial = try_split (PATTERN (last), last, 1);
6381 if (trial == last)
6382 continue;
6384 record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
6385 emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
6387 #endif
6389 /* If nothing falls through into the exit block, we don't need an
6390 epilogue. */
6392 if (exit_fallthru_edge == NULL)
6393 goto epilogue_done;
6395 #ifdef HAVE_epilogue
6396 if (HAVE_epilogue)
6398 start_sequence ();
6399 epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
6400 seq = gen_epilogue ();
6401 if (seq)
6402 emit_jump_insn (seq);
6404 /* Retain a map of the epilogue insns. */
6405 record_insns (seq, NULL, &epilogue_insn_hash);
6406 set_insn_locators (seq, epilogue_locator);
6408 seq = get_insns ();
6409 returnjump = get_last_insn ();
6410 end_sequence ();
6412 insert_insn_on_edge (seq, exit_fallthru_edge);
6413 inserted = true;
6415 if (JUMP_P (returnjump))
6416 set_return_jump_label (returnjump);
6418 else
6419 #endif
6421 basic_block cur_bb;
6423 if (! next_active_insn (BB_END (exit_fallthru_edge->src)))
6424 goto epilogue_done;
6425 /* We have a fall-through edge to the exit block, the source is not
6426 at the end of the function, and there will be an assembler epilogue
6427 at the end of the function.
6428 We can't use force_nonfallthru here, because that would try to
6429 use return. Inserting a jump 'by hand' is extremely messy, so
6430 we take advantage of cfg_layout_finalize using
6431 fixup_fallthru_exit_predecessor. */
6432 cfg_layout_initialize (0);
6433 FOR_EACH_BB (cur_bb)
6434 if (cur_bb->index >= NUM_FIXED_BLOCKS
6435 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
6436 cur_bb->aux = cur_bb->next_bb;
6437 cfg_layout_finalize ();
6440 epilogue_done:
6442 default_rtl_profile ();
6444 if (inserted)
6446 sbitmap blocks;
6448 commit_edge_insertions ();
6450 /* Look for basic blocks within the prologue insns. */
6451 blocks = sbitmap_alloc (last_basic_block);
6452 sbitmap_zero (blocks);
6453 SET_BIT (blocks, entry_edge->dest->index);
6454 SET_BIT (blocks, orig_entry_edge->dest->index);
6455 find_many_sub_basic_blocks (blocks);
6456 sbitmap_free (blocks);
6458 /* The epilogue insns we inserted may cause the exit edge to no longer
6459 be fallthru. */
6460 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6462 if (((e->flags & EDGE_FALLTHRU) != 0)
6463 && returnjump_p (BB_END (e->src)))
6464 e->flags &= ~EDGE_FALLTHRU;
6468 #ifdef HAVE_simple_return
6469 /* If there were branches to an empty LAST_BB which we tried to
6470 convert to conditional simple_returns, but couldn't for some
6471 reason, create a block to hold a simple_return insn and redirect
6472 those remaining edges. */
6473 if (!VEC_empty (edge, unconverted_simple_returns))
6475 basic_block simple_return_block_hot = NULL;
6476 basic_block simple_return_block_cold = NULL;
6477 edge pending_edge_hot = NULL;
6478 edge pending_edge_cold = NULL;
6479 basic_block exit_pred = EXIT_BLOCK_PTR->prev_bb;
6480 int i;
6482 gcc_assert (entry_edge != orig_entry_edge);
6484 /* See if we can reuse the last insn that was emitted for the
6485 epilogue. */
6486 if (returnjump != NULL_RTX
6487 && JUMP_LABEL (returnjump) == simple_return_rtx)
6489 e = split_block (BLOCK_FOR_INSN (returnjump), PREV_INSN (returnjump));
6490 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
6491 simple_return_block_hot = e->dest;
6492 else
6493 simple_return_block_cold = e->dest;
6496 /* Also check returns we might need to add to tail blocks. */
6497 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6498 if (EDGE_COUNT (e->src->preds) != 0
6499 && (e->flags & EDGE_FAKE) != 0
6500 && !bitmap_bit_p (&bb_flags, e->src->index))
6502 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
6503 pending_edge_hot = e;
6504 else
6505 pending_edge_cold = e;
6508 FOR_EACH_VEC_ELT (edge, unconverted_simple_returns, i, e)
6510 basic_block *pdest_bb;
6511 edge pending;
6513 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
6515 pdest_bb = &simple_return_block_hot;
6516 pending = pending_edge_hot;
6518 else
6520 pdest_bb = &simple_return_block_cold;
6521 pending = pending_edge_cold;
6524 if (*pdest_bb == NULL && pending != NULL)
6526 emit_return_into_block (true, pending->src);
6527 pending->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
6528 *pdest_bb = pending->src;
6530 else if (*pdest_bb == NULL)
6532 basic_block bb;
6533 rtx start;
6535 bb = create_basic_block (NULL, NULL, exit_pred);
6536 BB_COPY_PARTITION (bb, e->src);
6537 start = emit_jump_insn_after (gen_simple_return (),
6538 BB_END (bb));
6539 JUMP_LABEL (start) = simple_return_rtx;
6540 emit_barrier_after (start);
6542 *pdest_bb = bb;
6543 make_edge (bb, EXIT_BLOCK_PTR, 0);
6545 redirect_edge_and_branch_force (e, *pdest_bb);
6547 VEC_free (edge, heap, unconverted_simple_returns);
6550 if (entry_edge != orig_entry_edge)
6552 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6553 if (EDGE_COUNT (e->src->preds) != 0
6554 && (e->flags & EDGE_FAKE) != 0
6555 && !bitmap_bit_p (&bb_flags, e->src->index))
6557 emit_return_into_block (true, e->src);
6558 e->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
6561 #endif
6563 #ifdef HAVE_sibcall_epilogue
6564 /* Emit sibling epilogues before any sibling call sites. */
6565 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
6567 basic_block bb = e->src;
6568 rtx insn = BB_END (bb);
6569 rtx ep_seq;
6571 if (!CALL_P (insn)
6572 || ! SIBLING_CALL_P (insn)
6573 #ifdef HAVE_simple_return
6574 || (entry_edge != orig_entry_edge
6575 && !bitmap_bit_p (&bb_flags, bb->index))
6576 #endif
6579 ei_next (&ei);
6580 continue;
6583 ep_seq = gen_sibcall_epilogue ();
6584 if (ep_seq)
6586 start_sequence ();
6587 emit_note (NOTE_INSN_EPILOGUE_BEG);
6588 emit_insn (ep_seq);
6589 seq = get_insns ();
6590 end_sequence ();
6592 /* Retain a map of the epilogue insns. Used in life analysis to
6593 avoid getting rid of sibcall epilogue insns. Do this before we
6594 actually emit the sequence. */
6595 record_insns (seq, NULL, &epilogue_insn_hash);
6596 set_insn_locators (seq, epilogue_locator);
6598 emit_insn_before (seq, insn);
6600 ei_next (&ei);
6602 #endif
6604 #ifdef HAVE_epilogue
6605 if (epilogue_end)
6607 rtx insn, next;
6609 /* Similarly, move any line notes that appear after the epilogue.
6610 There is no need, however, to be quite so anal about the existence
6611 of such a note. Also possibly move
6612 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
6613 info generation. */
6614 for (insn = epilogue_end; insn; insn = next)
6616 next = NEXT_INSN (insn);
6617 if (NOTE_P (insn)
6618 && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
6619 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
6622 #endif
6624 #ifdef HAVE_simple_return
6625 bitmap_clear (&bb_flags);
6626 #endif
6628 /* Threading the prologue and epilogue changes the artificial refs
6629 in the entry and exit blocks. */
6630 epilogue_completed = 1;
6631 df_update_entry_exit_and_calls ();
6634 /* Reposition the prologue-end and epilogue-begin notes after
6635 instruction scheduling. */
6637 void
6638 reposition_prologue_and_epilogue_notes (void)
6640 #if defined (HAVE_prologue) || defined (HAVE_epilogue) \
6641 || defined (HAVE_sibcall_epilogue)
6642 /* Since the hash table is created on demand, the fact that it is
6643 non-null is a signal that it is non-empty. */
6644 if (prologue_insn_hash != NULL)
6646 size_t len = htab_elements (prologue_insn_hash);
6647 rtx insn, last = NULL, note = NULL;
6649 /* Scan from the beginning until we reach the last prologue insn. */
6650 /* ??? While we do have the CFG intact, there are two problems:
6651 (1) The prologue can contain loops (typically probing the stack),
6652 which means that the end of the prologue isn't in the first bb.
6653 (2) Sometimes the PROLOGUE_END note gets pushed into the next bb. */
6654 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6656 if (NOTE_P (insn))
6658 if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
6659 note = insn;
6661 else if (contains (insn, prologue_insn_hash))
6663 last = insn;
6664 if (--len == 0)
6665 break;
6669 if (last)
6671 if (note == NULL)
6673 /* Scan forward looking for the PROLOGUE_END note. It should
6674 be right at the beginning of the block, possibly with other
6675 insn notes that got moved there. */
6676 for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
6678 if (NOTE_P (note)
6679 && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
6680 break;
6684 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
6685 if (LABEL_P (last))
6686 last = NEXT_INSN (last);
6687 reorder_insns (note, note, last);
6691 if (epilogue_insn_hash != NULL)
6693 edge_iterator ei;
6694 edge e;
6696 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6698 rtx insn, first = NULL, note = NULL;
6699 basic_block bb = e->src;
6701 /* Scan from the beginning until we reach the first epilogue insn. */
6702 FOR_BB_INSNS (bb, insn)
6704 if (NOTE_P (insn))
6706 if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
6708 note = insn;
6709 if (first != NULL)
6710 break;
6713 else if (first == NULL && contains (insn, epilogue_insn_hash))
6715 first = insn;
6716 if (note != NULL)
6717 break;
6721 if (note)
6723 /* If the function has a single basic block, and no real
6724 epilogue insns (e.g. sibcall with no cleanup), the
6725 epilogue note can get scheduled before the prologue
6726 note. If we have frame related prologue insns, having
6727 them scanned during the epilogue will result in a crash.
6728 In this case re-order the epilogue note to just before
6729 the last insn in the block. */
6730 if (first == NULL)
6731 first = BB_END (bb);
6733 if (PREV_INSN (first) != note)
6734 reorder_insns (note, note, PREV_INSN (first));
6738 #endif /* HAVE_prologue or HAVE_epilogue */
6741 /* Returns the name of the current function. */
6742 const char *
6743 current_function_name (void)
6745 if (cfun == NULL)
6746 return "<none>";
6747 return lang_hooks.decl_printable_name (cfun->decl, 2);
6751 static unsigned int
6752 rest_of_handle_check_leaf_regs (void)
6754 #ifdef LEAF_REGISTERS
6755 crtl->uses_only_leaf_regs
6756 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
6757 #endif
6758 return 0;
6761 /* Insert a TYPE into the used types hash table of CFUN. */
6763 static void
6764 used_types_insert_helper (tree type, struct function *func)
6766 if (type != NULL && func != NULL)
6768 void **slot;
6770 if (func->used_types_hash == NULL)
6771 func->used_types_hash = htab_create_ggc (37, htab_hash_pointer,
6772 htab_eq_pointer, NULL);
6773 slot = htab_find_slot (func->used_types_hash, type, INSERT);
6774 if (*slot == NULL)
6775 *slot = type;
6779 /* Given a type, insert it into the used hash table in cfun. */
6780 void
6781 used_types_insert (tree t)
6783 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
6784 if (TYPE_NAME (t))
6785 break;
6786 else
6787 t = TREE_TYPE (t);
6788 if (TREE_CODE (t) == ERROR_MARK)
6789 return;
6790 if (TYPE_NAME (t) == NULL_TREE
6791 || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
6792 t = TYPE_MAIN_VARIANT (t);
6793 if (debug_info_level > DINFO_LEVEL_NONE)
6795 if (cfun)
6796 used_types_insert_helper (t, cfun);
6797 else
6798 /* So this might be a type referenced by a global variable.
6799 Record that type so that we can later decide to emit its debug
6800 information. */
6801 VEC_safe_push (tree, gc, types_used_by_cur_var_decl, t);
6805 /* Helper to Hash a struct types_used_by_vars_entry. */
6807 static hashval_t
6808 hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
6810 gcc_assert (entry && entry->var_decl && entry->type);
6812 return iterative_hash_object (entry->type,
6813 iterative_hash_object (entry->var_decl, 0));
6816 /* Hash function of the types_used_by_vars_entry hash table. */
6818 hashval_t
6819 types_used_by_vars_do_hash (const void *x)
6821 const struct types_used_by_vars_entry *entry =
6822 (const struct types_used_by_vars_entry *) x;
6824 return hash_types_used_by_vars_entry (entry);
6827 /*Equality function of the types_used_by_vars_entry hash table. */
6830 types_used_by_vars_eq (const void *x1, const void *x2)
6832 const struct types_used_by_vars_entry *e1 =
6833 (const struct types_used_by_vars_entry *) x1;
6834 const struct types_used_by_vars_entry *e2 =
6835 (const struct types_used_by_vars_entry *)x2;
6837 return (e1->var_decl == e2->var_decl && e1->type == e2->type);
6840 /* Inserts an entry into the types_used_by_vars_hash hash table. */
6842 void
6843 types_used_by_var_decl_insert (tree type, tree var_decl)
6845 if (type != NULL && var_decl != NULL)
6847 void **slot;
6848 struct types_used_by_vars_entry e;
6849 e.var_decl = var_decl;
6850 e.type = type;
6851 if (types_used_by_vars_hash == NULL)
6852 types_used_by_vars_hash =
6853 htab_create_ggc (37, types_used_by_vars_do_hash,
6854 types_used_by_vars_eq, NULL);
6855 slot = htab_find_slot_with_hash (types_used_by_vars_hash, &e,
6856 hash_types_used_by_vars_entry (&e), INSERT);
6857 if (*slot == NULL)
6859 struct types_used_by_vars_entry *entry;
6860 entry = ggc_alloc_types_used_by_vars_entry ();
6861 entry->type = type;
6862 entry->var_decl = var_decl;
6863 *slot = entry;
6868 struct rtl_opt_pass pass_leaf_regs =
6871 RTL_PASS,
6872 "*leaf_regs", /* name */
6873 NULL, /* gate */
6874 rest_of_handle_check_leaf_regs, /* execute */
6875 NULL, /* sub */
6876 NULL, /* next */
6877 0, /* static_pass_number */
6878 TV_NONE, /* tv_id */
6879 0, /* properties_required */
6880 0, /* properties_provided */
6881 0, /* properties_destroyed */
6882 0, /* todo_flags_start */
6883 0 /* todo_flags_finish */
6887 static unsigned int
6888 rest_of_handle_thread_prologue_and_epilogue (void)
6890 if (optimize)
6891 cleanup_cfg (CLEANUP_EXPENSIVE);
6893 /* On some machines, the prologue and epilogue code, or parts thereof,
6894 can be represented as RTL. Doing so lets us schedule insns between
6895 it and the rest of the code and also allows delayed branch
6896 scheduling to operate in the epilogue. */
6897 thread_prologue_and_epilogue_insns ();
6899 /* The stack usage info is finalized during prologue expansion. */
6900 if (flag_stack_usage_info)
6901 output_stack_usage ();
6903 return 0;
6906 struct rtl_opt_pass pass_thread_prologue_and_epilogue =
6909 RTL_PASS,
6910 "pro_and_epilogue", /* name */
6911 NULL, /* gate */
6912 rest_of_handle_thread_prologue_and_epilogue, /* execute */
6913 NULL, /* sub */
6914 NULL, /* next */
6915 0, /* static_pass_number */
6916 TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
6917 0, /* properties_required */
6918 0, /* properties_provided */
6919 0, /* properties_destroyed */
6920 TODO_verify_flow, /* todo_flags_start */
6921 TODO_df_verify |
6922 TODO_df_finish | TODO_verify_rtl_sharing |
6923 TODO_ggc_collect /* todo_flags_finish */
6928 /* This mini-pass fixes fall-out from SSA in asm statements that have
6929 in-out constraints. Say you start with
6931 orig = inout;
6932 asm ("": "+mr" (inout));
6933 use (orig);
6935 which is transformed very early to use explicit output and match operands:
6937 orig = inout;
6938 asm ("": "=mr" (inout) : "0" (inout));
6939 use (orig);
6941 Or, after SSA and copyprop,
6943 asm ("": "=mr" (inout_2) : "0" (inout_1));
6944 use (inout_1);
6946 Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
6947 they represent two separate values, so they will get different pseudo
6948 registers during expansion. Then, since the two operands need to match
6949 per the constraints, but use different pseudo registers, reload can
6950 only register a reload for these operands. But reloads can only be
6951 satisfied by hardregs, not by memory, so we need a register for this
6952 reload, just because we are presented with non-matching operands.
6953 So, even though we allow memory for this operand, no memory can be
6954 used for it, just because the two operands don't match. This can
6955 cause reload failures on register-starved targets.
6957 So it's a symptom of reload not being able to use memory for reloads
6958 or, alternatively it's also a symptom of both operands not coming into
6959 reload as matching (in which case the pseudo could go to memory just
6960 fine, as the alternative allows it, and no reload would be necessary).
6961 We fix the latter problem here, by transforming
6963 asm ("": "=mr" (inout_2) : "0" (inout_1));
6965 back to
6967 inout_2 = inout_1;
6968 asm ("": "=mr" (inout_2) : "0" (inout_2)); */
6970 static void
6971 match_asm_constraints_1 (rtx insn, rtx *p_sets, int noutputs)
6973 int i;
6974 bool changed = false;
6975 rtx op = SET_SRC (p_sets[0]);
6976 int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
6977 rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
6978 bool *output_matched = XALLOCAVEC (bool, noutputs);
6980 memset (output_matched, 0, noutputs * sizeof (bool));
6981 for (i = 0; i < ninputs; i++)
6983 rtx input, output, insns;
6984 const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
6985 char *end;
6986 int match, j;
6988 if (*constraint == '%')
6989 constraint++;
6991 match = strtoul (constraint, &end, 10);
6992 if (end == constraint)
6993 continue;
6995 gcc_assert (match < noutputs);
6996 output = SET_DEST (p_sets[match]);
6997 input = RTVEC_ELT (inputs, i);
6998 /* Only do the transformation for pseudos. */
6999 if (! REG_P (output)
7000 || rtx_equal_p (output, input)
7001 || (GET_MODE (input) != VOIDmode
7002 && GET_MODE (input) != GET_MODE (output)))
7003 continue;
7005 /* We can't do anything if the output is also used as input,
7006 as we're going to overwrite it. */
7007 for (j = 0; j < ninputs; j++)
7008 if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
7009 break;
7010 if (j != ninputs)
7011 continue;
7013 /* Avoid changing the same input several times. For
7014 asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
7015 only change in once (to out1), rather than changing it
7016 first to out1 and afterwards to out2. */
7017 if (i > 0)
7019 for (j = 0; j < noutputs; j++)
7020 if (output_matched[j] && input == SET_DEST (p_sets[j]))
7021 break;
7022 if (j != noutputs)
7023 continue;
7025 output_matched[match] = true;
7027 start_sequence ();
7028 emit_move_insn (output, input);
7029 insns = get_insns ();
7030 end_sequence ();
7031 emit_insn_before (insns, insn);
7033 /* Now replace all mentions of the input with output. We can't
7034 just replace the occurrence in inputs[i], as the register might
7035 also be used in some other input (or even in an address of an
7036 output), which would mean possibly increasing the number of
7037 inputs by one (namely 'output' in addition), which might pose
7038 a too complicated problem for reload to solve. E.g. this situation:
7040 asm ("" : "=r" (output), "=m" (input) : "0" (input))
7042 Here 'input' is used in two occurrences as input (once for the
7043 input operand, once for the address in the second output operand).
7044 If we would replace only the occurrence of the input operand (to
7045 make the matching) we would be left with this:
7047 output = input
7048 asm ("" : "=r" (output), "=m" (input) : "0" (output))
7050 Now we suddenly have two different input values (containing the same
7051 value, but different pseudos) where we formerly had only one.
7052 With more complicated asms this might lead to reload failures
7053 which wouldn't have happen without this pass. So, iterate over
7054 all operands and replace all occurrences of the register used. */
7055 for (j = 0; j < noutputs; j++)
7056 if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
7057 && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
7058 SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
7059 input, output);
7060 for (j = 0; j < ninputs; j++)
7061 if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
7062 RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
7063 input, output);
7065 changed = true;
7068 if (changed)
7069 df_insn_rescan (insn);
7072 static unsigned
7073 rest_of_match_asm_constraints (void)
7075 basic_block bb;
7076 rtx insn, pat, *p_sets;
7077 int noutputs;
7079 if (!crtl->has_asm_statement)
7080 return 0;
7082 df_set_flags (DF_DEFER_INSN_RESCAN);
7083 FOR_EACH_BB (bb)
7085 FOR_BB_INSNS (bb, insn)
7087 if (!INSN_P (insn))
7088 continue;
7090 pat = PATTERN (insn);
7091 if (GET_CODE (pat) == PARALLEL)
7092 p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
7093 else if (GET_CODE (pat) == SET)
7094 p_sets = &PATTERN (insn), noutputs = 1;
7095 else
7096 continue;
7098 if (GET_CODE (*p_sets) == SET
7099 && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
7100 match_asm_constraints_1 (insn, p_sets, noutputs);
7104 return TODO_df_finish;
7107 struct rtl_opt_pass pass_match_asm_constraints =
7110 RTL_PASS,
7111 "asmcons", /* name */
7112 NULL, /* gate */
7113 rest_of_match_asm_constraints, /* execute */
7114 NULL, /* sub */
7115 NULL, /* next */
7116 0, /* static_pass_number */
7117 TV_NONE, /* tv_id */
7118 0, /* properties_required */
7119 0, /* properties_provided */
7120 0, /* properties_destroyed */
7121 0, /* todo_flags_start */
7122 0 /* todo_flags_finish */
7127 #include "gt-function.h"