* lto.c (do_stream_out): Add PART parameter; open dump file.
[official-gcc.git] / gcc / function.c
blob142cdaec2ce4ca4b1d8ad2c33f897271969263ef
1 /* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file handles the generation of rtl code from tree structure
21 at the level of the function as a whole.
22 It creates the rtl expressions for parameters and auto variables
23 and has full responsibility for allocating stack slots.
25 `expand_function_start' is called at the beginning of a function,
26 before the function body is parsed, and `expand_function_end' is
27 called after parsing the body.
29 Call `assign_stack_local' to allocate a stack slot for a local variable.
30 This is usually done during the RTL generation for the function body,
31 but it can also be done in the reload pass when a pseudo-register does
32 not get a hard register. */
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "backend.h"
38 #include "target.h"
39 #include "rtl.h"
40 #include "tree.h"
41 #include "gimple-expr.h"
42 #include "cfghooks.h"
43 #include "df.h"
44 #include "memmodel.h"
45 #include "tm_p.h"
46 #include "stringpool.h"
47 #include "expmed.h"
48 #include "optabs.h"
49 #include "regs.h"
50 #include "emit-rtl.h"
51 #include "recog.h"
52 #include "rtl-error.h"
53 #include "alias.h"
54 #include "fold-const.h"
55 #include "stor-layout.h"
56 #include "varasm.h"
57 #include "except.h"
58 #include "dojump.h"
59 #include "explow.h"
60 #include "calls.h"
61 #include "expr.h"
62 #include "optabs-tree.h"
63 #include "output.h"
64 #include "langhooks.h"
65 #include "common/common-target.h"
66 #include "gimplify.h"
67 #include "tree-pass.h"
68 #include "cfgrtl.h"
69 #include "cfganal.h"
70 #include "cfgbuild.h"
71 #include "cfgcleanup.h"
72 #include "cfgexpand.h"
73 #include "shrink-wrap.h"
74 #include "toplev.h"
75 #include "rtl-iter.h"
76 #include "tree-dfa.h"
77 #include "tree-ssa.h"
78 #include "stringpool.h"
79 #include "attribs.h"
80 #include "gimple.h"
81 #include "options.h"
83 /* So we can assign to cfun in this file. */
84 #undef cfun
86 #ifndef STACK_ALIGNMENT_NEEDED
87 #define STACK_ALIGNMENT_NEEDED 1
88 #endif
90 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
92 /* Round a value to the lowest integer less than it that is a multiple of
93 the required alignment. Avoid using division in case the value is
94 negative. Assume the alignment is a power of two. */
95 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
97 /* Similar, but round to the next highest integer that meets the
98 alignment. */
99 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
101 /* Nonzero once virtual register instantiation has been done.
102 assign_stack_local uses frame_pointer_rtx when this is nonzero.
103 calls.c:emit_library_call_value_1 uses it to set up
104 post-instantiation libcalls. */
105 int virtuals_instantiated;
107 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
108 static GTY(()) int funcdef_no;
110 /* These variables hold pointers to functions to create and destroy
111 target specific, per-function data structures. */
112 struct machine_function * (*init_machine_status) (void);
114 /* The currently compiled function. */
115 struct function *cfun = 0;
117 /* These hashes record the prologue and epilogue insns. */
119 struct insn_cache_hasher : ggc_cache_ptr_hash<rtx_def>
121 static hashval_t hash (rtx x) { return htab_hash_pointer (x); }
122 static bool equal (rtx a, rtx b) { return a == b; }
125 static GTY((cache))
126 hash_table<insn_cache_hasher> *prologue_insn_hash;
127 static GTY((cache))
128 hash_table<insn_cache_hasher> *epilogue_insn_hash;
131 hash_table<used_type_hasher> *types_used_by_vars_hash = NULL;
132 vec<tree, va_gc> *types_used_by_cur_var_decl;
134 /* Forward declarations. */
136 static struct temp_slot *find_temp_slot_from_address (rtx);
137 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
138 static void pad_below (struct args_size *, machine_mode, tree);
139 static void reorder_blocks_1 (rtx_insn *, tree, vec<tree> *);
140 static int all_blocks (tree, tree *);
141 static tree *get_block_vector (tree, int *);
142 extern tree debug_find_var_in_block_tree (tree, tree);
143 /* We always define `record_insns' even if it's not used so that we
144 can always export `prologue_epilogue_contains'. */
145 static void record_insns (rtx_insn *, rtx, hash_table<insn_cache_hasher> **)
146 ATTRIBUTE_UNUSED;
147 static bool contains (const rtx_insn *, hash_table<insn_cache_hasher> *);
148 static void prepare_function_start (void);
149 static void do_clobber_return_reg (rtx, void *);
150 static void do_use_return_reg (rtx, void *);
153 /* Stack of nested functions. */
154 /* Keep track of the cfun stack. */
156 static vec<function *> function_context_stack;
158 /* Save the current context for compilation of a nested function.
159 This is called from language-specific code. */
161 void
162 push_function_context (void)
164 if (cfun == 0)
165 allocate_struct_function (NULL, false);
167 function_context_stack.safe_push (cfun);
168 set_cfun (NULL);
171 /* Restore the last saved context, at the end of a nested function.
172 This function is called from language-specific code. */
174 void
175 pop_function_context (void)
177 struct function *p = function_context_stack.pop ();
178 set_cfun (p);
179 current_function_decl = p->decl;
181 /* Reset variables that have known state during rtx generation. */
182 virtuals_instantiated = 0;
183 generating_concat_p = 1;
186 /* Clear out all parts of the state in F that can safely be discarded
187 after the function has been parsed, but not compiled, to let
188 garbage collection reclaim the memory. */
190 void
191 free_after_parsing (struct function *f)
193 f->language = 0;
196 /* Clear out all parts of the state in F that can safely be discarded
197 after the function has been compiled, to let garbage collection
198 reclaim the memory. */
200 void
201 free_after_compilation (struct function *f)
203 prologue_insn_hash = NULL;
204 epilogue_insn_hash = NULL;
206 free (crtl->emit.regno_pointer_align);
208 memset (crtl, 0, sizeof (struct rtl_data));
209 f->eh = NULL;
210 f->machine = NULL;
211 f->cfg = NULL;
212 f->curr_properties &= ~PROP_cfg;
214 regno_reg_rtx = NULL;
217 /* Return size needed for stack frame based on slots so far allocated.
218 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
219 the caller may have to do that. */
221 poly_int64
222 get_frame_size (void)
224 if (FRAME_GROWS_DOWNWARD)
225 return -frame_offset;
226 else
227 return frame_offset;
230 /* Issue an error message and return TRUE if frame OFFSET overflows in
231 the signed target pointer arithmetics for function FUNC. Otherwise
232 return FALSE. */
234 bool
235 frame_offset_overflow (poly_int64 offset, tree func)
237 poly_uint64 size = FRAME_GROWS_DOWNWARD ? -offset : offset;
238 unsigned HOST_WIDE_INT limit
239 = ((HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (Pmode) - 1))
240 /* Leave room for the fixed part of the frame. */
241 - 64 * UNITS_PER_WORD);
243 if (!coeffs_in_range_p (size, 0U, limit))
245 error_at (DECL_SOURCE_LOCATION (func),
246 "total size of local objects too large");
247 return true;
250 return false;
253 /* Return the minimum spill slot alignment for a register of mode MODE. */
255 unsigned int
256 spill_slot_alignment (machine_mode mode ATTRIBUTE_UNUSED)
258 return STACK_SLOT_ALIGNMENT (NULL_TREE, mode, GET_MODE_ALIGNMENT (mode));
261 /* Return stack slot alignment in bits for TYPE and MODE. */
263 static unsigned int
264 get_stack_local_alignment (tree type, machine_mode mode)
266 unsigned int alignment;
268 if (mode == BLKmode)
269 alignment = BIGGEST_ALIGNMENT;
270 else
271 alignment = GET_MODE_ALIGNMENT (mode);
273 /* Allow the frond-end to (possibly) increase the alignment of this
274 stack slot. */
275 if (! type)
276 type = lang_hooks.types.type_for_mode (mode, 0);
278 return STACK_SLOT_ALIGNMENT (type, mode, alignment);
281 /* Determine whether it is possible to fit a stack slot of size SIZE and
282 alignment ALIGNMENT into an area in the stack frame that starts at
283 frame offset START and has a length of LENGTH. If so, store the frame
284 offset to be used for the stack slot in *POFFSET and return true;
285 return false otherwise. This function will extend the frame size when
286 given a start/length pair that lies at the end of the frame. */
288 static bool
289 try_fit_stack_local (poly_int64 start, poly_int64 length,
290 poly_int64 size, unsigned int alignment,
291 poly_int64_pod *poffset)
293 poly_int64 this_frame_offset;
294 int frame_off, frame_alignment, frame_phase;
296 /* Calculate how many bytes the start of local variables is off from
297 stack alignment. */
298 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
299 frame_off = targetm.starting_frame_offset () % frame_alignment;
300 frame_phase = frame_off ? frame_alignment - frame_off : 0;
302 /* Round the frame offset to the specified alignment. */
304 if (FRAME_GROWS_DOWNWARD)
305 this_frame_offset
306 = (aligned_lower_bound (start + length - size - frame_phase, alignment)
307 + frame_phase);
308 else
309 this_frame_offset
310 = aligned_upper_bound (start - frame_phase, alignment) + frame_phase;
312 /* See if it fits. If this space is at the edge of the frame,
313 consider extending the frame to make it fit. Our caller relies on
314 this when allocating a new slot. */
315 if (maybe_lt (this_frame_offset, start))
317 if (known_eq (frame_offset, start))
318 frame_offset = this_frame_offset;
319 else
320 return false;
322 else if (maybe_gt (this_frame_offset + size, start + length))
324 if (known_eq (frame_offset, start + length))
325 frame_offset = this_frame_offset + size;
326 else
327 return false;
330 *poffset = this_frame_offset;
331 return true;
334 /* Create a new frame_space structure describing free space in the stack
335 frame beginning at START and ending at END, and chain it into the
336 function's frame_space_list. */
338 static void
339 add_frame_space (poly_int64 start, poly_int64 end)
341 struct frame_space *space = ggc_alloc<frame_space> ();
342 space->next = crtl->frame_space_list;
343 crtl->frame_space_list = space;
344 space->start = start;
345 space->length = end - start;
348 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
349 with machine mode MODE.
351 ALIGN controls the amount of alignment for the address of the slot:
352 0 means according to MODE,
353 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
354 -2 means use BITS_PER_UNIT,
355 positive specifies alignment boundary in bits.
357 KIND has ASLK_REDUCE_ALIGN bit set if it is OK to reduce
358 alignment and ASLK_RECORD_PAD bit set if we should remember
359 extra space we allocated for alignment purposes. When we are
360 called from assign_stack_temp_for_type, it is not set so we don't
361 track the same stack slot in two independent lists.
363 We do not round to stack_boundary here. */
366 assign_stack_local_1 (machine_mode mode, poly_int64 size,
367 int align, int kind)
369 rtx x, addr;
370 poly_int64 bigend_correction = 0;
371 poly_int64 slot_offset = 0, old_frame_offset;
372 unsigned int alignment, alignment_in_bits;
374 if (align == 0)
376 alignment = get_stack_local_alignment (NULL, mode);
377 alignment /= BITS_PER_UNIT;
379 else if (align == -1)
381 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
382 size = aligned_upper_bound (size, alignment);
384 else if (align == -2)
385 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
386 else
387 alignment = align / BITS_PER_UNIT;
389 alignment_in_bits = alignment * BITS_PER_UNIT;
391 /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
392 if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
394 alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
395 alignment = alignment_in_bits / BITS_PER_UNIT;
398 if (SUPPORTS_STACK_ALIGNMENT)
400 if (crtl->stack_alignment_estimated < alignment_in_bits)
402 if (!crtl->stack_realign_processed)
403 crtl->stack_alignment_estimated = alignment_in_bits;
404 else
406 /* If stack is realigned and stack alignment value
407 hasn't been finalized, it is OK not to increase
408 stack_alignment_estimated. The bigger alignment
409 requirement is recorded in stack_alignment_needed
410 below. */
411 gcc_assert (!crtl->stack_realign_finalized);
412 if (!crtl->stack_realign_needed)
414 /* It is OK to reduce the alignment as long as the
415 requested size is 0 or the estimated stack
416 alignment >= mode alignment. */
417 gcc_assert ((kind & ASLK_REDUCE_ALIGN)
418 || known_eq (size, 0)
419 || (crtl->stack_alignment_estimated
420 >= GET_MODE_ALIGNMENT (mode)));
421 alignment_in_bits = crtl->stack_alignment_estimated;
422 alignment = alignment_in_bits / BITS_PER_UNIT;
428 if (crtl->stack_alignment_needed < alignment_in_bits)
429 crtl->stack_alignment_needed = alignment_in_bits;
430 if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
431 crtl->max_used_stack_slot_alignment = alignment_in_bits;
433 if (mode != BLKmode || maybe_ne (size, 0))
435 if (kind & ASLK_RECORD_PAD)
437 struct frame_space **psp;
439 for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
441 struct frame_space *space = *psp;
442 if (!try_fit_stack_local (space->start, space->length, size,
443 alignment, &slot_offset))
444 continue;
445 *psp = space->next;
446 if (known_gt (slot_offset, space->start))
447 add_frame_space (space->start, slot_offset);
448 if (known_lt (slot_offset + size, space->start + space->length))
449 add_frame_space (slot_offset + size,
450 space->start + space->length);
451 goto found_space;
455 else if (!STACK_ALIGNMENT_NEEDED)
457 slot_offset = frame_offset;
458 goto found_space;
461 old_frame_offset = frame_offset;
463 if (FRAME_GROWS_DOWNWARD)
465 frame_offset -= size;
466 try_fit_stack_local (frame_offset, size, size, alignment, &slot_offset);
468 if (kind & ASLK_RECORD_PAD)
470 if (known_gt (slot_offset, frame_offset))
471 add_frame_space (frame_offset, slot_offset);
472 if (known_lt (slot_offset + size, old_frame_offset))
473 add_frame_space (slot_offset + size, old_frame_offset);
476 else
478 frame_offset += size;
479 try_fit_stack_local (old_frame_offset, size, size, alignment, &slot_offset);
481 if (kind & ASLK_RECORD_PAD)
483 if (known_gt (slot_offset, old_frame_offset))
484 add_frame_space (old_frame_offset, slot_offset);
485 if (known_lt (slot_offset + size, frame_offset))
486 add_frame_space (slot_offset + size, frame_offset);
490 found_space:
491 /* On a big-endian machine, if we are allocating more space than we will use,
492 use the least significant bytes of those that are allocated. */
493 if (mode != BLKmode)
495 /* The slot size can sometimes be smaller than the mode size;
496 e.g. the rs6000 port allocates slots with a vector mode
497 that have the size of only one element. However, the slot
498 size must always be ordered wrt to the mode size, in the
499 same way as for a subreg. */
500 gcc_checking_assert (ordered_p (GET_MODE_SIZE (mode), size));
501 if (BYTES_BIG_ENDIAN && maybe_lt (GET_MODE_SIZE (mode), size))
502 bigend_correction = size - GET_MODE_SIZE (mode);
505 /* If we have already instantiated virtual registers, return the actual
506 address relative to the frame pointer. */
507 if (virtuals_instantiated)
508 addr = plus_constant (Pmode, frame_pointer_rtx,
509 trunc_int_for_mode
510 (slot_offset + bigend_correction
511 + targetm.starting_frame_offset (), Pmode));
512 else
513 addr = plus_constant (Pmode, virtual_stack_vars_rtx,
514 trunc_int_for_mode
515 (slot_offset + bigend_correction,
516 Pmode));
518 x = gen_rtx_MEM (mode, addr);
519 set_mem_align (x, alignment_in_bits);
520 MEM_NOTRAP_P (x) = 1;
522 vec_safe_push (stack_slot_list, x);
524 if (frame_offset_overflow (frame_offset, current_function_decl))
525 frame_offset = 0;
527 return x;
530 /* Wrap up assign_stack_local_1 with last parameter as false. */
533 assign_stack_local (machine_mode mode, poly_int64 size, int align)
535 return assign_stack_local_1 (mode, size, align, ASLK_RECORD_PAD);
538 /* In order to evaluate some expressions, such as function calls returning
539 structures in memory, we need to temporarily allocate stack locations.
540 We record each allocated temporary in the following structure.
542 Associated with each temporary slot is a nesting level. When we pop up
543 one level, all temporaries associated with the previous level are freed.
544 Normally, all temporaries are freed after the execution of the statement
545 in which they were created. However, if we are inside a ({...}) grouping,
546 the result may be in a temporary and hence must be preserved. If the
547 result could be in a temporary, we preserve it if we can determine which
548 one it is in. If we cannot determine which temporary may contain the
549 result, all temporaries are preserved. A temporary is preserved by
550 pretending it was allocated at the previous nesting level. */
552 struct GTY(()) temp_slot {
553 /* Points to next temporary slot. */
554 struct temp_slot *next;
555 /* Points to previous temporary slot. */
556 struct temp_slot *prev;
557 /* The rtx to used to reference the slot. */
558 rtx slot;
559 /* The size, in units, of the slot. */
560 poly_int64 size;
561 /* The type of the object in the slot, or zero if it doesn't correspond
562 to a type. We use this to determine whether a slot can be reused.
563 It can be reused if objects of the type of the new slot will always
564 conflict with objects of the type of the old slot. */
565 tree type;
566 /* The alignment (in bits) of the slot. */
567 unsigned int align;
568 /* Nonzero if this temporary is currently in use. */
569 char in_use;
570 /* Nesting level at which this slot is being used. */
571 int level;
572 /* The offset of the slot from the frame_pointer, including extra space
573 for alignment. This info is for combine_temp_slots. */
574 poly_int64 base_offset;
575 /* The size of the slot, including extra space for alignment. This
576 info is for combine_temp_slots. */
577 poly_int64 full_size;
580 /* Entry for the below hash table. */
581 struct GTY((for_user)) temp_slot_address_entry {
582 hashval_t hash;
583 rtx address;
584 struct temp_slot *temp_slot;
587 struct temp_address_hasher : ggc_ptr_hash<temp_slot_address_entry>
589 static hashval_t hash (temp_slot_address_entry *);
590 static bool equal (temp_slot_address_entry *, temp_slot_address_entry *);
593 /* A table of addresses that represent a stack slot. The table is a mapping
594 from address RTXen to a temp slot. */
595 static GTY(()) hash_table<temp_address_hasher> *temp_slot_address_table;
596 static size_t n_temp_slots_in_use;
598 /* Removes temporary slot TEMP from LIST. */
600 static void
601 cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
603 if (temp->next)
604 temp->next->prev = temp->prev;
605 if (temp->prev)
606 temp->prev->next = temp->next;
607 else
608 *list = temp->next;
610 temp->prev = temp->next = NULL;
613 /* Inserts temporary slot TEMP to LIST. */
615 static void
616 insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
618 temp->next = *list;
619 if (*list)
620 (*list)->prev = temp;
621 temp->prev = NULL;
622 *list = temp;
625 /* Returns the list of used temp slots at LEVEL. */
627 static struct temp_slot **
628 temp_slots_at_level (int level)
630 if (level >= (int) vec_safe_length (used_temp_slots))
631 vec_safe_grow_cleared (used_temp_slots, level + 1);
633 return &(*used_temp_slots)[level];
636 /* Returns the maximal temporary slot level. */
638 static int
639 max_slot_level (void)
641 if (!used_temp_slots)
642 return -1;
644 return used_temp_slots->length () - 1;
647 /* Moves temporary slot TEMP to LEVEL. */
649 static void
650 move_slot_to_level (struct temp_slot *temp, int level)
652 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
653 insert_slot_to_list (temp, temp_slots_at_level (level));
654 temp->level = level;
657 /* Make temporary slot TEMP available. */
659 static void
660 make_slot_available (struct temp_slot *temp)
662 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
663 insert_slot_to_list (temp, &avail_temp_slots);
664 temp->in_use = 0;
665 temp->level = -1;
666 n_temp_slots_in_use--;
669 /* Compute the hash value for an address -> temp slot mapping.
670 The value is cached on the mapping entry. */
671 static hashval_t
672 temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
674 int do_not_record = 0;
675 return hash_rtx (t->address, GET_MODE (t->address),
676 &do_not_record, NULL, false);
679 /* Return the hash value for an address -> temp slot mapping. */
680 hashval_t
681 temp_address_hasher::hash (temp_slot_address_entry *t)
683 return t->hash;
686 /* Compare two address -> temp slot mapping entries. */
687 bool
688 temp_address_hasher::equal (temp_slot_address_entry *t1,
689 temp_slot_address_entry *t2)
691 return exp_equiv_p (t1->address, t2->address, 0, true);
694 /* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping. */
695 static void
696 insert_temp_slot_address (rtx address, struct temp_slot *temp_slot)
698 struct temp_slot_address_entry *t = ggc_alloc<temp_slot_address_entry> ();
699 t->address = address;
700 t->temp_slot = temp_slot;
701 t->hash = temp_slot_address_compute_hash (t);
702 *temp_slot_address_table->find_slot_with_hash (t, t->hash, INSERT) = t;
705 /* Remove an address -> temp slot mapping entry if the temp slot is
706 not in use anymore. Callback for remove_unused_temp_slot_addresses. */
708 remove_unused_temp_slot_addresses_1 (temp_slot_address_entry **slot, void *)
710 const struct temp_slot_address_entry *t = *slot;
711 if (! t->temp_slot->in_use)
712 temp_slot_address_table->clear_slot (slot);
713 return 1;
716 /* Remove all mappings of addresses to unused temp slots. */
717 static void
718 remove_unused_temp_slot_addresses (void)
720 /* Use quicker clearing if there aren't any active temp slots. */
721 if (n_temp_slots_in_use)
722 temp_slot_address_table->traverse
723 <void *, remove_unused_temp_slot_addresses_1> (NULL);
724 else
725 temp_slot_address_table->empty ();
728 /* Find the temp slot corresponding to the object at address X. */
730 static struct temp_slot *
731 find_temp_slot_from_address (rtx x)
733 struct temp_slot *p;
734 struct temp_slot_address_entry tmp, *t;
736 /* First try the easy way:
737 See if X exists in the address -> temp slot mapping. */
738 tmp.address = x;
739 tmp.temp_slot = NULL;
740 tmp.hash = temp_slot_address_compute_hash (&tmp);
741 t = temp_slot_address_table->find_with_hash (&tmp, tmp.hash);
742 if (t)
743 return t->temp_slot;
745 /* If we have a sum involving a register, see if it points to a temp
746 slot. */
747 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
748 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
749 return p;
750 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
751 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
752 return p;
754 /* Last resort: Address is a virtual stack var address. */
755 poly_int64 offset;
756 if (strip_offset (x, &offset) == virtual_stack_vars_rtx)
758 int i;
759 for (i = max_slot_level (); i >= 0; i--)
760 for (p = *temp_slots_at_level (i); p; p = p->next)
761 if (known_in_range_p (offset, p->base_offset, p->full_size))
762 return p;
765 return NULL;
768 /* Allocate a temporary stack slot and record it for possible later
769 reuse.
771 MODE is the machine mode to be given to the returned rtx.
773 SIZE is the size in units of the space required. We do no rounding here
774 since assign_stack_local will do any required rounding.
776 TYPE is the type that will be used for the stack slot. */
779 assign_stack_temp_for_type (machine_mode mode, poly_int64 size, tree type)
781 unsigned int align;
782 struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
783 rtx slot;
785 gcc_assert (known_size_p (size));
787 align = get_stack_local_alignment (type, mode);
789 /* Try to find an available, already-allocated temporary of the proper
790 mode which meets the size and alignment requirements. Choose the
791 smallest one with the closest alignment.
793 If assign_stack_temp is called outside of the tree->rtl expansion,
794 we cannot reuse the stack slots (that may still refer to
795 VIRTUAL_STACK_VARS_REGNUM). */
796 if (!virtuals_instantiated)
798 for (p = avail_temp_slots; p; p = p->next)
800 if (p->align >= align
801 && known_ge (p->size, size)
802 && GET_MODE (p->slot) == mode
803 && objects_must_conflict_p (p->type, type)
804 && (best_p == 0
805 || (known_eq (best_p->size, p->size)
806 ? best_p->align > p->align
807 : known_ge (best_p->size, p->size))))
809 if (p->align == align && known_eq (p->size, size))
811 selected = p;
812 cut_slot_from_list (selected, &avail_temp_slots);
813 best_p = 0;
814 break;
816 best_p = p;
821 /* Make our best, if any, the one to use. */
822 if (best_p)
824 selected = best_p;
825 cut_slot_from_list (selected, &avail_temp_slots);
827 /* If there are enough aligned bytes left over, make them into a new
828 temp_slot so that the extra bytes don't get wasted. Do this only
829 for BLKmode slots, so that we can be sure of the alignment. */
830 if (GET_MODE (best_p->slot) == BLKmode)
832 int alignment = best_p->align / BITS_PER_UNIT;
833 poly_int64 rounded_size = aligned_upper_bound (size, alignment);
835 if (known_ge (best_p->size - rounded_size, alignment))
837 p = ggc_alloc<temp_slot> ();
838 p->in_use = 0;
839 p->size = best_p->size - rounded_size;
840 p->base_offset = best_p->base_offset + rounded_size;
841 p->full_size = best_p->full_size - rounded_size;
842 p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
843 p->align = best_p->align;
844 p->type = best_p->type;
845 insert_slot_to_list (p, &avail_temp_slots);
847 vec_safe_push (stack_slot_list, p->slot);
849 best_p->size = rounded_size;
850 best_p->full_size = rounded_size;
855 /* If we still didn't find one, make a new temporary. */
856 if (selected == 0)
858 poly_int64 frame_offset_old = frame_offset;
860 p = ggc_alloc<temp_slot> ();
862 /* We are passing an explicit alignment request to assign_stack_local.
863 One side effect of that is assign_stack_local will not round SIZE
864 to ensure the frame offset remains suitably aligned.
866 So for requests which depended on the rounding of SIZE, we go ahead
867 and round it now. We also make sure ALIGNMENT is at least
868 BIGGEST_ALIGNMENT. */
869 gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
870 p->slot = assign_stack_local_1 (mode,
871 (mode == BLKmode
872 ? aligned_upper_bound (size,
873 (int) align
874 / BITS_PER_UNIT)
875 : size),
876 align, 0);
878 p->align = align;
880 /* The following slot size computation is necessary because we don't
881 know the actual size of the temporary slot until assign_stack_local
882 has performed all the frame alignment and size rounding for the
883 requested temporary. Note that extra space added for alignment
884 can be either above or below this stack slot depending on which
885 way the frame grows. We include the extra space if and only if it
886 is above this slot. */
887 if (FRAME_GROWS_DOWNWARD)
888 p->size = frame_offset_old - frame_offset;
889 else
890 p->size = size;
892 /* Now define the fields used by combine_temp_slots. */
893 if (FRAME_GROWS_DOWNWARD)
895 p->base_offset = frame_offset;
896 p->full_size = frame_offset_old - frame_offset;
898 else
900 p->base_offset = frame_offset_old;
901 p->full_size = frame_offset - frame_offset_old;
904 selected = p;
907 p = selected;
908 p->in_use = 1;
909 p->type = type;
910 p->level = temp_slot_level;
911 n_temp_slots_in_use++;
913 pp = temp_slots_at_level (p->level);
914 insert_slot_to_list (p, pp);
915 insert_temp_slot_address (XEXP (p->slot, 0), p);
917 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
918 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
919 vec_safe_push (stack_slot_list, slot);
921 /* If we know the alias set for the memory that will be used, use
922 it. If there's no TYPE, then we don't know anything about the
923 alias set for the memory. */
924 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
925 set_mem_align (slot, align);
927 /* If a type is specified, set the relevant flags. */
928 if (type != 0)
929 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
930 MEM_NOTRAP_P (slot) = 1;
932 return slot;
935 /* Allocate a temporary stack slot and record it for possible later
936 reuse. First two arguments are same as in preceding function. */
939 assign_stack_temp (machine_mode mode, poly_int64 size)
941 return assign_stack_temp_for_type (mode, size, NULL_TREE);
944 /* Assign a temporary.
945 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
946 and so that should be used in error messages. In either case, we
947 allocate of the given type.
948 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
949 it is 0 if a register is OK.
950 DONT_PROMOTE is 1 if we should not promote values in register
951 to wider modes. */
954 assign_temp (tree type_or_decl, int memory_required,
955 int dont_promote ATTRIBUTE_UNUSED)
957 tree type, decl;
958 machine_mode mode;
959 #ifdef PROMOTE_MODE
960 int unsignedp;
961 #endif
963 if (DECL_P (type_or_decl))
964 decl = type_or_decl, type = TREE_TYPE (decl);
965 else
966 decl = NULL, type = type_or_decl;
968 mode = TYPE_MODE (type);
969 #ifdef PROMOTE_MODE
970 unsignedp = TYPE_UNSIGNED (type);
971 #endif
973 /* Allocating temporaries of TREE_ADDRESSABLE type must be done in the front
974 end. See also create_tmp_var for the gimplification-time check. */
975 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
977 if (mode == BLKmode || memory_required)
979 poly_int64 size;
980 rtx tmp;
982 /* Unfortunately, we don't yet know how to allocate variable-sized
983 temporaries. However, sometimes we can find a fixed upper limit on
984 the size, so try that instead. */
985 if (!poly_int_tree_p (TYPE_SIZE_UNIT (type), &size))
986 size = max_int_size_in_bytes (type);
988 /* Zero sized arrays are a GNU C extension. Set size to 1 to avoid
989 problems with allocating the stack space. */
990 if (known_eq (size, 0))
991 size = 1;
993 /* The size of the temporary may be too large to fit into an integer. */
994 /* ??? Not sure this should happen except for user silliness, so limit
995 this to things that aren't compiler-generated temporaries. The
996 rest of the time we'll die in assign_stack_temp_for_type. */
997 if (decl
998 && !known_size_p (size)
999 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
1001 error ("size of variable %q+D is too large", decl);
1002 size = 1;
1005 tmp = assign_stack_temp_for_type (mode, size, type);
1006 return tmp;
1009 #ifdef PROMOTE_MODE
1010 if (! dont_promote)
1011 mode = promote_mode (type, mode, &unsignedp);
1012 #endif
1014 return gen_reg_rtx (mode);
1017 /* Combine temporary stack slots which are adjacent on the stack.
1019 This allows for better use of already allocated stack space. This is only
1020 done for BLKmode slots because we can be sure that we won't have alignment
1021 problems in this case. */
1023 static void
1024 combine_temp_slots (void)
1026 struct temp_slot *p, *q, *next, *next_q;
1027 int num_slots;
1029 /* We can't combine slots, because the information about which slot
1030 is in which alias set will be lost. */
1031 if (flag_strict_aliasing)
1032 return;
1034 /* If there are a lot of temp slots, don't do anything unless
1035 high levels of optimization. */
1036 if (! flag_expensive_optimizations)
1037 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
1038 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
1039 return;
1041 for (p = avail_temp_slots; p; p = next)
1043 int delete_p = 0;
1045 next = p->next;
1047 if (GET_MODE (p->slot) != BLKmode)
1048 continue;
1050 for (q = p->next; q; q = next_q)
1052 int delete_q = 0;
1054 next_q = q->next;
1056 if (GET_MODE (q->slot) != BLKmode)
1057 continue;
1059 if (known_eq (p->base_offset + p->full_size, q->base_offset))
1061 /* Q comes after P; combine Q into P. */
1062 p->size += q->size;
1063 p->full_size += q->full_size;
1064 delete_q = 1;
1066 else if (known_eq (q->base_offset + q->full_size, p->base_offset))
1068 /* P comes after Q; combine P into Q. */
1069 q->size += p->size;
1070 q->full_size += p->full_size;
1071 delete_p = 1;
1072 break;
1074 if (delete_q)
1075 cut_slot_from_list (q, &avail_temp_slots);
1078 /* Either delete P or advance past it. */
1079 if (delete_p)
1080 cut_slot_from_list (p, &avail_temp_slots);
1084 /* Indicate that NEW_RTX is an alternate way of referring to the temp
1085 slot that previously was known by OLD_RTX. */
1087 void
1088 update_temp_slot_address (rtx old_rtx, rtx new_rtx)
1090 struct temp_slot *p;
1092 if (rtx_equal_p (old_rtx, new_rtx))
1093 return;
1095 p = find_temp_slot_from_address (old_rtx);
1097 /* If we didn't find one, see if both OLD_RTX is a PLUS. If so, and
1098 NEW_RTX is a register, see if one operand of the PLUS is a
1099 temporary location. If so, NEW_RTX points into it. Otherwise,
1100 if both OLD_RTX and NEW_RTX are a PLUS and if there is a register
1101 in common between them. If so, try a recursive call on those
1102 values. */
1103 if (p == 0)
1105 if (GET_CODE (old_rtx) != PLUS)
1106 return;
1108 if (REG_P (new_rtx))
1110 update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1111 update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
1112 return;
1114 else if (GET_CODE (new_rtx) != PLUS)
1115 return;
1117 if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 0)))
1118 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 1));
1119 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 0)))
1120 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 1));
1121 else if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 1)))
1122 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 0));
1123 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 1)))
1124 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 0));
1126 return;
1129 /* Otherwise add an alias for the temp's address. */
1130 insert_temp_slot_address (new_rtx, p);
1133 /* If X could be a reference to a temporary slot, mark that slot as
1134 belonging to the to one level higher than the current level. If X
1135 matched one of our slots, just mark that one. Otherwise, we can't
1136 easily predict which it is, so upgrade all of them.
1138 This is called when an ({...}) construct occurs and a statement
1139 returns a value in memory. */
1141 void
1142 preserve_temp_slots (rtx x)
1144 struct temp_slot *p = 0, *next;
1146 if (x == 0)
1147 return;
1149 /* If X is a register that is being used as a pointer, see if we have
1150 a temporary slot we know it points to. */
1151 if (REG_P (x) && REG_POINTER (x))
1152 p = find_temp_slot_from_address (x);
1154 /* If X is not in memory or is at a constant address, it cannot be in
1155 a temporary slot. */
1156 if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1157 return;
1159 /* First see if we can find a match. */
1160 if (p == 0)
1161 p = find_temp_slot_from_address (XEXP (x, 0));
1163 if (p != 0)
1165 if (p->level == temp_slot_level)
1166 move_slot_to_level (p, temp_slot_level - 1);
1167 return;
1170 /* Otherwise, preserve all non-kept slots at this level. */
1171 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1173 next = p->next;
1174 move_slot_to_level (p, temp_slot_level - 1);
1178 /* Free all temporaries used so far. This is normally called at the
1179 end of generating code for a statement. */
1181 void
1182 free_temp_slots (void)
1184 struct temp_slot *p, *next;
1185 bool some_available = false;
1187 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1189 next = p->next;
1190 make_slot_available (p);
1191 some_available = true;
1194 if (some_available)
1196 remove_unused_temp_slot_addresses ();
1197 combine_temp_slots ();
1201 /* Push deeper into the nesting level for stack temporaries. */
1203 void
1204 push_temp_slots (void)
1206 temp_slot_level++;
1209 /* Pop a temporary nesting level. All slots in use in the current level
1210 are freed. */
1212 void
1213 pop_temp_slots (void)
1215 free_temp_slots ();
1216 temp_slot_level--;
1219 /* Initialize temporary slots. */
1221 void
1222 init_temp_slots (void)
1224 /* We have not allocated any temporaries yet. */
1225 avail_temp_slots = 0;
1226 vec_alloc (used_temp_slots, 0);
1227 temp_slot_level = 0;
1228 n_temp_slots_in_use = 0;
1230 /* Set up the table to map addresses to temp slots. */
1231 if (! temp_slot_address_table)
1232 temp_slot_address_table = hash_table<temp_address_hasher>::create_ggc (32);
1233 else
1234 temp_slot_address_table->empty ();
1237 /* Functions and data structures to keep track of the values hard regs
1238 had at the start of the function. */
1240 /* Private type used by get_hard_reg_initial_reg, get_hard_reg_initial_val,
1241 and has_hard_reg_initial_val.. */
1242 struct GTY(()) initial_value_pair {
1243 rtx hard_reg;
1244 rtx pseudo;
1246 /* ??? This could be a VEC but there is currently no way to define an
1247 opaque VEC type. This could be worked around by defining struct
1248 initial_value_pair in function.h. */
1249 struct GTY(()) initial_value_struct {
1250 int num_entries;
1251 int max_entries;
1252 initial_value_pair * GTY ((length ("%h.num_entries"))) entries;
1255 /* If a pseudo represents an initial hard reg (or expression), return
1256 it, else return NULL_RTX. */
1259 get_hard_reg_initial_reg (rtx reg)
1261 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1262 int i;
1264 if (ivs == 0)
1265 return NULL_RTX;
1267 for (i = 0; i < ivs->num_entries; i++)
1268 if (rtx_equal_p (ivs->entries[i].pseudo, reg))
1269 return ivs->entries[i].hard_reg;
1271 return NULL_RTX;
1274 /* Make sure that there's a pseudo register of mode MODE that stores the
1275 initial value of hard register REGNO. Return an rtx for such a pseudo. */
1278 get_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1280 struct initial_value_struct *ivs;
1281 rtx rv;
1283 rv = has_hard_reg_initial_val (mode, regno);
1284 if (rv)
1285 return rv;
1287 ivs = crtl->hard_reg_initial_vals;
1288 if (ivs == 0)
1290 ivs = ggc_alloc<initial_value_struct> ();
1291 ivs->num_entries = 0;
1292 ivs->max_entries = 5;
1293 ivs->entries = ggc_vec_alloc<initial_value_pair> (5);
1294 crtl->hard_reg_initial_vals = ivs;
1297 if (ivs->num_entries >= ivs->max_entries)
1299 ivs->max_entries += 5;
1300 ivs->entries = GGC_RESIZEVEC (initial_value_pair, ivs->entries,
1301 ivs->max_entries);
1304 ivs->entries[ivs->num_entries].hard_reg = gen_rtx_REG (mode, regno);
1305 ivs->entries[ivs->num_entries].pseudo = gen_reg_rtx (mode);
1307 return ivs->entries[ivs->num_entries++].pseudo;
1310 /* See if get_hard_reg_initial_val has been used to create a pseudo
1311 for the initial value of hard register REGNO in mode MODE. Return
1312 the associated pseudo if so, otherwise return NULL. */
1315 has_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1317 struct initial_value_struct *ivs;
1318 int i;
1320 ivs = crtl->hard_reg_initial_vals;
1321 if (ivs != 0)
1322 for (i = 0; i < ivs->num_entries; i++)
1323 if (GET_MODE (ivs->entries[i].hard_reg) == mode
1324 && REGNO (ivs->entries[i].hard_reg) == regno)
1325 return ivs->entries[i].pseudo;
1327 return NULL_RTX;
1330 unsigned int
1331 emit_initial_value_sets (void)
1333 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1334 int i;
1335 rtx_insn *seq;
1337 if (ivs == 0)
1338 return 0;
1340 start_sequence ();
1341 for (i = 0; i < ivs->num_entries; i++)
1342 emit_move_insn (ivs->entries[i].pseudo, ivs->entries[i].hard_reg);
1343 seq = get_insns ();
1344 end_sequence ();
1346 emit_insn_at_entry (seq);
1347 return 0;
1350 /* Return the hardreg-pseudoreg initial values pair entry I and
1351 TRUE if I is a valid entry, or FALSE if I is not a valid entry. */
1352 bool
1353 initial_value_entry (int i, rtx *hreg, rtx *preg)
1355 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1356 if (!ivs || i >= ivs->num_entries)
1357 return false;
1359 *hreg = ivs->entries[i].hard_reg;
1360 *preg = ivs->entries[i].pseudo;
1361 return true;
1364 /* These routines are responsible for converting virtual register references
1365 to the actual hard register references once RTL generation is complete.
1367 The following four variables are used for communication between the
1368 routines. They contain the offsets of the virtual registers from their
1369 respective hard registers. */
1371 static poly_int64 in_arg_offset;
1372 static poly_int64 var_offset;
1373 static poly_int64 dynamic_offset;
1374 static poly_int64 out_arg_offset;
1375 static poly_int64 cfa_offset;
1377 /* In most machines, the stack pointer register is equivalent to the bottom
1378 of the stack. */
1380 #ifndef STACK_POINTER_OFFSET
1381 #define STACK_POINTER_OFFSET 0
1382 #endif
1384 #if defined (REG_PARM_STACK_SPACE) && !defined (INCOMING_REG_PARM_STACK_SPACE)
1385 #define INCOMING_REG_PARM_STACK_SPACE REG_PARM_STACK_SPACE
1386 #endif
1388 /* If not defined, pick an appropriate default for the offset of dynamically
1389 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1390 INCOMING_REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
1392 #ifndef STACK_DYNAMIC_OFFSET
1394 /* The bottom of the stack points to the actual arguments. If
1395 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1396 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1397 stack space for register parameters is not pushed by the caller, but
1398 rather part of the fixed stack areas and hence not included in
1399 `crtl->outgoing_args_size'. Nevertheless, we must allow
1400 for it when allocating stack dynamic objects. */
1402 #ifdef INCOMING_REG_PARM_STACK_SPACE
1403 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1404 ((ACCUMULATE_OUTGOING_ARGS \
1405 ? (crtl->outgoing_args_size \
1406 + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1407 : INCOMING_REG_PARM_STACK_SPACE (FNDECL))) \
1408 : 0) + (STACK_POINTER_OFFSET))
1409 #else
1410 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1411 ((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : poly_int64 (0)) \
1412 + (STACK_POINTER_OFFSET))
1413 #endif
1414 #endif
1417 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1418 is a virtual register, return the equivalent hard register and set the
1419 offset indirectly through the pointer. Otherwise, return 0. */
1421 static rtx
1422 instantiate_new_reg (rtx x, poly_int64_pod *poffset)
1424 rtx new_rtx;
1425 poly_int64 offset;
1427 if (x == virtual_incoming_args_rtx)
1429 if (stack_realign_drap)
1431 /* Replace virtual_incoming_args_rtx with internal arg
1432 pointer if DRAP is used to realign stack. */
1433 new_rtx = crtl->args.internal_arg_pointer;
1434 offset = 0;
1436 else
1437 new_rtx = arg_pointer_rtx, offset = in_arg_offset;
1439 else if (x == virtual_stack_vars_rtx)
1440 new_rtx = frame_pointer_rtx, offset = var_offset;
1441 else if (x == virtual_stack_dynamic_rtx)
1442 new_rtx = stack_pointer_rtx, offset = dynamic_offset;
1443 else if (x == virtual_outgoing_args_rtx)
1444 new_rtx = stack_pointer_rtx, offset = out_arg_offset;
1445 else if (x == virtual_cfa_rtx)
1447 #ifdef FRAME_POINTER_CFA_OFFSET
1448 new_rtx = frame_pointer_rtx;
1449 #else
1450 new_rtx = arg_pointer_rtx;
1451 #endif
1452 offset = cfa_offset;
1454 else if (x == virtual_preferred_stack_boundary_rtx)
1456 new_rtx = GEN_INT (crtl->preferred_stack_boundary / BITS_PER_UNIT);
1457 offset = 0;
1459 else
1460 return NULL_RTX;
1462 *poffset = offset;
1463 return new_rtx;
1466 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1467 registers present inside of *LOC. The expression is simplified,
1468 as much as possible, but is not to be considered "valid" in any sense
1469 implied by the target. Return true if any change is made. */
1471 static bool
1472 instantiate_virtual_regs_in_rtx (rtx *loc)
1474 if (!*loc)
1475 return false;
1476 bool changed = false;
1477 subrtx_ptr_iterator::array_type array;
1478 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
1480 rtx *loc = *iter;
1481 if (rtx x = *loc)
1483 rtx new_rtx;
1484 poly_int64 offset;
1485 switch (GET_CODE (x))
1487 case REG:
1488 new_rtx = instantiate_new_reg (x, &offset);
1489 if (new_rtx)
1491 *loc = plus_constant (GET_MODE (x), new_rtx, offset);
1492 changed = true;
1494 iter.skip_subrtxes ();
1495 break;
1497 case PLUS:
1498 new_rtx = instantiate_new_reg (XEXP (x, 0), &offset);
1499 if (new_rtx)
1501 XEXP (x, 0) = new_rtx;
1502 *loc = plus_constant (GET_MODE (x), x, offset, true);
1503 changed = true;
1504 iter.skip_subrtxes ();
1505 break;
1508 /* FIXME -- from old code */
1509 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1510 we can commute the PLUS and SUBREG because pointers into the
1511 frame are well-behaved. */
1512 break;
1514 default:
1515 break;
1519 return changed;
1522 /* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1523 matches the predicate for insn CODE operand OPERAND. */
1525 static int
1526 safe_insn_predicate (int code, int operand, rtx x)
1528 return code < 0 || insn_operand_matches ((enum insn_code) code, operand, x);
1531 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1532 registers present inside of insn. The result will be a valid insn. */
1534 static void
1535 instantiate_virtual_regs_in_insn (rtx_insn *insn)
1537 poly_int64 offset;
1538 int insn_code, i;
1539 bool any_change = false;
1540 rtx set, new_rtx, x;
1541 rtx_insn *seq;
1543 /* There are some special cases to be handled first. */
1544 set = single_set (insn);
1545 if (set)
1547 /* We're allowed to assign to a virtual register. This is interpreted
1548 to mean that the underlying register gets assigned the inverse
1549 transformation. This is used, for example, in the handling of
1550 non-local gotos. */
1551 new_rtx = instantiate_new_reg (SET_DEST (set), &offset);
1552 if (new_rtx)
1554 start_sequence ();
1556 instantiate_virtual_regs_in_rtx (&SET_SRC (set));
1557 x = simplify_gen_binary (PLUS, GET_MODE (new_rtx), SET_SRC (set),
1558 gen_int_mode (-offset, GET_MODE (new_rtx)));
1559 x = force_operand (x, new_rtx);
1560 if (x != new_rtx)
1561 emit_move_insn (new_rtx, x);
1563 seq = get_insns ();
1564 end_sequence ();
1566 emit_insn_before (seq, insn);
1567 delete_insn (insn);
1568 return;
1571 /* Handle a straight copy from a virtual register by generating a
1572 new add insn. The difference between this and falling through
1573 to the generic case is avoiding a new pseudo and eliminating a
1574 move insn in the initial rtl stream. */
1575 new_rtx = instantiate_new_reg (SET_SRC (set), &offset);
1576 if (new_rtx
1577 && maybe_ne (offset, 0)
1578 && REG_P (SET_DEST (set))
1579 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1581 start_sequence ();
1583 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS, new_rtx,
1584 gen_int_mode (offset,
1585 GET_MODE (SET_DEST (set))),
1586 SET_DEST (set), 1, OPTAB_LIB_WIDEN);
1587 if (x != SET_DEST (set))
1588 emit_move_insn (SET_DEST (set), x);
1590 seq = get_insns ();
1591 end_sequence ();
1593 emit_insn_before (seq, insn);
1594 delete_insn (insn);
1595 return;
1598 extract_insn (insn);
1599 insn_code = INSN_CODE (insn);
1601 /* Handle a plus involving a virtual register by determining if the
1602 operands remain valid if they're modified in place. */
1603 poly_int64 delta;
1604 if (GET_CODE (SET_SRC (set)) == PLUS
1605 && recog_data.n_operands >= 3
1606 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1607 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1608 && poly_int_rtx_p (recog_data.operand[2], &delta)
1609 && (new_rtx = instantiate_new_reg (recog_data.operand[1], &offset)))
1611 offset += delta;
1613 /* If the sum is zero, then replace with a plain move. */
1614 if (known_eq (offset, 0)
1615 && REG_P (SET_DEST (set))
1616 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1618 start_sequence ();
1619 emit_move_insn (SET_DEST (set), new_rtx);
1620 seq = get_insns ();
1621 end_sequence ();
1623 emit_insn_before (seq, insn);
1624 delete_insn (insn);
1625 return;
1628 x = gen_int_mode (offset, recog_data.operand_mode[2]);
1630 /* Using validate_change and apply_change_group here leaves
1631 recog_data in an invalid state. Since we know exactly what
1632 we want to check, do those two by hand. */
1633 if (safe_insn_predicate (insn_code, 1, new_rtx)
1634 && safe_insn_predicate (insn_code, 2, x))
1636 *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
1637 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1638 any_change = true;
1640 /* Fall through into the regular operand fixup loop in
1641 order to take care of operands other than 1 and 2. */
1645 else
1647 extract_insn (insn);
1648 insn_code = INSN_CODE (insn);
1651 /* In the general case, we expect virtual registers to appear only in
1652 operands, and then only as either bare registers or inside memories. */
1653 for (i = 0; i < recog_data.n_operands; ++i)
1655 x = recog_data.operand[i];
1656 switch (GET_CODE (x))
1658 case MEM:
1660 rtx addr = XEXP (x, 0);
1662 if (!instantiate_virtual_regs_in_rtx (&addr))
1663 continue;
1665 start_sequence ();
1666 x = replace_equiv_address (x, addr, true);
1667 /* It may happen that the address with the virtual reg
1668 was valid (e.g. based on the virtual stack reg, which might
1669 be acceptable to the predicates with all offsets), whereas
1670 the address now isn't anymore, for instance when the address
1671 is still offsetted, but the base reg isn't virtual-stack-reg
1672 anymore. Below we would do a force_reg on the whole operand,
1673 but this insn might actually only accept memory. Hence,
1674 before doing that last resort, try to reload the address into
1675 a register, so this operand stays a MEM. */
1676 if (!safe_insn_predicate (insn_code, i, x))
1678 addr = force_reg (GET_MODE (addr), addr);
1679 x = replace_equiv_address (x, addr, true);
1681 seq = get_insns ();
1682 end_sequence ();
1683 if (seq)
1684 emit_insn_before (seq, insn);
1686 break;
1688 case REG:
1689 new_rtx = instantiate_new_reg (x, &offset);
1690 if (new_rtx == NULL)
1691 continue;
1692 if (known_eq (offset, 0))
1693 x = new_rtx;
1694 else
1696 start_sequence ();
1698 /* Careful, special mode predicates may have stuff in
1699 insn_data[insn_code].operand[i].mode that isn't useful
1700 to us for computing a new value. */
1701 /* ??? Recognize address_operand and/or "p" constraints
1702 to see if (plus new offset) is a valid before we put
1703 this through expand_simple_binop. */
1704 x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
1705 gen_int_mode (offset, GET_MODE (x)),
1706 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1707 seq = get_insns ();
1708 end_sequence ();
1709 emit_insn_before (seq, insn);
1711 break;
1713 case SUBREG:
1714 new_rtx = instantiate_new_reg (SUBREG_REG (x), &offset);
1715 if (new_rtx == NULL)
1716 continue;
1717 if (maybe_ne (offset, 0))
1719 start_sequence ();
1720 new_rtx = expand_simple_binop
1721 (GET_MODE (new_rtx), PLUS, new_rtx,
1722 gen_int_mode (offset, GET_MODE (new_rtx)),
1723 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1724 seq = get_insns ();
1725 end_sequence ();
1726 emit_insn_before (seq, insn);
1728 x = simplify_gen_subreg (recog_data.operand_mode[i], new_rtx,
1729 GET_MODE (new_rtx), SUBREG_BYTE (x));
1730 gcc_assert (x);
1731 break;
1733 default:
1734 continue;
1737 /* At this point, X contains the new value for the operand.
1738 Validate the new value vs the insn predicate. Note that
1739 asm insns will have insn_code -1 here. */
1740 if (!safe_insn_predicate (insn_code, i, x))
1742 start_sequence ();
1743 if (REG_P (x))
1745 gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1746 x = copy_to_reg (x);
1748 else
1749 x = force_reg (insn_data[insn_code].operand[i].mode, x);
1750 seq = get_insns ();
1751 end_sequence ();
1752 if (seq)
1753 emit_insn_before (seq, insn);
1756 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1757 any_change = true;
1760 if (any_change)
1762 /* Propagate operand changes into the duplicates. */
1763 for (i = 0; i < recog_data.n_dups; ++i)
1764 *recog_data.dup_loc[i]
1765 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1767 /* Force re-recognition of the instruction for validation. */
1768 INSN_CODE (insn) = -1;
1771 if (asm_noperands (PATTERN (insn)) >= 0)
1773 if (!check_asm_operands (PATTERN (insn)))
1775 error_for_asm (insn, "impossible constraint in %<asm%>");
1776 /* For asm goto, instead of fixing up all the edges
1777 just clear the template and clear input operands
1778 (asm goto doesn't have any output operands). */
1779 if (JUMP_P (insn))
1781 rtx asm_op = extract_asm_operands (PATTERN (insn));
1782 ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
1783 ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
1784 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
1786 else
1787 delete_insn (insn);
1790 else
1792 if (recog_memoized (insn) < 0)
1793 fatal_insn_not_found (insn);
1797 /* Subroutine of instantiate_decls. Given RTL representing a decl,
1798 do any instantiation required. */
1800 void
1801 instantiate_decl_rtl (rtx x)
1803 rtx addr;
1805 if (x == 0)
1806 return;
1808 /* If this is a CONCAT, recurse for the pieces. */
1809 if (GET_CODE (x) == CONCAT)
1811 instantiate_decl_rtl (XEXP (x, 0));
1812 instantiate_decl_rtl (XEXP (x, 1));
1813 return;
1816 /* If this is not a MEM, no need to do anything. Similarly if the
1817 address is a constant or a register that is not a virtual register. */
1818 if (!MEM_P (x))
1819 return;
1821 addr = XEXP (x, 0);
1822 if (CONSTANT_P (addr)
1823 || (REG_P (addr)
1824 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
1825 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
1826 return;
1828 instantiate_virtual_regs_in_rtx (&XEXP (x, 0));
1831 /* Helper for instantiate_decls called via walk_tree: Process all decls
1832 in the given DECL_VALUE_EXPR. */
1834 static tree
1835 instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1837 tree t = *tp;
1838 if (! EXPR_P (t))
1840 *walk_subtrees = 0;
1841 if (DECL_P (t))
1843 if (DECL_RTL_SET_P (t))
1844 instantiate_decl_rtl (DECL_RTL (t));
1845 if (TREE_CODE (t) == PARM_DECL && DECL_NAMELESS (t)
1846 && DECL_INCOMING_RTL (t))
1847 instantiate_decl_rtl (DECL_INCOMING_RTL (t));
1848 if ((VAR_P (t) || TREE_CODE (t) == RESULT_DECL)
1849 && DECL_HAS_VALUE_EXPR_P (t))
1851 tree v = DECL_VALUE_EXPR (t);
1852 walk_tree (&v, instantiate_expr, NULL, NULL);
1856 return NULL;
1859 /* Subroutine of instantiate_decls: Process all decls in the given
1860 BLOCK node and all its subblocks. */
1862 static void
1863 instantiate_decls_1 (tree let)
1865 tree t;
1867 for (t = BLOCK_VARS (let); t; t = DECL_CHAIN (t))
1869 if (DECL_RTL_SET_P (t))
1870 instantiate_decl_rtl (DECL_RTL (t));
1871 if (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
1873 tree v = DECL_VALUE_EXPR (t);
1874 walk_tree (&v, instantiate_expr, NULL, NULL);
1878 /* Process all subblocks. */
1879 for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
1880 instantiate_decls_1 (t);
1883 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
1884 all virtual registers in their DECL_RTL's. */
1886 static void
1887 instantiate_decls (tree fndecl)
1889 tree decl;
1890 unsigned ix;
1892 /* Process all parameters of the function. */
1893 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = DECL_CHAIN (decl))
1895 instantiate_decl_rtl (DECL_RTL (decl));
1896 instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
1897 if (DECL_HAS_VALUE_EXPR_P (decl))
1899 tree v = DECL_VALUE_EXPR (decl);
1900 walk_tree (&v, instantiate_expr, NULL, NULL);
1904 if ((decl = DECL_RESULT (fndecl))
1905 && TREE_CODE (decl) == RESULT_DECL)
1907 if (DECL_RTL_SET_P (decl))
1908 instantiate_decl_rtl (DECL_RTL (decl));
1909 if (DECL_HAS_VALUE_EXPR_P (decl))
1911 tree v = DECL_VALUE_EXPR (decl);
1912 walk_tree (&v, instantiate_expr, NULL, NULL);
1916 /* Process the saved static chain if it exists. */
1917 decl = DECL_STRUCT_FUNCTION (fndecl)->static_chain_decl;
1918 if (decl && DECL_HAS_VALUE_EXPR_P (decl))
1919 instantiate_decl_rtl (DECL_RTL (DECL_VALUE_EXPR (decl)));
1921 /* Now process all variables defined in the function or its subblocks. */
1922 if (DECL_INITIAL (fndecl))
1923 instantiate_decls_1 (DECL_INITIAL (fndecl));
1925 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
1926 if (DECL_RTL_SET_P (decl))
1927 instantiate_decl_rtl (DECL_RTL (decl));
1928 vec_free (cfun->local_decls);
1931 /* Pass through the INSNS of function FNDECL and convert virtual register
1932 references to hard register references. */
1934 static unsigned int
1935 instantiate_virtual_regs (void)
1937 rtx_insn *insn;
1939 /* Compute the offsets to use for this function. */
1940 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1941 var_offset = targetm.starting_frame_offset ();
1942 dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
1943 out_arg_offset = STACK_POINTER_OFFSET;
1944 #ifdef FRAME_POINTER_CFA_OFFSET
1945 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1946 #else
1947 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1948 #endif
1950 /* Initialize recognition, indicating that volatile is OK. */
1951 init_recog ();
1953 /* Scan through all the insns, instantiating every virtual register still
1954 present. */
1955 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1956 if (INSN_P (insn))
1958 /* These patterns in the instruction stream can never be recognized.
1959 Fortunately, they shouldn't contain virtual registers either. */
1960 if (GET_CODE (PATTERN (insn)) == USE
1961 || GET_CODE (PATTERN (insn)) == CLOBBER
1962 || GET_CODE (PATTERN (insn)) == ASM_INPUT
1963 || DEBUG_MARKER_INSN_P (insn))
1964 continue;
1965 else if (DEBUG_BIND_INSN_P (insn))
1966 instantiate_virtual_regs_in_rtx (INSN_VAR_LOCATION_PTR (insn));
1967 else
1968 instantiate_virtual_regs_in_insn (insn);
1970 if (insn->deleted ())
1971 continue;
1973 instantiate_virtual_regs_in_rtx (&REG_NOTES (insn));
1975 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
1976 if (CALL_P (insn))
1977 instantiate_virtual_regs_in_rtx (&CALL_INSN_FUNCTION_USAGE (insn));
1980 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
1981 instantiate_decls (current_function_decl);
1983 targetm.instantiate_decls ();
1985 /* Indicate that, from now on, assign_stack_local should use
1986 frame_pointer_rtx. */
1987 virtuals_instantiated = 1;
1989 return 0;
1992 namespace {
1994 const pass_data pass_data_instantiate_virtual_regs =
1996 RTL_PASS, /* type */
1997 "vregs", /* name */
1998 OPTGROUP_NONE, /* optinfo_flags */
1999 TV_NONE, /* tv_id */
2000 0, /* properties_required */
2001 0, /* properties_provided */
2002 0, /* properties_destroyed */
2003 0, /* todo_flags_start */
2004 0, /* todo_flags_finish */
2007 class pass_instantiate_virtual_regs : public rtl_opt_pass
2009 public:
2010 pass_instantiate_virtual_regs (gcc::context *ctxt)
2011 : rtl_opt_pass (pass_data_instantiate_virtual_regs, ctxt)
2014 /* opt_pass methods: */
2015 virtual unsigned int execute (function *)
2017 return instantiate_virtual_regs ();
2020 }; // class pass_instantiate_virtual_regs
2022 } // anon namespace
2024 rtl_opt_pass *
2025 make_pass_instantiate_virtual_regs (gcc::context *ctxt)
2027 return new pass_instantiate_virtual_regs (ctxt);
2031 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
2032 This means a type for which function calls must pass an address to the
2033 function or get an address back from the function.
2034 EXP may be a type node or an expression (whose type is tested). */
2037 aggregate_value_p (const_tree exp, const_tree fntype)
2039 const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
2040 int i, regno, nregs;
2041 rtx reg;
2043 if (fntype)
2044 switch (TREE_CODE (fntype))
2046 case CALL_EXPR:
2048 tree fndecl = get_callee_fndecl (fntype);
2049 if (fndecl)
2050 fntype = TREE_TYPE (fndecl);
2051 else if (CALL_EXPR_FN (fntype))
2052 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype)));
2053 else
2054 /* For internal functions, assume nothing needs to be
2055 returned in memory. */
2056 return 0;
2058 break;
2059 case FUNCTION_DECL:
2060 fntype = TREE_TYPE (fntype);
2061 break;
2062 case FUNCTION_TYPE:
2063 case METHOD_TYPE:
2064 break;
2065 case IDENTIFIER_NODE:
2066 fntype = NULL_TREE;
2067 break;
2068 default:
2069 /* We don't expect other tree types here. */
2070 gcc_unreachable ();
2073 if (VOID_TYPE_P (type))
2074 return 0;
2076 /* If a record should be passed the same as its first (and only) member
2077 don't pass it as an aggregate. */
2078 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2079 return aggregate_value_p (first_field (type), fntype);
2081 /* If the front end has decided that this needs to be passed by
2082 reference, do so. */
2083 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
2084 && DECL_BY_REFERENCE (exp))
2085 return 1;
2087 /* Function types that are TREE_ADDRESSABLE force return in memory. */
2088 if (fntype && TREE_ADDRESSABLE (fntype))
2089 return 1;
2091 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
2092 and thus can't be returned in registers. */
2093 if (TREE_ADDRESSABLE (type))
2094 return 1;
2096 if (TYPE_EMPTY_P (type))
2097 return 0;
2099 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
2100 return 1;
2102 if (targetm.calls.return_in_memory (type, fntype))
2103 return 1;
2105 /* Make sure we have suitable call-clobbered regs to return
2106 the value in; if not, we must return it in memory. */
2107 reg = hard_function_value (type, 0, fntype, 0);
2109 /* If we have something other than a REG (e.g. a PARALLEL), then assume
2110 it is OK. */
2111 if (!REG_P (reg))
2112 return 0;
2114 regno = REGNO (reg);
2115 nregs = hard_regno_nregs (regno, TYPE_MODE (type));
2116 for (i = 0; i < nregs; i++)
2117 if (! call_used_regs[regno + i])
2118 return 1;
2120 return 0;
2123 /* Return true if we should assign DECL a pseudo register; false if it
2124 should live on the local stack. */
2126 bool
2127 use_register_for_decl (const_tree decl)
2129 if (TREE_CODE (decl) == SSA_NAME)
2131 /* We often try to use the SSA_NAME, instead of its underlying
2132 decl, to get type information and guide decisions, to avoid
2133 differences of behavior between anonymous and named
2134 variables, but in this one case we have to go for the actual
2135 variable if there is one. The main reason is that, at least
2136 at -O0, we want to place user variables on the stack, but we
2137 don't mind using pseudos for anonymous or ignored temps.
2138 Should we take the SSA_NAME, we'd conclude all SSA_NAMEs
2139 should go in pseudos, whereas their corresponding variables
2140 might have to go on the stack. So, disregarding the decl
2141 here would negatively impact debug info at -O0, enable
2142 coalescing between SSA_NAMEs that ought to get different
2143 stack/pseudo assignments, and get the incoming argument
2144 processing thoroughly confused by PARM_DECLs expected to live
2145 in stack slots but assigned to pseudos. */
2146 if (!SSA_NAME_VAR (decl))
2147 return TYPE_MODE (TREE_TYPE (decl)) != BLKmode
2148 && !(flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)));
2150 decl = SSA_NAME_VAR (decl);
2153 /* Honor volatile. */
2154 if (TREE_SIDE_EFFECTS (decl))
2155 return false;
2157 /* Honor addressability. */
2158 if (TREE_ADDRESSABLE (decl))
2159 return false;
2161 /* RESULT_DECLs are a bit special in that they're assigned without
2162 regard to use_register_for_decl, but we generally only store in
2163 them. If we coalesce their SSA NAMEs, we'd better return a
2164 result that matches the assignment in expand_function_start. */
2165 if (TREE_CODE (decl) == RESULT_DECL)
2167 /* If it's not an aggregate, we're going to use a REG or a
2168 PARALLEL containing a REG. */
2169 if (!aggregate_value_p (decl, current_function_decl))
2170 return true;
2172 /* If expand_function_start determines the return value, we'll
2173 use MEM if it's not by reference. */
2174 if (cfun->returns_pcc_struct
2175 || (targetm.calls.struct_value_rtx
2176 (TREE_TYPE (current_function_decl), 1)))
2177 return DECL_BY_REFERENCE (decl);
2179 /* Otherwise, we're taking an extra all.function_result_decl
2180 argument. It's set up in assign_parms_augmented_arg_list,
2181 under the (negated) conditions above, and then it's used to
2182 set up the RESULT_DECL rtl in assign_params, after looping
2183 over all parameters. Now, if the RESULT_DECL is not by
2184 reference, we'll use a MEM either way. */
2185 if (!DECL_BY_REFERENCE (decl))
2186 return false;
2188 /* Otherwise, if RESULT_DECL is DECL_BY_REFERENCE, it will take
2189 the function_result_decl's assignment. Since it's a pointer,
2190 we can short-circuit a number of the tests below, and we must
2191 duplicat e them because we don't have the
2192 function_result_decl to test. */
2193 if (!targetm.calls.allocate_stack_slots_for_args ())
2194 return true;
2195 /* We don't set DECL_IGNORED_P for the function_result_decl. */
2196 if (optimize)
2197 return true;
2198 /* We don't set DECL_REGISTER for the function_result_decl. */
2199 return false;
2202 /* Only register-like things go in registers. */
2203 if (DECL_MODE (decl) == BLKmode)
2204 return false;
2206 /* If -ffloat-store specified, don't put explicit float variables
2207 into registers. */
2208 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2209 propagates values across these stores, and it probably shouldn't. */
2210 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2211 return false;
2213 if (!targetm.calls.allocate_stack_slots_for_args ())
2214 return true;
2216 /* If we're not interested in tracking debugging information for
2217 this decl, then we can certainly put it in a register. */
2218 if (DECL_IGNORED_P (decl))
2219 return true;
2221 if (optimize)
2222 return true;
2224 if (!DECL_REGISTER (decl))
2225 return false;
2227 /* When not optimizing, disregard register keyword for types that
2228 could have methods, otherwise the methods won't be callable from
2229 the debugger. */
2230 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
2231 return false;
2233 return true;
2236 /* Structures to communicate between the subroutines of assign_parms.
2237 The first holds data persistent across all parameters, the second
2238 is cleared out for each parameter. */
2240 struct assign_parm_data_all
2242 /* When INIT_CUMULATIVE_ARGS gets revamped, allocating CUMULATIVE_ARGS
2243 should become a job of the target or otherwise encapsulated. */
2244 CUMULATIVE_ARGS args_so_far_v;
2245 cumulative_args_t args_so_far;
2246 struct args_size stack_args_size;
2247 tree function_result_decl;
2248 tree orig_fnargs;
2249 rtx_insn *first_conversion_insn;
2250 rtx_insn *last_conversion_insn;
2251 HOST_WIDE_INT pretend_args_size;
2252 HOST_WIDE_INT extra_pretend_bytes;
2253 int reg_parm_stack_space;
2256 struct assign_parm_data_one
2258 tree nominal_type;
2259 tree passed_type;
2260 rtx entry_parm;
2261 rtx stack_parm;
2262 machine_mode nominal_mode;
2263 machine_mode passed_mode;
2264 machine_mode promoted_mode;
2265 struct locate_and_pad_arg_data locate;
2266 int partial;
2267 BOOL_BITFIELD named_arg : 1;
2268 BOOL_BITFIELD passed_pointer : 1;
2269 BOOL_BITFIELD on_stack : 1;
2270 BOOL_BITFIELD loaded_in_reg : 1;
2273 /* A subroutine of assign_parms. Initialize ALL. */
2275 static void
2276 assign_parms_initialize_all (struct assign_parm_data_all *all)
2278 tree fntype ATTRIBUTE_UNUSED;
2280 memset (all, 0, sizeof (*all));
2282 fntype = TREE_TYPE (current_function_decl);
2284 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
2285 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far_v, fntype, NULL_RTX);
2286 #else
2287 INIT_CUMULATIVE_ARGS (all->args_so_far_v, fntype, NULL_RTX,
2288 current_function_decl, -1);
2289 #endif
2290 all->args_so_far = pack_cumulative_args (&all->args_so_far_v);
2292 #ifdef INCOMING_REG_PARM_STACK_SPACE
2293 all->reg_parm_stack_space
2294 = INCOMING_REG_PARM_STACK_SPACE (current_function_decl);
2295 #endif
2298 /* If ARGS contains entries with complex types, split the entry into two
2299 entries of the component type. Return a new list of substitutions are
2300 needed, else the old list. */
2302 static void
2303 split_complex_args (vec<tree> *args)
2305 unsigned i;
2306 tree p;
2308 FOR_EACH_VEC_ELT (*args, i, p)
2310 tree type = TREE_TYPE (p);
2311 if (TREE_CODE (type) == COMPLEX_TYPE
2312 && targetm.calls.split_complex_arg (type))
2314 tree decl;
2315 tree subtype = TREE_TYPE (type);
2316 bool addressable = TREE_ADDRESSABLE (p);
2318 /* Rewrite the PARM_DECL's type with its component. */
2319 p = copy_node (p);
2320 TREE_TYPE (p) = subtype;
2321 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2322 SET_DECL_MODE (p, VOIDmode);
2323 DECL_SIZE (p) = NULL;
2324 DECL_SIZE_UNIT (p) = NULL;
2325 /* If this arg must go in memory, put it in a pseudo here.
2326 We can't allow it to go in memory as per normal parms,
2327 because the usual place might not have the imag part
2328 adjacent to the real part. */
2329 DECL_ARTIFICIAL (p) = addressable;
2330 DECL_IGNORED_P (p) = addressable;
2331 TREE_ADDRESSABLE (p) = 0;
2332 layout_decl (p, 0);
2333 (*args)[i] = p;
2335 /* Build a second synthetic decl. */
2336 decl = build_decl (EXPR_LOCATION (p),
2337 PARM_DECL, NULL_TREE, subtype);
2338 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2339 DECL_ARTIFICIAL (decl) = addressable;
2340 DECL_IGNORED_P (decl) = addressable;
2341 layout_decl (decl, 0);
2342 args->safe_insert (++i, decl);
2347 /* A subroutine of assign_parms. Adjust the parameter list to incorporate
2348 the hidden struct return argument, and (abi willing) complex args.
2349 Return the new parameter list. */
2351 static vec<tree>
2352 assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2354 tree fndecl = current_function_decl;
2355 tree fntype = TREE_TYPE (fndecl);
2356 vec<tree> fnargs = vNULL;
2357 tree arg;
2359 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
2360 fnargs.safe_push (arg);
2362 all->orig_fnargs = DECL_ARGUMENTS (fndecl);
2364 /* If struct value address is treated as the first argument, make it so. */
2365 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
2366 && ! cfun->returns_pcc_struct
2367 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2369 tree type = build_pointer_type (TREE_TYPE (fntype));
2370 tree decl;
2372 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2373 PARM_DECL, get_identifier (".result_ptr"), type);
2374 DECL_ARG_TYPE (decl) = type;
2375 DECL_ARTIFICIAL (decl) = 1;
2376 DECL_NAMELESS (decl) = 1;
2377 TREE_CONSTANT (decl) = 1;
2378 /* We don't set DECL_IGNORED_P or DECL_REGISTER here. If this
2379 changes, the end of the RESULT_DECL handling block in
2380 use_register_for_decl must be adjusted to match. */
2382 DECL_CHAIN (decl) = all->orig_fnargs;
2383 all->orig_fnargs = decl;
2384 fnargs.safe_insert (0, decl);
2386 all->function_result_decl = decl;
2389 /* If the target wants to split complex arguments into scalars, do so. */
2390 if (targetm.calls.split_complex_arg)
2391 split_complex_args (&fnargs);
2393 return fnargs;
2396 /* A subroutine of assign_parms. Examine PARM and pull out type and mode
2397 data for the parameter. Incorporate ABI specifics such as pass-by-
2398 reference and type promotion. */
2400 static void
2401 assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2402 struct assign_parm_data_one *data)
2404 tree nominal_type, passed_type;
2405 machine_mode nominal_mode, passed_mode, promoted_mode;
2406 int unsignedp;
2408 memset (data, 0, sizeof (*data));
2410 /* NAMED_ARG is a misnomer. We really mean 'non-variadic'. */
2411 if (!cfun->stdarg)
2412 data->named_arg = 1; /* No variadic parms. */
2413 else if (DECL_CHAIN (parm))
2414 data->named_arg = 1; /* Not the last non-variadic parm. */
2415 else if (targetm.calls.strict_argument_naming (all->args_so_far))
2416 data->named_arg = 1; /* Only variadic ones are unnamed. */
2417 else
2418 data->named_arg = 0; /* Treat as variadic. */
2420 nominal_type = TREE_TYPE (parm);
2421 passed_type = DECL_ARG_TYPE (parm);
2423 /* Look out for errors propagating this far. Also, if the parameter's
2424 type is void then its value doesn't matter. */
2425 if (TREE_TYPE (parm) == error_mark_node
2426 /* This can happen after weird syntax errors
2427 or if an enum type is defined among the parms. */
2428 || TREE_CODE (parm) != PARM_DECL
2429 || passed_type == NULL
2430 || VOID_TYPE_P (nominal_type))
2432 nominal_type = passed_type = void_type_node;
2433 nominal_mode = passed_mode = promoted_mode = VOIDmode;
2434 goto egress;
2437 /* Find mode of arg as it is passed, and mode of arg as it should be
2438 during execution of this function. */
2439 passed_mode = TYPE_MODE (passed_type);
2440 nominal_mode = TYPE_MODE (nominal_type);
2442 /* If the parm is to be passed as a transparent union or record, use the
2443 type of the first field for the tests below. We have already verified
2444 that the modes are the same. */
2445 if ((TREE_CODE (passed_type) == UNION_TYPE
2446 || TREE_CODE (passed_type) == RECORD_TYPE)
2447 && TYPE_TRANSPARENT_AGGR (passed_type))
2448 passed_type = TREE_TYPE (first_field (passed_type));
2450 /* See if this arg was passed by invisible reference. */
2451 if (pass_by_reference (&all->args_so_far_v, passed_mode,
2452 passed_type, data->named_arg))
2454 passed_type = nominal_type = build_pointer_type (passed_type);
2455 data->passed_pointer = true;
2456 passed_mode = nominal_mode = TYPE_MODE (nominal_type);
2459 /* Find mode as it is passed by the ABI. */
2460 unsignedp = TYPE_UNSIGNED (passed_type);
2461 promoted_mode = promote_function_mode (passed_type, passed_mode, &unsignedp,
2462 TREE_TYPE (current_function_decl), 0);
2464 egress:
2465 data->nominal_type = nominal_type;
2466 data->passed_type = passed_type;
2467 data->nominal_mode = nominal_mode;
2468 data->passed_mode = passed_mode;
2469 data->promoted_mode = promoted_mode;
2472 /* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
2474 static void
2475 assign_parms_setup_varargs (struct assign_parm_data_all *all,
2476 struct assign_parm_data_one *data, bool no_rtl)
2478 int varargs_pretend_bytes = 0;
2480 targetm.calls.setup_incoming_varargs (all->args_so_far,
2481 data->promoted_mode,
2482 data->passed_type,
2483 &varargs_pretend_bytes, no_rtl);
2485 /* If the back-end has requested extra stack space, record how much is
2486 needed. Do not change pretend_args_size otherwise since it may be
2487 nonzero from an earlier partial argument. */
2488 if (varargs_pretend_bytes > 0)
2489 all->pretend_args_size = varargs_pretend_bytes;
2492 /* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2493 the incoming location of the current parameter. */
2495 static void
2496 assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2497 struct assign_parm_data_one *data)
2499 HOST_WIDE_INT pretend_bytes = 0;
2500 rtx entry_parm;
2501 bool in_regs;
2503 if (data->promoted_mode == VOIDmode)
2505 data->entry_parm = data->stack_parm = const0_rtx;
2506 return;
2509 targetm.calls.warn_parameter_passing_abi (all->args_so_far,
2510 data->passed_type);
2512 entry_parm = targetm.calls.function_incoming_arg (all->args_so_far,
2513 data->promoted_mode,
2514 data->passed_type,
2515 data->named_arg);
2517 if (entry_parm == 0)
2518 data->promoted_mode = data->passed_mode;
2520 /* Determine parm's home in the stack, in case it arrives in the stack
2521 or we should pretend it did. Compute the stack position and rtx where
2522 the argument arrives and its size.
2524 There is one complexity here: If this was a parameter that would
2525 have been passed in registers, but wasn't only because it is
2526 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2527 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2528 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2529 as it was the previous time. */
2530 in_regs = (entry_parm != 0);
2531 #ifdef STACK_PARMS_IN_REG_PARM_AREA
2532 in_regs = true;
2533 #endif
2534 if (!in_regs && !data->named_arg)
2536 if (targetm.calls.pretend_outgoing_varargs_named (all->args_so_far))
2538 rtx tem;
2539 tem = targetm.calls.function_incoming_arg (all->args_so_far,
2540 data->promoted_mode,
2541 data->passed_type, true);
2542 in_regs = tem != NULL;
2546 /* If this parameter was passed both in registers and in the stack, use
2547 the copy on the stack. */
2548 if (targetm.calls.must_pass_in_stack (data->promoted_mode,
2549 data->passed_type))
2550 entry_parm = 0;
2552 if (entry_parm)
2554 int partial;
2556 partial = targetm.calls.arg_partial_bytes (all->args_so_far,
2557 data->promoted_mode,
2558 data->passed_type,
2559 data->named_arg);
2560 data->partial = partial;
2562 /* The caller might already have allocated stack space for the
2563 register parameters. */
2564 if (partial != 0 && all->reg_parm_stack_space == 0)
2566 /* Part of this argument is passed in registers and part
2567 is passed on the stack. Ask the prologue code to extend
2568 the stack part so that we can recreate the full value.
2570 PRETEND_BYTES is the size of the registers we need to store.
2571 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2572 stack space that the prologue should allocate.
2574 Internally, gcc assumes that the argument pointer is aligned
2575 to STACK_BOUNDARY bits. This is used both for alignment
2576 optimizations (see init_emit) and to locate arguments that are
2577 aligned to more than PARM_BOUNDARY bits. We must preserve this
2578 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2579 a stack boundary. */
2581 /* We assume at most one partial arg, and it must be the first
2582 argument on the stack. */
2583 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2585 pretend_bytes = partial;
2586 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2588 /* We want to align relative to the actual stack pointer, so
2589 don't include this in the stack size until later. */
2590 all->extra_pretend_bytes = all->pretend_args_size;
2594 locate_and_pad_parm (data->promoted_mode, data->passed_type, in_regs,
2595 all->reg_parm_stack_space,
2596 entry_parm ? data->partial : 0, current_function_decl,
2597 &all->stack_args_size, &data->locate);
2599 /* Update parm_stack_boundary if this parameter is passed in the
2600 stack. */
2601 if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2602 crtl->parm_stack_boundary = data->locate.boundary;
2604 /* Adjust offsets to include the pretend args. */
2605 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2606 data->locate.slot_offset.constant += pretend_bytes;
2607 data->locate.offset.constant += pretend_bytes;
2609 data->entry_parm = entry_parm;
2612 /* A subroutine of assign_parms. If there is actually space on the stack
2613 for this parm, count it in stack_args_size and return true. */
2615 static bool
2616 assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2617 struct assign_parm_data_one *data)
2619 /* Trivially true if we've no incoming register. */
2620 if (data->entry_parm == NULL)
2622 /* Also true if we're partially in registers and partially not,
2623 since we've arranged to drop the entire argument on the stack. */
2624 else if (data->partial != 0)
2626 /* Also true if the target says that it's passed in both registers
2627 and on the stack. */
2628 else if (GET_CODE (data->entry_parm) == PARALLEL
2629 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2631 /* Also true if the target says that there's stack allocated for
2632 all register parameters. */
2633 else if (all->reg_parm_stack_space > 0)
2635 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2636 else
2637 return false;
2639 all->stack_args_size.constant += data->locate.size.constant;
2640 if (data->locate.size.var)
2641 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2643 return true;
2646 /* A subroutine of assign_parms. Given that this parameter is allocated
2647 stack space by the ABI, find it. */
2649 static void
2650 assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2652 rtx offset_rtx, stack_parm;
2653 unsigned int align, boundary;
2655 /* If we're passing this arg using a reg, make its stack home the
2656 aligned stack slot. */
2657 if (data->entry_parm)
2658 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2659 else
2660 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2662 stack_parm = crtl->args.internal_arg_pointer;
2663 if (offset_rtx != const0_rtx)
2664 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2665 stack_parm = gen_rtx_MEM (data->promoted_mode, stack_parm);
2667 if (!data->passed_pointer)
2669 set_mem_attributes (stack_parm, parm, 1);
2670 /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2671 while promoted mode's size is needed. */
2672 if (data->promoted_mode != BLKmode
2673 && data->promoted_mode != DECL_MODE (parm))
2675 set_mem_size (stack_parm, GET_MODE_SIZE (data->promoted_mode));
2676 if (MEM_EXPR (stack_parm) && MEM_OFFSET_KNOWN_P (stack_parm))
2678 poly_int64 offset = subreg_lowpart_offset (DECL_MODE (parm),
2679 data->promoted_mode);
2680 if (maybe_ne (offset, 0))
2681 set_mem_offset (stack_parm, MEM_OFFSET (stack_parm) - offset);
2686 boundary = data->locate.boundary;
2687 align = BITS_PER_UNIT;
2689 /* If we're padding upward, we know that the alignment of the slot
2690 is TARGET_FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2691 intentionally forcing upward padding. Otherwise we have to come
2692 up with a guess at the alignment based on OFFSET_RTX. */
2693 poly_int64 offset;
2694 if (data->locate.where_pad != PAD_DOWNWARD || data->entry_parm)
2695 align = boundary;
2696 else if (poly_int_rtx_p (offset_rtx, &offset))
2698 align = least_bit_hwi (boundary);
2699 unsigned int offset_align = known_alignment (offset) * BITS_PER_UNIT;
2700 if (offset_align != 0)
2701 align = MIN (align, offset_align);
2703 set_mem_align (stack_parm, align);
2705 if (data->entry_parm)
2706 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2708 data->stack_parm = stack_parm;
2711 /* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2712 always valid and contiguous. */
2714 static void
2715 assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2717 rtx entry_parm = data->entry_parm;
2718 rtx stack_parm = data->stack_parm;
2720 /* If this parm was passed part in regs and part in memory, pretend it
2721 arrived entirely in memory by pushing the register-part onto the stack.
2722 In the special case of a DImode or DFmode that is split, we could put
2723 it together in a pseudoreg directly, but for now that's not worth
2724 bothering with. */
2725 if (data->partial != 0)
2727 /* Handle calls that pass values in multiple non-contiguous
2728 locations. The Irix 6 ABI has examples of this. */
2729 if (GET_CODE (entry_parm) == PARALLEL)
2730 emit_group_store (validize_mem (copy_rtx (stack_parm)), entry_parm,
2731 data->passed_type,
2732 int_size_in_bytes (data->passed_type));
2733 else
2735 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2736 move_block_from_reg (REGNO (entry_parm),
2737 validize_mem (copy_rtx (stack_parm)),
2738 data->partial / UNITS_PER_WORD);
2741 entry_parm = stack_parm;
2744 /* If we didn't decide this parm came in a register, by default it came
2745 on the stack. */
2746 else if (entry_parm == NULL)
2747 entry_parm = stack_parm;
2749 /* When an argument is passed in multiple locations, we can't make use
2750 of this information, but we can save some copying if the whole argument
2751 is passed in a single register. */
2752 else if (GET_CODE (entry_parm) == PARALLEL
2753 && data->nominal_mode != BLKmode
2754 && data->passed_mode != BLKmode)
2756 size_t i, len = XVECLEN (entry_parm, 0);
2758 for (i = 0; i < len; i++)
2759 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2760 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2761 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2762 == data->passed_mode)
2763 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2765 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2766 break;
2770 data->entry_parm = entry_parm;
2773 /* A subroutine of assign_parms. Reconstitute any values which were
2774 passed in multiple registers and would fit in a single register. */
2776 static void
2777 assign_parm_remove_parallels (struct assign_parm_data_one *data)
2779 rtx entry_parm = data->entry_parm;
2781 /* Convert the PARALLEL to a REG of the same mode as the parallel.
2782 This can be done with register operations rather than on the
2783 stack, even if we will store the reconstituted parameter on the
2784 stack later. */
2785 if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
2787 rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
2788 emit_group_store (parmreg, entry_parm, data->passed_type,
2789 GET_MODE_SIZE (GET_MODE (entry_parm)));
2790 entry_parm = parmreg;
2793 data->entry_parm = entry_parm;
2796 /* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2797 always valid and properly aligned. */
2799 static void
2800 assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2802 rtx stack_parm = data->stack_parm;
2804 /* If we can't trust the parm stack slot to be aligned enough for its
2805 ultimate type, don't use that slot after entry. We'll make another
2806 stack slot, if we need one. */
2807 if (stack_parm
2808 && ((STRICT_ALIGNMENT
2809 && GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm))
2810 || (data->nominal_type
2811 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2812 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2813 stack_parm = NULL;
2815 /* If parm was passed in memory, and we need to convert it on entry,
2816 don't store it back in that same slot. */
2817 else if (data->entry_parm == stack_parm
2818 && data->nominal_mode != BLKmode
2819 && data->nominal_mode != data->passed_mode)
2820 stack_parm = NULL;
2822 /* If stack protection is in effect for this function, don't leave any
2823 pointers in their passed stack slots. */
2824 else if (crtl->stack_protect_guard
2825 && (flag_stack_protect == 2
2826 || data->passed_pointer
2827 || POINTER_TYPE_P (data->nominal_type)))
2828 stack_parm = NULL;
2830 data->stack_parm = stack_parm;
2833 /* A subroutine of assign_parms. Return true if the current parameter
2834 should be stored as a BLKmode in the current frame. */
2836 static bool
2837 assign_parm_setup_block_p (struct assign_parm_data_one *data)
2839 if (data->nominal_mode == BLKmode)
2840 return true;
2841 if (GET_MODE (data->entry_parm) == BLKmode)
2842 return true;
2844 #ifdef BLOCK_REG_PADDING
2845 /* Only assign_parm_setup_block knows how to deal with register arguments
2846 that are padded at the least significant end. */
2847 if (REG_P (data->entry_parm)
2848 && known_lt (GET_MODE_SIZE (data->promoted_mode), UNITS_PER_WORD)
2849 && (BLOCK_REG_PADDING (data->passed_mode, data->passed_type, 1)
2850 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
2851 return true;
2852 #endif
2854 return false;
2857 /* A subroutine of assign_parms. Arrange for the parameter to be
2858 present and valid in DATA->STACK_RTL. */
2860 static void
2861 assign_parm_setup_block (struct assign_parm_data_all *all,
2862 tree parm, struct assign_parm_data_one *data)
2864 rtx entry_parm = data->entry_parm;
2865 rtx stack_parm = data->stack_parm;
2866 rtx target_reg = NULL_RTX;
2867 bool in_conversion_seq = false;
2868 HOST_WIDE_INT size;
2869 HOST_WIDE_INT size_stored;
2871 if (GET_CODE (entry_parm) == PARALLEL)
2872 entry_parm = emit_group_move_into_temps (entry_parm);
2874 /* If we want the parameter in a pseudo, don't use a stack slot. */
2875 if (is_gimple_reg (parm) && use_register_for_decl (parm))
2877 tree def = ssa_default_def (cfun, parm);
2878 gcc_assert (def);
2879 machine_mode mode = promote_ssa_mode (def, NULL);
2880 rtx reg = gen_reg_rtx (mode);
2881 if (GET_CODE (reg) != CONCAT)
2882 stack_parm = reg;
2883 else
2885 target_reg = reg;
2886 /* Avoid allocating a stack slot, if there isn't one
2887 preallocated by the ABI. It might seem like we should
2888 always prefer a pseudo, but converting between
2889 floating-point and integer modes goes through the stack
2890 on various machines, so it's better to use the reserved
2891 stack slot than to risk wasting it and allocating more
2892 for the conversion. */
2893 if (stack_parm == NULL_RTX)
2895 int save = generating_concat_p;
2896 generating_concat_p = 0;
2897 stack_parm = gen_reg_rtx (mode);
2898 generating_concat_p = save;
2901 data->stack_parm = NULL;
2904 size = int_size_in_bytes (data->passed_type);
2905 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2906 if (stack_parm == 0)
2908 SET_DECL_ALIGN (parm, MAX (DECL_ALIGN (parm), BITS_PER_WORD));
2909 stack_parm = assign_stack_local (BLKmode, size_stored,
2910 DECL_ALIGN (parm));
2911 if (known_eq (GET_MODE_SIZE (GET_MODE (entry_parm)), size))
2912 PUT_MODE (stack_parm, GET_MODE (entry_parm));
2913 set_mem_attributes (stack_parm, parm, 1);
2916 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2917 calls that pass values in multiple non-contiguous locations. */
2918 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2920 rtx mem;
2922 /* Note that we will be storing an integral number of words.
2923 So we have to be careful to ensure that we allocate an
2924 integral number of words. We do this above when we call
2925 assign_stack_local if space was not allocated in the argument
2926 list. If it was, this will not work if PARM_BOUNDARY is not
2927 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2928 if it becomes a problem. Exception is when BLKmode arrives
2929 with arguments not conforming to word_mode. */
2931 if (data->stack_parm == 0)
2933 else if (GET_CODE (entry_parm) == PARALLEL)
2935 else
2936 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2938 mem = validize_mem (copy_rtx (stack_parm));
2940 /* Handle values in multiple non-contiguous locations. */
2941 if (GET_CODE (entry_parm) == PARALLEL && !MEM_P (mem))
2942 emit_group_store (mem, entry_parm, data->passed_type, size);
2943 else if (GET_CODE (entry_parm) == PARALLEL)
2945 push_to_sequence2 (all->first_conversion_insn,
2946 all->last_conversion_insn);
2947 emit_group_store (mem, entry_parm, data->passed_type, size);
2948 all->first_conversion_insn = get_insns ();
2949 all->last_conversion_insn = get_last_insn ();
2950 end_sequence ();
2951 in_conversion_seq = true;
2954 else if (size == 0)
2957 /* If SIZE is that of a mode no bigger than a word, just use
2958 that mode's store operation. */
2959 else if (size <= UNITS_PER_WORD)
2961 unsigned int bits = size * BITS_PER_UNIT;
2962 machine_mode mode = int_mode_for_size (bits, 0).else_blk ();
2964 if (mode != BLKmode
2965 #ifdef BLOCK_REG_PADDING
2966 && (size == UNITS_PER_WORD
2967 || (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2968 != (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
2969 #endif
2972 rtx reg;
2974 /* We are really truncating a word_mode value containing
2975 SIZE bytes into a value of mode MODE. If such an
2976 operation requires no actual instructions, we can refer
2977 to the value directly in mode MODE, otherwise we must
2978 start with the register in word_mode and explicitly
2979 convert it. */
2980 if (targetm.truly_noop_truncation (size * BITS_PER_UNIT,
2981 BITS_PER_WORD))
2982 reg = gen_rtx_REG (mode, REGNO (entry_parm));
2983 else
2985 reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2986 reg = convert_to_mode (mode, copy_to_reg (reg), 1);
2988 emit_move_insn (change_address (mem, mode, 0), reg);
2991 #ifdef BLOCK_REG_PADDING
2992 /* Storing the register in memory as a full word, as
2993 move_block_from_reg below would do, and then using the
2994 MEM in a smaller mode, has the effect of shifting right
2995 if BYTES_BIG_ENDIAN. If we're bypassing memory, the
2996 shifting must be explicit. */
2997 else if (!MEM_P (mem))
2999 rtx x;
3001 /* If the assert below fails, we should have taken the
3002 mode != BLKmode path above, unless we have downward
3003 padding of smaller-than-word arguments on a machine
3004 with little-endian bytes, which would likely require
3005 additional changes to work correctly. */
3006 gcc_checking_assert (BYTES_BIG_ENDIAN
3007 && (BLOCK_REG_PADDING (mode,
3008 data->passed_type, 1)
3009 == PAD_UPWARD));
3011 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
3013 x = gen_rtx_REG (word_mode, REGNO (entry_parm));
3014 x = expand_shift (RSHIFT_EXPR, word_mode, x, by,
3015 NULL_RTX, 1);
3016 x = force_reg (word_mode, x);
3017 x = gen_lowpart_SUBREG (GET_MODE (mem), x);
3019 emit_move_insn (mem, x);
3021 #endif
3023 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
3024 machine must be aligned to the left before storing
3025 to memory. Note that the previous test doesn't
3026 handle all cases (e.g. SIZE == 3). */
3027 else if (size != UNITS_PER_WORD
3028 #ifdef BLOCK_REG_PADDING
3029 && (BLOCK_REG_PADDING (mode, data->passed_type, 1)
3030 == PAD_DOWNWARD)
3031 #else
3032 && BYTES_BIG_ENDIAN
3033 #endif
3036 rtx tem, x;
3037 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
3038 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
3040 x = expand_shift (LSHIFT_EXPR, word_mode, reg, by, NULL_RTX, 1);
3041 tem = change_address (mem, word_mode, 0);
3042 emit_move_insn (tem, x);
3044 else
3045 move_block_from_reg (REGNO (entry_parm), mem,
3046 size_stored / UNITS_PER_WORD);
3048 else if (!MEM_P (mem))
3050 gcc_checking_assert (size > UNITS_PER_WORD);
3051 #ifdef BLOCK_REG_PADDING
3052 gcc_checking_assert (BLOCK_REG_PADDING (GET_MODE (mem),
3053 data->passed_type, 0)
3054 == PAD_UPWARD);
3055 #endif
3056 emit_move_insn (mem, entry_parm);
3058 else
3059 move_block_from_reg (REGNO (entry_parm), mem,
3060 size_stored / UNITS_PER_WORD);
3062 else if (data->stack_parm == 0)
3064 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3065 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
3066 BLOCK_OP_NORMAL);
3067 all->first_conversion_insn = get_insns ();
3068 all->last_conversion_insn = get_last_insn ();
3069 end_sequence ();
3070 in_conversion_seq = true;
3073 if (target_reg)
3075 if (!in_conversion_seq)
3076 emit_move_insn (target_reg, stack_parm);
3077 else
3079 push_to_sequence2 (all->first_conversion_insn,
3080 all->last_conversion_insn);
3081 emit_move_insn (target_reg, stack_parm);
3082 all->first_conversion_insn = get_insns ();
3083 all->last_conversion_insn = get_last_insn ();
3084 end_sequence ();
3086 stack_parm = target_reg;
3089 data->stack_parm = stack_parm;
3090 set_parm_rtl (parm, stack_parm);
3093 /* A subroutine of assign_parms. Allocate a pseudo to hold the current
3094 parameter. Get it there. Perform all ABI specified conversions. */
3096 static void
3097 assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
3098 struct assign_parm_data_one *data)
3100 rtx parmreg, validated_mem;
3101 rtx equiv_stack_parm;
3102 machine_mode promoted_nominal_mode;
3103 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
3104 bool did_conversion = false;
3105 bool need_conversion, moved;
3106 rtx rtl;
3108 /* Store the parm in a pseudoregister during the function, but we may
3109 need to do it in a wider mode. Using 2 here makes the result
3110 consistent with promote_decl_mode and thus expand_expr_real_1. */
3111 promoted_nominal_mode
3112 = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
3113 TREE_TYPE (current_function_decl), 2);
3115 parmreg = gen_reg_rtx (promoted_nominal_mode);
3116 if (!DECL_ARTIFICIAL (parm))
3117 mark_user_reg (parmreg);
3119 /* If this was an item that we received a pointer to,
3120 set rtl appropriately. */
3121 if (data->passed_pointer)
3123 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->passed_type)), parmreg);
3124 set_mem_attributes (rtl, parm, 1);
3126 else
3127 rtl = parmreg;
3129 assign_parm_remove_parallels (data);
3131 /* Copy the value into the register, thus bridging between
3132 assign_parm_find_data_types and expand_expr_real_1. */
3134 equiv_stack_parm = data->stack_parm;
3135 validated_mem = validize_mem (copy_rtx (data->entry_parm));
3137 need_conversion = (data->nominal_mode != data->passed_mode
3138 || promoted_nominal_mode != data->promoted_mode);
3139 moved = false;
3141 if (need_conversion
3142 && GET_MODE_CLASS (data->nominal_mode) == MODE_INT
3143 && data->nominal_mode == data->passed_mode
3144 && data->nominal_mode == GET_MODE (data->entry_parm))
3146 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
3147 mode, by the caller. We now have to convert it to
3148 NOMINAL_MODE, if different. However, PARMREG may be in
3149 a different mode than NOMINAL_MODE if it is being stored
3150 promoted.
3152 If ENTRY_PARM is a hard register, it might be in a register
3153 not valid for operating in its mode (e.g., an odd-numbered
3154 register for a DFmode). In that case, moves are the only
3155 thing valid, so we can't do a convert from there. This
3156 occurs when the calling sequence allow such misaligned
3157 usages.
3159 In addition, the conversion may involve a call, which could
3160 clobber parameters which haven't been copied to pseudo
3161 registers yet.
3163 First, we try to emit an insn which performs the necessary
3164 conversion. We verify that this insn does not clobber any
3165 hard registers. */
3167 enum insn_code icode;
3168 rtx op0, op1;
3170 icode = can_extend_p (promoted_nominal_mode, data->passed_mode,
3171 unsignedp);
3173 op0 = parmreg;
3174 op1 = validated_mem;
3175 if (icode != CODE_FOR_nothing
3176 && insn_operand_matches (icode, 0, op0)
3177 && insn_operand_matches (icode, 1, op1))
3179 enum rtx_code code = unsignedp ? ZERO_EXTEND : SIGN_EXTEND;
3180 rtx_insn *insn, *insns;
3181 rtx t = op1;
3182 HARD_REG_SET hardregs;
3184 start_sequence ();
3185 /* If op1 is a hard register that is likely spilled, first
3186 force it into a pseudo, otherwise combiner might extend
3187 its lifetime too much. */
3188 if (GET_CODE (t) == SUBREG)
3189 t = SUBREG_REG (t);
3190 if (REG_P (t)
3191 && HARD_REGISTER_P (t)
3192 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (t))
3193 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (t))))
3195 t = gen_reg_rtx (GET_MODE (op1));
3196 emit_move_insn (t, op1);
3198 else
3199 t = op1;
3200 rtx_insn *pat = gen_extend_insn (op0, t, promoted_nominal_mode,
3201 data->passed_mode, unsignedp);
3202 emit_insn (pat);
3203 insns = get_insns ();
3205 moved = true;
3206 CLEAR_HARD_REG_SET (hardregs);
3207 for (insn = insns; insn && moved; insn = NEXT_INSN (insn))
3209 if (INSN_P (insn))
3210 note_stores (PATTERN (insn), record_hard_reg_sets,
3211 &hardregs);
3212 if (!hard_reg_set_empty_p (hardregs))
3213 moved = false;
3216 end_sequence ();
3218 if (moved)
3220 emit_insn (insns);
3221 if (equiv_stack_parm != NULL_RTX)
3222 equiv_stack_parm = gen_rtx_fmt_e (code, GET_MODE (parmreg),
3223 equiv_stack_parm);
3228 if (moved)
3229 /* Nothing to do. */
3231 else if (need_conversion)
3233 /* We did not have an insn to convert directly, or the sequence
3234 generated appeared unsafe. We must first copy the parm to a
3235 pseudo reg, and save the conversion until after all
3236 parameters have been moved. */
3238 int save_tree_used;
3239 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3241 emit_move_insn (tempreg, validated_mem);
3243 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3244 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
3246 if (partial_subreg_p (tempreg)
3247 && GET_MODE (tempreg) == data->nominal_mode
3248 && REG_P (SUBREG_REG (tempreg))
3249 && data->nominal_mode == data->passed_mode
3250 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm))
3252 /* The argument is already sign/zero extended, so note it
3253 into the subreg. */
3254 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
3255 SUBREG_PROMOTED_SET (tempreg, unsignedp);
3258 /* TREE_USED gets set erroneously during expand_assignment. */
3259 save_tree_used = TREE_USED (parm);
3260 SET_DECL_RTL (parm, rtl);
3261 expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
3262 SET_DECL_RTL (parm, NULL_RTX);
3263 TREE_USED (parm) = save_tree_used;
3264 all->first_conversion_insn = get_insns ();
3265 all->last_conversion_insn = get_last_insn ();
3266 end_sequence ();
3268 did_conversion = true;
3270 else
3271 emit_move_insn (parmreg, validated_mem);
3273 /* If we were passed a pointer but the actual value can safely live
3274 in a register, retrieve it and use it directly. */
3275 if (data->passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode)
3277 /* We can't use nominal_mode, because it will have been set to
3278 Pmode above. We must use the actual mode of the parm. */
3279 if (use_register_for_decl (parm))
3281 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3282 mark_user_reg (parmreg);
3284 else
3286 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3287 TYPE_MODE (TREE_TYPE (parm)),
3288 TYPE_ALIGN (TREE_TYPE (parm)));
3289 parmreg
3290 = assign_stack_local (TYPE_MODE (TREE_TYPE (parm)),
3291 GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parm))),
3292 align);
3293 set_mem_attributes (parmreg, parm, 1);
3296 /* We need to preserve an address based on VIRTUAL_STACK_VARS_REGNUM for
3297 the debug info in case it is not legitimate. */
3298 if (GET_MODE (parmreg) != GET_MODE (rtl))
3300 rtx tempreg = gen_reg_rtx (GET_MODE (rtl));
3301 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
3303 push_to_sequence2 (all->first_conversion_insn,
3304 all->last_conversion_insn);
3305 emit_move_insn (tempreg, rtl);
3306 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
3307 emit_move_insn (MEM_P (parmreg) ? copy_rtx (parmreg) : parmreg,
3308 tempreg);
3309 all->first_conversion_insn = get_insns ();
3310 all->last_conversion_insn = get_last_insn ();
3311 end_sequence ();
3313 did_conversion = true;
3315 else
3316 emit_move_insn (MEM_P (parmreg) ? copy_rtx (parmreg) : parmreg, rtl);
3318 rtl = parmreg;
3320 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3321 now the parm. */
3322 data->stack_parm = NULL;
3325 set_parm_rtl (parm, rtl);
3327 /* Mark the register as eliminable if we did no conversion and it was
3328 copied from memory at a fixed offset, and the arg pointer was not
3329 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
3330 offset formed an invalid address, such memory-equivalences as we
3331 make here would screw up life analysis for it. */
3332 if (data->nominal_mode == data->passed_mode
3333 && !did_conversion
3334 && data->stack_parm != 0
3335 && MEM_P (data->stack_parm)
3336 && data->locate.offset.var == 0
3337 && reg_mentioned_p (virtual_incoming_args_rtx,
3338 XEXP (data->stack_parm, 0)))
3340 rtx_insn *linsn = get_last_insn ();
3341 rtx_insn *sinsn;
3342 rtx set;
3344 /* Mark complex types separately. */
3345 if (GET_CODE (parmreg) == CONCAT)
3347 scalar_mode submode = GET_MODE_INNER (GET_MODE (parmreg));
3348 int regnor = REGNO (XEXP (parmreg, 0));
3349 int regnoi = REGNO (XEXP (parmreg, 1));
3350 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3351 rtx stacki = adjust_address_nv (data->stack_parm, submode,
3352 GET_MODE_SIZE (submode));
3354 /* Scan backwards for the set of the real and
3355 imaginary parts. */
3356 for (sinsn = linsn; sinsn != 0;
3357 sinsn = prev_nonnote_insn (sinsn))
3359 set = single_set (sinsn);
3360 if (set == 0)
3361 continue;
3363 if (SET_DEST (set) == regno_reg_rtx [regnoi])
3364 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
3365 else if (SET_DEST (set) == regno_reg_rtx [regnor])
3366 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
3369 else
3370 set_dst_reg_note (linsn, REG_EQUIV, equiv_stack_parm, parmreg);
3373 /* For pointer data type, suggest pointer register. */
3374 if (POINTER_TYPE_P (TREE_TYPE (parm)))
3375 mark_reg_pointer (parmreg,
3376 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3379 /* A subroutine of assign_parms. Allocate stack space to hold the current
3380 parameter. Get it there. Perform all ABI specified conversions. */
3382 static void
3383 assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3384 struct assign_parm_data_one *data)
3386 /* Value must be stored in the stack slot STACK_PARM during function
3387 execution. */
3388 bool to_conversion = false;
3390 assign_parm_remove_parallels (data);
3392 if (data->promoted_mode != data->nominal_mode)
3394 /* Conversion is required. */
3395 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3397 emit_move_insn (tempreg, validize_mem (copy_rtx (data->entry_parm)));
3399 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3400 to_conversion = true;
3402 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3403 TYPE_UNSIGNED (TREE_TYPE (parm)));
3405 if (data->stack_parm)
3407 poly_int64 offset
3408 = subreg_lowpart_offset (data->nominal_mode,
3409 GET_MODE (data->stack_parm));
3410 /* ??? This may need a big-endian conversion on sparc64. */
3411 data->stack_parm
3412 = adjust_address (data->stack_parm, data->nominal_mode, 0);
3413 if (maybe_ne (offset, 0) && MEM_OFFSET_KNOWN_P (data->stack_parm))
3414 set_mem_offset (data->stack_parm,
3415 MEM_OFFSET (data->stack_parm) + offset);
3419 if (data->entry_parm != data->stack_parm)
3421 rtx src, dest;
3423 if (data->stack_parm == 0)
3425 int align = STACK_SLOT_ALIGNMENT (data->passed_type,
3426 GET_MODE (data->entry_parm),
3427 TYPE_ALIGN (data->passed_type));
3428 data->stack_parm
3429 = assign_stack_local (GET_MODE (data->entry_parm),
3430 GET_MODE_SIZE (GET_MODE (data->entry_parm)),
3431 align);
3432 set_mem_attributes (data->stack_parm, parm, 1);
3435 dest = validize_mem (copy_rtx (data->stack_parm));
3436 src = validize_mem (copy_rtx (data->entry_parm));
3438 if (MEM_P (src))
3440 /* Use a block move to handle potentially misaligned entry_parm. */
3441 if (!to_conversion)
3442 push_to_sequence2 (all->first_conversion_insn,
3443 all->last_conversion_insn);
3444 to_conversion = true;
3446 emit_block_move (dest, src,
3447 GEN_INT (int_size_in_bytes (data->passed_type)),
3448 BLOCK_OP_NORMAL);
3450 else
3452 if (!REG_P (src))
3453 src = force_reg (GET_MODE (src), src);
3454 emit_move_insn (dest, src);
3458 if (to_conversion)
3460 all->first_conversion_insn = get_insns ();
3461 all->last_conversion_insn = get_last_insn ();
3462 end_sequence ();
3465 set_parm_rtl (parm, data->stack_parm);
3468 /* A subroutine of assign_parms. If the ABI splits complex arguments, then
3469 undo the frobbing that we did in assign_parms_augmented_arg_list. */
3471 static void
3472 assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3473 vec<tree> fnargs)
3475 tree parm;
3476 tree orig_fnargs = all->orig_fnargs;
3477 unsigned i = 0;
3479 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
3481 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3482 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3484 rtx tmp, real, imag;
3485 scalar_mode inner = GET_MODE_INNER (DECL_MODE (parm));
3487 real = DECL_RTL (fnargs[i]);
3488 imag = DECL_RTL (fnargs[i + 1]);
3489 if (inner != GET_MODE (real))
3491 real = gen_lowpart_SUBREG (inner, real);
3492 imag = gen_lowpart_SUBREG (inner, imag);
3495 if (TREE_ADDRESSABLE (parm))
3497 rtx rmem, imem;
3498 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
3499 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3500 DECL_MODE (parm),
3501 TYPE_ALIGN (TREE_TYPE (parm)));
3503 /* split_complex_arg put the real and imag parts in
3504 pseudos. Move them to memory. */
3505 tmp = assign_stack_local (DECL_MODE (parm), size, align);
3506 set_mem_attributes (tmp, parm, 1);
3507 rmem = adjust_address_nv (tmp, inner, 0);
3508 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
3509 push_to_sequence2 (all->first_conversion_insn,
3510 all->last_conversion_insn);
3511 emit_move_insn (rmem, real);
3512 emit_move_insn (imem, imag);
3513 all->first_conversion_insn = get_insns ();
3514 all->last_conversion_insn = get_last_insn ();
3515 end_sequence ();
3517 else
3518 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3519 set_parm_rtl (parm, tmp);
3521 real = DECL_INCOMING_RTL (fnargs[i]);
3522 imag = DECL_INCOMING_RTL (fnargs[i + 1]);
3523 if (inner != GET_MODE (real))
3525 real = gen_lowpart_SUBREG (inner, real);
3526 imag = gen_lowpart_SUBREG (inner, imag);
3528 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3529 set_decl_incoming_rtl (parm, tmp, false);
3530 i++;
3535 /* Assign RTL expressions to the function's parameters. This may involve
3536 copying them into registers and using those registers as the DECL_RTL. */
3538 static void
3539 assign_parms (tree fndecl)
3541 struct assign_parm_data_all all;
3542 tree parm;
3543 vec<tree> fnargs;
3544 unsigned i;
3546 crtl->args.internal_arg_pointer
3547 = targetm.calls.internal_arg_pointer ();
3549 assign_parms_initialize_all (&all);
3550 fnargs = assign_parms_augmented_arg_list (&all);
3552 FOR_EACH_VEC_ELT (fnargs, i, parm)
3554 struct assign_parm_data_one data;
3556 /* Extract the type of PARM; adjust it according to ABI. */
3557 assign_parm_find_data_types (&all, parm, &data);
3559 /* Early out for errors and void parameters. */
3560 if (data.passed_mode == VOIDmode)
3562 SET_DECL_RTL (parm, const0_rtx);
3563 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3564 continue;
3567 /* Estimate stack alignment from parameter alignment. */
3568 if (SUPPORTS_STACK_ALIGNMENT)
3570 unsigned int align
3571 = targetm.calls.function_arg_boundary (data.promoted_mode,
3572 data.passed_type);
3573 align = MINIMUM_ALIGNMENT (data.passed_type, data.promoted_mode,
3574 align);
3575 if (TYPE_ALIGN (data.nominal_type) > align)
3576 align = MINIMUM_ALIGNMENT (data.nominal_type,
3577 TYPE_MODE (data.nominal_type),
3578 TYPE_ALIGN (data.nominal_type));
3579 if (crtl->stack_alignment_estimated < align)
3581 gcc_assert (!crtl->stack_realign_processed);
3582 crtl->stack_alignment_estimated = align;
3586 /* Find out where the parameter arrives in this function. */
3587 assign_parm_find_entry_rtl (&all, &data);
3589 /* Find out where stack space for this parameter might be. */
3590 if (assign_parm_is_stack_parm (&all, &data))
3592 assign_parm_find_stack_rtl (parm, &data);
3593 assign_parm_adjust_entry_rtl (&data);
3595 /* Record permanently how this parm was passed. */
3596 if (data.passed_pointer)
3598 rtx incoming_rtl
3599 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data.passed_type)),
3600 data.entry_parm);
3601 set_decl_incoming_rtl (parm, incoming_rtl, true);
3603 else
3604 set_decl_incoming_rtl (parm, data.entry_parm, false);
3606 assign_parm_adjust_stack_rtl (&data);
3608 if (assign_parm_setup_block_p (&data))
3609 assign_parm_setup_block (&all, parm, &data);
3610 else if (data.passed_pointer || use_register_for_decl (parm))
3611 assign_parm_setup_reg (&all, parm, &data);
3612 else
3613 assign_parm_setup_stack (&all, parm, &data);
3615 if (cfun->stdarg && !DECL_CHAIN (parm))
3616 assign_parms_setup_varargs (&all, &data, false);
3618 /* Update info on where next arg arrives in registers. */
3619 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3620 data.passed_type, data.named_arg);
3623 if (targetm.calls.split_complex_arg)
3624 assign_parms_unsplit_complex (&all, fnargs);
3626 fnargs.release ();
3628 /* Output all parameter conversion instructions (possibly including calls)
3629 now that all parameters have been copied out of hard registers. */
3630 emit_insn (all.first_conversion_insn);
3632 /* Estimate reload stack alignment from scalar return mode. */
3633 if (SUPPORTS_STACK_ALIGNMENT)
3635 if (DECL_RESULT (fndecl))
3637 tree type = TREE_TYPE (DECL_RESULT (fndecl));
3638 machine_mode mode = TYPE_MODE (type);
3640 if (mode != BLKmode
3641 && mode != VOIDmode
3642 && !AGGREGATE_TYPE_P (type))
3644 unsigned int align = GET_MODE_ALIGNMENT (mode);
3645 if (crtl->stack_alignment_estimated < align)
3647 gcc_assert (!crtl->stack_realign_processed);
3648 crtl->stack_alignment_estimated = align;
3654 /* If we are receiving a struct value address as the first argument, set up
3655 the RTL for the function result. As this might require code to convert
3656 the transmitted address to Pmode, we do this here to ensure that possible
3657 preliminary conversions of the address have been emitted already. */
3658 if (all.function_result_decl)
3660 tree result = DECL_RESULT (current_function_decl);
3661 rtx addr = DECL_RTL (all.function_result_decl);
3662 rtx x;
3664 if (DECL_BY_REFERENCE (result))
3666 SET_DECL_VALUE_EXPR (result, all.function_result_decl);
3667 x = addr;
3669 else
3671 SET_DECL_VALUE_EXPR (result,
3672 build1 (INDIRECT_REF, TREE_TYPE (result),
3673 all.function_result_decl));
3674 addr = convert_memory_address (Pmode, addr);
3675 x = gen_rtx_MEM (DECL_MODE (result), addr);
3676 set_mem_attributes (x, result, 1);
3679 DECL_HAS_VALUE_EXPR_P (result) = 1;
3681 set_parm_rtl (result, x);
3684 /* We have aligned all the args, so add space for the pretend args. */
3685 crtl->args.pretend_args_size = all.pretend_args_size;
3686 all.stack_args_size.constant += all.extra_pretend_bytes;
3687 crtl->args.size = all.stack_args_size.constant;
3689 /* Adjust function incoming argument size for alignment and
3690 minimum length. */
3692 crtl->args.size = upper_bound (crtl->args.size, all.reg_parm_stack_space);
3693 crtl->args.size = aligned_upper_bound (crtl->args.size,
3694 PARM_BOUNDARY / BITS_PER_UNIT);
3696 if (ARGS_GROW_DOWNWARD)
3698 crtl->args.arg_offset_rtx
3699 = (all.stack_args_size.var == 0
3700 ? gen_int_mode (-all.stack_args_size.constant, Pmode)
3701 : expand_expr (size_diffop (all.stack_args_size.var,
3702 size_int (-all.stack_args_size.constant)),
3703 NULL_RTX, VOIDmode, EXPAND_NORMAL));
3705 else
3706 crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3708 /* See how many bytes, if any, of its args a function should try to pop
3709 on return. */
3711 crtl->args.pops_args = targetm.calls.return_pops_args (fndecl,
3712 TREE_TYPE (fndecl),
3713 crtl->args.size);
3715 /* For stdarg.h function, save info about
3716 regs and stack space used by the named args. */
3718 crtl->args.info = all.args_so_far_v;
3720 /* Set the rtx used for the function return value. Put this in its
3721 own variable so any optimizers that need this information don't have
3722 to include tree.h. Do this here so it gets done when an inlined
3723 function gets output. */
3725 crtl->return_rtx
3726 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3727 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3729 /* If scalar return value was computed in a pseudo-reg, or was a named
3730 return value that got dumped to the stack, copy that to the hard
3731 return register. */
3732 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3734 tree decl_result = DECL_RESULT (fndecl);
3735 rtx decl_rtl = DECL_RTL (decl_result);
3737 if (REG_P (decl_rtl)
3738 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3739 : DECL_REGISTER (decl_result))
3741 rtx real_decl_rtl;
3743 real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3744 fndecl, true);
3745 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3746 /* The delay slot scheduler assumes that crtl->return_rtx
3747 holds the hard register containing the return value, not a
3748 temporary pseudo. */
3749 crtl->return_rtx = real_decl_rtl;
3754 /* A subroutine of gimplify_parameters, invoked via walk_tree.
3755 For all seen types, gimplify their sizes. */
3757 static tree
3758 gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3760 tree t = *tp;
3762 *walk_subtrees = 0;
3763 if (TYPE_P (t))
3765 if (POINTER_TYPE_P (t))
3766 *walk_subtrees = 1;
3767 else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3768 && !TYPE_SIZES_GIMPLIFIED (t))
3770 gimplify_type_sizes (t, (gimple_seq *) data);
3771 *walk_subtrees = 1;
3775 return NULL;
3778 /* Gimplify the parameter list for current_function_decl. This involves
3779 evaluating SAVE_EXPRs of variable sized parameters and generating code
3780 to implement callee-copies reference parameters. Returns a sequence of
3781 statements to add to the beginning of the function. */
3783 gimple_seq
3784 gimplify_parameters (gimple_seq *cleanup)
3786 struct assign_parm_data_all all;
3787 tree parm;
3788 gimple_seq stmts = NULL;
3789 vec<tree> fnargs;
3790 unsigned i;
3792 assign_parms_initialize_all (&all);
3793 fnargs = assign_parms_augmented_arg_list (&all);
3795 FOR_EACH_VEC_ELT (fnargs, i, parm)
3797 struct assign_parm_data_one data;
3799 /* Extract the type of PARM; adjust it according to ABI. */
3800 assign_parm_find_data_types (&all, parm, &data);
3802 /* Early out for errors and void parameters. */
3803 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3804 continue;
3806 /* Update info on where next arg arrives in registers. */
3807 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3808 data.passed_type, data.named_arg);
3810 /* ??? Once upon a time variable_size stuffed parameter list
3811 SAVE_EXPRs (amongst others) onto a pending sizes list. This
3812 turned out to be less than manageable in the gimple world.
3813 Now we have to hunt them down ourselves. */
3814 walk_tree_without_duplicates (&data.passed_type,
3815 gimplify_parm_type, &stmts);
3817 if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
3819 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3820 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3823 if (data.passed_pointer)
3825 tree type = TREE_TYPE (data.passed_type);
3826 if (reference_callee_copied (&all.args_so_far_v, TYPE_MODE (type),
3827 type, data.named_arg))
3829 tree local, t;
3831 /* For constant-sized objects, this is trivial; for
3832 variable-sized objects, we have to play games. */
3833 if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3834 && !(flag_stack_check == GENERIC_STACK_CHECK
3835 && compare_tree_int (DECL_SIZE_UNIT (parm),
3836 STACK_CHECK_MAX_VAR_SIZE) > 0))
3838 local = create_tmp_var (type, get_name (parm));
3839 DECL_IGNORED_P (local) = 0;
3840 /* If PARM was addressable, move that flag over
3841 to the local copy, as its address will be taken,
3842 not the PARMs. Keep the parms address taken
3843 as we'll query that flag during gimplification. */
3844 if (TREE_ADDRESSABLE (parm))
3845 TREE_ADDRESSABLE (local) = 1;
3846 else if (TREE_CODE (type) == COMPLEX_TYPE
3847 || TREE_CODE (type) == VECTOR_TYPE)
3848 DECL_GIMPLE_REG_P (local) = 1;
3850 if (!is_gimple_reg (local)
3851 && flag_stack_reuse != SR_NONE)
3853 tree clobber = build_constructor (type, NULL);
3854 gimple *clobber_stmt;
3855 TREE_THIS_VOLATILE (clobber) = 1;
3856 clobber_stmt = gimple_build_assign (local, clobber);
3857 gimple_seq_add_stmt (cleanup, clobber_stmt);
3860 else
3862 tree ptr_type, addr;
3864 ptr_type = build_pointer_type (type);
3865 addr = create_tmp_reg (ptr_type, get_name (parm));
3866 DECL_IGNORED_P (addr) = 0;
3867 local = build_fold_indirect_ref (addr);
3869 t = build_alloca_call_expr (DECL_SIZE_UNIT (parm),
3870 DECL_ALIGN (parm),
3871 max_int_size_in_bytes (type));
3872 /* The call has been built for a variable-sized object. */
3873 CALL_ALLOCA_FOR_VAR_P (t) = 1;
3874 t = fold_convert (ptr_type, t);
3875 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
3876 gimplify_and_add (t, &stmts);
3879 gimplify_assign (local, parm, &stmts);
3881 SET_DECL_VALUE_EXPR (parm, local);
3882 DECL_HAS_VALUE_EXPR_P (parm) = 1;
3887 fnargs.release ();
3889 return stmts;
3892 /* Compute the size and offset from the start of the stacked arguments for a
3893 parm passed in mode PASSED_MODE and with type TYPE.
3895 INITIAL_OFFSET_PTR points to the current offset into the stacked
3896 arguments.
3898 The starting offset and size for this parm are returned in
3899 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
3900 nonzero, the offset is that of stack slot, which is returned in
3901 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
3902 padding required from the initial offset ptr to the stack slot.
3904 IN_REGS is nonzero if the argument will be passed in registers. It will
3905 never be set if REG_PARM_STACK_SPACE is not defined.
3907 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
3908 for arguments which are passed in registers.
3910 FNDECL is the function in which the argument was defined.
3912 There are two types of rounding that are done. The first, controlled by
3913 TARGET_FUNCTION_ARG_BOUNDARY, forces the offset from the start of the
3914 argument list to be aligned to the specific boundary (in bits). This
3915 rounding affects the initial and starting offsets, but not the argument
3916 size.
3918 The second, controlled by TARGET_FUNCTION_ARG_PADDING and PARM_BOUNDARY,
3919 optionally rounds the size of the parm to PARM_BOUNDARY. The
3920 initial offset is not affected by this rounding, while the size always
3921 is and the starting offset may be. */
3923 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
3924 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
3925 callers pass in the total size of args so far as
3926 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
3928 void
3929 locate_and_pad_parm (machine_mode passed_mode, tree type, int in_regs,
3930 int reg_parm_stack_space, int partial,
3931 tree fndecl ATTRIBUTE_UNUSED,
3932 struct args_size *initial_offset_ptr,
3933 struct locate_and_pad_arg_data *locate)
3935 tree sizetree;
3936 pad_direction where_pad;
3937 unsigned int boundary, round_boundary;
3938 int part_size_in_regs;
3940 /* If we have found a stack parm before we reach the end of the
3941 area reserved for registers, skip that area. */
3942 if (! in_regs)
3944 if (reg_parm_stack_space > 0)
3946 if (initial_offset_ptr->var
3947 || !ordered_p (initial_offset_ptr->constant,
3948 reg_parm_stack_space))
3950 initial_offset_ptr->var
3951 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
3952 ssize_int (reg_parm_stack_space));
3953 initial_offset_ptr->constant = 0;
3955 else
3956 initial_offset_ptr->constant
3957 = ordered_max (initial_offset_ptr->constant,
3958 reg_parm_stack_space);
3962 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
3964 sizetree = (type
3965 ? arg_size_in_bytes (type)
3966 : size_int (GET_MODE_SIZE (passed_mode)));
3967 where_pad = targetm.calls.function_arg_padding (passed_mode, type);
3968 boundary = targetm.calls.function_arg_boundary (passed_mode, type);
3969 round_boundary = targetm.calls.function_arg_round_boundary (passed_mode,
3970 type);
3971 locate->where_pad = where_pad;
3973 /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT. */
3974 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
3975 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
3977 locate->boundary = boundary;
3979 if (SUPPORTS_STACK_ALIGNMENT)
3981 /* stack_alignment_estimated can't change after stack has been
3982 realigned. */
3983 if (crtl->stack_alignment_estimated < boundary)
3985 if (!crtl->stack_realign_processed)
3986 crtl->stack_alignment_estimated = boundary;
3987 else
3989 /* If stack is realigned and stack alignment value
3990 hasn't been finalized, it is OK not to increase
3991 stack_alignment_estimated. The bigger alignment
3992 requirement is recorded in stack_alignment_needed
3993 below. */
3994 gcc_assert (!crtl->stack_realign_finalized
3995 && crtl->stack_realign_needed);
4000 /* Remember if the outgoing parameter requires extra alignment on the
4001 calling function side. */
4002 if (crtl->stack_alignment_needed < boundary)
4003 crtl->stack_alignment_needed = boundary;
4004 if (crtl->preferred_stack_boundary < boundary)
4005 crtl->preferred_stack_boundary = boundary;
4007 if (ARGS_GROW_DOWNWARD)
4009 locate->slot_offset.constant = -initial_offset_ptr->constant;
4010 if (initial_offset_ptr->var)
4011 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
4012 initial_offset_ptr->var);
4015 tree s2 = sizetree;
4016 if (where_pad != PAD_NONE
4017 && (!tree_fits_uhwi_p (sizetree)
4018 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4019 s2 = round_up (s2, round_boundary / BITS_PER_UNIT);
4020 SUB_PARM_SIZE (locate->slot_offset, s2);
4023 locate->slot_offset.constant += part_size_in_regs;
4025 if (!in_regs || reg_parm_stack_space > 0)
4026 pad_to_arg_alignment (&locate->slot_offset, boundary,
4027 &locate->alignment_pad);
4029 locate->size.constant = (-initial_offset_ptr->constant
4030 - locate->slot_offset.constant);
4031 if (initial_offset_ptr->var)
4032 locate->size.var = size_binop (MINUS_EXPR,
4033 size_binop (MINUS_EXPR,
4034 ssize_int (0),
4035 initial_offset_ptr->var),
4036 locate->slot_offset.var);
4038 /* Pad_below needs the pre-rounded size to know how much to pad
4039 below. */
4040 locate->offset = locate->slot_offset;
4041 if (where_pad == PAD_DOWNWARD)
4042 pad_below (&locate->offset, passed_mode, sizetree);
4045 else
4047 if (!in_regs || reg_parm_stack_space > 0)
4048 pad_to_arg_alignment (initial_offset_ptr, boundary,
4049 &locate->alignment_pad);
4050 locate->slot_offset = *initial_offset_ptr;
4052 #ifdef PUSH_ROUNDING
4053 if (passed_mode != BLKmode)
4054 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
4055 #endif
4057 /* Pad_below needs the pre-rounded size to know how much to pad below
4058 so this must be done before rounding up. */
4059 locate->offset = locate->slot_offset;
4060 if (where_pad == PAD_DOWNWARD)
4061 pad_below (&locate->offset, passed_mode, sizetree);
4063 if (where_pad != PAD_NONE
4064 && (!tree_fits_uhwi_p (sizetree)
4065 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4066 sizetree = round_up (sizetree, round_boundary / BITS_PER_UNIT);
4068 ADD_PARM_SIZE (locate->size, sizetree);
4070 locate->size.constant -= part_size_in_regs;
4073 locate->offset.constant
4074 += targetm.calls.function_arg_offset (passed_mode, type);
4077 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
4078 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
4080 static void
4081 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
4082 struct args_size *alignment_pad)
4084 tree save_var = NULL_TREE;
4085 poly_int64 save_constant = 0;
4086 int boundary_in_bytes = boundary / BITS_PER_UNIT;
4087 poly_int64 sp_offset = STACK_POINTER_OFFSET;
4089 #ifdef SPARC_STACK_BOUNDARY_HACK
4090 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
4091 the real alignment of %sp. However, when it does this, the
4092 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
4093 if (SPARC_STACK_BOUNDARY_HACK)
4094 sp_offset = 0;
4095 #endif
4097 if (boundary > PARM_BOUNDARY)
4099 save_var = offset_ptr->var;
4100 save_constant = offset_ptr->constant;
4103 alignment_pad->var = NULL_TREE;
4104 alignment_pad->constant = 0;
4106 if (boundary > BITS_PER_UNIT)
4108 int misalign;
4109 if (offset_ptr->var
4110 || !known_misalignment (offset_ptr->constant + sp_offset,
4111 boundary_in_bytes, &misalign))
4113 tree sp_offset_tree = ssize_int (sp_offset);
4114 tree offset = size_binop (PLUS_EXPR,
4115 ARGS_SIZE_TREE (*offset_ptr),
4116 sp_offset_tree);
4117 tree rounded;
4118 if (ARGS_GROW_DOWNWARD)
4119 rounded = round_down (offset, boundary / BITS_PER_UNIT);
4120 else
4121 rounded = round_up (offset, boundary / BITS_PER_UNIT);
4123 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
4124 /* ARGS_SIZE_TREE includes constant term. */
4125 offset_ptr->constant = 0;
4126 if (boundary > PARM_BOUNDARY)
4127 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
4128 save_var);
4130 else
4132 if (ARGS_GROW_DOWNWARD)
4133 offset_ptr->constant -= misalign;
4134 else
4135 offset_ptr->constant += -misalign & (boundary_in_bytes - 1);
4137 if (boundary > PARM_BOUNDARY)
4138 alignment_pad->constant = offset_ptr->constant - save_constant;
4143 static void
4144 pad_below (struct args_size *offset_ptr, machine_mode passed_mode, tree sizetree)
4146 unsigned int align = PARM_BOUNDARY / BITS_PER_UNIT;
4147 int misalign;
4148 if (passed_mode != BLKmode
4149 && known_misalignment (GET_MODE_SIZE (passed_mode), align, &misalign))
4150 offset_ptr->constant += -misalign & (align - 1);
4151 else
4153 if (TREE_CODE (sizetree) != INTEGER_CST
4154 || (TREE_INT_CST_LOW (sizetree) & (align - 1)) != 0)
4156 /* Round the size up to multiple of PARM_BOUNDARY bits. */
4157 tree s2 = round_up (sizetree, align);
4158 /* Add it in. */
4159 ADD_PARM_SIZE (*offset_ptr, s2);
4160 SUB_PARM_SIZE (*offset_ptr, sizetree);
4166 /* True if register REGNO was alive at a place where `setjmp' was
4167 called and was set more than once or is an argument. Such regs may
4168 be clobbered by `longjmp'. */
4170 static bool
4171 regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
4173 /* There appear to be cases where some local vars never reach the
4174 backend but have bogus regnos. */
4175 if (regno >= max_reg_num ())
4176 return false;
4178 return ((REG_N_SETS (regno) > 1
4179 || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
4180 regno))
4181 && REGNO_REG_SET_P (setjmp_crosses, regno));
4184 /* Walk the tree of blocks describing the binding levels within a
4185 function and warn about variables the might be killed by setjmp or
4186 vfork. This is done after calling flow_analysis before register
4187 allocation since that will clobber the pseudo-regs to hard
4188 regs. */
4190 static void
4191 setjmp_vars_warning (bitmap setjmp_crosses, tree block)
4193 tree decl, sub;
4195 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
4197 if (VAR_P (decl)
4198 && DECL_RTL_SET_P (decl)
4199 && REG_P (DECL_RTL (decl))
4200 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4201 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
4202 " %<longjmp%> or %<vfork%>", decl);
4205 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
4206 setjmp_vars_warning (setjmp_crosses, sub);
4209 /* Do the appropriate part of setjmp_vars_warning
4210 but for arguments instead of local variables. */
4212 static void
4213 setjmp_args_warning (bitmap setjmp_crosses)
4215 tree decl;
4216 for (decl = DECL_ARGUMENTS (current_function_decl);
4217 decl; decl = DECL_CHAIN (decl))
4218 if (DECL_RTL (decl) != 0
4219 && REG_P (DECL_RTL (decl))
4220 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4221 warning (OPT_Wclobbered,
4222 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
4223 decl);
4226 /* Generate warning messages for variables live across setjmp. */
4228 void
4229 generate_setjmp_warnings (void)
4231 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
4233 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS
4234 || bitmap_empty_p (setjmp_crosses))
4235 return;
4237 setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
4238 setjmp_args_warning (setjmp_crosses);
4242 /* Reverse the order of elements in the fragment chain T of blocks,
4243 and return the new head of the chain (old last element).
4244 In addition to that clear BLOCK_SAME_RANGE flags when needed
4245 and adjust BLOCK_SUPERCONTEXT from the super fragment to
4246 its super fragment origin. */
4248 static tree
4249 block_fragments_nreverse (tree t)
4251 tree prev = 0, block, next, prev_super = 0;
4252 tree super = BLOCK_SUPERCONTEXT (t);
4253 if (BLOCK_FRAGMENT_ORIGIN (super))
4254 super = BLOCK_FRAGMENT_ORIGIN (super);
4255 for (block = t; block; block = next)
4257 next = BLOCK_FRAGMENT_CHAIN (block);
4258 BLOCK_FRAGMENT_CHAIN (block) = prev;
4259 if ((prev && !BLOCK_SAME_RANGE (prev))
4260 || (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (block))
4261 != prev_super))
4262 BLOCK_SAME_RANGE (block) = 0;
4263 prev_super = BLOCK_SUPERCONTEXT (block);
4264 BLOCK_SUPERCONTEXT (block) = super;
4265 prev = block;
4267 t = BLOCK_FRAGMENT_ORIGIN (t);
4268 if (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (t))
4269 != prev_super)
4270 BLOCK_SAME_RANGE (t) = 0;
4271 BLOCK_SUPERCONTEXT (t) = super;
4272 return prev;
4275 /* Reverse the order of elements in the chain T of blocks,
4276 and return the new head of the chain (old last element).
4277 Also do the same on subblocks and reverse the order of elements
4278 in BLOCK_FRAGMENT_CHAIN as well. */
4280 static tree
4281 blocks_nreverse_all (tree t)
4283 tree prev = 0, block, next;
4284 for (block = t; block; block = next)
4286 next = BLOCK_CHAIN (block);
4287 BLOCK_CHAIN (block) = prev;
4288 if (BLOCK_FRAGMENT_CHAIN (block)
4289 && BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE)
4291 BLOCK_FRAGMENT_CHAIN (block)
4292 = block_fragments_nreverse (BLOCK_FRAGMENT_CHAIN (block));
4293 if (!BLOCK_SAME_RANGE (BLOCK_FRAGMENT_CHAIN (block)))
4294 BLOCK_SAME_RANGE (block) = 0;
4296 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4297 prev = block;
4299 return prev;
4303 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
4304 and create duplicate blocks. */
4305 /* ??? Need an option to either create block fragments or to create
4306 abstract origin duplicates of a source block. It really depends
4307 on what optimization has been performed. */
4309 void
4310 reorder_blocks (void)
4312 tree block = DECL_INITIAL (current_function_decl);
4314 if (block == NULL_TREE)
4315 return;
4317 auto_vec<tree, 10> block_stack;
4319 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
4320 clear_block_marks (block);
4322 /* Prune the old trees away, so that they don't get in the way. */
4323 BLOCK_SUBBLOCKS (block) = NULL_TREE;
4324 BLOCK_CHAIN (block) = NULL_TREE;
4326 /* Recreate the block tree from the note nesting. */
4327 reorder_blocks_1 (get_insns (), block, &block_stack);
4328 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4331 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
4333 void
4334 clear_block_marks (tree block)
4336 while (block)
4338 TREE_ASM_WRITTEN (block) = 0;
4339 clear_block_marks (BLOCK_SUBBLOCKS (block));
4340 block = BLOCK_CHAIN (block);
4344 static void
4345 reorder_blocks_1 (rtx_insn *insns, tree current_block,
4346 vec<tree> *p_block_stack)
4348 rtx_insn *insn;
4349 tree prev_beg = NULL_TREE, prev_end = NULL_TREE;
4351 for (insn = insns; insn; insn = NEXT_INSN (insn))
4353 if (NOTE_P (insn))
4355 if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
4357 tree block = NOTE_BLOCK (insn);
4358 tree origin;
4360 gcc_assert (BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE);
4361 origin = block;
4363 if (prev_end)
4364 BLOCK_SAME_RANGE (prev_end) = 0;
4365 prev_end = NULL_TREE;
4367 /* If we have seen this block before, that means it now
4368 spans multiple address regions. Create a new fragment. */
4369 if (TREE_ASM_WRITTEN (block))
4371 tree new_block = copy_node (block);
4373 BLOCK_SAME_RANGE (new_block) = 0;
4374 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
4375 BLOCK_FRAGMENT_CHAIN (new_block)
4376 = BLOCK_FRAGMENT_CHAIN (origin);
4377 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
4379 NOTE_BLOCK (insn) = new_block;
4380 block = new_block;
4383 if (prev_beg == current_block && prev_beg)
4384 BLOCK_SAME_RANGE (block) = 1;
4386 prev_beg = origin;
4388 BLOCK_SUBBLOCKS (block) = 0;
4389 TREE_ASM_WRITTEN (block) = 1;
4390 /* When there's only one block for the entire function,
4391 current_block == block and we mustn't do this, it
4392 will cause infinite recursion. */
4393 if (block != current_block)
4395 tree super;
4396 if (block != origin)
4397 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block
4398 || BLOCK_FRAGMENT_ORIGIN (BLOCK_SUPERCONTEXT
4399 (origin))
4400 == current_block);
4401 if (p_block_stack->is_empty ())
4402 super = current_block;
4403 else
4405 super = p_block_stack->last ();
4406 gcc_assert (super == current_block
4407 || BLOCK_FRAGMENT_ORIGIN (super)
4408 == current_block);
4410 BLOCK_SUPERCONTEXT (block) = super;
4411 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4412 BLOCK_SUBBLOCKS (current_block) = block;
4413 current_block = origin;
4415 p_block_stack->safe_push (block);
4417 else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
4419 NOTE_BLOCK (insn) = p_block_stack->pop ();
4420 current_block = BLOCK_SUPERCONTEXT (current_block);
4421 if (BLOCK_FRAGMENT_ORIGIN (current_block))
4422 current_block = BLOCK_FRAGMENT_ORIGIN (current_block);
4423 prev_beg = NULL_TREE;
4424 prev_end = BLOCK_SAME_RANGE (NOTE_BLOCK (insn))
4425 ? NOTE_BLOCK (insn) : NULL_TREE;
4428 else
4430 prev_beg = NULL_TREE;
4431 if (prev_end)
4432 BLOCK_SAME_RANGE (prev_end) = 0;
4433 prev_end = NULL_TREE;
4438 /* Reverse the order of elements in the chain T of blocks,
4439 and return the new head of the chain (old last element). */
4441 tree
4442 blocks_nreverse (tree t)
4444 tree prev = 0, block, next;
4445 for (block = t; block; block = next)
4447 next = BLOCK_CHAIN (block);
4448 BLOCK_CHAIN (block) = prev;
4449 prev = block;
4451 return prev;
4454 /* Concatenate two chains of blocks (chained through BLOCK_CHAIN)
4455 by modifying the last node in chain 1 to point to chain 2. */
4457 tree
4458 block_chainon (tree op1, tree op2)
4460 tree t1;
4462 if (!op1)
4463 return op2;
4464 if (!op2)
4465 return op1;
4467 for (t1 = op1; BLOCK_CHAIN (t1); t1 = BLOCK_CHAIN (t1))
4468 continue;
4469 BLOCK_CHAIN (t1) = op2;
4471 #ifdef ENABLE_TREE_CHECKING
4473 tree t2;
4474 for (t2 = op2; t2; t2 = BLOCK_CHAIN (t2))
4475 gcc_assert (t2 != t1);
4477 #endif
4479 return op1;
4482 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
4483 non-NULL, list them all into VECTOR, in a depth-first preorder
4484 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
4485 blocks. */
4487 static int
4488 all_blocks (tree block, tree *vector)
4490 int n_blocks = 0;
4492 while (block)
4494 TREE_ASM_WRITTEN (block) = 0;
4496 /* Record this block. */
4497 if (vector)
4498 vector[n_blocks] = block;
4500 ++n_blocks;
4502 /* Record the subblocks, and their subblocks... */
4503 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4504 vector ? vector + n_blocks : 0);
4505 block = BLOCK_CHAIN (block);
4508 return n_blocks;
4511 /* Return a vector containing all the blocks rooted at BLOCK. The
4512 number of elements in the vector is stored in N_BLOCKS_P. The
4513 vector is dynamically allocated; it is the caller's responsibility
4514 to call `free' on the pointer returned. */
4516 static tree *
4517 get_block_vector (tree block, int *n_blocks_p)
4519 tree *block_vector;
4521 *n_blocks_p = all_blocks (block, NULL);
4522 block_vector = XNEWVEC (tree, *n_blocks_p);
4523 all_blocks (block, block_vector);
4525 return block_vector;
4528 static GTY(()) int next_block_index = 2;
4530 /* Set BLOCK_NUMBER for all the blocks in FN. */
4532 void
4533 number_blocks (tree fn)
4535 int i;
4536 int n_blocks;
4537 tree *block_vector;
4539 /* For XCOFF debugging output, we start numbering the blocks
4540 from 1 within each function, rather than keeping a running
4541 count. */
4542 #if defined (XCOFF_DEBUGGING_INFO)
4543 if (write_symbols == XCOFF_DEBUG)
4544 next_block_index = 1;
4545 #endif
4547 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4549 /* The top-level BLOCK isn't numbered at all. */
4550 for (i = 1; i < n_blocks; ++i)
4551 /* We number the blocks from two. */
4552 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4554 free (block_vector);
4556 return;
4559 /* If VAR is present in a subblock of BLOCK, return the subblock. */
4561 DEBUG_FUNCTION tree
4562 debug_find_var_in_block_tree (tree var, tree block)
4564 tree t;
4566 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4567 if (t == var)
4568 return block;
4570 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4572 tree ret = debug_find_var_in_block_tree (var, t);
4573 if (ret)
4574 return ret;
4577 return NULL_TREE;
4580 /* Keep track of whether we're in a dummy function context. If we are,
4581 we don't want to invoke the set_current_function hook, because we'll
4582 get into trouble if the hook calls target_reinit () recursively or
4583 when the initial initialization is not yet complete. */
4585 static bool in_dummy_function;
4587 /* Invoke the target hook when setting cfun. Update the optimization options
4588 if the function uses different options than the default. */
4590 static void
4591 invoke_set_current_function_hook (tree fndecl)
4593 if (!in_dummy_function)
4595 tree opts = ((fndecl)
4596 ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4597 : optimization_default_node);
4599 if (!opts)
4600 opts = optimization_default_node;
4602 /* Change optimization options if needed. */
4603 if (optimization_current_node != opts)
4605 optimization_current_node = opts;
4606 cl_optimization_restore (&global_options, TREE_OPTIMIZATION (opts));
4609 targetm.set_current_function (fndecl);
4610 this_fn_optabs = this_target_optabs;
4612 /* Initialize global alignment variables after op. */
4613 parse_alignment_opts ();
4615 if (opts != optimization_default_node)
4617 init_tree_optimization_optabs (opts);
4618 if (TREE_OPTIMIZATION_OPTABS (opts))
4619 this_fn_optabs = (struct target_optabs *)
4620 TREE_OPTIMIZATION_OPTABS (opts);
4625 /* cfun should never be set directly; use this function. */
4627 void
4628 set_cfun (struct function *new_cfun, bool force)
4630 if (cfun != new_cfun || force)
4632 cfun = new_cfun;
4633 invoke_set_current_function_hook (new_cfun ? new_cfun->decl : NULL_TREE);
4634 redirect_edge_var_map_empty ();
4638 /* Initialized with NOGC, making this poisonous to the garbage collector. */
4640 static vec<function *> cfun_stack;
4642 /* Push the current cfun onto the stack, and set cfun to new_cfun. Also set
4643 current_function_decl accordingly. */
4645 void
4646 push_cfun (struct function *new_cfun)
4648 gcc_assert ((!cfun && !current_function_decl)
4649 || (cfun && current_function_decl == cfun->decl));
4650 cfun_stack.safe_push (cfun);
4651 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4652 set_cfun (new_cfun);
4655 /* Pop cfun from the stack. Also set current_function_decl accordingly. */
4657 void
4658 pop_cfun (void)
4660 struct function *new_cfun = cfun_stack.pop ();
4661 /* When in_dummy_function, we do have a cfun but current_function_decl is
4662 NULL. We also allow pushing NULL cfun and subsequently changing
4663 current_function_decl to something else and have both restored by
4664 pop_cfun. */
4665 gcc_checking_assert (in_dummy_function
4666 || !cfun
4667 || current_function_decl == cfun->decl);
4668 set_cfun (new_cfun);
4669 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4672 /* Return value of funcdef and increase it. */
4674 get_next_funcdef_no (void)
4676 return funcdef_no++;
4679 /* Return value of funcdef. */
4681 get_last_funcdef_no (void)
4683 return funcdef_no;
4686 /* Allocate a function structure for FNDECL and set its contents
4687 to the defaults. Set cfun to the newly-allocated object.
4688 Some of the helper functions invoked during initialization assume
4689 that cfun has already been set. Therefore, assign the new object
4690 directly into cfun and invoke the back end hook explicitly at the
4691 very end, rather than initializing a temporary and calling set_cfun
4692 on it.
4694 ABSTRACT_P is true if this is a function that will never be seen by
4695 the middle-end. Such functions are front-end concepts (like C++
4696 function templates) that do not correspond directly to functions
4697 placed in object files. */
4699 void
4700 allocate_struct_function (tree fndecl, bool abstract_p)
4702 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4704 cfun = ggc_cleared_alloc<function> ();
4706 init_eh_for_function ();
4708 if (init_machine_status)
4709 cfun->machine = (*init_machine_status) ();
4711 #ifdef OVERRIDE_ABI_FORMAT
4712 OVERRIDE_ABI_FORMAT (fndecl);
4713 #endif
4715 if (fndecl != NULL_TREE)
4717 DECL_STRUCT_FUNCTION (fndecl) = cfun;
4718 cfun->decl = fndecl;
4719 current_function_funcdef_no = get_next_funcdef_no ();
4722 invoke_set_current_function_hook (fndecl);
4724 if (fndecl != NULL_TREE)
4726 tree result = DECL_RESULT (fndecl);
4728 if (!abstract_p)
4730 /* Now that we have activated any function-specific attributes
4731 that might affect layout, particularly vector modes, relayout
4732 each of the parameters and the result. */
4733 relayout_decl (result);
4734 for (tree parm = DECL_ARGUMENTS (fndecl); parm;
4735 parm = DECL_CHAIN (parm))
4736 relayout_decl (parm);
4738 /* Similarly relayout the function decl. */
4739 targetm.target_option.relayout_function (fndecl);
4742 if (!abstract_p && aggregate_value_p (result, fndecl))
4744 #ifdef PCC_STATIC_STRUCT_RETURN
4745 cfun->returns_pcc_struct = 1;
4746 #endif
4747 cfun->returns_struct = 1;
4750 cfun->stdarg = stdarg_p (fntype);
4752 /* Assume all registers in stdarg functions need to be saved. */
4753 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4754 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4756 /* ??? This could be set on a per-function basis by the front-end
4757 but is this worth the hassle? */
4758 cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4759 cfun->can_delete_dead_exceptions = flag_delete_dead_exceptions;
4761 if (!profile_flag && !flag_instrument_function_entry_exit)
4762 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl) = 1;
4765 /* Don't enable begin stmt markers if var-tracking at assignments is
4766 disabled. The markers make little sense without the variable
4767 binding annotations among them. */
4768 cfun->debug_nonbind_markers = lang_hooks.emits_begin_stmt
4769 && MAY_HAVE_DEBUG_MARKER_STMTS;
4772 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4773 instead of just setting it. */
4775 void
4776 push_struct_function (tree fndecl)
4778 /* When in_dummy_function we might be in the middle of a pop_cfun and
4779 current_function_decl and cfun may not match. */
4780 gcc_assert (in_dummy_function
4781 || (!cfun && !current_function_decl)
4782 || (cfun && current_function_decl == cfun->decl));
4783 cfun_stack.safe_push (cfun);
4784 current_function_decl = fndecl;
4785 allocate_struct_function (fndecl, false);
4788 /* Reset crtl and other non-struct-function variables to defaults as
4789 appropriate for emitting rtl at the start of a function. */
4791 static void
4792 prepare_function_start (void)
4794 gcc_assert (!get_last_insn ());
4795 init_temp_slots ();
4796 init_emit ();
4797 init_varasm_status ();
4798 init_expr ();
4799 default_rtl_profile ();
4801 if (flag_stack_usage_info)
4803 cfun->su = ggc_cleared_alloc<stack_usage> ();
4804 cfun->su->static_stack_size = -1;
4807 cse_not_expected = ! optimize;
4809 /* Caller save not needed yet. */
4810 caller_save_needed = 0;
4812 /* We haven't done register allocation yet. */
4813 reg_renumber = 0;
4815 /* Indicate that we have not instantiated virtual registers yet. */
4816 virtuals_instantiated = 0;
4818 /* Indicate that we want CONCATs now. */
4819 generating_concat_p = 1;
4821 /* Indicate we have no need of a frame pointer yet. */
4822 frame_pointer_needed = 0;
4825 void
4826 push_dummy_function (bool with_decl)
4828 tree fn_decl, fn_type, fn_result_decl;
4830 gcc_assert (!in_dummy_function);
4831 in_dummy_function = true;
4833 if (with_decl)
4835 fn_type = build_function_type_list (void_type_node, NULL_TREE);
4836 fn_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
4837 fn_type);
4838 fn_result_decl = build_decl (UNKNOWN_LOCATION, RESULT_DECL,
4839 NULL_TREE, void_type_node);
4840 DECL_RESULT (fn_decl) = fn_result_decl;
4842 else
4843 fn_decl = NULL_TREE;
4845 push_struct_function (fn_decl);
4848 /* Initialize the rtl expansion mechanism so that we can do simple things
4849 like generate sequences. This is used to provide a context during global
4850 initialization of some passes. You must call expand_dummy_function_end
4851 to exit this context. */
4853 void
4854 init_dummy_function_start (void)
4856 push_dummy_function (false);
4857 prepare_function_start ();
4860 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4861 and initialize static variables for generating RTL for the statements
4862 of the function. */
4864 void
4865 init_function_start (tree subr)
4867 /* Initialize backend, if needed. */
4868 initialize_rtl ();
4870 prepare_function_start ();
4871 decide_function_section (subr);
4873 /* Warn if this value is an aggregate type,
4874 regardless of which calling convention we are using for it. */
4875 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
4876 warning (OPT_Waggregate_return, "function returns an aggregate");
4879 /* Expand code to verify the stack_protect_guard. This is invoked at
4880 the end of a function to be protected. */
4882 void
4883 stack_protect_epilogue (void)
4885 tree guard_decl = targetm.stack_protect_guard ();
4886 rtx_code_label *label = gen_label_rtx ();
4887 rtx x, y;
4888 rtx_insn *seq;
4890 x = expand_normal (crtl->stack_protect_guard);
4891 if (guard_decl)
4892 y = expand_normal (guard_decl);
4893 else
4894 y = const0_rtx;
4896 /* Allow the target to compare Y with X without leaking either into
4897 a register. */
4898 if (targetm.have_stack_protect_test ()
4899 && ((seq = targetm.gen_stack_protect_test (x, y, label)) != NULL_RTX))
4900 emit_insn (seq);
4901 else
4902 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
4904 /* The noreturn predictor has been moved to the tree level. The rtl-level
4905 predictors estimate this branch about 20%, which isn't enough to get
4906 things moved out of line. Since this is the only extant case of adding
4907 a noreturn function at the rtl level, it doesn't seem worth doing ought
4908 except adding the prediction by hand. */
4909 rtx_insn *tmp = get_last_insn ();
4910 if (JUMP_P (tmp))
4911 predict_insn_def (tmp, PRED_NORETURN, TAKEN);
4913 expand_call (targetm.stack_protect_fail (), NULL_RTX, /*ignore=*/true);
4914 free_temp_slots ();
4915 emit_label (label);
4918 /* Start the RTL for a new function, and set variables used for
4919 emitting RTL.
4920 SUBR is the FUNCTION_DECL node.
4921 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
4922 the function's parameters, which must be run at any return statement. */
4924 void
4925 expand_function_start (tree subr)
4927 /* Make sure volatile mem refs aren't considered
4928 valid operands of arithmetic insns. */
4929 init_recog_no_volatile ();
4931 crtl->profile
4932 = (profile_flag
4933 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
4935 crtl->limit_stack
4936 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
4938 /* Make the label for return statements to jump to. Do not special
4939 case machines with special return instructions -- they will be
4940 handled later during jump, ifcvt, or epilogue creation. */
4941 return_label = gen_label_rtx ();
4943 /* Initialize rtx used to return the value. */
4944 /* Do this before assign_parms so that we copy the struct value address
4945 before any library calls that assign parms might generate. */
4947 /* Decide whether to return the value in memory or in a register. */
4948 tree res = DECL_RESULT (subr);
4949 if (aggregate_value_p (res, subr))
4951 /* Returning something that won't go in a register. */
4952 rtx value_address = 0;
4954 #ifdef PCC_STATIC_STRUCT_RETURN
4955 if (cfun->returns_pcc_struct)
4957 int size = int_size_in_bytes (TREE_TYPE (res));
4958 value_address = assemble_static_space (size);
4960 else
4961 #endif
4963 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
4964 /* Expect to be passed the address of a place to store the value.
4965 If it is passed as an argument, assign_parms will take care of
4966 it. */
4967 if (sv)
4969 value_address = gen_reg_rtx (Pmode);
4970 emit_move_insn (value_address, sv);
4973 if (value_address)
4975 rtx x = value_address;
4976 if (!DECL_BY_REFERENCE (res))
4978 x = gen_rtx_MEM (DECL_MODE (res), x);
4979 set_mem_attributes (x, res, 1);
4981 set_parm_rtl (res, x);
4984 else if (DECL_MODE (res) == VOIDmode)
4985 /* If return mode is void, this decl rtl should not be used. */
4986 set_parm_rtl (res, NULL_RTX);
4987 else
4989 /* Compute the return values into a pseudo reg, which we will copy
4990 into the true return register after the cleanups are done. */
4991 tree return_type = TREE_TYPE (res);
4993 /* If we may coalesce this result, make sure it has the expected mode
4994 in case it was promoted. But we need not bother about BLKmode. */
4995 machine_mode promoted_mode
4996 = flag_tree_coalesce_vars && is_gimple_reg (res)
4997 ? promote_ssa_mode (ssa_default_def (cfun, res), NULL)
4998 : BLKmode;
5000 if (promoted_mode != BLKmode)
5001 set_parm_rtl (res, gen_reg_rtx (promoted_mode));
5002 else if (TYPE_MODE (return_type) != BLKmode
5003 && targetm.calls.return_in_msb (return_type))
5004 /* expand_function_end will insert the appropriate padding in
5005 this case. Use the return value's natural (unpadded) mode
5006 within the function proper. */
5007 set_parm_rtl (res, gen_reg_rtx (TYPE_MODE (return_type)));
5008 else
5010 /* In order to figure out what mode to use for the pseudo, we
5011 figure out what the mode of the eventual return register will
5012 actually be, and use that. */
5013 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
5015 /* Structures that are returned in registers are not
5016 aggregate_value_p, so we may see a PARALLEL or a REG. */
5017 if (REG_P (hard_reg))
5018 set_parm_rtl (res, gen_reg_rtx (GET_MODE (hard_reg)));
5019 else
5021 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
5022 set_parm_rtl (res, gen_group_rtx (hard_reg));
5026 /* Set DECL_REGISTER flag so that expand_function_end will copy the
5027 result to the real return register(s). */
5028 DECL_REGISTER (res) = 1;
5031 /* Initialize rtx for parameters and local variables.
5032 In some cases this requires emitting insns. */
5033 assign_parms (subr);
5035 /* If function gets a static chain arg, store it. */
5036 if (cfun->static_chain_decl)
5038 tree parm = cfun->static_chain_decl;
5039 rtx local, chain;
5040 rtx_insn *insn;
5041 int unsignedp;
5043 local = gen_reg_rtx (promote_decl_mode (parm, &unsignedp));
5044 chain = targetm.calls.static_chain (current_function_decl, true);
5046 set_decl_incoming_rtl (parm, chain, false);
5047 set_parm_rtl (parm, local);
5048 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
5050 if (GET_MODE (local) != GET_MODE (chain))
5052 convert_move (local, chain, unsignedp);
5053 insn = get_last_insn ();
5055 else
5056 insn = emit_move_insn (local, chain);
5058 /* Mark the register as eliminable, similar to parameters. */
5059 if (MEM_P (chain)
5060 && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
5061 set_dst_reg_note (insn, REG_EQUIV, chain, local);
5063 /* If we aren't optimizing, save the static chain onto the stack. */
5064 if (!optimize)
5066 tree saved_static_chain_decl
5067 = build_decl (DECL_SOURCE_LOCATION (parm), VAR_DECL,
5068 DECL_NAME (parm), TREE_TYPE (parm));
5069 rtx saved_static_chain_rtx
5070 = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5071 SET_DECL_RTL (saved_static_chain_decl, saved_static_chain_rtx);
5072 emit_move_insn (saved_static_chain_rtx, chain);
5073 SET_DECL_VALUE_EXPR (parm, saved_static_chain_decl);
5074 DECL_HAS_VALUE_EXPR_P (parm) = 1;
5078 /* The following was moved from init_function_start.
5079 The move was supposed to make sdb output more accurate. */
5080 /* Indicate the beginning of the function body,
5081 as opposed to parm setup. */
5082 emit_note (NOTE_INSN_FUNCTION_BEG);
5084 gcc_assert (NOTE_P (get_last_insn ()));
5086 parm_birth_insn = get_last_insn ();
5088 /* If the function receives a non-local goto, then store the
5089 bits we need to restore the frame pointer. */
5090 if (cfun->nonlocal_goto_save_area)
5092 tree t_save;
5093 rtx r_save;
5095 tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
5096 gcc_assert (DECL_RTL_SET_P (var));
5098 t_save = build4 (ARRAY_REF,
5099 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
5100 cfun->nonlocal_goto_save_area,
5101 integer_zero_node, NULL_TREE, NULL_TREE);
5102 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
5103 gcc_assert (GET_MODE (r_save) == Pmode);
5105 emit_move_insn (r_save, targetm.builtin_setjmp_frame_value ());
5106 update_nonlocal_goto_save_area ();
5109 if (crtl->profile)
5111 #ifdef PROFILE_HOOK
5112 PROFILE_HOOK (current_function_funcdef_no);
5113 #endif
5116 /* If we are doing generic stack checking, the probe should go here. */
5117 if (flag_stack_check == GENERIC_STACK_CHECK)
5118 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
5121 void
5122 pop_dummy_function (void)
5124 pop_cfun ();
5125 in_dummy_function = false;
5128 /* Undo the effects of init_dummy_function_start. */
5129 void
5130 expand_dummy_function_end (void)
5132 gcc_assert (in_dummy_function);
5134 /* End any sequences that failed to be closed due to syntax errors. */
5135 while (in_sequence_p ())
5136 end_sequence ();
5138 /* Outside function body, can't compute type's actual size
5139 until next function's body starts. */
5141 free_after_parsing (cfun);
5142 free_after_compilation (cfun);
5143 pop_dummy_function ();
5146 /* Helper for diddle_return_value. */
5148 void
5149 diddle_return_value_1 (void (*doit) (rtx, void *), void *arg, rtx outgoing)
5151 if (! outgoing)
5152 return;
5154 if (REG_P (outgoing))
5155 (*doit) (outgoing, arg);
5156 else if (GET_CODE (outgoing) == PARALLEL)
5158 int i;
5160 for (i = 0; i < XVECLEN (outgoing, 0); i++)
5162 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
5164 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
5165 (*doit) (x, arg);
5170 /* Call DOIT for each hard register used as a return value from
5171 the current function. */
5173 void
5174 diddle_return_value (void (*doit) (rtx, void *), void *arg)
5176 diddle_return_value_1 (doit, arg, crtl->return_bnd);
5177 diddle_return_value_1 (doit, arg, crtl->return_rtx);
5180 static void
5181 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5183 emit_clobber (reg);
5186 void
5187 clobber_return_register (void)
5189 diddle_return_value (do_clobber_return_reg, NULL);
5191 /* In case we do use pseudo to return value, clobber it too. */
5192 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5194 tree decl_result = DECL_RESULT (current_function_decl);
5195 rtx decl_rtl = DECL_RTL (decl_result);
5196 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
5198 do_clobber_return_reg (decl_rtl, NULL);
5203 static void
5204 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5206 emit_use (reg);
5209 static void
5210 use_return_register (void)
5212 diddle_return_value (do_use_return_reg, NULL);
5215 /* Set the location of the insn chain starting at INSN to LOC. */
5217 static void
5218 set_insn_locations (rtx_insn *insn, int loc)
5220 while (insn != NULL)
5222 if (INSN_P (insn))
5223 INSN_LOCATION (insn) = loc;
5224 insn = NEXT_INSN (insn);
5228 /* Generate RTL for the end of the current function. */
5230 void
5231 expand_function_end (void)
5233 /* If arg_pointer_save_area was referenced only from a nested
5234 function, we will not have initialized it yet. Do that now. */
5235 if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
5236 get_arg_pointer_save_area ();
5238 /* If we are doing generic stack checking and this function makes calls,
5239 do a stack probe at the start of the function to ensure we have enough
5240 space for another stack frame. */
5241 if (flag_stack_check == GENERIC_STACK_CHECK)
5243 rtx_insn *insn, *seq;
5245 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5246 if (CALL_P (insn))
5248 rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
5249 start_sequence ();
5250 if (STACK_CHECK_MOVING_SP)
5251 anti_adjust_stack_and_probe (max_frame_size, true);
5252 else
5253 probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
5254 seq = get_insns ();
5255 end_sequence ();
5256 set_insn_locations (seq, prologue_location);
5257 emit_insn_before (seq, stack_check_probe_note);
5258 break;
5262 /* End any sequences that failed to be closed due to syntax errors. */
5263 while (in_sequence_p ())
5264 end_sequence ();
5266 clear_pending_stack_adjust ();
5267 do_pending_stack_adjust ();
5269 /* Output a linenumber for the end of the function.
5270 SDB depended on this. */
5271 set_curr_insn_location (input_location);
5273 /* Before the return label (if any), clobber the return
5274 registers so that they are not propagated live to the rest of
5275 the function. This can only happen with functions that drop
5276 through; if there had been a return statement, there would
5277 have either been a return rtx, or a jump to the return label.
5279 We delay actual code generation after the current_function_value_rtx
5280 is computed. */
5281 rtx_insn *clobber_after = get_last_insn ();
5283 /* Output the label for the actual return from the function. */
5284 emit_label (return_label);
5286 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
5288 /* Let except.c know where it should emit the call to unregister
5289 the function context for sjlj exceptions. */
5290 if (flag_exceptions)
5291 sjlj_emit_function_exit_after (get_last_insn ());
5293 else
5295 /* We want to ensure that instructions that may trap are not
5296 moved into the epilogue by scheduling, because we don't
5297 always emit unwind information for the epilogue. */
5298 if (cfun->can_throw_non_call_exceptions)
5299 emit_insn (gen_blockage ());
5302 /* If this is an implementation of throw, do what's necessary to
5303 communicate between __builtin_eh_return and the epilogue. */
5304 expand_eh_return ();
5306 /* If scalar return value was computed in a pseudo-reg, or was a named
5307 return value that got dumped to the stack, copy that to the hard
5308 return register. */
5309 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5311 tree decl_result = DECL_RESULT (current_function_decl);
5312 rtx decl_rtl = DECL_RTL (decl_result);
5314 if (REG_P (decl_rtl)
5315 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5316 : DECL_REGISTER (decl_result))
5318 rtx real_decl_rtl = crtl->return_rtx;
5319 complex_mode cmode;
5321 /* This should be set in assign_parms. */
5322 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
5324 /* If this is a BLKmode structure being returned in registers,
5325 then use the mode computed in expand_return. Note that if
5326 decl_rtl is memory, then its mode may have been changed,
5327 but that crtl->return_rtx has not. */
5328 if (GET_MODE (real_decl_rtl) == BLKmode)
5329 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
5331 /* If a non-BLKmode return value should be padded at the least
5332 significant end of the register, shift it left by the appropriate
5333 amount. BLKmode results are handled using the group load/store
5334 machinery. */
5335 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
5336 && REG_P (real_decl_rtl)
5337 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
5339 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
5340 REGNO (real_decl_rtl)),
5341 decl_rtl);
5342 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
5344 else if (GET_CODE (real_decl_rtl) == PARALLEL)
5346 /* If expand_function_start has created a PARALLEL for decl_rtl,
5347 move the result to the real return registers. Otherwise, do
5348 a group load from decl_rtl for a named return. */
5349 if (GET_CODE (decl_rtl) == PARALLEL)
5350 emit_group_move (real_decl_rtl, decl_rtl);
5351 else
5352 emit_group_load (real_decl_rtl, decl_rtl,
5353 TREE_TYPE (decl_result),
5354 int_size_in_bytes (TREE_TYPE (decl_result)));
5356 /* In the case of complex integer modes smaller than a word, we'll
5357 need to generate some non-trivial bitfield insertions. Do that
5358 on a pseudo and not the hard register. */
5359 else if (GET_CODE (decl_rtl) == CONCAT
5360 && is_complex_int_mode (GET_MODE (decl_rtl), &cmode)
5361 && GET_MODE_BITSIZE (cmode) <= BITS_PER_WORD)
5363 int old_generating_concat_p;
5364 rtx tmp;
5366 old_generating_concat_p = generating_concat_p;
5367 generating_concat_p = 0;
5368 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
5369 generating_concat_p = old_generating_concat_p;
5371 emit_move_insn (tmp, decl_rtl);
5372 emit_move_insn (real_decl_rtl, tmp);
5374 /* If a named return value dumped decl_return to memory, then
5375 we may need to re-do the PROMOTE_MODE signed/unsigned
5376 extension. */
5377 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
5379 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
5380 promote_function_mode (TREE_TYPE (decl_result),
5381 GET_MODE (decl_rtl), &unsignedp,
5382 TREE_TYPE (current_function_decl), 1);
5384 convert_move (real_decl_rtl, decl_rtl, unsignedp);
5386 else
5387 emit_move_insn (real_decl_rtl, decl_rtl);
5391 /* If returning a structure, arrange to return the address of the value
5392 in a place where debuggers expect to find it.
5394 If returning a structure PCC style,
5395 the caller also depends on this value.
5396 And cfun->returns_pcc_struct is not necessarily set. */
5397 if ((cfun->returns_struct || cfun->returns_pcc_struct)
5398 && !targetm.calls.omit_struct_return_reg)
5400 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
5401 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5402 rtx outgoing;
5404 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
5405 type = TREE_TYPE (type);
5406 else
5407 value_address = XEXP (value_address, 0);
5409 outgoing = targetm.calls.function_value (build_pointer_type (type),
5410 current_function_decl, true);
5412 /* Mark this as a function return value so integrate will delete the
5413 assignment and USE below when inlining this function. */
5414 REG_FUNCTION_VALUE_P (outgoing) = 1;
5416 /* The address may be ptr_mode and OUTGOING may be Pmode. */
5417 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (outgoing));
5418 value_address = convert_memory_address (mode, value_address);
5420 emit_move_insn (outgoing, value_address);
5422 /* Show return register used to hold result (in this case the address
5423 of the result. */
5424 crtl->return_rtx = outgoing;
5427 /* Emit the actual code to clobber return register. Don't emit
5428 it if clobber_after is a barrier, then the previous basic block
5429 certainly doesn't fall thru into the exit block. */
5430 if (!BARRIER_P (clobber_after))
5432 start_sequence ();
5433 clobber_return_register ();
5434 rtx_insn *seq = get_insns ();
5435 end_sequence ();
5437 emit_insn_after (seq, clobber_after);
5440 /* Output the label for the naked return from the function. */
5441 if (naked_return_label)
5442 emit_label (naked_return_label);
5444 /* @@@ This is a kludge. We want to ensure that instructions that
5445 may trap are not moved into the epilogue by scheduling, because
5446 we don't always emit unwind information for the epilogue. */
5447 if (cfun->can_throw_non_call_exceptions
5448 && targetm_common.except_unwind_info (&global_options) != UI_SJLJ)
5449 emit_insn (gen_blockage ());
5451 /* If stack protection is enabled for this function, check the guard. */
5452 if (crtl->stack_protect_guard && targetm.stack_protect_runtime_enabled_p ())
5453 stack_protect_epilogue ();
5455 /* If we had calls to alloca, and this machine needs
5456 an accurate stack pointer to exit the function,
5457 insert some code to save and restore the stack pointer. */
5458 if (! EXIT_IGNORE_STACK
5459 && cfun->calls_alloca)
5461 rtx tem = 0;
5463 start_sequence ();
5464 emit_stack_save (SAVE_FUNCTION, &tem);
5465 rtx_insn *seq = get_insns ();
5466 end_sequence ();
5467 emit_insn_before (seq, parm_birth_insn);
5469 emit_stack_restore (SAVE_FUNCTION, tem);
5472 /* ??? This should no longer be necessary since stupid is no longer with
5473 us, but there are some parts of the compiler (eg reload_combine, and
5474 sh mach_dep_reorg) that still try and compute their own lifetime info
5475 instead of using the general framework. */
5476 use_return_register ();
5480 get_arg_pointer_save_area (void)
5482 rtx ret = arg_pointer_save_area;
5484 if (! ret)
5486 ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5487 arg_pointer_save_area = ret;
5490 if (! crtl->arg_pointer_save_area_init)
5492 /* Save the arg pointer at the beginning of the function. The
5493 generated stack slot may not be a valid memory address, so we
5494 have to check it and fix it if necessary. */
5495 start_sequence ();
5496 emit_move_insn (validize_mem (copy_rtx (ret)),
5497 crtl->args.internal_arg_pointer);
5498 rtx_insn *seq = get_insns ();
5499 end_sequence ();
5501 push_topmost_sequence ();
5502 emit_insn_after (seq, entry_of_function ());
5503 pop_topmost_sequence ();
5505 crtl->arg_pointer_save_area_init = true;
5508 return ret;
5512 /* If debugging dumps are requested, dump information about how the
5513 target handled -fstack-check=clash for the prologue.
5515 PROBES describes what if any probes were emitted.
5517 RESIDUALS indicates if the prologue had any residual allocation
5518 (i.e. total allocation was not a multiple of PROBE_INTERVAL). */
5520 void
5521 dump_stack_clash_frame_info (enum stack_clash_probes probes, bool residuals)
5523 if (!dump_file)
5524 return;
5526 switch (probes)
5528 case NO_PROBE_NO_FRAME:
5529 fprintf (dump_file,
5530 "Stack clash no probe no stack adjustment in prologue.\n");
5531 break;
5532 case NO_PROBE_SMALL_FRAME:
5533 fprintf (dump_file,
5534 "Stack clash no probe small stack adjustment in prologue.\n");
5535 break;
5536 case PROBE_INLINE:
5537 fprintf (dump_file, "Stack clash inline probes in prologue.\n");
5538 break;
5539 case PROBE_LOOP:
5540 fprintf (dump_file, "Stack clash probe loop in prologue.\n");
5541 break;
5544 if (residuals)
5545 fprintf (dump_file, "Stack clash residual allocation in prologue.\n");
5546 else
5547 fprintf (dump_file, "Stack clash no residual allocation in prologue.\n");
5549 if (frame_pointer_needed)
5550 fprintf (dump_file, "Stack clash frame pointer needed.\n");
5551 else
5552 fprintf (dump_file, "Stack clash no frame pointer needed.\n");
5554 if (TREE_THIS_VOLATILE (cfun->decl))
5555 fprintf (dump_file,
5556 "Stack clash noreturn prologue, assuming no implicit"
5557 " probes in caller.\n");
5558 else
5559 fprintf (dump_file,
5560 "Stack clash not noreturn prologue.\n");
5563 /* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
5564 for the first time. */
5566 static void
5567 record_insns (rtx_insn *insns, rtx end, hash_table<insn_cache_hasher> **hashp)
5569 rtx_insn *tmp;
5570 hash_table<insn_cache_hasher> *hash = *hashp;
5572 if (hash == NULL)
5573 *hashp = hash = hash_table<insn_cache_hasher>::create_ggc (17);
5575 for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5577 rtx *slot = hash->find_slot (tmp, INSERT);
5578 gcc_assert (*slot == NULL);
5579 *slot = tmp;
5583 /* INSN has been duplicated or replaced by as COPY, perhaps by duplicating a
5584 basic block, splitting or peepholes. If INSN is a prologue or epilogue
5585 insn, then record COPY as well. */
5587 void
5588 maybe_copy_prologue_epilogue_insn (rtx insn, rtx copy)
5590 hash_table<insn_cache_hasher> *hash;
5591 rtx *slot;
5593 hash = epilogue_insn_hash;
5594 if (!hash || !hash->find (insn))
5596 hash = prologue_insn_hash;
5597 if (!hash || !hash->find (insn))
5598 return;
5601 slot = hash->find_slot (copy, INSERT);
5602 gcc_assert (*slot == NULL);
5603 *slot = copy;
5606 /* Determine if any INSNs in HASH are, or are part of, INSN. Because
5607 we can be running after reorg, SEQUENCE rtl is possible. */
5609 static bool
5610 contains (const rtx_insn *insn, hash_table<insn_cache_hasher> *hash)
5612 if (hash == NULL)
5613 return false;
5615 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5617 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
5618 int i;
5619 for (i = seq->len () - 1; i >= 0; i--)
5620 if (hash->find (seq->element (i)))
5621 return true;
5622 return false;
5625 return hash->find (const_cast<rtx_insn *> (insn)) != NULL;
5629 prologue_contains (const rtx_insn *insn)
5631 return contains (insn, prologue_insn_hash);
5635 epilogue_contains (const rtx_insn *insn)
5637 return contains (insn, epilogue_insn_hash);
5641 prologue_epilogue_contains (const rtx_insn *insn)
5643 if (contains (insn, prologue_insn_hash))
5644 return 1;
5645 if (contains (insn, epilogue_insn_hash))
5646 return 1;
5647 return 0;
5650 void
5651 record_prologue_seq (rtx_insn *seq)
5653 record_insns (seq, NULL, &prologue_insn_hash);
5656 void
5657 record_epilogue_seq (rtx_insn *seq)
5659 record_insns (seq, NULL, &epilogue_insn_hash);
5662 /* Set JUMP_LABEL for a return insn. */
5664 void
5665 set_return_jump_label (rtx_insn *returnjump)
5667 rtx pat = PATTERN (returnjump);
5668 if (GET_CODE (pat) == PARALLEL)
5669 pat = XVECEXP (pat, 0, 0);
5670 if (ANY_RETURN_P (pat))
5671 JUMP_LABEL (returnjump) = pat;
5672 else
5673 JUMP_LABEL (returnjump) = ret_rtx;
5676 /* Return a sequence to be used as the split prologue for the current
5677 function, or NULL. */
5679 static rtx_insn *
5680 make_split_prologue_seq (void)
5682 if (!flag_split_stack
5683 || lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl)))
5684 return NULL;
5686 start_sequence ();
5687 emit_insn (targetm.gen_split_stack_prologue ());
5688 rtx_insn *seq = get_insns ();
5689 end_sequence ();
5691 record_insns (seq, NULL, &prologue_insn_hash);
5692 set_insn_locations (seq, prologue_location);
5694 return seq;
5697 /* Return a sequence to be used as the prologue for the current function,
5698 or NULL. */
5700 static rtx_insn *
5701 make_prologue_seq (void)
5703 if (!targetm.have_prologue ())
5704 return NULL;
5706 start_sequence ();
5707 rtx_insn *seq = targetm.gen_prologue ();
5708 emit_insn (seq);
5710 /* Insert an explicit USE for the frame pointer
5711 if the profiling is on and the frame pointer is required. */
5712 if (crtl->profile && frame_pointer_needed)
5713 emit_use (hard_frame_pointer_rtx);
5715 /* Retain a map of the prologue insns. */
5716 record_insns (seq, NULL, &prologue_insn_hash);
5717 emit_note (NOTE_INSN_PROLOGUE_END);
5719 /* Ensure that instructions are not moved into the prologue when
5720 profiling is on. The call to the profiling routine can be
5721 emitted within the live range of a call-clobbered register. */
5722 if (!targetm.profile_before_prologue () && crtl->profile)
5723 emit_insn (gen_blockage ());
5725 seq = get_insns ();
5726 end_sequence ();
5727 set_insn_locations (seq, prologue_location);
5729 return seq;
5732 /* Return a sequence to be used as the epilogue for the current function,
5733 or NULL. */
5735 static rtx_insn *
5736 make_epilogue_seq (void)
5738 if (!targetm.have_epilogue ())
5739 return NULL;
5741 start_sequence ();
5742 emit_note (NOTE_INSN_EPILOGUE_BEG);
5743 rtx_insn *seq = targetm.gen_epilogue ();
5744 if (seq)
5745 emit_jump_insn (seq);
5747 /* Retain a map of the epilogue insns. */
5748 record_insns (seq, NULL, &epilogue_insn_hash);
5749 set_insn_locations (seq, epilogue_location);
5751 seq = get_insns ();
5752 rtx_insn *returnjump = get_last_insn ();
5753 end_sequence ();
5755 if (JUMP_P (returnjump))
5756 set_return_jump_label (returnjump);
5758 return seq;
5762 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
5763 this into place with notes indicating where the prologue ends and where
5764 the epilogue begins. Update the basic block information when possible.
5766 Notes on epilogue placement:
5767 There are several kinds of edges to the exit block:
5768 * a single fallthru edge from LAST_BB
5769 * possibly, edges from blocks containing sibcalls
5770 * possibly, fake edges from infinite loops
5772 The epilogue is always emitted on the fallthru edge from the last basic
5773 block in the function, LAST_BB, into the exit block.
5775 If LAST_BB is empty except for a label, it is the target of every
5776 other basic block in the function that ends in a return. If a
5777 target has a return or simple_return pattern (possibly with
5778 conditional variants), these basic blocks can be changed so that a
5779 return insn is emitted into them, and their target is adjusted to
5780 the real exit block.
5782 Notes on shrink wrapping: We implement a fairly conservative
5783 version of shrink-wrapping rather than the textbook one. We only
5784 generate a single prologue and a single epilogue. This is
5785 sufficient to catch a number of interesting cases involving early
5786 exits.
5788 First, we identify the blocks that require the prologue to occur before
5789 them. These are the ones that modify a call-saved register, or reference
5790 any of the stack or frame pointer registers. To simplify things, we then
5791 mark everything reachable from these blocks as also requiring a prologue.
5792 This takes care of loops automatically, and avoids the need to examine
5793 whether MEMs reference the frame, since it is sufficient to check for
5794 occurrences of the stack or frame pointer.
5796 We then compute the set of blocks for which the need for a prologue
5797 is anticipatable (borrowing terminology from the shrink-wrapping
5798 description in Muchnick's book). These are the blocks which either
5799 require a prologue themselves, or those that have only successors
5800 where the prologue is anticipatable. The prologue needs to be
5801 inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
5802 is not. For the moment, we ensure that only one such edge exists.
5804 The epilogue is placed as described above, but we make a
5805 distinction between inserting return and simple_return patterns
5806 when modifying other blocks that end in a return. Blocks that end
5807 in a sibcall omit the sibcall_epilogue if the block is not in
5808 ANTIC. */
5810 void
5811 thread_prologue_and_epilogue_insns (void)
5813 df_analyze ();
5815 /* Can't deal with multiple successors of the entry block at the
5816 moment. Function should always have at least one entry
5817 point. */
5818 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5820 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5821 edge orig_entry_edge = entry_edge;
5823 rtx_insn *split_prologue_seq = make_split_prologue_seq ();
5824 rtx_insn *prologue_seq = make_prologue_seq ();
5825 rtx_insn *epilogue_seq = make_epilogue_seq ();
5827 /* Try to perform a kind of shrink-wrapping, making sure the
5828 prologue/epilogue is emitted only around those parts of the
5829 function that require it. */
5830 try_shrink_wrapping (&entry_edge, prologue_seq);
5832 /* If the target can handle splitting the prologue/epilogue into separate
5833 components, try to shrink-wrap these components separately. */
5834 try_shrink_wrapping_separate (entry_edge->dest);
5836 /* If that did anything for any component we now need the generate the
5837 "main" prologue again. Because some targets require some of these
5838 to be called in a specific order (i386 requires the split prologue
5839 to be first, for example), we create all three sequences again here.
5840 If this does not work for some target, that target should not enable
5841 separate shrink-wrapping. */
5842 if (crtl->shrink_wrapped_separate)
5844 split_prologue_seq = make_split_prologue_seq ();
5845 prologue_seq = make_prologue_seq ();
5846 epilogue_seq = make_epilogue_seq ();
5849 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5851 /* A small fib -- epilogue is not yet completed, but we wish to re-use
5852 this marker for the splits of EH_RETURN patterns, and nothing else
5853 uses the flag in the meantime. */
5854 epilogue_completed = 1;
5856 /* Find non-fallthru edges that end with EH_RETURN instructions. On
5857 some targets, these get split to a special version of the epilogue
5858 code. In order to be able to properly annotate these with unwind
5859 info, try to split them now. If we get a valid split, drop an
5860 EPILOGUE_BEG note and mark the insns as epilogue insns. */
5861 edge e;
5862 edge_iterator ei;
5863 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5865 rtx_insn *prev, *last, *trial;
5867 if (e->flags & EDGE_FALLTHRU)
5868 continue;
5869 last = BB_END (e->src);
5870 if (!eh_returnjump_p (last))
5871 continue;
5873 prev = PREV_INSN (last);
5874 trial = try_split (PATTERN (last), last, 1);
5875 if (trial == last)
5876 continue;
5878 record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
5879 emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
5882 edge exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
5884 if (exit_fallthru_edge)
5886 if (epilogue_seq)
5888 insert_insn_on_edge (epilogue_seq, exit_fallthru_edge);
5889 commit_edge_insertions ();
5891 /* The epilogue insns we inserted may cause the exit edge to no longer
5892 be fallthru. */
5893 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5895 if (((e->flags & EDGE_FALLTHRU) != 0)
5896 && returnjump_p (BB_END (e->src)))
5897 e->flags &= ~EDGE_FALLTHRU;
5900 else if (next_active_insn (BB_END (exit_fallthru_edge->src)))
5902 /* We have a fall-through edge to the exit block, the source is not
5903 at the end of the function, and there will be an assembler epilogue
5904 at the end of the function.
5905 We can't use force_nonfallthru here, because that would try to
5906 use return. Inserting a jump 'by hand' is extremely messy, so
5907 we take advantage of cfg_layout_finalize using
5908 fixup_fallthru_exit_predecessor. */
5909 cfg_layout_initialize (0);
5910 basic_block cur_bb;
5911 FOR_EACH_BB_FN (cur_bb, cfun)
5912 if (cur_bb->index >= NUM_FIXED_BLOCKS
5913 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
5914 cur_bb->aux = cur_bb->next_bb;
5915 cfg_layout_finalize ();
5919 /* Insert the prologue. */
5921 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5923 if (split_prologue_seq || prologue_seq)
5925 rtx_insn *split_prologue_insn = split_prologue_seq;
5926 if (split_prologue_seq)
5928 while (split_prologue_insn && !NONDEBUG_INSN_P (split_prologue_insn))
5929 split_prologue_insn = NEXT_INSN (split_prologue_insn);
5930 insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
5933 rtx_insn *prologue_insn = prologue_seq;
5934 if (prologue_seq)
5936 while (prologue_insn && !NONDEBUG_INSN_P (prologue_insn))
5937 prologue_insn = NEXT_INSN (prologue_insn);
5938 insert_insn_on_edge (prologue_seq, entry_edge);
5941 commit_edge_insertions ();
5943 /* Look for basic blocks within the prologue insns. */
5944 if (split_prologue_insn
5945 && BLOCK_FOR_INSN (split_prologue_insn) == NULL)
5946 split_prologue_insn = NULL;
5947 if (prologue_insn
5948 && BLOCK_FOR_INSN (prologue_insn) == NULL)
5949 prologue_insn = NULL;
5950 if (split_prologue_insn || prologue_insn)
5952 auto_sbitmap blocks (last_basic_block_for_fn (cfun));
5953 bitmap_clear (blocks);
5954 if (split_prologue_insn)
5955 bitmap_set_bit (blocks,
5956 BLOCK_FOR_INSN (split_prologue_insn)->index);
5957 if (prologue_insn)
5958 bitmap_set_bit (blocks, BLOCK_FOR_INSN (prologue_insn)->index);
5959 find_many_sub_basic_blocks (blocks);
5963 default_rtl_profile ();
5965 /* Emit sibling epilogues before any sibling call sites. */
5966 for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
5967 (e = ei_safe_edge (ei));
5968 ei_next (&ei))
5970 /* Skip those already handled, the ones that run without prologue. */
5971 if (e->flags & EDGE_IGNORE)
5973 e->flags &= ~EDGE_IGNORE;
5974 continue;
5977 rtx_insn *insn = BB_END (e->src);
5979 if (!(CALL_P (insn) && SIBLING_CALL_P (insn)))
5980 continue;
5982 if (rtx_insn *ep_seq = targetm.gen_sibcall_epilogue ())
5984 start_sequence ();
5985 emit_note (NOTE_INSN_EPILOGUE_BEG);
5986 emit_insn (ep_seq);
5987 rtx_insn *seq = get_insns ();
5988 end_sequence ();
5990 /* Retain a map of the epilogue insns. Used in life analysis to
5991 avoid getting rid of sibcall epilogue insns. Do this before we
5992 actually emit the sequence. */
5993 record_insns (seq, NULL, &epilogue_insn_hash);
5994 set_insn_locations (seq, epilogue_location);
5996 emit_insn_before (seq, insn);
6000 if (epilogue_seq)
6002 rtx_insn *insn, *next;
6004 /* Similarly, move any line notes that appear after the epilogue.
6005 There is no need, however, to be quite so anal about the existence
6006 of such a note. Also possibly move
6007 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
6008 info generation. */
6009 for (insn = epilogue_seq; insn; insn = next)
6011 next = NEXT_INSN (insn);
6012 if (NOTE_P (insn)
6013 && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
6014 reorder_insns (insn, insn, PREV_INSN (epilogue_seq));
6018 /* Threading the prologue and epilogue changes the artificial refs
6019 in the entry and exit blocks. */
6020 epilogue_completed = 1;
6021 df_update_entry_exit_and_calls ();
6024 /* Reposition the prologue-end and epilogue-begin notes after
6025 instruction scheduling. */
6027 void
6028 reposition_prologue_and_epilogue_notes (void)
6030 if (!targetm.have_prologue ()
6031 && !targetm.have_epilogue ()
6032 && !targetm.have_sibcall_epilogue ())
6033 return;
6035 /* Since the hash table is created on demand, the fact that it is
6036 non-null is a signal that it is non-empty. */
6037 if (prologue_insn_hash != NULL)
6039 size_t len = prologue_insn_hash->elements ();
6040 rtx_insn *insn, *last = NULL, *note = NULL;
6042 /* Scan from the beginning until we reach the last prologue insn. */
6043 /* ??? While we do have the CFG intact, there are two problems:
6044 (1) The prologue can contain loops (typically probing the stack),
6045 which means that the end of the prologue isn't in the first bb.
6046 (2) Sometimes the PROLOGUE_END note gets pushed into the next bb. */
6047 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6049 if (NOTE_P (insn))
6051 if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
6052 note = insn;
6054 else if (contains (insn, prologue_insn_hash))
6056 last = insn;
6057 if (--len == 0)
6058 break;
6062 if (last)
6064 if (note == NULL)
6066 /* Scan forward looking for the PROLOGUE_END note. It should
6067 be right at the beginning of the block, possibly with other
6068 insn notes that got moved there. */
6069 for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
6071 if (NOTE_P (note)
6072 && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
6073 break;
6077 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
6078 if (LABEL_P (last))
6079 last = NEXT_INSN (last);
6080 reorder_insns (note, note, last);
6084 if (epilogue_insn_hash != NULL)
6086 edge_iterator ei;
6087 edge e;
6089 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6091 rtx_insn *insn, *first = NULL, *note = NULL;
6092 basic_block bb = e->src;
6094 /* Scan from the beginning until we reach the first epilogue insn. */
6095 FOR_BB_INSNS (bb, insn)
6097 if (NOTE_P (insn))
6099 if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
6101 note = insn;
6102 if (first != NULL)
6103 break;
6106 else if (first == NULL && contains (insn, epilogue_insn_hash))
6108 first = insn;
6109 if (note != NULL)
6110 break;
6114 if (note)
6116 /* If the function has a single basic block, and no real
6117 epilogue insns (e.g. sibcall with no cleanup), the
6118 epilogue note can get scheduled before the prologue
6119 note. If we have frame related prologue insns, having
6120 them scanned during the epilogue will result in a crash.
6121 In this case re-order the epilogue note to just before
6122 the last insn in the block. */
6123 if (first == NULL)
6124 first = BB_END (bb);
6126 if (PREV_INSN (first) != note)
6127 reorder_insns (note, note, PREV_INSN (first));
6133 /* Returns the name of function declared by FNDECL. */
6134 const char *
6135 fndecl_name (tree fndecl)
6137 if (fndecl == NULL)
6138 return "(nofn)";
6139 return lang_hooks.decl_printable_name (fndecl, 1);
6142 /* Returns the name of function FN. */
6143 const char *
6144 function_name (struct function *fn)
6146 tree fndecl = (fn == NULL) ? NULL : fn->decl;
6147 return fndecl_name (fndecl);
6150 /* Returns the name of the current function. */
6151 const char *
6152 current_function_name (void)
6154 return function_name (cfun);
6158 static unsigned int
6159 rest_of_handle_check_leaf_regs (void)
6161 #ifdef LEAF_REGISTERS
6162 crtl->uses_only_leaf_regs
6163 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
6164 #endif
6165 return 0;
6168 /* Insert a TYPE into the used types hash table of CFUN. */
6170 static void
6171 used_types_insert_helper (tree type, struct function *func)
6173 if (type != NULL && func != NULL)
6175 if (func->used_types_hash == NULL)
6176 func->used_types_hash = hash_set<tree>::create_ggc (37);
6178 func->used_types_hash->add (type);
6182 /* Given a type, insert it into the used hash table in cfun. */
6183 void
6184 used_types_insert (tree t)
6186 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
6187 if (TYPE_NAME (t))
6188 break;
6189 else
6190 t = TREE_TYPE (t);
6191 if (TREE_CODE (t) == ERROR_MARK)
6192 return;
6193 if (TYPE_NAME (t) == NULL_TREE
6194 || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
6195 t = TYPE_MAIN_VARIANT (t);
6196 if (debug_info_level > DINFO_LEVEL_NONE)
6198 if (cfun)
6199 used_types_insert_helper (t, cfun);
6200 else
6202 /* So this might be a type referenced by a global variable.
6203 Record that type so that we can later decide to emit its
6204 debug information. */
6205 vec_safe_push (types_used_by_cur_var_decl, t);
6210 /* Helper to Hash a struct types_used_by_vars_entry. */
6212 static hashval_t
6213 hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
6215 gcc_assert (entry && entry->var_decl && entry->type);
6217 return iterative_hash_object (entry->type,
6218 iterative_hash_object (entry->var_decl, 0));
6221 /* Hash function of the types_used_by_vars_entry hash table. */
6223 hashval_t
6224 used_type_hasher::hash (types_used_by_vars_entry *entry)
6226 return hash_types_used_by_vars_entry (entry);
6229 /*Equality function of the types_used_by_vars_entry hash table. */
6231 bool
6232 used_type_hasher::equal (types_used_by_vars_entry *e1,
6233 types_used_by_vars_entry *e2)
6235 return (e1->var_decl == e2->var_decl && e1->type == e2->type);
6238 /* Inserts an entry into the types_used_by_vars_hash hash table. */
6240 void
6241 types_used_by_var_decl_insert (tree type, tree var_decl)
6243 if (type != NULL && var_decl != NULL)
6245 types_used_by_vars_entry **slot;
6246 struct types_used_by_vars_entry e;
6247 e.var_decl = var_decl;
6248 e.type = type;
6249 if (types_used_by_vars_hash == NULL)
6250 types_used_by_vars_hash
6251 = hash_table<used_type_hasher>::create_ggc (37);
6253 slot = types_used_by_vars_hash->find_slot (&e, INSERT);
6254 if (*slot == NULL)
6256 struct types_used_by_vars_entry *entry;
6257 entry = ggc_alloc<types_used_by_vars_entry> ();
6258 entry->type = type;
6259 entry->var_decl = var_decl;
6260 *slot = entry;
6265 namespace {
6267 const pass_data pass_data_leaf_regs =
6269 RTL_PASS, /* type */
6270 "*leaf_regs", /* name */
6271 OPTGROUP_NONE, /* optinfo_flags */
6272 TV_NONE, /* tv_id */
6273 0, /* properties_required */
6274 0, /* properties_provided */
6275 0, /* properties_destroyed */
6276 0, /* todo_flags_start */
6277 0, /* todo_flags_finish */
6280 class pass_leaf_regs : public rtl_opt_pass
6282 public:
6283 pass_leaf_regs (gcc::context *ctxt)
6284 : rtl_opt_pass (pass_data_leaf_regs, ctxt)
6287 /* opt_pass methods: */
6288 virtual unsigned int execute (function *)
6290 return rest_of_handle_check_leaf_regs ();
6293 }; // class pass_leaf_regs
6295 } // anon namespace
6297 rtl_opt_pass *
6298 make_pass_leaf_regs (gcc::context *ctxt)
6300 return new pass_leaf_regs (ctxt);
6303 static unsigned int
6304 rest_of_handle_thread_prologue_and_epilogue (void)
6306 /* prepare_shrink_wrap is sensitive to the block structure of the control
6307 flow graph, so clean it up first. */
6308 if (optimize)
6309 cleanup_cfg (0);
6311 /* On some machines, the prologue and epilogue code, or parts thereof,
6312 can be represented as RTL. Doing so lets us schedule insns between
6313 it and the rest of the code and also allows delayed branch
6314 scheduling to operate in the epilogue. */
6315 thread_prologue_and_epilogue_insns ();
6317 /* Some non-cold blocks may now be only reachable from cold blocks.
6318 Fix that up. */
6319 fixup_partitions ();
6321 /* Shrink-wrapping can result in unreachable edges in the epilogue,
6322 see PR57320. */
6323 cleanup_cfg (optimize ? CLEANUP_EXPENSIVE : 0);
6325 /* The stack usage info is finalized during prologue expansion. */
6326 if (flag_stack_usage_info)
6327 output_stack_usage ();
6329 return 0;
6332 namespace {
6334 const pass_data pass_data_thread_prologue_and_epilogue =
6336 RTL_PASS, /* type */
6337 "pro_and_epilogue", /* name */
6338 OPTGROUP_NONE, /* optinfo_flags */
6339 TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
6340 0, /* properties_required */
6341 0, /* properties_provided */
6342 0, /* properties_destroyed */
6343 0, /* todo_flags_start */
6344 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
6347 class pass_thread_prologue_and_epilogue : public rtl_opt_pass
6349 public:
6350 pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6351 : rtl_opt_pass (pass_data_thread_prologue_and_epilogue, ctxt)
6354 /* opt_pass methods: */
6355 virtual unsigned int execute (function *)
6357 return rest_of_handle_thread_prologue_and_epilogue ();
6360 }; // class pass_thread_prologue_and_epilogue
6362 } // anon namespace
6364 rtl_opt_pass *
6365 make_pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6367 return new pass_thread_prologue_and_epilogue (ctxt);
6371 /* This mini-pass fixes fall-out from SSA in asm statements that have
6372 in-out constraints. Say you start with
6374 orig = inout;
6375 asm ("": "+mr" (inout));
6376 use (orig);
6378 which is transformed very early to use explicit output and match operands:
6380 orig = inout;
6381 asm ("": "=mr" (inout) : "0" (inout));
6382 use (orig);
6384 Or, after SSA and copyprop,
6386 asm ("": "=mr" (inout_2) : "0" (inout_1));
6387 use (inout_1);
6389 Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
6390 they represent two separate values, so they will get different pseudo
6391 registers during expansion. Then, since the two operands need to match
6392 per the constraints, but use different pseudo registers, reload can
6393 only register a reload for these operands. But reloads can only be
6394 satisfied by hardregs, not by memory, so we need a register for this
6395 reload, just because we are presented with non-matching operands.
6396 So, even though we allow memory for this operand, no memory can be
6397 used for it, just because the two operands don't match. This can
6398 cause reload failures on register-starved targets.
6400 So it's a symptom of reload not being able to use memory for reloads
6401 or, alternatively it's also a symptom of both operands not coming into
6402 reload as matching (in which case the pseudo could go to memory just
6403 fine, as the alternative allows it, and no reload would be necessary).
6404 We fix the latter problem here, by transforming
6406 asm ("": "=mr" (inout_2) : "0" (inout_1));
6408 back to
6410 inout_2 = inout_1;
6411 asm ("": "=mr" (inout_2) : "0" (inout_2)); */
6413 static void
6414 match_asm_constraints_1 (rtx_insn *insn, rtx *p_sets, int noutputs)
6416 int i;
6417 bool changed = false;
6418 rtx op = SET_SRC (p_sets[0]);
6419 int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
6420 rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
6421 bool *output_matched = XALLOCAVEC (bool, noutputs);
6423 memset (output_matched, 0, noutputs * sizeof (bool));
6424 for (i = 0; i < ninputs; i++)
6426 rtx input, output;
6427 rtx_insn *insns;
6428 const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
6429 char *end;
6430 int match, j;
6432 if (*constraint == '%')
6433 constraint++;
6435 match = strtoul (constraint, &end, 10);
6436 if (end == constraint)
6437 continue;
6439 gcc_assert (match < noutputs);
6440 output = SET_DEST (p_sets[match]);
6441 input = RTVEC_ELT (inputs, i);
6442 /* Only do the transformation for pseudos. */
6443 if (! REG_P (output)
6444 || rtx_equal_p (output, input)
6445 || !(REG_P (input) || SUBREG_P (input)
6446 || MEM_P (input) || CONSTANT_P (input))
6447 || !general_operand (input, GET_MODE (output)))
6448 continue;
6450 /* We can't do anything if the output is also used as input,
6451 as we're going to overwrite it. */
6452 for (j = 0; j < ninputs; j++)
6453 if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
6454 break;
6455 if (j != ninputs)
6456 continue;
6458 /* Avoid changing the same input several times. For
6459 asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
6460 only change in once (to out1), rather than changing it
6461 first to out1 and afterwards to out2. */
6462 if (i > 0)
6464 for (j = 0; j < noutputs; j++)
6465 if (output_matched[j] && input == SET_DEST (p_sets[j]))
6466 break;
6467 if (j != noutputs)
6468 continue;
6470 output_matched[match] = true;
6472 start_sequence ();
6473 emit_move_insn (output, input);
6474 insns = get_insns ();
6475 end_sequence ();
6476 emit_insn_before (insns, insn);
6478 /* Now replace all mentions of the input with output. We can't
6479 just replace the occurrence in inputs[i], as the register might
6480 also be used in some other input (or even in an address of an
6481 output), which would mean possibly increasing the number of
6482 inputs by one (namely 'output' in addition), which might pose
6483 a too complicated problem for reload to solve. E.g. this situation:
6485 asm ("" : "=r" (output), "=m" (input) : "0" (input))
6487 Here 'input' is used in two occurrences as input (once for the
6488 input operand, once for the address in the second output operand).
6489 If we would replace only the occurrence of the input operand (to
6490 make the matching) we would be left with this:
6492 output = input
6493 asm ("" : "=r" (output), "=m" (input) : "0" (output))
6495 Now we suddenly have two different input values (containing the same
6496 value, but different pseudos) where we formerly had only one.
6497 With more complicated asms this might lead to reload failures
6498 which wouldn't have happen without this pass. So, iterate over
6499 all operands and replace all occurrences of the register used. */
6500 for (j = 0; j < noutputs; j++)
6501 if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
6502 && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
6503 SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
6504 input, output);
6505 for (j = 0; j < ninputs; j++)
6506 if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
6507 RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
6508 input, output);
6510 changed = true;
6513 if (changed)
6514 df_insn_rescan (insn);
6517 /* Add the decl D to the local_decls list of FUN. */
6519 void
6520 add_local_decl (struct function *fun, tree d)
6522 gcc_assert (VAR_P (d));
6523 vec_safe_push (fun->local_decls, d);
6526 namespace {
6528 const pass_data pass_data_match_asm_constraints =
6530 RTL_PASS, /* type */
6531 "asmcons", /* name */
6532 OPTGROUP_NONE, /* optinfo_flags */
6533 TV_NONE, /* tv_id */
6534 0, /* properties_required */
6535 0, /* properties_provided */
6536 0, /* properties_destroyed */
6537 0, /* todo_flags_start */
6538 0, /* todo_flags_finish */
6541 class pass_match_asm_constraints : public rtl_opt_pass
6543 public:
6544 pass_match_asm_constraints (gcc::context *ctxt)
6545 : rtl_opt_pass (pass_data_match_asm_constraints, ctxt)
6548 /* opt_pass methods: */
6549 virtual unsigned int execute (function *);
6551 }; // class pass_match_asm_constraints
6553 unsigned
6554 pass_match_asm_constraints::execute (function *fun)
6556 basic_block bb;
6557 rtx_insn *insn;
6558 rtx pat, *p_sets;
6559 int noutputs;
6561 if (!crtl->has_asm_statement)
6562 return 0;
6564 df_set_flags (DF_DEFER_INSN_RESCAN);
6565 FOR_EACH_BB_FN (bb, fun)
6567 FOR_BB_INSNS (bb, insn)
6569 if (!INSN_P (insn))
6570 continue;
6572 pat = PATTERN (insn);
6573 if (GET_CODE (pat) == PARALLEL)
6574 p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
6575 else if (GET_CODE (pat) == SET)
6576 p_sets = &PATTERN (insn), noutputs = 1;
6577 else
6578 continue;
6580 if (GET_CODE (*p_sets) == SET
6581 && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
6582 match_asm_constraints_1 (insn, p_sets, noutputs);
6586 return TODO_df_finish;
6589 } // anon namespace
6591 rtl_opt_pass *
6592 make_pass_match_asm_constraints (gcc::context *ctxt)
6594 return new pass_match_asm_constraints (ctxt);
6598 #include "gt-function.h"